|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Koded\Session; |
| 4 | + |
| 5 | +use Koded\Http\{ServerRequest, ServerResponse, StatusCode}; |
| 6 | +use Koded\Stdlib\Config; |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Psr\Http\Message\{ResponseInterface, ServerRequestInterface}; |
| 9 | +use Psr\Http\Server\RequestHandlerInterface; |
| 10 | + |
| 11 | +class SessionAuthenticatedMiddlewareTest extends TestCase |
| 12 | +{ |
| 13 | + |
| 14 | + /** @var SessionAuthenticatedMiddleware */ |
| 15 | + private $middleware; |
| 16 | + |
| 17 | + public function test__construct() |
| 18 | + { |
| 19 | + $this->assertAttributeEquals('/signin', 'redirectTo', $this->middleware); |
| 20 | + } |
| 21 | + |
| 22 | + public function test_process_when_not_authenticated() |
| 23 | + { |
| 24 | + $handler = new class implements RequestHandlerInterface |
| 25 | + { |
| 26 | + public function handle(ServerRequestInterface $request): ResponseInterface |
| 27 | + { |
| 28 | + return new ServerResponse('hello'); |
| 29 | + } |
| 30 | + }; |
| 31 | + |
| 32 | + $this->assertFalse(isset($_SESSION[SessionAuthenticatedMiddleware::AUTHENTICATED])); |
| 33 | + |
| 34 | + $response = $this->middleware->process(new ServerRequest, $handler); |
| 35 | + |
| 36 | + $this->assertEquals('/signin', $response->getHeaderLine('location')); |
| 37 | + $this->assertSame(StatusCode::TEMPORARY_REDIRECT, $response->getStatusCode()); |
| 38 | + $this->assertEmpty((string)$response->getBody()); |
| 39 | + } |
| 40 | + |
| 41 | + public function test_process_ajax_when_not_authenticated() |
| 42 | + { |
| 43 | + $_SERVER['HTTP_X_REQUESTED_WITH'] = 'XmlHttpRequest'; |
| 44 | + |
| 45 | + $handler = new class implements RequestHandlerInterface |
| 46 | + { |
| 47 | + public function handle(ServerRequestInterface $request): ResponseInterface |
| 48 | + { |
| 49 | + return new ServerResponse(); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + $this->assertFalse(isset($_SESSION[SessionAuthenticatedMiddleware::AUTHENTICATED])); |
| 54 | + |
| 55 | + $response = $this->middleware->process(new ServerRequest, $handler); |
| 56 | + |
| 57 | + $this->assertEquals('', $response->getHeaderLine('location')); |
| 58 | + $this->assertSame(StatusCode::UNAUTHORIZED, $response->getStatusCode()); |
| 59 | + $this->assertSame('{"location":"\/signin","status":401}', (string)$response->getBody()); |
| 60 | + } |
| 61 | + |
| 62 | + public function test_process_when_authenticated() |
| 63 | + { |
| 64 | + $_SESSION[SessionAuthenticatedMiddleware::AUTHENTICATED] = true; |
| 65 | + $handler = new class implements RequestHandlerInterface |
| 66 | + { |
| 67 | + public function handle(ServerRequestInterface $request): ResponseInterface |
| 68 | + { |
| 69 | + return new ServerResponse('hello'); |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + $response = $this->middleware->process(new ServerRequest, $handler); |
| 74 | + |
| 75 | + $this->assertEquals('', $response->getHeaderLine('location')); |
| 76 | + $this->assertSame(StatusCode::OK, $response->getStatusCode()); |
| 77 | + $this->assertEquals('hello', (string)$response->getBody()); |
| 78 | + } |
| 79 | + |
| 80 | + protected function setUp() |
| 81 | + { |
| 82 | + $this->middleware = new SessionAuthenticatedMiddleware((new Config)->import([ |
| 83 | + SessionAuthenticatedMiddleware::LOGIN_URI => '/signin', |
| 84 | + ])); |
| 85 | + } |
| 86 | +} |
0 commit comments