Completed
Pull Request — master (#16)
by Guillaume
01:20
created

JwtManagerTest::testGetTokenWithTokenKeyOption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 38

Duplication

Lines 38
Ratio 100 %

Importance

Changes 0
Metric Value
dl 38
loc 38
rs 9.312
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Eljam\GuzzleJwt\Tests\Manager;
4
5
use Eljam\GuzzleJwt\JwtToken;
6
use Eljam\GuzzleJwt\Manager\JwtManager;
7
use Eljam\GuzzleJwt\Strategy\Auth\QueryAuthStrategy;
8
use GuzzleHttp\Client;
9
use GuzzleHttp\HandlerStack;
10
use GuzzleHttp\Handler\MockHandler;
11
use GuzzleHttp\Psr7\Response;
12
use Psr\Http\Message\RequestInterface;
13
14
/**
15
 * @author Guillaume Cavavana <[email protected]>
16
 */
17
class JwtManagerTest extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * testGetTokenWithSublevelResponse
21
     */
22
    public function testGetTokenWithSublevelResponse()
23
    {
24
        $mockHandler = new MockHandler([
25
            function (RequestInterface $request) {
26
27
                $this->assertTrue($request->hasHeader('timeout'));
28
                $this->assertEquals(
29
                    3,
30
                    $request->getHeaderLine('timeout')
31
                );
32
33
        $jsonPayload = <<<EOF
34
            {
35
                "status": "success",
36
                "message": "Login successful",
37
                "payload": {
38
                    "token": "1453720507"
39
                },
40
                "expires_in": 3600
41
            }
42
EOF;
43
44
                return new Response(
45
                    200,
46
                    ['Content-Type' => 'application/json'],
47
                    $jsonPayload
48
                );
49
            },
50
        ]);
51
52
        $handler = HandlerStack::create($mockHandler);
53
54
        $authClient = new Client([
55
            'handler' => $handler,
56
        ]);
57
58
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
59
60
        $jwtManager = new JwtManager(
61
            $authClient,
62
            $authStrategy,
63
            null,
64
            [
65
                'token_url' => '/api/token',
66
                'timeout' => 3,
67
                'token_key' => 'payload.token',
68
                'expire_key' => 'expires_in'
69
            ]
70
        );
71
        $token = $jwtManager->getJwtToken();
72
73
        $this->assertInstanceOf(JwtToken::class, $token);
74
        $this->assertEquals('1453720507', $token->getToken());
75
    }
76
77
    /**
78
     * testGetToken.
79
     */
80 View Code Duplication
    public function testGetToken()
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...
81
    {
82
        $mockHandler = new MockHandler([
83
            function (RequestInterface $request) {
84
85
                $this->assertTrue($request->hasHeader('timeout'));
86
                $this->assertEquals(
87
                    3,
88
                    $request->getHeaderLine('timeout')
89
                );
90
91
                return new Response(
92
                    200,
93
                    ['Content-Type' => 'application/json'],
94
                    json_encode(['token' => '1453720507'])
95
                );
96
            },
97
        ]);
98
99
        $handler = HandlerStack::create($mockHandler);
100
101
        $authClient = new Client([
102
            'handler' => $handler,
103
        ]);
104
105
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
106
107
        $jwtManager = new JwtManager(
108
            $authClient,
109
            $authStrategy,
110
            null,
111
            ['token_url' => '/api/token', 'timeout' => 3]
112
        );
113
        $token = $jwtManager->getJwtToken();
114
115
        $this->assertInstanceOf(JwtToken::class, $token);
116
        $this->assertEquals('1453720507', $token->getToken());
117
    }
118
119 View Code Duplication
    public function testGetTokenWithTokenKeyOption()
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...
120
    {
121
        $mockHandler = new MockHandler([
122
            function (RequestInterface $request) {
123
124
                $this->assertTrue($request->hasHeader('timeout'));
125
                $this->assertEquals(
126
                    3,
127
                    $request->getHeaderLine('timeout')
128
                );
129
130
                return new Response(
131
                    200,
132
                    ['Content-Type' => 'application/json'],
133
                    json_encode(['tokenkey' => '1453720507'])
134
                );
135
            },
136
        ]);
137
138
        $handler = HandlerStack::create($mockHandler);
139
140
        $authClient = new Client([
141
            'handler' => $handler,
142
        ]);
143
144
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
145
146
        $jwtManager = new JwtManager(
147
            $authClient,
148
            $authStrategy,
149
            null,
150
            ['token_url' => '/api/token', 'timeout' => 3, 'token_key' => 'tokenkey']
151
        );
152
        $token = $jwtManager->getJwtToken();
153
154
        $this->assertInstanceOf(JwtToken::class, $token);
155
        $this->assertEquals('1453720507', $token->getToken());
156
    }
157
158 View Code Duplication
    public function testGetTokenShouldGetNewTokenIfCachedTokenIsNotValid()
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...
159
    {
160
        $mockHandler = new MockHandler(
161
            [
162
                function (RequestInterface $request) {
163
164
                    $this->assertTrue($request->hasHeader('timeout'));
165
                    $this->assertEquals(
166
                        3,
167
                        $request->getHeaderLine('timeout')
168
                    );
169
170
                    return new Response(
171
                        200,
172
                        ['Content-Type' => 'application/json'],
173
                        json_encode(['token' => '1453720507'])
174
                    );
175
                },
176
                function (RequestInterface $request) {
177
178
                    $this->assertTrue($request->hasHeader('timeout'));
179
                    $this->assertEquals(
180
                        3,
181
                        $request->getHeaderLine('timeout')
182
                    );
183
184
                    return new Response(
185
                        200,
186
                        ['Content-Type' => 'application/json'],
187
                        json_encode(['token' => 'foo123'])
188
                    );
189
                },
190
            ]
191
        );
192
193
        $handler = HandlerStack::create($mockHandler);
194
195
        $authClient = new Client([
196
            'handler' => $handler,
197
        ]);
198
199
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
200
201
        $jwtManager = new JwtManager(
202
            $authClient,
203
            $authStrategy,
204
            null,
205
            ['token_url' => '/api/token', 'timeout' => 3]
206
        );
207
        $token = $jwtManager->getJwtToken();
208
209
        $this->assertInstanceOf(JwtToken::class, $token);
210
        $this->assertEquals('1453720507', $token->getToken());
211
212
        $token = $jwtManager->getJwtToken();
213
214
        $this->assertInstanceOf(JwtToken::class, $token);
215
        $this->assertEquals('foo123', $token->getToken());
216
    }
217
218 View Code Duplication
    public function testGetTokenShouldUseTheCachedTokenIfItIsValid()
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...
219
    {
220
        $mockHandler = new MockHandler(
221
            [
222
                function (RequestInterface $request) {
223
224
                    $this->assertTrue($request->hasHeader('timeout'));
225
                    $this->assertEquals(
226
                        3,
227
                        $request->getHeaderLine('timeout')
228
                    );
229
230
                    return new Response(
231
                        200,
232
                        ['Content-Type' => 'application/json'],
233
                        json_encode(['token' => '1453720507', 'expires_in' => 3600])
234
                    );
235
                },
236
                function (RequestInterface $request) {
237
238
                    $this->assertTrue($request->hasHeader('timeout'));
239
                    $this->assertEquals(
240
                        3,
241
                        $request->getHeaderLine('timeout')
242
                    );
243
244
                    return new Response(
245
                        200,
246
                        ['Content-Type' => 'application/json'],
247
                        json_encode(['token' => 'foo123'])
248
                    );
249
                },
250
            ]
251
        );
252
253
        $handler = HandlerStack::create($mockHandler);
254
255
        $authClient = new Client([
256
            'handler' => $handler,
257
        ]);
258
259
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
260
261
        $jwtManager = new JwtManager(
262
            $authClient,
263
            $authStrategy,
264
            null,
265
            ['token_url' => '/api/token', 'timeout' => 3]
266
        );
267
        $token = $jwtManager->getJwtToken();
268
269
        $this->assertInstanceOf(JwtToken::class, $token);
270
        $this->assertEquals('1453720507', $token->getToken());
271
272
        $token = $jwtManager->getJwtToken();
273
274
        $this->assertInstanceOf(JwtToken::class, $token);
275
        $this->assertEquals('1453720507', $token->getToken());
276
    }
277
278
    public function testGetTokenShouldUseTheCachedTokenIfItIsValidBasedOnExpField()
279
    {
280
        $jwtToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'
281
            . '.eyJleHAiOiIzMjUwMzY4MDAwMCJ9'
282
            . '.k4YJmJooaa9B4pAM_U8Pi-4ss6RdKFtj9iQqLIAndVA';
283
284
        $mockHandler = new MockHandler(
285
            [
286
                function (RequestInterface $request) use ($jwtToken) {
287
288
                    $this->assertTrue($request->hasHeader('timeout'));
289
                    $this->assertEquals(
290
                        3,
291
                        $request->getHeaderLine('timeout')
292
                    );
293
294
                    return new Response(
295
                        200,
296
                        ['Content-Type' => 'application/json'],
297
                        json_encode(['token' => $jwtToken])
298
                    );
299
                },
300
                function (RequestInterface $request) {
301
302
                    $this->assertTrue($request->hasHeader('timeout'));
303
                    $this->assertEquals(
304
                        3,
305
                        $request->getHeaderLine('timeout')
306
                    );
307
308
                    return new Response(
309
                        200,
310
                        ['Content-Type' => 'application/json'],
311
                        json_encode(['token' => uniqid('token', true)])
312
                    );
313
                },
314
            ]
315
        );
316
317
        $handler = HandlerStack::create($mockHandler);
318
319
        $authClient = new Client([
320
            'handler' => $handler,
321
        ]);
322
323
        $authStrategy = new QueryAuthStrategy(['username' => 'admin', 'password' => 'admin']);
324
325
        $jwtManager = new JwtManager(
326
            $authClient,
327
            $authStrategy,
328
            null,
329
            ['token_url' => '/api/token', 'timeout' => 3]
330
        );
331
        $token = $jwtManager->getJwtToken();
332
333
        $this->assertInstanceOf(JwtToken::class, $token);
334
        $this->assertEquals($jwtToken, $token->getToken());
335
336
        $token = $jwtManager->getJwtToken();
337
338
        $this->assertInstanceOf(JwtToken::class, $token);
339
        $this->assertEquals($jwtToken, $token->getToken());
340
    }
341
}
342