forked from tymondesigns/jwt-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactoryTest.php
253 lines (196 loc) · 9.92 KB
/
FactoryTest.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
<?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 Tymon\JWTAuth\Factory;
use Tymon\JWTAuth\Payload;
use Tymon\JWTAuth\Claims\JwtId;
use Tymon\JWTAuth\Claims\Custom;
use Tymon\JWTAuth\Claims\Issuer;
use Tymon\JWTAuth\Claims\Subject;
use Tymon\JWTAuth\Claims\IssuedAt;
use Tymon\JWTAuth\Claims\NotBefore;
use Tymon\JWTAuth\Claims\Collection;
use Tymon\JWTAuth\Claims\Expiration;
use Tymon\JWTAuth\Validators\PayloadValidator;
use Tymon\JWTAuth\Claims\Factory as ClaimFactory;
class FactoryTest extends AbstractTestCase
{
/**
* @var \Mockery\MockInterface|\Tymon\JWTAuth\Claims\Factory
*/
protected $claimFactory;
/**
* @var \Mockery\MockInterface|\Tymon\JWTAuth\Validators\PayloadValidator
*/
protected $validator;
/**
* @var \Tymon\JWTAuth\Factory
*/
protected $factory;
public function setUp()
{
parent::setUp();
$this->claimFactory = Mockery::mock(ClaimFactory::class);
$this->validator = Mockery::mock(PayloadValidator::class);
$this->factory = new Factory($this->claimFactory, $this->validator);
}
public function tearDown()
{
Mockery::close();
parent::tearDown();
}
/** @test */
public function it_should_return_a_payload_when_passing_an_array_of_claims()
{
$expTime = $this->testNowTimestamp + 3600;
// these are added from default claims
$this->claimFactory->shouldReceive('make')->twice()->with('iss')->andReturn(new Issuer('/foo'));
$this->claimFactory->shouldReceive('make')->twice()->with('exp')->andReturn(new Expiration($expTime));
$this->claimFactory->shouldReceive('make')->twice()->with('jti')->andReturn(new JwtId('foo'));
$this->claimFactory->shouldReceive('make')->twice()->with('nbf')->andReturn(new NotBefore(123));
$this->claimFactory->shouldReceive('make')->twice()->with('iat')->andReturn(new IssuedAt(123));
// custom claims that override
$this->claimFactory->shouldReceive('get')->twice()->with('sub', 1)->andReturn(new Subject(1));
$this->claimFactory->shouldReceive('get')->twice()->with('jti', 'foo')->andReturn(new JwtId('foo'));
$this->claimFactory->shouldReceive('get')->twice()->with('nbf', 123)->andReturn(new NotBefore(123));
$this->claimFactory->shouldReceive('get')->twice()->with('iat', 123)->andReturn(new IssuedAt(123));
$this->claimFactory->shouldReceive('getTTL')->andReturn(60);
// once
$claims = $this->factory->customClaims([
'sub' => 1,
'jti' => 'foo',
'iat' => 123,
'nbf' => 123,
])->buildClaimsCollection();
$this->validator->shouldReceive('setRefreshFlow->check')->andReturn($claims);
// twice
$payload = $this->factory->customClaims(['sub' => 1, 'jti' => 'foo', 'iat' => 123, 'nbf' => 123])->make();
$this->assertSame($payload->get('sub'), 1);
$this->assertSame($payload->get('iat'), 123);
$this->assertSame($payload['exp'], $expTime);
$this->assertSame($payload['jti'], 'foo');
$this->assertInstanceOf(Payload::class, $payload);
}
/** @test */
public function it_should_return_a_payload_when_chaining_claim_methods()
{
$this->claimFactory->shouldReceive('get')->twice()->with('sub', 1)->andReturn(new Subject(1));
$this->claimFactory->shouldReceive('get')->twice()->with('foo', 'baz')->andReturn(new Custom('foo', 'baz'));
$this->claimFactory->shouldReceive('make')->twice()->with('iss')->andReturn(new Issuer('/foo'));
$this->claimFactory->shouldReceive('make')->twice()->with('exp')->andReturn(new Expiration($this->testNowTimestamp + 3600));
$this->claimFactory->shouldReceive('make')->twice()->with('iat')->andReturn(new IssuedAt($this->testNowTimestamp));
$this->claimFactory->shouldReceive('make')->twice()->with('jti')->andReturn(new JwtId('foo'));
$this->claimFactory->shouldReceive('make')->twice()->with('nbf')->andReturn(new NotBefore($this->testNowTimestamp));
$this->claimFactory->shouldReceive('getTTL')->andReturn(60);
// once
$claims = $this->factory->sub(1)->foo('baz')->buildClaimsCollection();
$this->validator->shouldReceive('setRefreshFlow->check')->andReturn($claims);
// twice
$payload = $this->factory->sub(1)->foo('baz')->make();
$this->assertSame($payload['sub'], 1);
$this->assertSame($payload->get('jti'), 'foo');
$this->assertSame($payload->get('foo'), 'baz');
$this->assertInstanceOf(Payload::class, $payload);
}
/** @test */
public function it_should_return_a_payload_when_passing_miltidimensional_array_as_custom_claim_to_make_method()
{
// these are added from default claims
$this->claimFactory->shouldReceive('make')->twice()->with('iss')->andReturn(new Issuer('/foo'));
$this->claimFactory->shouldReceive('make')->twice()->with('exp')->andReturn(new Expiration($this->testNowTimestamp + 3600));
$this->claimFactory->shouldReceive('make')->twice()->with('jti')->andReturn(new JwtId('foo'));
$this->claimFactory->shouldReceive('make')->twice()->with('nbf')->andReturn(new NotBefore(123));
$this->claimFactory->shouldReceive('make')->twice()->with('iat')->andReturn(new IssuedAt(123));
// custom claims that override
$this->claimFactory->shouldReceive('get')->twice()->with('sub', 1)->andReturn(new Subject(1));
$this->claimFactory->shouldReceive('get')->twice()->with('foo', ['bar' => [0, 0, 0]])->andReturn(new Custom('foo', ['bar' => [0, 0, 0]]));
$this->claimFactory->shouldReceive('getTTL')->andReturn(60);
// once
$claims = $this->factory->sub(1)->foo(['bar' => [0, 0, 0]])->buildClaimsCollection();
$this->validator->shouldReceive('setRefreshFlow->check')->andReturn($claims);
// twice
$payload = $this->factory->sub(1)->foo(['bar' => [0, 0, 0]])->make();
$this->assertSame($payload->get('sub'), 1);
$this->assertSame($payload->get('jti'), 'foo');
$this->assertSame($payload->get('foo'), ['bar' => [0, 0, 0]]);
$this->assertSame($payload->get('foo.bar'), [0, 0, 0]);
$this->assertInstanceOf(Payload::class, $payload);
}
/** @test */
public function it_should_exclude_the_exp_claim_when_setting_ttl_to_null()
{
// these are added from default claims
$this->claimFactory->shouldReceive('make')->twice()->with('iss')->andReturn(new Issuer('/foo'));
$this->claimFactory->shouldReceive('make')->twice()->with('jti')->andReturn(new JwtId('foo'));
$this->claimFactory->shouldReceive('make')->twice()->with('nbf')->andReturn(new NotBefore(123));
$this->claimFactory->shouldReceive('make')->twice()->with('iat')->andReturn(new IssuedAt(123));
// custom claims that override
$this->claimFactory->shouldReceive('get')->twice()->with('sub', 1)->andReturn(new Subject(1));
$this->claimFactory->shouldReceive('setTTL')->with(null)->andReturn($this->claimFactory);
$this->claimFactory->shouldReceive('getTTL')->andReturn(null);
// once
$claims = $this->factory->setTTL(null)->sub(1)->buildClaimsCollection();
$this->validator->shouldReceive('setRefreshFlow->check')->andReturn($claims);
// twice
$payload = $this->factory->setTTL(null)->sub(1)->make();
$this->assertNull($payload->get('exp'));
$this->assertInstanceOf(Payload::class, $payload);
}
/** @test */
public function it_should_exclude_claims_from_previous_payloads()
{
$fooClaim = new Custom('foo', 'bar');
$barClaim = new Custom('baz', 'qux');
$this->claimFactory->shouldReceive('getTTL')->andReturn(60);
$this->claimFactory->shouldReceive('get')->with('foo', 'bar')->twice()->andReturn($fooClaim);
$this->claimFactory->shouldReceive('get')->with('baz', 'qux')->twice()->andReturn($barClaim);
$this->validator->shouldReceive('setRefreshFlow->check')->once()->andReturn(new Collection([$fooClaim, $barClaim]));
$payload = $this->factory->setDefaultClaims([])
->customClaims([
'foo' => 'bar',
'baz' => 'qux',
])->make();
$this->assertSame($payload->get('foo'), 'bar');
$this->assertSame($payload->get('baz'), 'qux');
$this->validator->shouldReceive('setRefreshFlow->check')->once()->andReturn(new Collection([$fooClaim]));
$payload = $this->factory->setDefaultClaims([])->customClaims(['foo' => 'bar'])->make(true);
$this->assertSame($payload->get('foo'), 'bar');
$this->assertFalse($payload->hasKey('baz'));
}
/** @test */
public function it_should_set_the_default_claims()
{
$this->factory->setDefaultClaims(['sub', 'iat']);
$this->assertSame($this->factory->getDefaultClaims(), ['sub', 'iat']);
}
/** @test */
public function it_should_get_payload_with_a_predefined_collection_of_claims()
{
$claims = [
new Subject(1),
new Issuer('http://example.com'),
new Expiration($this->testNowTimestamp + 3600),
new NotBefore($this->testNowTimestamp),
new IssuedAt($this->testNowTimestamp),
new JwtId('foo'),
];
$collection = Collection::make($claims);
$this->validator->shouldReceive('setRefreshFlow->check')->andReturn($collection);
$payload = $this->factory->withClaims($collection);
$this->assertInstanceOf(Payload::class, $payload);
$this->assertSame($payload->get('sub'), 1);
}
/** @test */
public function it_should_get_the_validator()
{
$this->assertInstanceOf(PayloadValidator::class, $this->factory->validator());
}
}