1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\JwtBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\JwtBundle\Tests\Functional; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\JwtBundle\Tests\Authenticator\AuthenticatorTest; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author John Kleijn <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class FunctionalTest extends WebTestCase |
18
|
|
|
{ |
19
|
|
|
// @codingStandardsIgnoreStart |
20
|
|
|
const PSK_TOKEN = AuthenticatorTest::TEST_TOKEN; |
21
|
|
|
const HMAC_TOKEN = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtleVR3byJ9.eyJwcm4iOiJqb2huIiwiaXNzIjoiaHR0cDovL2FwaS5zZXJ2ZXIyLmNvbS9vYXV0aDIvdG9rZW4ifQ.vdGhD5E4Ibj2Tndlh_0pPgJsOuRUpAn1QYu5miB6qwjrXhKCicuTKOuC9x2_2ErUOApv5KiblYds_gcWONdGKx1tQyQa1dsuhrkiVn_VJAsaaix8nJiHAuNv-ukm8mnSWJoVuOcTQIQG8IaupviyphEAEdjrm9QQhvzERgdFUT4bdCdfywrC37oYEAH5bHpiiUK2UzyNuUIHwOP_gWODodbEWRJOxtefwJ_vdpqHvSZzyW7Vei4mCtr2vE1k2qBvG_Qjw2ebLfEdX58k6-eYa7phle9hYjA_q-I8Y-S1ulBiVf_tpvayk8-4lWup9Wbg_BT2vDJOidQgM4l9jV9QHg'; |
22
|
|
|
const DYN_HMAC_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImtleVRocmVlIn0.eyJwcm4iOiJqb2UiLCJpc3MiOiJodHRwOi8vYXBpLnNlcnZlcjIuY29tL29hdXRoMi90b2tlbiJ9.fv9yrTk3AnPTle_ikBY2EjIFhb1xaxKO4-Vop2AxnME'; |
23
|
|
|
// @codingStandardsIgnoreEnd |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @test |
27
|
|
|
*/ |
28
|
|
|
public function canGetUnsecuredContentWithoutToken() |
29
|
|
|
{ |
30
|
|
|
$client = $this->createClient(); |
31
|
|
|
$client->request('GET', '/unsecured'); |
32
|
|
|
$this->assertSame('UNSECURED CONTENT', $client->getResponse()->getContent()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException |
38
|
|
|
*/ |
39
|
|
|
public function cannotGetSecuredContentWithoutToken() |
40
|
|
|
{ |
41
|
|
|
$client = $this->createClient(); |
42
|
|
|
$client->request('GET', '/secured'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @test |
47
|
|
|
* @expectedException \Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException |
48
|
|
|
*/ |
49
|
|
|
public function cannotGetSecuredContentWitInvalidToken() |
50
|
|
|
{ |
51
|
|
|
$client = $this->createClient(); |
52
|
|
|
$server = ['HTTP_AUTHORIZATION' => 'Bearer foo']; |
53
|
|
|
$client->request('GET', '/secured', $parameters = [], $files = [], $server); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @test |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function canGetSecuredContentWitValidPskToken() |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$client = $this->createClient(); |
62
|
|
|
$server = ['HTTP_AUTHORIZATION' => 'Bearer ' . self::PSK_TOKEN]; |
63
|
|
|
$client->request('GET', '/secured', $parameters = [], $files = [], $server); |
64
|
|
|
$this->assertSame('SECURED CONTENT', $client->getResponse()->getContent()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @test |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function canGetSecuredContentWitValidHmacToken() |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$client = $this->createClient(); |
73
|
|
|
$server = ['HTTP_AUTHORIZATION' => 'Bearer ' . self::HMAC_TOKEN]; |
74
|
|
|
$client->request('GET', '/secured', $parameters = [], $files = [], $server); |
75
|
|
|
$this->assertSame('SECURED CONTENT', $client->getResponse()->getContent()); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @test |
80
|
|
|
*/ |
81
|
|
View Code Duplication |
public function canGetSecuredContentWithSecretLoader() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$client = $this->createClient(); |
84
|
|
|
$server = ['HTTP_AUTHORIZATION' => 'Bearer ' . self::DYN_HMAC_TOKEN]; |
85
|
|
|
$client->request('GET', '/secured-with-secret-loader', $parameters = [], $files = [], $server); |
86
|
|
|
$this->assertSame('CONTENT SECURED WITH SECRET LOADER', $client->getResponse()->getContent()); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.