Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Mátyus committed Apr 18, 2024
1 parent 2df8690 commit e64be05
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 0 deletions.
80 changes: 80 additions & 0 deletions tests/Authorization/MultiAuthorizatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Tomaj\NetteApi\Test\Handler;

use PHPUnit\Framework\TestCase;
use Tomaj\NetteApi\Authorization\HeaderApiKeyAuthentication;
use Tomaj\NetteApi\Authorization\MultiAuthorizator;
use Tomaj\NetteApi\Authorization\QueryApiKeyAuthentication;
use Tomaj\NetteApi\Misc\StaticTokenRepository;
use Tomaj\NetteApi\Misc\StaticIpDetector;

class MultiAuthorizatorTest extends TestCase
{
public function testAllAuthorizedApiKey()
{
$_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'] = '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());
}
}
39 changes: 39 additions & 0 deletions tests/Handler/ApiListingHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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']));
}
}

0 comments on commit e64be05

Please sign in to comment.