Skip to content

Commit

Permalink
Fixed up some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gnat42 committed Oct 21, 2021
1 parent 0fe3945 commit 59ce877
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
28 changes: 16 additions & 12 deletions tests/OpenId/TokenVerifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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
Expand All @@ -45,27 +49,27 @@ 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
{
$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
{
$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
Expand All @@ -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;
Expand All @@ -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');
}
}

0 comments on commit 59ce877

Please sign in to comment.