From e64be059566895fd0a34c7fc46422ed491f809cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20M=C3=A1tyus?= Date: Thu, 18 Apr 2024 11:58:46 +0200 Subject: [PATCH] Add tests --- tests/Authorization/MultiAuthorizatorTest.php | 80 +++++++++++++++++++ tests/Handler/ApiListingHandlerTest.php | 39 +++++++++ 2 files changed, 119 insertions(+) create mode 100644 tests/Authorization/MultiAuthorizatorTest.php diff --git a/tests/Authorization/MultiAuthorizatorTest.php b/tests/Authorization/MultiAuthorizatorTest.php new file mode 100644 index 0000000..ee539c2 --- /dev/null +++ b/tests/Authorization/MultiAuthorizatorTest.php @@ -0,0 +1,80 @@ + '*']); + $ipDetector = new StaticIpDetector('34.24.126.44'); + $queryAuthorization = new QueryApiKeyAuthentication('api_key', $tokenRepository, $ipDetector); + + $_SERVER['HTTP_X_API_KEY'] = 'sad0f98uwegoihweg09i4hergy'; + $tokenRepository = new StaticTokenRepository(['sad0f98uwegoihweg09i4hergy' => '*']); + $ipDetector = new StaticIpDetector('34.24.126.44'); + $headerAuthorization = new HeaderApiKeyAuthentication('X-API-KEY', $tokenRepository, $ipDetector); + + $authorization = new MultiAuthorizator([$queryAuthorization, $headerAuthorization]); + $this->assertTrue($authorization->authorized()); + } + + public function testFirstAuthorizedApiKey() + { + $_GET['api_key'] = 'sad0f98uwegoihweg09i4hergy'; + $tokenRepository = new StaticTokenRepository(['sad0f98uwegoihweg09i4hergy' => '*']); + $ipDetector = new StaticIpDetector('34.24.126.44'); + $queryAuthorization = new QueryApiKeyAuthentication('api_key', $tokenRepository, $ipDetector); + + $_SERVER['HTTP_X_API_KEY'] = 'asflkhwetiohegedgfsdgwe'; + $tokenRepository = new StaticTokenRepository(['sad0f98uwegoihweg09i4hergy' => '*']); + $ipDetector = new StaticIpDetector('34.24.126.44'); + $headerAuthorization = new HeaderApiKeyAuthentication('X-API-KEY', $tokenRepository, $ipDetector); + + $authorization = new MultiAuthorizator([$queryAuthorization, $headerAuthorization]); + $this->assertTrue($authorization->authorized()); + } + + public function testSecondAuthorizedApiKey() + { + $_GET['api_key'] = 'asflkhwetiohegedgfsdgwe'; + $tokenRepository = new StaticTokenRepository(['sad0f98uwegoihweg09i4hergy' => '*']); + $ipDetector = new StaticIpDetector('34.24.126.44'); + $queryAuthorization = new QueryApiKeyAuthentication('api_key', $tokenRepository, $ipDetector); + + $_SERVER['HTTP_X_API_KEY'] = 'sad0f98uwegoihweg09i4hergy'; + $tokenRepository = new StaticTokenRepository(['sad0f98uwegoihweg09i4hergy' => '*']); + $ipDetector = new StaticIpDetector('34.24.126.44'); + $headerAuthorization = new HeaderApiKeyAuthentication('X-API-KEY', $tokenRepository, $ipDetector); + + $authorization = new MultiAuthorizator([$queryAuthorization, $headerAuthorization]); + $this->assertTrue($authorization->authorized()); + } + + public function testUnauthorizedApiKey() + { + $_GET['api_key'] = 'asflkhwetiohegedgfsdgwe'; + $tokenRepository = new StaticTokenRepository(['sad0f98uwegoihweg09i4hergy' => '*']); + $ipDetector = new StaticIpDetector('34.24.126.44'); + $queryAuthorization = new QueryApiKeyAuthentication('api_key', $tokenRepository, $ipDetector); + + $_SERVER['HTTP_X_API_KEY'] = 'asflkhwetiohegedgfsdgwe'; + $tokenRepository = new StaticTokenRepository(['sad0f98uwegoihweg09i4hergy' => '*']); + $ipDetector = new StaticIpDetector('34.24.126.44'); + $headerAuthorization = new HeaderApiKeyAuthentication('X-API-KEY', $tokenRepository, $ipDetector); + + $authorization = new MultiAuthorizator([$queryAuthorization, $headerAuthorization]); + $this->assertFalse($authorization->authorized()); + $this->assertEquals('Token doesn\'t exists or isn\'t active', $authorization->getErrorMessage()); + } +} diff --git a/tests/Handler/ApiListingHandlerTest.php b/tests/Handler/ApiListingHandlerTest.php index 791e6f7..414273c 100644 --- a/tests/Handler/ApiListingHandlerTest.php +++ b/tests/Handler/ApiListingHandlerTest.php @@ -9,12 +9,17 @@ use Nette\Http\UrlScript; use PHPUnit\Framework\TestCase; use Tomaj\NetteApi\ApiDecider; +use Tomaj\NetteApi\Authorization\HeaderApiKeyAuthentication; +use Tomaj\NetteApi\Authorization\MultiAuthorizator; use Tomaj\NetteApi\Authorization\NoAuthorization; +use Tomaj\NetteApi\Authorization\QueryApiKeyAuthentication; use Tomaj\NetteApi\EndpointIdentifier; use Tomaj\NetteApi\Handlers\AlwaysOkHandler; use Tomaj\NetteApi\Handlers\EchoHandler; use Tomaj\NetteApi\Handlers\ApiListingHandler; use Tomaj\NetteApi\Link\ApiLink; +use Tomaj\NetteApi\Misc\StaticIpDetector; +use Tomaj\NetteApi\Misc\StaticTokenRepository; class ApiListingHandlerTest extends TestCase { @@ -72,4 +77,38 @@ public function testHandlerWithParam() $this->assertEquals(2, count($payload['endpoints'])); $this->assertEquals(2, count($payload['endpoints'][0]['params'])); } + + public function testMultiauthorizatorHandler() + { + $linkGenerator = new LinkGenerator(new SimpleRouter([]), new UrlScript('http://test/')); + $apiLink = new ApiLink($linkGenerator); + + $apiDecider = new ApiDecider(); + + $_GET['api_key'] = 'asflkhwetiohegedgfsdgwe'; + $tokenRepository = new StaticTokenRepository(['sad0f98uwegoihweg09i4hergy' => '*']); + $ipDetector = new StaticIpDetector('34.24.126.44'); + $queryAuthorization = new QueryApiKeyAuthentication('api_key', $tokenRepository, $ipDetector); + + $_SERVER['HTTP_X_API_KEY'] = 'sad0f98uwegoihweg09i4hergy'; + $tokenRepository = new StaticTokenRepository(['sad0f98uwegoihweg09i4hergy' => '*']); + $ipDetector = new StaticIpDetector('34.24.126.44'); + $headerAuthorization = new HeaderApiKeyAuthentication('X-API-KEY', $tokenRepository, $ipDetector); + + $multiAuthorizator = new MultiAuthorizator([$queryAuthorization, $headerAuthorization]); + + $apiDecider->addApi( + new EndpointIdentifier('GET', 1, 'endpoints'), + new ApiListingHandler($apiDecider, $apiLink), + $multiAuthorizator + ); + + $result = $apiDecider->getApi('GET', 1, 'endpoints'); + $handler = $result->getHandler(); + + $response = $handler->handle([]); + $this->assertEquals(200, $response->getCode()); + $payload = $response->getPayload(); + $this->assertEquals(1, count($payload['endpoints'])); + } }