Skip to content

Commit 51d588c

Browse files
update auth middleware test
1 parent 5989e41 commit 51d588c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

tests/unit/Core/Middleware/AuthenticationMiddlewareTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,39 @@
88
namespace SELN\App\Test\UnitTest\Core\Middleware;
99

1010
use Codeception\Test\Unit;
11+
use Laminas\Diactoros\ResponseFactory;
12+
use Laminas\Diactoros\ServerRequestFactory;
13+
use Psr\Http\Server\RequestHandlerInterface;
14+
use SELN\App\Core\Authentication\JWTService;
15+
use SELN\App\Core\Authentication\Passport\Passport;
16+
use SELN\App\Core\HTTP\Middleware\AuthenticationMiddleware;
1117
use SELN\App\Test\UnitTest\Core\UnitTemplate;
1218

1319
class AuthenticationMiddlewareTest extends Unit implements UnitTemplate
1420
{
21+
private const TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpZCI6ImQ1YmI0NmI0LTU0MDgtNGE0NC1hNTNjLWVlZWVmMmEzMTA2OSIsImNyZWF0ZWRBdCI6eyJkYXRlIjoiMjAyMS0wOC0xMCAwOToyODoxMS40MjUyNTEiLCJ0aW1lem9uZV90eXBlIjozLCJ0aW1lem9uZSI6IlVUQyJ9fQ.AAxYREfAxllBSPCpbk6c8TeVf7cTquS8k9jn9grC1fCflMmjQADMmF8vC1WnJkbhoGl5RlwAbJ8W1lyfexkqJA';
22+
private const ID = 'd5bb46b4-5408-4a44-a53c-eeeef2a31069';
1523

1624
public function testSuccess(): void
1725
{
26+
$jwt = new JWTService('h7P41RolcpHIS8v6D5gjfVLyVsy0xl4D');
27+
$middleware = new AuthenticationMiddleware($jwt);
1828

29+
$handler = $this->createStub(RequestHandlerInterface::class);
30+
$handler->method('handle')
31+
->willReturn($source = (new ResponseFactory())->createResponse());
32+
33+
$request = (new ServerRequestFactory())
34+
->createServerRequest('POST', 'http://localhost')
35+
->withHeader('Authorization', self::TOKEN);
36+
37+
$response = $middleware->process($request, $handler);
38+
39+
$decode = $jwt->decode($request->getHeaderLine('Authorization'));
40+
$this->assertIsBool($response->hasHeader('Authorization'));
41+
$this->assertIsObject($request->withAttribute(Passport::ATTRIBUTE, new Passport($decode->id)));
42+
$this->assertEquals(self::ID, $decode->id);
43+
$this->assertEquals($source, $response);
1944
}
2045

2146
public function testException(): void

0 commit comments

Comments
 (0)