From 0be54a1dd464440b9320eaf5958e7214af91efd6 Mon Sep 17 00:00:00 2001 From: Justin Lyons Date: Mon, 23 Jun 2025 10:25:09 -0500 Subject: [PATCH] Fixed getAccessToken to properly throw the SigmaAuthenticationException when the API call fails --- src/API/Authentication.php | 21 +++++++++------- tests/Mocks/SigmaRESTMock.php | 40 +++++++++++++++++++++++++++++++ tests/Unit/AuthenticationTest.php | 16 +++++++++++++ 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/src/API/Authentication.php b/src/API/Authentication.php index 0dc406c..e4a679a 100644 --- a/src/API/Authentication.php +++ b/src/API/Authentication.php @@ -42,14 +42,19 @@ public function getAccessToken(): mixed 'Content-Type' => 'application/x-www-form-urlencoded', ]; - // Make the call - $response = $this->call( - url : $url, - args : $args, - headers: $headers, - method : 'POST', - asForm : true - ); + try { + // Make the call + $response = $this->call( + url : $url, + args : $args, + headers: $headers, + method : 'POST', + asForm : true + ); + } catch (\Exception $e) { + // If the call fails, throw a SigmaAuthenticationException instead of a generic Exception + throw new SigmaAuthenticationException('Authentication failed: ' . $e->getMessage(), $e->getCode(), $e); + } // Transform response to an array /** @var array $responseArr */ diff --git a/tests/Mocks/SigmaRESTMock.php b/tests/Mocks/SigmaRESTMock.php index a01bd84..4fc67ad 100644 --- a/tests/Mocks/SigmaRESTMock.php +++ b/tests/Mocks/SigmaRESTMock.php @@ -45,4 +45,44 @@ public static function mockWithCall(mixed $expectedResponse, string $url): mixed return $mock; } + + /** + * Mock the SigmaREST class call function to simulate a failed call. + * + * @param int $status + * @param string $body + * @param string $url + * + * @return mixed[]|Response + */ + public static function mockWithFailedCall(int $status, string $body, string $url): mixed + { + // Mock the SigmaREST class, specifically to check if _authenticate is called + $mock = Mockery::mock(SigmaREST::class)->makePartial()->shouldAllowMockingProtectedMethods(); + + // Ensure the _authenticate method is called in the constructor + $mock->shouldReceive('_authenticate') + ->once() + ->andReturnNull(); + + // Instantiate the class to trigger the constructor + $mock->__construct(); + + // Create a mock for the Illuminate\Http\Client\Response class that simulates a failed response + $responseMock = Mockery::mock(Response::class); + $responseMock->shouldReceive('body')->andReturn($body); + $responseMock->shouldReceive('json')->andReturn(json_decode($body, true)); + $responseMock->shouldReceive('status')->andReturn($status); + $responseMock->shouldReceive('failed')->andReturnTrue(); + $responseMock->shouldReceive('successful')->andReturnFalse(); + + // Mock the call method to return the response mock + $mock->shouldReceive('call') + ->withArgs(function ($passedUrl) use ($url) { + return $passedUrl === $url; + }) + ->andReturn($responseMock); + + return $mock; + } } diff --git a/tests/Unit/AuthenticationTest.php b/tests/Unit/AuthenticationTest.php index 26fbb5a..c9d9efe 100644 --- a/tests/Unit/AuthenticationTest.php +++ b/tests/Unit/AuthenticationTest.php @@ -37,3 +37,19 @@ // Clean up Mockery expectations Mockery::close(); }); + +test('throws SigmaAuthenticationException when authentication fails', function () { + // Create a mock for the class that will simulate a failed authentication + $mock = SigmaRESTMock::mockWithFailedCall( + status: 401, + body: '{"error": "invalid_credentials"}', + url: 'auth/token' + ); + + // Expect the specific exception to be thrown + expect(fn() => $mock->getAccessToken()) + ->toThrow(\InterWorks\SigmaREST\Exceptions\SigmaAuthenticationException::class); + + // Clean up Mockery expectations + Mockery::close(); +});