|
8 | 8 | namespace SELN\App\Test\UnitTest\Core\Middleware; |
9 | 9 |
|
10 | 10 | 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; |
11 | 17 | use SELN\App\Test\UnitTest\Core\UnitTemplate; |
12 | 18 |
|
13 | 19 | class AuthenticationMiddlewareTest extends Unit implements UnitTemplate |
14 | 20 | { |
| 21 | + private const TOKEN = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJpZCI6ImQ1YmI0NmI0LTU0MDgtNGE0NC1hNTNjLWVlZWVmMmEzMTA2OSIsImNyZWF0ZWRBdCI6eyJkYXRlIjoiMjAyMS0wOC0xMCAwOToyODoxMS40MjUyNTEiLCJ0aW1lem9uZV90eXBlIjozLCJ0aW1lem9uZSI6IlVUQyJ9fQ.AAxYREfAxllBSPCpbk6c8TeVf7cTquS8k9jn9grC1fCflMmjQADMmF8vC1WnJkbhoGl5RlwAbJ8W1lyfexkqJA'; |
| 22 | + private const ID = 'd5bb46b4-5408-4a44-a53c-eeeef2a31069'; |
15 | 23 |
|
16 | 24 | public function testSuccess(): void |
17 | 25 | { |
| 26 | + $jwt = new JWTService('h7P41RolcpHIS8v6D5gjfVLyVsy0xl4D'); |
| 27 | + $middleware = new AuthenticationMiddleware($jwt); |
18 | 28 |
|
| 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); |
19 | 44 | } |
20 | 45 |
|
21 | 46 | public function testException(): void |
|
0 commit comments