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
|
|
|
|
66
|
|
|
$handler = new class implements RequestHandlerInterface |
67
|
|
|
{ |
68
|
|
|
public function handle(ServerRequestInterface $request): ResponseInterface |
69
|
|
|
{ |
70
|
|
|
return new ServerResponse('hello'); |
71
|
|
|
} |
72
|
|
|
}; |
73
|
|
|
|
74
|
|
|
$response = $this->middleware->process(new ServerRequest, $handler); |
75
|
|
|
|
76
|
|
|
$this->assertEquals('', $response->getHeaderLine('location')); |
77
|
|
|
$this->assertSame(StatusCode::OK, $response->getStatusCode()); |
78
|
|
|
$this->assertEquals('hello', (string)$response->getBody()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
protected function setUp() |
82
|
|
|
{ |
83
|
|
|
$this->middleware = new SessionAuthenticatedMiddleware((new Config)->import([ |
84
|
|
|
SessionAuthenticatedMiddleware::LOGIN_URI => '/signin', |
85
|
|
|
])); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|