1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Spiral Framework. |
5
|
|
|
* |
6
|
|
|
* @license MIT |
7
|
|
|
* @author Anton Titov (Wolfy-J) |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Framework\Http; |
13
|
|
|
|
14
|
|
|
use Spiral\Encrypter\EncrypterFactory; |
15
|
|
|
use Spiral\Framework\HttpTest; |
16
|
|
|
use Spiral\Http\Http; |
17
|
|
|
|
18
|
|
|
class AuthSessionTest extends HttpTest |
19
|
|
|
{ |
20
|
|
|
public function setUp(): void |
21
|
|
|
{ |
22
|
|
|
$this->app = $this->makeApp(); |
23
|
|
|
$key = $this->app->get(EncrypterFactory::class)->generateKey(); |
24
|
|
|
|
25
|
|
|
$this->app = $this->makeApp([ |
26
|
|
|
'ENCRYPTER_KEY' => $key |
27
|
|
|
]); |
28
|
|
|
|
29
|
|
|
$this->http = $this->app->get(Http::class); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testNoToken(): void |
33
|
|
|
{ |
34
|
|
|
$this->assertSame( |
35
|
|
|
'none', |
36
|
|
|
(string)$this->get('/auth/token')->getBody() |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testLogin(): void |
41
|
|
|
{ |
42
|
|
|
$result = $this->get('/auth/login'); |
43
|
|
|
|
44
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
45
|
|
|
|
46
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
47
|
|
|
$this->assertTrue(isset($cookies['token'])); |
48
|
|
|
$this->assertTrue(isset($cookies['sid'])); |
49
|
|
|
|
50
|
|
|
$result = $this->get('/auth/token', [], [], $cookies); |
51
|
|
|
|
52
|
|
|
$this->assertNotSame('none', (string)$result->getBody()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testLogout(): void |
56
|
|
|
{ |
57
|
|
|
$result = $this->get('/auth/login'); |
58
|
|
|
|
59
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
60
|
|
|
|
61
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
62
|
|
|
$this->assertTrue(isset($cookies['token'])); |
63
|
|
|
$this->assertTrue(isset($cookies['sid'])); |
64
|
|
|
|
65
|
|
|
$result = $this->get('/auth/token', [], [], $cookies); |
66
|
|
|
$this->assertNotSame('none', (string)$result->getBody()); |
67
|
|
|
|
68
|
|
|
$result = $this->get('/auth/logout', [], [], $cookies); |
69
|
|
|
$this->assertSame('closed', (string)$result->getBody()); |
70
|
|
|
|
71
|
|
|
$result = $this->get('/auth/token', [], [], $cookies); |
72
|
|
|
$this->assertSame('none', (string)$result->getBody()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testLoginScope(): void |
76
|
|
|
{ |
77
|
|
|
$result = $this->get('/auth/login2'); |
78
|
|
|
|
79
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
80
|
|
|
|
81
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
82
|
|
|
$this->assertTrue(isset($cookies['token'])); |
83
|
|
|
$this->assertTrue(isset($cookies['sid'])); |
84
|
|
|
|
85
|
|
|
$result = $this->get('/auth/token2', [], [], $cookies); |
86
|
|
|
|
87
|
|
|
$this->assertNotSame('none', (string)$result->getBody()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function testLoginPayload(): void |
91
|
|
|
{ |
92
|
|
|
$result = $this->get('/auth/login2'); |
93
|
|
|
|
94
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
95
|
|
|
|
96
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
97
|
|
|
$this->assertTrue(isset($cookies['token'])); |
98
|
|
|
$this->assertTrue(isset($cookies['sid'])); |
99
|
|
|
|
100
|
|
|
$result = $this->get('/auth/token3', [], [], $cookies); |
101
|
|
|
|
102
|
|
|
$this->assertSame('{"userID":1}', (string)$result->getBody()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|