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\Tests\Framework\Http; |
13
|
|
|
|
14
|
|
|
use Cycle\ORM\ORMInterface; |
15
|
|
|
use Cycle\ORM\TransactionInterface; |
16
|
|
|
use Spiral\Auth\Cycle\Token; |
17
|
|
|
use Spiral\Encrypter\EncrypterFactory; |
18
|
|
|
use Spiral\Http\Http; |
19
|
|
|
use Spiral\App\User\User; |
20
|
|
|
use Spiral\Tests\Framework\HttpTest; |
21
|
|
|
|
22
|
|
|
class AuthCycleTest extends HttpTest |
23
|
|
|
{ |
24
|
|
|
public function setUp(): void |
25
|
|
|
{ |
26
|
|
|
$this->app = $this->makeApp(); |
27
|
|
|
$key = $this->app->get(EncrypterFactory::class)->generateKey(); |
28
|
|
|
|
29
|
|
|
$this->app = $this->makeApp([ |
30
|
|
|
'ENCRYPTER_KEY' => $key, |
31
|
|
|
'CYCLE_AUTH' => true |
32
|
|
|
]); |
33
|
|
|
|
34
|
|
|
$this->app->console()->run('cycle:sync'); |
35
|
|
|
|
36
|
|
|
$this->http = $this->app->get(Http::class); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testNoToken(): void |
40
|
|
|
{ |
41
|
|
|
$this->assertSame( |
42
|
|
|
'none', |
43
|
|
|
(string)$this->get('/auth/token')->getBody() |
44
|
|
|
); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function testLogin(): void |
48
|
|
|
{ |
49
|
|
|
$result = $this->get('/auth/login'); |
50
|
|
|
|
51
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
52
|
|
|
|
53
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
54
|
|
|
$this->assertTrue(isset($cookies['token'])); |
55
|
|
|
|
56
|
|
|
$token = $this->app->get(ORMInterface::class)->getRepository(Token::class)->findOne(); |
57
|
|
|
|
58
|
|
|
$this->assertSame(['userID' => 1], $token->getPayload()); |
59
|
|
|
|
60
|
|
|
$result = $this->get('/auth/token', [], [], $cookies); |
61
|
|
|
|
62
|
|
|
$this->assertNotSame('none', (string)$result->getBody()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testGetActorNone(): void |
66
|
|
|
{ |
67
|
|
|
$result = $this->get('/auth/login'); |
68
|
|
|
|
69
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
70
|
|
|
|
71
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
72
|
|
|
$this->assertTrue(isset($cookies['token'])); |
73
|
|
|
|
74
|
|
|
$token = $this->app->get(ORMInterface::class)->getRepository(Token::class)->findOne(); |
75
|
|
|
|
76
|
|
|
$this->assertSame(['userID' => 1], $token->getPayload()); |
77
|
|
|
|
78
|
|
|
$result = $this->get('/auth/actor', [], [], $cookies); |
79
|
|
|
|
80
|
|
|
$this->assertSame('none', (string)$result->getBody()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testGetActorReal(): void |
84
|
|
|
{ |
85
|
|
|
$result = $this->get('/auth/login'); |
86
|
|
|
|
87
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
88
|
|
|
|
89
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
90
|
|
|
$this->assertTrue(isset($cookies['token'])); |
91
|
|
|
|
92
|
|
|
$token = $this->app->get(ORMInterface::class)->getRepository(Token::class)->findOne(); |
93
|
|
|
|
94
|
|
|
$this->assertSame(['userID' => 1], $token->getPayload()); |
95
|
|
|
|
96
|
|
|
$user = new User('Antony'); |
97
|
|
|
$user->id = 1; |
98
|
|
|
$this->app->get(TransactionInterface::class)->persist($user)->run(); |
99
|
|
|
|
100
|
|
|
$result = $this->get('/auth/actor', [], [], $cookies); |
101
|
|
|
$this->assertSame('Antony', (string)$result->getBody()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testLogout(): void |
105
|
|
|
{ |
106
|
|
|
$result = $this->get('/auth/login'); |
107
|
|
|
|
108
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
109
|
|
|
|
110
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
111
|
|
|
$this->assertTrue(isset($cookies['token'])); |
112
|
|
|
|
113
|
|
|
$result = $this->get('/auth/token', [], [], $cookies); |
114
|
|
|
$this->assertNotSame('none', (string)$result->getBody()); |
115
|
|
|
|
116
|
|
|
$result = $this->get('/auth/logout', [], [], $cookies); |
117
|
|
|
$this->assertSame('closed', (string)$result->getBody()); |
118
|
|
|
|
119
|
|
|
$result = $this->get('/auth/token', [], [], $cookies); |
120
|
|
|
$this->assertSame('none', (string)$result->getBody()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function testLoginScope(): void |
124
|
|
|
{ |
125
|
|
|
$result = $this->get('/auth/login2'); |
126
|
|
|
|
127
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
128
|
|
|
|
129
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
130
|
|
|
$this->assertTrue(isset($cookies['token'])); |
131
|
|
|
|
132
|
|
|
$token = $this->app->get(ORMInterface::class)->getRepository(Token::class)->findOne(); |
133
|
|
|
|
134
|
|
|
$this->assertSame(['userID' => 1], $token->getPayload()); |
135
|
|
|
|
136
|
|
|
$result = $this->get('/auth/token2', [], [], $cookies); |
137
|
|
|
|
138
|
|
|
$this->assertNotSame('none', (string)$result->getBody()); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testLoginPayload(): void |
142
|
|
|
{ |
143
|
|
|
$result = $this->get('/auth/login2'); |
144
|
|
|
|
145
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
146
|
|
|
|
147
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
148
|
|
|
$this->assertTrue(isset($cookies['token'])); |
149
|
|
|
|
150
|
|
|
$result = $this->get('/auth/token3', [], [], $cookies); |
151
|
|
|
|
152
|
|
|
$this->assertSame('{"userID":1}', (string)$result->getBody()); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|