forked from tymondesigns/jwt-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPayloadTest.php
More file actions
255 lines (210 loc) 路 7.04 KB
/
Copy pathPayloadTest.php
File metadata and controls
255 lines (210 loc) 路 7.04 KB
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
<?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\Payload;
use Tymon\JWTAuth\Claims\Claim;
use Tymon\JWTAuth\Claims\JwtId;
use Tymon\JWTAuth\Claims\Issuer;
use Tymon\JWTAuth\Claims\Subject;
use Tymon\JWTAuth\Claims\Audience;
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;
class PayloadTest extends AbstractTestCase
{
/**
* @var \Mockery\MockInterface|\Tymon\JWTAuth\Validators\PayloadValidator
*/
protected $validator;
/**
* @var \Tymon\JWTAuth\Payload
*/
protected $payload;
public function setUp()
{
parent::setUp();
$this->payload = $this->getTestPayload();
}
/**
* @param array $extraClaims
*
* @return \Tymon\JWTAuth\Payload
*/
private function getTestPayload(array $extraClaims = [])
{
$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'),
];
if ($extraClaims) {
$claims = array_merge($claims, $extraClaims);
}
$collection = Collection::make($claims);
$this->validator = Mockery::mock(PayloadValidator::class);
$this->validator->shouldReceive('setRefreshFlow->check')->andReturn($collection);
return new Payload($collection, $this->validator);
}
/**
* @test
* @expectedException \Tymon\JWTAuth\Exceptions\PayloadException
* @expectedExceptionMessage The payload is immutable
*/
public function it_should_throw_an_exception_when_trying_to_add_to_the_payload()
{
$this->payload['foo'] = 'bar';
}
/**
* @test
* @expectedException \Tymon\JWTAuth\Exceptions\PayloadException
* @expectedExceptionMessage The payload is immutable
*/
public function it_should_throw_an_exception_when_trying_to_remove_a_key_from_the_payload()
{
unset($this->payload['foo']);
}
/** @test */
public function it_should_cast_the_payload_to_a_string_as_json()
{
$this->assertSame((string) $this->payload, json_encode($this->payload->get(), JSON_UNESCAPED_SLASHES));
$this->assertJsonStringEqualsJsonString((string) $this->payload, json_encode($this->payload->get()));
}
/** @test */
public function it_should_allow_array_access_on_the_payload()
{
$this->assertTrue(isset($this->payload['iat']));
$this->assertSame($this->payload['sub'], 1);
$this->assertArrayHasKey('exp', $this->payload);
}
/** @test */
public function it_should_get_properties_of_payload_via_get_method()
{
$this->assertInternalType('array', $this->payload->get());
$this->assertSame($this->payload->get('sub'), 1);
$this->assertSame(
$this->payload->get(function () {
return 'jti';
}),
'foo'
);
}
/** @test */
public function it_should_get_multiple_properties_when_passing_an_array_to_the_get_method()
{
$values = $this->payload->get(['sub', 'jti']);
list($sub, $jti) = $values;
$this->assertInternalType('array', $values);
$this->assertSame($sub, 1);
$this->assertSame($jti, 'foo');
}
/** @test */
public function it_should_determine_whether_the_payload_has_a_claim()
{
$this->assertTrue($this->payload->has(new Subject(1)));
$this->assertFalse($this->payload->has(new Audience(1)));
}
/** @test */
public function it_should_magically_get_a_property()
{
$sub = $this->payload->getSubject();
$jti = $this->payload->getJwtId();
$iss = $this->payload->getIssuer();
$this->assertSame($sub, 1);
$this->assertSame($jti, 'foo');
$this->assertSame($iss, 'http://example.com');
}
/** @test */
public function it_should_invoke_the_instance_as_a_callable()
{
$payload = $this->payload;
$sub = $payload('sub');
$jti = $payload('jti');
$iss = $payload('iss');
$this->assertSame($sub, 1);
$this->assertSame($jti, 'foo');
$this->assertSame($iss, 'http://example.com');
$this->assertSame($payload(), $this->payload->toArray());
}
/**
* @test
* @expectedException \BadMethodCallException
* @expectedExceptionMessage The claim [getFoo] does not exist on the payload.
*/
public function it_should_throw_an_exception_when_magically_getting_a_property_that_does_not_exist()
{
$this->payload->getFoo();
}
/** @test */
public function it_should_get_the_claims()
{
$claims = $this->payload->getClaims();
$this->assertInstanceOf(Expiration::class, $claims['exp']);
$this->assertInstanceOf(JwtId::class, $claims['jti']);
$this->assertInstanceOf(Subject::class, $claims['sub']);
$this->assertContainsOnlyInstancesOf(Claim::class, $claims);
}
/** @test */
public function it_should_get_the_object_as_json()
{
$this->assertJsonStringEqualsJsonString(json_encode($this->payload), $this->payload->toJson());
}
/** @test */
public function it_should_count_the_claims()
{
$this->assertSame(6, $this->payload->count());
$this->assertCount(6, $this->payload);
}
/** @test */
public function it_should_match_values()
{
$values = $this->payload->toArray();
$values['sub'] = (string) $values['sub'];
$this->assertTrue($this->payload->matches($values));
}
/** @test */
public function it_should_match_strict_values()
{
$values = $this->payload->toArray();
$this->assertTrue($this->payload->matchesStrict($values));
$this->assertTrue($this->payload->matches($values, true));
}
/** @test */
public function it_should_not_match_empty_values()
{
$this->assertFalse($this->payload->matches([]));
}
/** @test */
public function it_should_not_match_values()
{
$values = $this->payload->toArray();
$values['sub'] = 'dummy_subject';
$this->assertFalse($this->payload->matches($values));
}
/** @test */
public function it_should_not_match_strict_values()
{
$values = $this->payload->toArray();
$values['sub'] = (string) $values['sub'];
$this->assertFalse($this->payload->matchesStrict($values));
$this->assertFalse($this->payload->matches($values, true));
}
/** @test */
public function it_should_not_match_a_non_existing_claim()
{
$values = ['foo' => 'bar'];
$this->assertFalse($this->payload->matches($values));
}
}