Completed
Push — develop ( 206596...c4148f )
by Wisoot
03:01
created

JwtServiceTest::getClaim()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 35
rs 8.8571
cc 1
eloc 26
nc 1
nop 2
1
<?php
2
3
use Carbon\Carbon;
4
use Illuminate\Support\Facades\Config;
5
use WWON\JwtGuard\Claim;
6
use WWON\JwtGuard\Contract\ClaimManager;
7
use WWON\JwtGuard\JwtService;
8
9
class JwtServiceTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
12
    /**
13
     * @var Mockery\MockInterface
14
     */
15
    private $claimManager;
16
    
17
    /**
18
     * @var JwtService
19
     */
20
    private $jwtService;
21
22
    /**
23
     * setUp method
24
     */
25
    public function setUp()
26
    {
27
        parent::setUp();
28
29
        Config::shouldReceive('get')
30
            ->once()
31
            ->with('jwt.secret')
32
            ->andReturn('abcdefg');
33
34
        Config::shouldReceive('get')
35
            ->once()
36
            ->with('jwt.leeway')
37
            ->andReturn(0);
38
39
        $this->claimManager = Mockery::mock(ClaimManager::class);
40
        $this->jwtService = new JwtService($this->claimManager);
41
    }
42
43
    /**
44
     * tearDown method
45
     */
46
    public function tearDown()
47
    {
48
        unset($this->jwtService);
49
50
        parent::tearDown();
51
    }
52
53
    /**
54
     * testGetTokenForClaim method
55
     */
56
    public function testGetTokenForClaim()
57
    {
58
        $now = Carbon::now()->timestamp;
59
60
        $token = $this->getToken();
61
62
        $items = explode('.', $token);
63
        $claimBody = json_decode(base64_decode($items[1]));
64
65
        $this->assertEquals('http://www.test.com', $claimBody->iss);
66
        $this->assertEquals($now, $claimBody->iat);
67
        $this->assertEquals($now + 6000, $claimBody->exp);
68
        $this->assertEquals($now + 6000, $claimBody->nat);
69
    }
70
71
    /**
72
     * testGetClaimFromToken method
73
     */
74
    public function testGetClaimFromToken()
75
    {
76
        $token = $this->getToken();
77
78
        $this->claimManager->shouldReceive('check')
79
            ->once()
80
            ->with(Mockery::on(function($claim) {
81
                return $claim->sub == 5
82
                    && $claim->iss == 'http://www.test.com';
83
            }))
84
            ->andReturn(true);
85
86
        $claim = $this->jwtService->getClaimFromToken($token);
87
88
        $this->assertEquals(5, $claim->sub);
89
        $this->assertEquals('User', $claim->aud);
90
        $this->assertEquals('http://www.test.com', $claim->iss);
91
    }
92
93
    /**
94
     * testGetUserIdFromTokenWithInvalidClaim method
95
     */
96 View Code Duplication
    public function testGetUserIdFromTokenWithInvalidClaim()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
97
    {
98
        $token = $this->getToken();
99
100
        $this->claimManager->shouldReceive('check')
101
            ->once()
102
            ->with(Mockery::on(function($claim) {
103
                return $claim->sub == 5
104
                    && $claim->iss == 'http://www.test.com';
105
            }))
106
            ->andReturn(false);
107
108
        $this->setExpectedException(\WWON\JwtGuard\Exceptions\InvalidTokenException::class);
109
110
        $claim = $this->jwtService->getClaimFromToken($token);
0 ignored issues
show
Unused Code introduced by
$claim is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
111
    }
112
113
    /**
114
     * testRefreshToken method
115
     */
116
    public function testRefreshToken()
117
    {
118
        $now = Carbon::now()->timestamp;
119
120
        $token = $this->getToken(true);
121
122
        $this->claimManager->shouldReceive('check')
123
            ->once()
124
            ->with(Mockery::on(function($claim) {
125
                return $claim->sub == 5
126
                    && $claim->iss == 'http://www.test.com';
127
            }))
128
            ->andReturn(true);
129
130
        $this->claimManager->shouldReceive('remove')
131
            ->once()
132
            ->with(Mockery::on(function($claim) {
133
                return $claim->sub == 5
134
                    && $claim->iss == 'http://www.test.com';
135
            }));
136
137
        $newToken = $this->jwtService->refreshToken($token);
138
139
        $items = explode('.', $newToken);
140
        $claimBody = json_decode(base64_decode($items[1]));
141
142
        $this->assertEquals('http://www.test.com', $claimBody->iss);
143
        $this->assertEquals($now, $claimBody->iat);
144
        $this->assertEquals($now + 60000, $claimBody->exp);
145
        $this->assertEquals($now + 6000, $claimBody->nat);
146
    }
147
148
    /**
149
     * testRefreshUnRefreshableToken method
150
     */
151 View Code Duplication
    public function testRefreshUnRefreshableToken()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
152
    {
153
        $now = Carbon::now()->timestamp;
0 ignored issues
show
Unused Code introduced by
$now is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
154
155
        $token = $this->getToken();
156
157
        $this->claimManager->shouldReceive('check')
158
            ->once()
159
            ->with(Mockery::on(function($claim) {
160
                return $claim->sub == 5
161
                    && $claim->iss == 'http://www.test.com';
162
            }))
163
            ->andReturn(true);
164
165
        $this->setExpectedException(\WWON\JwtGuard\Exceptions\UnRefreshableException::class);
166
167
        $newToken = $this->jwtService->refreshToken($token);
0 ignored issues
show
Unused Code introduced by
$newToken is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
168
    }
169
170
    /**
171
     * testInvalidateToken method
172
     */
173
    public function testInvalidateToken()
174
    {
175
        $now = Carbon::now()->timestamp;
0 ignored issues
show
Unused Code introduced by
$now is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
176
177
        $token = $this->getToken();
178
179
        $this->claimManager->shouldReceive('check')
180
            ->once()
181
            ->with(Mockery::on(function($claim) {
182
                return $claim->sub == 5
183
                    && $claim->iss == 'http://www.test.com';
184
            }))
185
            ->andReturn(true);
186
187
        $this->claimManager->shouldReceive('remove')
188
            ->once()
189
            ->with(Mockery::on(function($claim) {
190
                return $claim->sub == 5
191
                    && $claim->iss == 'http://www.test.com';
192
            }));
193
194
        $this->jwtService->invalidateToken($token);
195
    }
196
197
    /**
198
     * testWipeUserTokens method
199
     */
200
    public function testWipeUserTokens()
201
    {
202
        $claim = $this->getClaim();
203
204
        $this->claimManager->shouldReceive('removeAll')
205
            ->once()
206
            ->with(Mockery::on(function($claim) {
207
                return $claim->sub == 5
208
                    && $claim->iss == 'http://www.test.com';
209
            }));
210
211
        $this->jwtService->wipeUserTokens($claim);
212
    }
213
214
    /**
215
     * getToken method
216
     *
217
     * @param bool $refreshable
218
     * @param int $ttl
219
     * @return string
220
     */
221
    protected function getToken($refreshable = false, $ttl = 100)
222
    {
223
        $claim = $this->getClaim($refreshable, $ttl);
224
225
        $this->claimManager->shouldReceive('add')
226
            ->once()
227
            ->with(Mockery::on(function($claim) {
228
                return $claim->sub == 5
229
                    && $claim->iss == 'http://www.test.com';
230
            }));
231
232
        $token = $this->jwtService->getTokenForClaim($claim);
233
234
        return $token;
235
    }
236
237
    /**
238
     * getClaim method
239
     *
240
     * @param bool $refreshable
241
     * @param int $ttl
242
     * @return Claim
243
     */
244
    protected function getClaim($refreshable = false, $ttl = 100)
245
    {
246
        Config::shouldReceive('get')
247
            ->once()
248
            ->with('app.url')
249
            ->andReturn('http://www.test.com');
250
251
        Config::shouldReceive('get')
252
            ->once()
253
            ->with('jwt.ttl')
254
            ->andReturn($ttl);
255
256
        Config::shouldReceive('get')
257
            ->once()
258
            ->with('jwt.ttl')
259
            ->andReturn($ttl);
260
261
        Config::shouldReceive('get')
262
            ->once()
263
            ->with('jwt.leeway')
264
            ->andReturn(0);
265
266
        Config::shouldReceive('get')
267
            ->once()
268
            ->with('jwt.algo')
269
            ->andReturn('HS256');
270
271
        $claim = new Claim([
272
            'sub' => 5,
273
            'aud' => 'User',
274
            'refresh' => $refreshable
275
        ]);
276
277
        return $claim;
278
    }
279
280
}