1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Carbon\Carbon; |
4
|
|
|
use Illuminate\Support\Facades\Config; |
5
|
|
|
use WWON\JwtGuard\Claim; |
6
|
|
|
|
7
|
|
|
class ClaimTest extends PHPUnit_Framework_TestCase |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* testConstructor method |
12
|
|
|
*/ |
13
|
|
|
public function testConstructor() |
14
|
|
|
{ |
15
|
|
|
$ttl = 100; |
16
|
|
|
$now = Carbon::now()->timestamp; |
17
|
|
|
|
18
|
|
|
Config::shouldReceive('get') |
19
|
|
|
->once() |
20
|
|
|
->with('app.url') |
21
|
|
|
->andReturn('http://www.test.com'); |
22
|
|
|
|
23
|
|
|
Config::shouldReceive('get') |
24
|
|
|
->once() |
25
|
|
|
->with('jwt.ttl') |
26
|
|
|
->andReturn($ttl); |
27
|
|
|
|
28
|
|
|
Config::shouldReceive('get') |
29
|
|
|
->once() |
30
|
|
|
->with('jwt.ttl') |
31
|
|
|
->andReturn($ttl); |
32
|
|
|
|
33
|
|
|
Config::shouldReceive('get') |
34
|
|
|
->once() |
35
|
|
|
->with('jwt.leeway') |
36
|
|
|
->andReturn(0); |
37
|
|
|
|
38
|
|
|
$claim = new Claim(); |
39
|
|
|
|
40
|
|
|
$this->assertEquals('http://www.test.com', $claim->iss); |
41
|
|
|
$this->assertEquals($now, $claim->iat, '', 2); |
42
|
|
|
$this->assertEquals($now + 6000, $claim->exp, '', 2); |
43
|
|
|
$this->assertEquals($now + 6000, $claim->nat, '', 2); |
44
|
|
|
$this->assertEquals(0, $claim->leeway); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* testCreateClaim method |
49
|
|
|
*/ |
50
|
|
|
public function testCreateClaim() |
51
|
|
|
{ |
52
|
|
|
$now = Carbon::now()->timestamp; |
53
|
|
|
|
54
|
|
|
Config::shouldReceive('get') |
55
|
|
|
->once() |
56
|
|
|
->with('jwt.leeway') |
57
|
|
|
->andReturn(0); |
58
|
|
|
|
59
|
|
|
$claim = new Claim([ |
60
|
|
|
'iss' => 'http://www.test.com', |
61
|
|
|
'iat' => $now, |
62
|
|
|
'exp' => $now + 6000, |
63
|
|
|
'nat' => $now + 6000, |
64
|
|
|
'jti' => 'asdjhasiudhasud' |
65
|
|
|
]); |
66
|
|
|
|
67
|
|
|
$this->assertEquals('http://www.test.com', $claim->iss); |
68
|
|
|
$this->assertEquals($now, $claim->iat); |
69
|
|
|
$this->assertEquals($now + 6000, $claim->exp); |
70
|
|
|
$this->assertEquals($now + 6000, $claim->nat); |
71
|
|
|
$this->assertEquals('asdjhasiudhasud', $claim->jti); |
72
|
|
|
$this->assertEquals(0, $claim->leeway); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* testCreateClaimWithMalformedException method |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function testCreateClaimWithMalformedException() |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$now = Carbon::now()->timestamp; |
81
|
|
|
|
82
|
|
|
Config::shouldReceive('get') |
83
|
|
|
->once() |
84
|
|
|
->with('jwt.leeway') |
85
|
|
|
->andReturn(0); |
86
|
|
|
|
87
|
|
|
$this->setExpectedException(\WWON\JwtGuard\Exceptions\MalformedException::class); |
88
|
|
|
|
89
|
|
|
$claim = new Claim([ |
|
|
|
|
90
|
|
|
'iss' => 'http://www.test.com', |
91
|
|
|
'iat' => $now, |
92
|
|
|
'exp' => $now - 6000, |
93
|
|
|
'nat' => $now - 6000, |
94
|
|
|
'jti' => 'asdjhasiudhasud' |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* testCreateClaimWithInaccessibleException method |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function testCreateClaimWithInaccessibleException() |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
$now = Carbon::now()->timestamp; |
104
|
|
|
|
105
|
|
|
Config::shouldReceive('get') |
106
|
|
|
->once() |
107
|
|
|
->with('jwt.leeway') |
108
|
|
|
->andReturn(0); |
109
|
|
|
|
110
|
|
|
$this->setExpectedException(\WWON\JwtGuard\Exceptions\InaccessibleException::class); |
111
|
|
|
|
112
|
|
|
$claim = new Claim([ |
|
|
|
|
113
|
|
|
'iss' => 'http://www.test.com', |
114
|
|
|
'iat' => $now - 6000, |
115
|
|
|
'exp' => $now + 6000, |
116
|
|
|
'nat' => $now - 10, |
117
|
|
|
'jti' => 'asdjhasiudhasud' |
118
|
|
|
]); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* testCreateClaimWithTokenExpiredException method |
123
|
|
|
*/ |
124
|
|
View Code Duplication |
public function testCreateClaimWithTokenExpiredException() |
|
|
|
|
125
|
|
|
{ |
126
|
|
|
$now = Carbon::now()->timestamp; |
127
|
|
|
|
128
|
|
|
Config::shouldReceive('get') |
129
|
|
|
->once() |
130
|
|
|
->with('jwt.leeway') |
131
|
|
|
->andReturn(0); |
132
|
|
|
|
133
|
|
|
$this->setExpectedException(\WWON\JwtGuard\Exceptions\TokenExpiredException::class); |
134
|
|
|
|
135
|
|
|
$claim = new Claim([ |
|
|
|
|
136
|
|
|
'iss' => 'http://www.test.com', |
137
|
|
|
'iat' => $now - 6000, |
138
|
|
|
'exp' => $now - 1000, |
139
|
|
|
'nat' => $now - 1000, |
140
|
|
|
'jti' => 'asdjhasiudhasud' |
141
|
|
|
]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.