1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Mockery as m; |
4
|
|
|
|
5
|
|
|
class BlacklistTest extends PHPUnit_Framework_TestCase |
6
|
|
|
{ |
7
|
|
|
public function testGetKey() |
8
|
|
|
{ |
9
|
|
|
$id = uniqid(); |
10
|
|
|
$exp = time() + 3600; |
11
|
|
|
$claims = [ |
12
|
|
|
'foo' => 'bar', |
13
|
|
|
]; |
14
|
|
|
|
15
|
|
|
$storage = m::mock(\Framgia\Jwt\Contracts\Storage::class); |
16
|
|
|
|
17
|
|
|
$list = new \Framgia\Jwt\Blacklist($storage); |
18
|
|
|
|
19
|
|
|
$token = $this->tokenStub($id, $exp, $claims); |
20
|
|
|
|
21
|
|
|
$this->assertEquals($id, $list->getKey($token)); |
22
|
|
|
|
23
|
|
|
$list->setKey('foo'); |
24
|
|
|
|
25
|
|
|
$this->assertEquals('bar', $list->getKey($token)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testAdd() |
29
|
|
|
{ |
30
|
|
|
$id = uniqid(); |
31
|
|
|
$exp = time() + 3600; |
32
|
|
|
|
33
|
|
|
$storage = m::mock(\Framgia\Jwt\Contracts\Storage::class); |
34
|
|
|
|
35
|
|
|
$list = new \Framgia\Jwt\Blacklist($storage); |
36
|
|
|
|
37
|
|
|
$storage->shouldReceive('add')->once()->with($id, m::any(), m::any()); |
38
|
|
|
|
39
|
|
|
$list->add($this->tokenStub($id, $exp)); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function testAddForever() |
43
|
|
|
{ |
44
|
|
|
$id = uniqid(); |
45
|
|
|
|
46
|
|
|
$storage = m::mock(\Framgia\Jwt\Contracts\Storage::class); |
47
|
|
|
|
48
|
|
|
$list = new \Framgia\Jwt\Blacklist($storage); |
49
|
|
|
|
50
|
|
|
$storage->shouldReceive('forever')->once()->with($id, 'forever'); |
51
|
|
|
|
52
|
|
|
$list->add($this->tokenStub($id)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testHas() |
56
|
|
|
{ |
57
|
|
|
$id = uniqid(); |
58
|
|
|
$exp = time() - 3600; |
59
|
|
|
|
60
|
|
|
$storage = m::mock(\Framgia\Jwt\Contracts\Storage::class); |
61
|
|
|
|
62
|
|
|
$list = new \Framgia\Jwt\Blacklist($storage); |
63
|
|
|
|
64
|
|
|
$storage->shouldReceive('get')->once()->with($id)->andReturn([ |
65
|
|
|
'valid_until' => $exp, |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
$this->assertTrue($list->has($this->tokenStub($id, $exp))); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function testHasNonExisting() |
72
|
|
|
{ |
73
|
|
|
$id = uniqid(); |
74
|
|
|
$exp = time() + 3600; |
75
|
|
|
|
76
|
|
|
$storage = m::mock(\Framgia\Jwt\Contracts\Storage::class); |
77
|
|
|
|
78
|
|
|
$list = new \Framgia\Jwt\Blacklist($storage); |
79
|
|
|
|
80
|
|
|
$storage->shouldReceive('get')->once()->with($id)->andReturn(null); |
81
|
|
|
|
82
|
|
|
$this->assertFalse($list->has($this->tokenStub($id, $exp))); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testHasFuture() |
86
|
|
|
{ |
87
|
|
|
$id = uniqid(); |
88
|
|
|
$exp = time() + 3600; |
89
|
|
|
|
90
|
|
|
$storage = m::mock(\Framgia\Jwt\Contracts\Storage::class); |
91
|
|
|
|
92
|
|
|
$list = new \Framgia\Jwt\Blacklist($storage); |
93
|
|
|
|
94
|
|
|
$storage->shouldReceive('get')->once()->with($id)->andReturn([ |
95
|
|
|
'valid_until' => $exp, |
96
|
|
|
]); |
97
|
|
|
|
98
|
|
|
$this->assertFalse($list->has($this->tokenStub($id, $exp))); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testHasForever() |
102
|
|
|
{ |
103
|
|
|
$id = uniqid(); |
104
|
|
|
|
105
|
|
|
$storage = m::mock(\Framgia\Jwt\Contracts\Storage::class); |
106
|
|
|
|
107
|
|
|
$list = new \Framgia\Jwt\Blacklist($storage); |
108
|
|
|
|
109
|
|
|
$storage->shouldReceive('get')->once()->with($id)->andReturn('forever'); |
110
|
|
|
|
111
|
|
|
$this->assertTrue($list->has($this->tokenStub($id))); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function testRemove() |
115
|
|
|
{ |
116
|
|
|
$id = uniqid(); |
117
|
|
|
|
118
|
|
|
$storage = m::mock(\Framgia\Jwt\Contracts\Storage::class); |
119
|
|
|
|
120
|
|
|
$list = new \Framgia\Jwt\Blacklist($storage); |
121
|
|
|
|
122
|
|
|
$storage->shouldReceive('destroy')->once()->with($id)->andReturn(true); |
123
|
|
|
|
124
|
|
|
$this->assertTrue($list->remove($this->tokenStub($id))); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function testClear() |
128
|
|
|
{ |
129
|
|
|
$storage = m::mock(\Framgia\Jwt\Contracts\Storage::class); |
130
|
|
|
|
131
|
|
|
$list = new \Framgia\Jwt\Blacklist($storage); |
132
|
|
|
|
133
|
|
|
$storage->shouldReceive('flush')->once()->withNoArgs(); |
134
|
|
|
|
135
|
|
|
$this->assertTrue($list->clear()); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testSetRefreshTTL() |
139
|
|
|
{ |
140
|
|
|
$storage = m::mock(\Framgia\Jwt\Contracts\Storage::class); |
141
|
|
|
|
142
|
|
|
$list = new \Framgia\Jwt\Blacklist($storage); |
143
|
|
|
|
144
|
|
|
$reflection = new ReflectionClass($list); |
145
|
|
|
$property = $reflection->getProperty('refreshTTL'); |
146
|
|
|
$property->setAccessible(true); |
147
|
|
|
|
148
|
|
|
$this->assertEquals($list, $list->setRefreshTTL(500)); |
149
|
|
|
$this->assertEquals(500, $property->getValue($list)); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testSetGracePeriod() |
153
|
|
|
{ |
154
|
|
|
$storage = m::mock(\Framgia\Jwt\Contracts\Storage::class); |
155
|
|
|
|
156
|
|
|
$list = new \Framgia\Jwt\Blacklist($storage); |
157
|
|
|
|
158
|
|
|
$reflection = new ReflectionClass($list); |
159
|
|
|
$property = $reflection->getProperty('gracePeriod'); |
160
|
|
|
$property->setAccessible(true); |
161
|
|
|
|
162
|
|
|
$this->assertEquals($list, $list->setGracePeriod(500)); |
163
|
|
|
$this->assertEquals(500, $property->getValue($list)); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
protected function tokenStub($id = null, $exp = null, $claims = []) |
167
|
|
|
{ |
168
|
|
|
$builder = new \Lcobucci\JWT\Builder(); |
169
|
|
|
|
170
|
|
|
if ($id) { |
171
|
|
|
$builder->setId($id); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if ($exp) { |
175
|
|
|
$builder->setExpiration($exp); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
foreach ($claims as $key => $value) { |
179
|
|
|
$builder->set($key, $value); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return $builder->getToken(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|