Completed
Push — develop ( bd9a33...3fa966 )
by Wisoot
02:32
created

JwtServiceTest::testRefreshToken()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 31
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 31
rs 8.8571
cc 3
eloc 21
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
     * getToken method
169
     *
170
     * @param bool $refreshable
171
     * @param int $ttl
172
     * @return string
173
     */
174
    protected function getToken($refreshable = false, $ttl = 100)
175
    {
176
        Config::shouldReceive('get')
177
            ->once()
178
            ->with('app.url')
179
            ->andReturn('http://www.test.com');
180
181
        Config::shouldReceive('get')
182
            ->once()
183
            ->with('jwt.ttl')
184
            ->andReturn($ttl);
185
186
        Config::shouldReceive('get')
187
            ->once()
188
            ->with('jwt.ttl')
189
            ->andReturn($ttl);
190
191
        Config::shouldReceive('get')
192
            ->once()
193
            ->with('jwt.leeway')
194
            ->andReturn(0);
195
196
        Config::shouldReceive('get')
197
            ->once()
198
            ->with('jwt.algo')
199
            ->andReturn('HS256');
200
201
        $this->tokenManager->shouldReceive('add')
202
            ->once()
203
            ->with(Mockery::on(function($claim) {
204
                return $claim->sub == 5
205
                && $claim->iss == 'http://www.test.com';
206
            }));
207
208
        $token = $this->jwtService->getTokenForUser(new User(), $refreshable);
209
210
        return $token;
211
    }
212
213
}