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 Cycle\ORM\ORMInterface; |
15
|
|
|
use Spiral\Auth\Cycle\Token; |
16
|
|
|
use Spiral\Encrypter\EncrypterFactory; |
17
|
|
|
use Spiral\Framework\HttpTest; |
18
|
|
|
use Spiral\Http\Http; |
19
|
|
|
|
20
|
|
|
class AuthCycleTest extends HttpTest |
21
|
|
|
{ |
22
|
|
|
public function setUp(): void |
23
|
|
|
{ |
24
|
|
|
$this->app = $this->makeApp(); |
25
|
|
|
$key = $this->app->get(EncrypterFactory::class)->generateKey(); |
26
|
|
|
|
27
|
|
|
$this->app = $this->makeApp([ |
28
|
|
|
'ENCRYPTER_KEY' => $key, |
29
|
|
|
'CYCLE_AUTH' => true |
30
|
|
|
]); |
31
|
|
|
|
32
|
|
|
$this->app->console()->run('cycle:sync'); |
33
|
|
|
|
34
|
|
|
$this->http = $this->app->get(Http::class); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function testNoToken(): void |
38
|
|
|
{ |
39
|
|
|
$this->assertSame( |
40
|
|
|
'none', |
41
|
|
|
(string)$this->get('/auth/token')->getBody() |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function testLogin(): void |
46
|
|
|
{ |
47
|
|
|
$result = $this->get('/auth/login'); |
48
|
|
|
|
49
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
50
|
|
|
|
51
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
52
|
|
|
$this->assertTrue(isset($cookies['token'])); |
53
|
|
|
|
54
|
|
|
$token = $this->app->get(ORMInterface::class)->getRepository(Token::class)->findOne(); |
55
|
|
|
|
56
|
|
|
$this->assertSame(['userID' => 1], $token->getPayload()); |
57
|
|
|
|
58
|
|
|
$result = $this->get('/auth/token', [], [], $cookies); |
59
|
|
|
|
60
|
|
|
$this->assertNotSame('none', (string)$result->getBody()); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function testLogout(): void |
64
|
|
|
{ |
65
|
|
|
$result = $this->get('/auth/login'); |
66
|
|
|
|
67
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
68
|
|
|
|
69
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
70
|
|
|
$this->assertTrue(isset($cookies['token'])); |
71
|
|
|
|
72
|
|
|
$result = $this->get('/auth/token', [], [], $cookies); |
73
|
|
|
$this->assertNotSame('none', (string)$result->getBody()); |
74
|
|
|
|
75
|
|
|
$result = $this->get('/auth/logout', [], [], $cookies); |
76
|
|
|
$this->assertSame('closed', (string)$result->getBody()); |
77
|
|
|
|
78
|
|
|
$result = $this->get('/auth/token', [], [], $cookies); |
79
|
|
|
$this->assertSame('none', (string)$result->getBody()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testLoginScope(): void |
83
|
|
|
{ |
84
|
|
|
$result = $this->get('/auth/login2'); |
85
|
|
|
|
86
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
87
|
|
|
|
88
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
89
|
|
|
$this->assertTrue(isset($cookies['token'])); |
90
|
|
|
|
91
|
|
|
$token = $this->app->get(ORMInterface::class)->getRepository(Token::class)->findOne(); |
92
|
|
|
|
93
|
|
|
$this->assertSame(['userID' => 1], $token->getPayload()); |
94
|
|
|
|
95
|
|
|
$result = $this->get('/auth/token2', [], [], $cookies); |
96
|
|
|
|
97
|
|
|
$this->assertNotSame('none', (string)$result->getBody()); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testLoginPayload(): void |
101
|
|
|
{ |
102
|
|
|
$result = $this->get('/auth/login2'); |
103
|
|
|
|
104
|
|
|
$this->assertSame('OK', (string)$result->getBody()); |
105
|
|
|
|
106
|
|
|
$cookies = $this->fetchCookies($result->getHeader('Set-Cookie')); |
107
|
|
|
$this->assertTrue(isset($cookies['token'])); |
108
|
|
|
|
109
|
|
|
$result = $this->get('/auth/token3', [], [], $cookies); |
110
|
|
|
|
111
|
|
|
$this->assertSame('{"userID":1}', (string)$result->getBody()); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|