Completed
Push — develop ( 574df4...10ba76 )
by Wisoot
06:15
created

JwtServiceTest::testInvalidateToken()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 23
rs 9.0856
cc 3
eloc 15
nc 1
nop 0
1
<?php
2
3
use Carbon\Carbon;
4
use Illuminate\Support\Facades\Config;
5
use WWON\JwtGuard\Contract\TokenManager;
6
use WWON\JwtGuard\JwtService;
7
8
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...
9
{
10
11
    /**
12
     * @var Mockery\MockInterface
13
     */
14
    private $tokenManager;
15
    
16
    /**
17
     * @var JwtService
18
     */
19
    private $jwtService;
20
21
    /**
22
     * setUp method
23
     */
24
    public function setUp()
25
    {
26
        parent::setUp();
27
28
        Config::shouldReceive('get')
29
            ->once()
30
            ->with('jwt.secret')
31
            ->andReturn('abcdefg');
32
33
        Config::shouldReceive('get')
34
            ->once()
35
            ->with('jwt.leeway')
36
            ->andReturn(0);
37
38
        $this->tokenManager = Mockery::mock(TokenManager::class);
39
        $this->jwtService = new JwtService($this->tokenManager);
40
    }
41
42
    /**
43
     * tearDown method
44
     */
45
    public function tearDown()
46
    {
47
        unset($this->jwtService);
48
49
        parent::tearDown();
50
    }
51
52
    /**
53
     * testGetTokenForUser method
54
     */
55
    public function testGetTokenForUser()
56
    {
57
        $now = Carbon::now()->timestamp;
58
59
        $token = $this->getToken();
60
61
        $items = explode('.', $token);
62
        $claimBody = json_decode(base64_decode($items[1]));
63
64
        $this->assertEquals('http://www.test.com', $claimBody->iss);
65
        $this->assertEquals($now, $claimBody->iat);
66
        $this->assertEquals($now + 6000, $claimBody->exp);
67
        $this->assertEquals($now + 6000, $claimBody->nat);
68
    }
69
70
    /**
71
     * testGetUserIdFromToken method
72
     */
73 View Code Duplication
    public function testGetUserIdFromToken()
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...
74
    {
75
        $token = $this->getToken();
76
77
        $this->tokenManager->shouldReceive('check')
78
            ->once()
79
            ->with(Mockery::on(function($claim) {
80
                return $claim->sub == 5
81
                    && $claim->iss == 'http://www.test.com';
82
            }))
83
            ->andReturn(true);
84
85
        $userId = $this->jwtService->getUserIdFromToken($token);
86
87
        $this->assertEquals(5, $userId);
88
    }
89
90
    /**
91
     * testGetUserIdFromTokenWithInvalidClaim method
92
     */
93 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...
94
    {
95
        $token = $this->getToken();
96
97
        $this->tokenManager->shouldReceive('check')
98
            ->once()
99
            ->with(Mockery::on(function($claim) {
100
                return $claim->sub == 5
101
                    && $claim->iss == 'http://www.test.com';
102
            }))
103
            ->andReturn(false);
104
105
        $this->setExpectedException(\WWON\JwtGuard\Exceptions\InvalidTokenException::class);
106
107
        $userId = $this->jwtService->getUserIdFromToken($token);
0 ignored issues
show
Unused Code introduced by
$userId 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...
108
    }
109
110
    /**
111
     * testRefreshToken method
112
     */
113
    public function testRefreshToken()
114
    {
115
        $now = Carbon::now()->timestamp;
116
117
        $token = $this->getToken(true);
118
119
        $this->tokenManager->shouldReceive('check')
120
            ->once()
121
            ->with(Mockery::on(function($claim) {
122
                return $claim->sub == 5
123
                    && $claim->iss == 'http://www.test.com';
124
            }))
125
            ->andReturn(true);
126
127
        $this->tokenManager->shouldReceive('remove')
128
            ->once()
129
            ->with(Mockery::on(function($claim) {
130
                return $claim->sub == 5
131
                    && $claim->iss == 'http://www.test.com';
132
            }));
133
134
        $newToken = $this->jwtService->refreshToken($token);
135
136
        $items = explode('.', $newToken);
137
        $claimBody = json_decode(base64_decode($items[1]));
138
139
        $this->assertEquals('http://www.test.com', $claimBody->iss);
140
        $this->assertEquals($now, $claimBody->iat);
141
        $this->assertEquals($now + 6000, $claimBody->exp);
142
        $this->assertEquals($now + 6000, $claimBody->nat);
143
    }
144
145
    /**
146
     * testRefreshUnRefreshableToken method
147
     */
148 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...
149
    {
150
        $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...
151
152
        $token = $this->getToken();
153
154
        $this->tokenManager->shouldReceive('check')
155
            ->once()
156
            ->with(Mockery::on(function($claim) {
157
                return $claim->sub == 5
158
                && $claim->iss == 'http://www.test.com';
159
            }))
160
            ->andReturn(true);
161
162
        $this->setExpectedException(\WWON\JwtGuard\Exceptions\UnRefreshableException::class);
163
164
        $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...
165
    }
166
167
    /**
168
     * testInvalidateToken method
169
     */
170
    public function testInvalidateToken()
171
    {
172
        $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...
173
174
        $token = $this->getToken();
175
176
        $this->tokenManager->shouldReceive('check')
177
            ->once()
178
            ->with(Mockery::on(function($claim) {
179
                return $claim->sub == 5
180
                    && $claim->iss == 'http://www.test.com';
181
            }))
182
            ->andReturn(true);
183
184
        $this->tokenManager->shouldReceive('remove')
185
            ->once()
186
            ->with(Mockery::on(function($claim) {
187
                return $claim->sub == 5
188
                    && $claim->iss == 'http://www.test.com';
189
            }));
190
191
        $this->jwtService->invalidateToken($token);
192
    }
193
194
    /**
195
     * testWipeUserTokens method
196
     */
197
    public function testWipeUserTokens()
198
    {
199
        $this->tokenManager->shouldReceive('removeAll')
200
            ->once()
201
            ->with(5);
202
203
        $this->jwtService->wipeUserTokens(new User());
204
    }
205
206
    /**
207
     * getToken method
208
     *
209
     * @param bool $refreshable
210
     * @param int $ttl
211
     * @return string
212
     */
213
    protected function getToken($refreshable = false, $ttl = 100)
214
    {
215
        Config::shouldReceive('get')
216
            ->once()
217
            ->with('app.url')
218
            ->andReturn('http://www.test.com');
219
220
        Config::shouldReceive('get')
221
            ->once()
222
            ->with('jwt.ttl')
223
            ->andReturn($ttl);
224
225
        Config::shouldReceive('get')
226
            ->once()
227
            ->with('jwt.ttl')
228
            ->andReturn($ttl);
229
230
        Config::shouldReceive('get')
231
            ->once()
232
            ->with('jwt.leeway')
233
            ->andReturn(0);
234
235
        Config::shouldReceive('get')
236
            ->once()
237
            ->with('jwt.algo')
238
            ->andReturn('HS256');
239
240
        $this->tokenManager->shouldReceive('add')
241
            ->once()
242
            ->with(Mockery::on(function($claim) {
243
                return $claim->sub == 5
244
                && $claim->iss == 'http://www.test.com';
245
            }));
246
247
        $token = $this->jwtService->getTokenForUser(new User(), $refreshable);
248
249
        return $token;
250
    }
251
252
}