From 59ce87786627e913e0e720e812e50c6134c44ffc Mon Sep 17 00:00:00 2001 From: "Nathanael d. Noblet" Date: Wed, 20 Oct 2021 22:42:39 -0600 Subject: [PATCH] Fixed up some tests --- composer.json | 4 ++-- tests/OpenId/TokenVerifierTest.php | 28 ++++++++++++++++------------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index b7cb860..2c9bd58 100644 --- a/composer.json +++ b/composer.json @@ -15,12 +15,12 @@ "ext-json": "*", "symfony/framework-bundle": "^5.0", "twig/twig": "^2.0|^3.0", - "lcobucci/jwt": "^3.2", + "lcobucci/jwt": "^3.4", "codercat/jwk-to-pem": "^1.1", "symfony/http-client": "^5.0" }, "require-dev": { - "phpunit/phpunit": "^5.0|^7.0" + "phpunit/phpunit": "^5.0|^7.0|^8.0" }, "autoload": { "psr-4": { "NS\\TokenBundle\\": "src" } diff --git a/tests/OpenId/TokenVerifierTest.php b/tests/OpenId/TokenVerifierTest.php index ec4e0ec..126344b 100644 --- a/tests/OpenId/TokenVerifierTest.php +++ b/tests/OpenId/TokenVerifierTest.php @@ -9,6 +9,7 @@ use NS\TokenBundle\OpenId\TokenVerifier; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; +use Psr\Log\LoggerInterface; use Symfony\Component\HttpClient\Exception\TransportException; use Symfony\Contracts\HttpClient\HttpClientInterface; use Symfony\Contracts\HttpClient\ResponseInterface; @@ -21,6 +22,8 @@ class TokenVerifierTest extends TestCase /** @var HttpClientInterface|MockObject */ private $httpClient; + /** @var LoggerInterface|MockObject */ + private $logger; private ?TokenVerifier $tokenVerifier = null; public function setUp(): void @@ -29,13 +32,14 @@ public function setUp(): void $this->tokenConverter = new JWKConverter(); $this->validator = new Validator(); $this->httpClient = $this->createMock(HttpClientInterface::class); - $this->tokenVerifier = new TokenVerifier($this->httpClient, $this->parser, $this->validator, $this->tokenConverter); + $this->logger = $this->createMock(LoggerInterface::class); + $this->tokenVerifier = new TokenVerifier($this->httpClient, $this->parser, $this->validator, $this->tokenConverter, $this->logger); } public function testNonUrlIssuerThrowsException(): void { $this->expectException(\InvalidArgumentException::class); - $this->tokenVerifier->isValid('a string','non_url'); + $this->tokenVerifier->isValid('a string', 'non_url'); } public function testUnableToLocateOpenIdConfigurationThrowsException(): void @@ -45,7 +49,7 @@ public function testUnableToLocateOpenIdConfigurationThrowsException(): void $response = $this->createMock(ResponseInterface::class); $response->method('getContent')->willThrowException(new TransportException()); $this->httpClient->method('request')->with('GET', 'http://example.net/.well-known/openid-configuration')->willReturn($response); - $this->tokenVerifier->isValid('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c','https://example.net'); + $this->tokenVerifier->isValid('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c', 'https://example.net'); } public function testOpenIdInvalidResponse(): void @@ -53,9 +57,9 @@ public function testOpenIdInvalidResponse(): void $this->expectException(InvalidTokenException::class); $response = $this->createMock(ResponseInterface::class); - $response->method('getContent')->willReturn(file_get_contents(__DIR__.'/Fixtures/not-json.response')); + $response->method('getContent')->willReturn(file_get_contents(__DIR__ . '/Fixtures/not-json.response')); $this->httpClient->method('request')->with('GET', 'http://example.net/.well-known/openid-configuration')->willReturn($response); - $this->tokenVerifier->isValid('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c','https://example.net'); + $this->tokenVerifier->isValid('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c', 'https://example.net'); } public function testOpenIdMissingJwksUri(): void @@ -63,9 +67,9 @@ public function testOpenIdMissingJwksUri(): void $this->expectException(InvalidTokenException::class); $response = $this->createMock(ResponseInterface::class); - $response->method('getContent')->willReturn(file_get_contents(__DIR__.'/Fixtures/openid-missing-jwks_uri.response')); + $response->method('getContent')->willReturn(file_get_contents(__DIR__ . '/Fixtures/openid-missing-jwks_uri.response')); $this->httpClient->method('request')->with('GET', 'http://example.net/.well-known/openid-configuration')->willReturn($response); - $this->tokenVerifier->isValid('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c','https://example.net'); + $this->tokenVerifier->isValid('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c', 'https://example.net'); } public function testJwksEmpty(): void @@ -74,10 +78,10 @@ public function testJwksEmpty(): void $openIdResponse = $this->createMock(ResponseInterface::class); $jwksIdResponse = $this->createMock(ResponseInterface::class); - $openIdResponse->method('getContent')->willReturn(file_get_contents(__DIR__.'/Fixtures/openid-config.response')); - $jwksIdResponse->method('getContent')->willReturn(new TransportException()); + $openIdResponse->method('getContent')->willReturn(file_get_contents(__DIR__ . '/Fixtures/openid-config.response')); + $jwksIdResponse->method('getContent')->willThrowException(new TransportException()); - $this->httpClient->method('request')->willReturnCallback(static function($method, $url) use ($openIdResponse, $jwksIdResponse) { + $this->httpClient->method('request')->willReturnCallback(static function ($method, $url) use ($openIdResponse, $jwksIdResponse) { self::assertSame('GET', $method); if ($url === 'https://example.net/.well-known/openid-configuration') { return $openIdResponse; @@ -87,8 +91,8 @@ public function testJwksEmpty(): void return $jwksIdResponse; } - throw new \RuntimeException('No matching url for '.$url); + throw new \RuntimeException('No matching url for ' . $url); }); - $this->tokenVerifier->isValid('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c','https://example.net'); + $this->tokenVerifier->isValid('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c', 'https://example.net'); } }