From c29cb7ce78d00639e653a3c50c2da7eb4912e668 Mon Sep 17 00:00:00 2001 From: Aditya Dhade Date: Thu, 19 Dec 2024 23:25:00 +0530 Subject: [PATCH] Add test for REST API forbidden error handling --- ...n-detective-rest-api-site-health-check.php | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/plugins/optimization-detective/tests/test-optimization-detective-rest-api-site-health-check.php b/plugins/optimization-detective/tests/test-optimization-detective-rest-api-site-health-check.php index f9cc7354e..f45e0599f 100644 --- a/plugins/optimization-detective/tests/test-optimization-detective-rest-api-site-health-check.php +++ b/plugins/optimization-detective/tests/test-optimization-detective-rest-api-site-health-check.php @@ -52,7 +52,7 @@ public function test_rest_api_available(): void { } /** - * Test behavior when REST API returns unauthorized error. + * Test behavior when REST API returns an unauthorized error. */ public function test_rest_api_unauthorized(): void { $this->mocked_responses = array( @@ -70,6 +70,25 @@ public function test_rest_api_unauthorized(): void { $this->assertFalse( isset( $od_rest_api_info['available'] ) ? $od_rest_api_info['available'] : true ); } + /** + * Test behavior when REST API returns an forbidden error. + */ + public function test_rest_api_forbidden(): void { + $this->mocked_responses = array( + get_rest_url( null, OD_REST_API_NAMESPACE . OD_URL_METRICS_ROUTE ) => $this->build_mock_response( + 403, + 'Forbidden' + ), + ); + + $result = od_optimization_detective_rest_api_test(); + $od_rest_api_info = get_option( 'od_rest_api_info', array() ); + + $this->assertSame( 'recommended', $result['status'] ); + $this->assertSame( 'forbidden', isset( $od_rest_api_info['status'] ) ? $od_rest_api_info['status'] : '' ); + $this->assertFalse( isset( $od_rest_api_info['available'] ) ? $od_rest_api_info['available'] : true ); + } + /** * Mock HTTP requests for assets to simulate different responses. *