Skip to content

Commit

Permalink
more work
Browse files Browse the repository at this point in the history
  • Loading branch information
fezfez committed Sep 27, 2023
1 parent dc6d9e6 commit 1ecbe0e
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 129 deletions.
113 changes: 38 additions & 75 deletions tests/Functional/JwtAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
namespace Tuupola\Tests\Middleware;

use Equip\Dispatch\MiddlewareCollection;
use Laminas\Diactoros\ServerRequest;
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Signer\Key\InMemory;
use Lcobucci\JWT\Token;
Expand All @@ -45,7 +46,6 @@
use RuntimeException;
use Throwable;
use Tuupola\Http\Factory\ResponseFactory;
use Tuupola\Http\Factory\ServerRequestFactory;
use Tuupola\Http\Factory\StreamFactory;
use Tuupola\Middleware\JwtAuthentication;
use Tuupola\Middleware\JwtAuthentication\RequestMethodRule;
Expand Down Expand Up @@ -79,8 +79,7 @@ class JwtAuthenticationTest extends TestCase

public function testShouldReturn401WithoutToken(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api');
$request = new ServerRequest([], [], 'https://example.com/api', 'GET');

$default = static function (): ResponseInterface {
$response = (new ResponseFactory())->createResponse();
Expand Down Expand Up @@ -108,8 +107,7 @@ public function testShouldReturn401WithoutToken(): void

public function testShouldReturn200WithTokenFromHeader(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('X-Token', 'Bearer ' . self::$acmeToken);

$default = static function (): ResponseInterface {
Expand Down Expand Up @@ -138,8 +136,7 @@ public function testShouldReturn200WithTokenFromHeader(): void

public function testShouldReturn200WithTokenFromHeaderWithCustomRegexp(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('X-Token', self::$acmeToken);

$default = static function (): ResponseInterface {
Expand Down Expand Up @@ -170,8 +167,7 @@ public function testShouldReturn200WithTokenFromHeaderWithCustomRegexp(): void

public function testShouldReturn200WithTokenFromCookie(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withCookieParams(['nekot' => self::$acmeToken]);

$default = static function (): ResponseInterface {
Expand Down Expand Up @@ -201,8 +197,7 @@ public function testShouldReturn200WithTokenFromCookie(): void

public function testShouldReturn200WithTokenFromCookieButEmptyValue(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withCookieParams(['nekot' => '']);

$default = static function (): ResponseInterface {
Expand Down Expand Up @@ -234,8 +229,7 @@ public function testShouldReturn200WithTokenFromCookieButEmptyValue(): void

public function testShouldReturn200WithTokenFromBearerCookie(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withCookieParams(['nekot' => 'Bearer ' . self::$acmeToken]);

$default = static function (): ResponseInterface {
Expand Down Expand Up @@ -265,8 +259,7 @@ public function testShouldReturn200WithTokenFromBearerCookie(): void

public function testShouldAlterResponseWithAnonymousAfter(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (): ResponseInterface {
Expand Down Expand Up @@ -297,8 +290,7 @@ public function __invoke(ResponseInterface $response, Plain $token): ResponseInt

public function testWronParser(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (): ResponseInterface {
Expand Down Expand Up @@ -331,8 +323,7 @@ public function testWronParser(): void

public function testShouldAlterResponseWithInvokableAfter(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (): ResponseInterface {
Expand All @@ -358,8 +349,7 @@ public function testShouldAlterResponseWithInvokableAfter(): void

public function testShouldReturn200WithOptions(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withMethod('OPTIONS');

$default = static function (): ResponseInterface {
Expand All @@ -384,8 +374,7 @@ public function testShouldReturn200WithOptions(): void

public function testShouldReturn400WithInvalidToken(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer invalid' . self::$acmeToken);

$default = static function (): ResponseInterface {
Expand All @@ -410,8 +399,7 @@ public function testShouldReturn400WithInvalidToken(): void

public function testShouldReturn400WithExpiredToken(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$expired);

$default = static function (): ResponseInterface {
Expand Down Expand Up @@ -440,8 +428,7 @@ public function testShouldReturn400WithExpiredToken(): void

public function testShouldReturn200WithoutTokenWithPath(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/public');
$request = new ServerRequest([], [], 'https://example.com/public', 'GET');

$default = static function (): ResponseInterface {
$response = (new ResponseFactory())->createResponse();
Expand All @@ -466,8 +453,7 @@ public function testShouldReturn200WithoutTokenWithPath(): void

public function testShouldReturn200WithoutTokenWithIgnore(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api/ping');
$request = new ServerRequest([], [], 'https://example.com/api/ping', 'GET');

$default = static function (): ResponseInterface {
$response = (new ResponseFactory())->createResponse();
Expand All @@ -492,8 +478,7 @@ public function testShouldReturn200WithoutTokenWithIgnore(): void

public function testShouldNotAllowInsecure(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'http://example.com/api')
$request = (new ServerRequest([], [], 'http://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (): ResponseInterface {
Expand All @@ -517,8 +502,7 @@ public function testShouldNotAllowInsecure(): void

public function testShouldAllowInsecure(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'http://example.com/api')
$request = (new ServerRequest([], [], 'http://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (): ResponseInterface {
Expand All @@ -544,8 +528,7 @@ public function testShouldAllowInsecure(): void

public function testShouldRelaxInsecureInLocalhost(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'http://localhost/api')
$request = (new ServerRequest([], [], 'http://localhost/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (): ResponseInterface {
Expand All @@ -570,8 +553,7 @@ public function testShouldRelaxInsecureInLocalhost(): void

public function testShouldRelaxInsecureInExampleCom(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'http://example.com/api')
$request = (new ServerRequest([], [], 'http://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (): ResponseInterface {
Expand All @@ -597,8 +579,7 @@ public function testShouldRelaxInsecureInExampleCom(): void

public function testShouldAttachToken(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (ServerRequestInterface $request): ResponseInterface {
Expand Down Expand Up @@ -631,8 +612,7 @@ public function testShouldAttachToken(): void

public function testShouldAttachCustomToken(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (ServerRequestInterface $request): ResponseInterface {
Expand Down Expand Up @@ -665,8 +645,7 @@ public function testShouldAttachCustomToken(): void

public function testShouldCallAfterWithProperArguments(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (): ResponseInterface {
Expand Down Expand Up @@ -698,8 +677,7 @@ public function __invoke(ResponseInterface $response, Plain $token): ResponseInt

public function testShouldCallBeforeWithProperArguments(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (ServerRequestInterface $request): ResponseInterface {
Expand Down Expand Up @@ -733,8 +711,7 @@ public function __invoke(ServerRequestInterface $request, Plain $token): ServerR

public function testShouldCallAnonymousErrorFunction(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api');
$request = new ServerRequest([], [], 'https://example.com/api', 'GET');

$default = static function (): ResponseInterface {
$response = (new ResponseFactory())->createResponse();
Expand Down Expand Up @@ -768,8 +745,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res

public function testShouldCallInvokableErrorClass(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api');
$request = new ServerRequest([], [], 'https://example.com/api', 'GET');

$default = static function (): ResponseInterface {
$response = (new ResponseFactory())->createResponse();
Expand All @@ -795,8 +771,7 @@ public function testShouldCallInvokableErrorClass(): void

public function testShouldCallErrorAndModifyBody(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api');
$request = new ServerRequest([], [], 'https://example.com/api', 'GET');

$default = static function (): ResponseInterface {
$response = (new ResponseFactory())->createResponse();
Expand Down Expand Up @@ -828,8 +803,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res

public function testShouldAllowUnauthenticatedHttp(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/public/foo');
$request = new ServerRequest([], [], 'https://example.com/public/foo', 'GET');

$default = static function (): ResponseInterface {
$response = (new ResponseFactory())->createResponse();
Expand All @@ -853,8 +827,7 @@ public function testShouldAllowUnauthenticatedHttp(): void

public function testShouldReturn401FromAfter(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (): ResponseInterface {
Expand Down Expand Up @@ -887,8 +860,7 @@ public function __invoke(ResponseInterface $response, Plain $token): ResponseInt

public function testShouldModifyRequestUsingAnonymousBefore(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/')
$request = (new ServerRequest([], [], 'https://example.com/', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (ServerRequestInterface $request): ResponseInterface {
Expand Down Expand Up @@ -922,8 +894,7 @@ public function __invoke(ServerRequestInterface $request, Plain $token): ServerR

public function testShouldModifyRequestUsingInvokableBefore(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/')
$request = (new ServerRequest([], [], 'https://example.com/', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (ServerRequestInterface $request): ResponseInterface {
Expand Down Expand Up @@ -952,8 +923,7 @@ public function testShouldModifyRequestUsingInvokableBefore(): void

public function testShouldHandleRulesArrayBug84(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api');
$request = new ServerRequest([], [], 'https://example.com/api', 'GET');

$default = static function (): ResponseInterface {
$response = (new ResponseFactory())->createResponse();
Expand Down Expand Up @@ -981,8 +951,7 @@ public function testShouldHandleRulesArrayBug84(): void
self::assertEquals(401, $response->getStatusCode());
self::assertEquals('', $response->getBody());

$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api/login');
$request = new ServerRequest([], [], 'https://example.com/api/login', 'GET');

$response = $collection->dispatch($request, $default);

Expand All @@ -992,8 +961,7 @@ public function testShouldHandleRulesArrayBug84(): void

public function testShouldHandleDefaultPathBug118(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api');
$request = new ServerRequest([], [], 'https://example.com/api', 'GET');

$default = static function (): ResponseInterface {
$response = (new ResponseFactory())->createResponse();
Expand All @@ -1018,8 +986,7 @@ public function testShouldHandleDefaultPathBug118(): void
self::assertEquals(401, $response->getStatusCode());
self::assertEquals('', $response->getBody());

$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api/login');
$request = new ServerRequest([], [], 'https://example.com/api/login', 'GET');

$response = $collection->dispatch($request, $default);

Expand All @@ -1029,8 +996,7 @@ public function testShouldHandleDefaultPathBug118(): void

public function testShouldBindToMiddleware(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/')
$request = (new ServerRequest([], [], 'https://example.com/', 'GET'))
->withHeader('Authorization', 'Bearer ' . self::$acmeToken);

$default = static function (ServerRequestInterface $request): ResponseInterface {
Expand Down Expand Up @@ -1071,8 +1037,7 @@ public function __invoke(ServerRequestInterface $request, Plain $token): ServerR

public function testShouldHandlePsr7(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withHeader('X-Token', 'Bearer ' . self::$acmeToken);

$response = (new ResponseFactory())->createResponse();
Expand All @@ -1096,8 +1061,7 @@ public function testShouldHandlePsr7(): void

public function testShouldHaveUriInErrorHandlerIssue96(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api/foo?bar=pop');
$request = new ServerRequest([], [], 'https://example.com/api/foo?bar=pop', 'GET');

$default = static function (): ResponseInterface {
$response = (new ResponseFactory())->createResponse();
Expand Down Expand Up @@ -1131,8 +1095,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res

public function testShouldUseCookieIfHeaderMissingIssue156(): void
{
$request = (new ServerRequestFactory())
->createServerRequest('GET', 'https://example.com/api')
$request = (new ServerRequest([], [], 'https://example.com/api', 'GET'))
->withCookieParams(['token' => self::$acmeToken]);

$default = static function (): ResponseInterface {
Expand Down

0 comments on commit 1ecbe0e

Please sign in to comment.