forked from tymondesigns/jwt-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJWTAuthTest.php
308 lines (237 loc) · 9.43 KB
/
JWTAuthTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<?php
/*
* This file is part of jwt-auth.
*
* (c) Sean Tymon <tymon148@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tymon\JWTAuth\Test;
use Mockery;
use StdClass;
use Tymon\JWTAuth\Token;
use Tymon\JWTAuth\Factory;
use Tymon\JWTAuth\JWTAuth;
use Tymon\JWTAuth\Manager;
use Tymon\JWTAuth\Payload;
use Illuminate\Http\Request;
use Tymon\JWTAuth\Http\Parser\Parser;
use Tymon\JWTAuth\Test\Stubs\UserStub;
use Tymon\JWTAuth\Exceptions\JWTException;
use Tymon\JWTAuth\Contracts\Providers\Auth;
use Tymon\JWTAuth\Exceptions\TokenInvalidException;
class JWTAuthTest extends AbstractTestCase
{
/**
* @var \Mockery\MockInterface|\Tymon\JWTAuth\Manager
*/
protected $manager;
/**
* @var \Mockery\MockInterface|\Tymon\JWTAuth\Contracts\Providers\Auth
*/
protected $auth;
/**
* @var \Mockery\MockInterface|\Tymon\JWTAuth\Http\Parser\Parser
*/
protected $parser;
/**
* @var \Tymon\JWTAuth\JWTAuth
*/
protected $jwtAuth;
public function setUp()
{
$this->manager = Mockery::mock(Manager::class);
$this->auth = Mockery::mock(Auth::class);
$this->parser = Mockery::mock(Parser::class);
$this->jwtAuth = new JWTAuth($this->manager, $this->auth, $this->parser);
}
public function tearDown()
{
Mockery::close();
parent::tearDown();
}
/** @test */
public function it_should_return_a_token_when_passing_a_user()
{
$payloadFactory = Mockery::mock(Factory::class);
$payloadFactory->shouldReceive('make')->andReturn(Mockery::mock(Payload::class));
$this->manager
->shouldReceive('getPayloadFactory->customClaims')
->once()
->with(['sub' => 1, 'foo' => 'bar', 'role' => 'admin'])
->andReturn($payloadFactory);
$this->manager->shouldReceive('encode->get')->once()->andReturn('foo.bar.baz');
$token = $this->jwtAuth->fromUser(new UserStub);
$this->assertSame($token, 'foo.bar.baz');
}
/** @test */
public function it_should_return_a_token_when_passing_valid_credentials_to_attempt_method()
{
$payloadFactory = Mockery::mock(Factory::class);
$payloadFactory->shouldReceive('make')->andReturn(Mockery::mock(Payload::class));
$this->manager
->shouldReceive('getPayloadFactory->customClaims')
->once()
->with(['sub' => 1, 'foo' => 'bar', 'role' => 'admin'])
->andReturn($payloadFactory);
$this->manager->shouldReceive('encode->get')->once()->andReturn('foo.bar.baz');
$this->auth->shouldReceive('byCredentials')->once()->andReturn(true);
$this->auth->shouldReceive('user')->once()->andReturn(new UserStub);
$token = $this->jwtAuth->attempt(['foo' => 'bar']);
$this->assertSame($token, 'foo.bar.baz');
}
/** @test */
public function it_should_return_false_when_passing_invalid_credentials_to_attempt_method()
{
$this->manager->shouldReceive('encode->get')->never();
$this->auth->shouldReceive('byCredentials')->once()->andReturn(false);
$this->auth->shouldReceive('user')->never();
$token = $this->jwtAuth->attempt(['foo' => 'bar']);
$this->assertFalse($token);
}
/**
* @test
* @expectedException \Tymon\JWTAuth\Exceptions\JWTException
* @expectedExceptionMessage A token is required
*/
public function it_should_throw_an_exception_when_not_providing_a_token()
{
$this->jwtAuth->toUser();
}
/** @test */
public function it_should_return_the_owning_user_from_a_token_containing_an_existing_user()
{
$payload = Mockery::mock(Payload::class);
$payload->shouldReceive('get')->once()->with('sub')->andReturn(1);
$this->manager->shouldReceive('decode')->once()->andReturn($payload);
$this->auth->shouldReceive('byId')->once()->with(1)->andReturn(true);
$this->auth->shouldReceive('user')->once()->andReturn((object) ['id' => 1]);
$user = $this->jwtAuth->setToken('foo.bar.baz')->customClaims(['foo' => 'bar'])->authenticate();
$this->assertSame($user->id, 1);
}
/** @test */
public function it_should_return_false_when_passing_a_token_not_containing_an_existing_user()
{
$payload = Mockery::mock(Payload::class);
$payload->shouldReceive('get')->once()->with('sub')->andReturn(1);
$this->manager->shouldReceive('decode')->once()->andReturn($payload);
$this->auth->shouldReceive('byId')->once()->with(1)->andReturn(false);
$this->auth->shouldReceive('user')->never();
$user = $this->jwtAuth->setToken('foo.bar.baz')->authenticate();
$this->assertFalse($user);
}
/** @test */
public function it_should_refresh_a_token()
{
$newToken = Mockery::mock(Token::class);
$newToken->shouldReceive('get')->once()->andReturn('baz.bar.foo');
$this->manager->shouldReceive('customClaims->refresh')->once()->andReturn($newToken);
$result = $this->jwtAuth->setToken('foo.bar.baz')->refresh();
$this->assertSame($result, 'baz.bar.foo');
}
/** @test */
public function it_should_invalidate_a_token()
{
$token = new Token('foo.bar.baz');
$this->manager->shouldReceive('invalidate')->once()->with($token, false)->andReturn(true);
$this->jwtAuth->setToken($token)->invalidate();
}
/** @test */
public function it_should_force_invalidate_a_token_forever()
{
$token = new Token('foo.bar.baz');
$this->manager->shouldReceive('invalidate')->once()->with($token, true)->andReturn(true);
$this->jwtAuth->setToken($token)->invalidate(true);
}
/** @test */
public function it_should_retrieve_the_token_from_the_request()
{
$this->parser->shouldReceive('parseToken')->andReturn('foo.bar.baz');
$this->assertInstanceOf(Token::class, $this->jwtAuth->parseToken()->getToken());
$this->assertEquals($this->jwtAuth->getToken(), 'foo.bar.baz');
}
/** @test */
public function it_should_get_the_authenticated_user()
{
$manager = $this->jwtAuth->manager();
$this->assertInstanceOf(Manager::class, $manager);
}
/** @test */
public function it_should_return_false_if_the_token_is_invalid()
{
$this->parser->shouldReceive('parseToken')->andReturn('foo.bar.baz');
$this->manager->shouldReceive('decode')->once()->andThrow(new TokenInvalidException);
$this->assertFalse($this->jwtAuth->parseToken()->check());
}
/** @test */
public function it_should_return_true_if_the_token_is_valid()
{
$payload = Mockery::mock(Payload::class);
$this->parser->shouldReceive('parseToken')->andReturn('foo.bar.baz');
$this->manager->shouldReceive('decode')->once()->andReturn($payload);
$this->assertTrue($this->jwtAuth->parseToken()->check());
}
/**
* @test
* @expectedException \Tymon\JWTAuth\Exceptions\JWTException
* @expectedExceptionMessage The token could not be parsed from the request
*/
public function it_should_throw_an_exception_when_token_not_present_in_request()
{
$this->parser->shouldReceive('parseToken')->andReturn(false);
$this->jwtAuth->parseToken();
}
/** @test */
public function it_should_return_false_when_no_token_is_set()
{
$this->parser->shouldReceive('parseToken')->andReturn(false);
$this->assertFalse($this->jwtAuth->getToken());
}
/** @test */
public function it_should_magically_call_the_manager()
{
$this->manager->shouldReceive('getBlacklist')->andReturn(new StdClass);
$blacklist = $this->jwtAuth->manager()->getBlacklist();
$this->assertInstanceOf(StdClass::class, $blacklist);
}
/** @test */
public function it_should_set_the_request()
{
$request = Request::create('/foo', 'GET', ['token' => 'some.random.token']);
$this->parser->shouldReceive('setRequest')->once()->with($request);
$this->parser->shouldReceive('parseToken')->andReturn('some.random.token');
$token = $this->jwtAuth->setRequest($request)->getToken();
$this->assertEquals('some.random.token', $token);
}
/** @test */
public function it_should_unset_the_token()
{
$this->parser->shouldReceive('parseToken')->andThrow(new JWTException);
$token = new Token('foo.bar.baz');
$this->jwtAuth->setToken($token);
$this->assertSame($this->jwtAuth->getToken(), $token);
$this->jwtAuth->unsetToken();
$this->assertFalse($this->jwtAuth->getToken());
}
/** @test */
public function it_should_get_the_manager_instance()
{
$manager = $this->jwtAuth->manager();
$this->assertInstanceOf(Manager::class, $manager);
}
/** @test */
public function it_should_get_the_parser_instance()
{
$parser = $this->jwtAuth->parser();
$this->assertInstanceOf(Parser::class, $parser);
}
/** @test */
public function it_should_get_a_claim_value()
{
$payload = Mockery::mock(Payload::class);
$payload->shouldReceive('get')->once()->with('sub')->andReturn(1);
$this->manager->shouldReceive('decode')->once()->andReturn($payload);
$this->assertSame($this->jwtAuth->setToken('foo.bar.baz')->getClaim('sub'), 1);
}
}