1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Fetching authentication information from Cookie header. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Graviton\SecurityBundle\Authentication\Strategies; |
7
|
|
|
|
8
|
|
|
use Graviton\TestBundle\Test\WebTestCase; |
9
|
|
|
use Symfony\Component\BrowserKit\Cookie; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class CookieFieldStrategyTest |
13
|
|
|
* |
14
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
15
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
16
|
|
|
* @link http://swisscom.ch |
17
|
|
|
*/ |
18
|
|
|
class CookieFieldStrategyTest extends WebTestCase |
19
|
|
|
{ |
20
|
|
|
protected $strategy; |
21
|
|
|
protected $client; |
22
|
|
|
protected $propertyKey; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* UnitTest Starts this on reach test |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
public function setUp() |
29
|
|
|
{ |
30
|
|
|
parent::setUp(); |
31
|
|
|
|
32
|
|
|
/** @var \Symfony\Bundle\FrameworkBundle\Client client */ |
33
|
|
|
$this->client = static::createClient(); |
34
|
|
|
$this->propertyKey = |
35
|
|
|
$this->client->getKernel()->getContainer()->getParameter('graviton.security.authentication.strategy_key'); |
36
|
|
|
$this->strategy = new CookieFieldStrategy( |
37
|
|
|
$this->propertyKey |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::apply |
44
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo |
45
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField |
46
|
|
|
* |
47
|
|
|
* @dataProvider stringProvider |
48
|
|
|
* |
49
|
|
|
* @param string $fieldValue value to check |
50
|
|
|
* |
51
|
|
|
* @return void |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function testApply($fieldValue) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$cookie = new Cookie( |
56
|
|
|
$this->propertyKey, |
57
|
|
|
$fieldValue, |
58
|
|
|
time() + 3600 * 24 * 7, |
59
|
|
|
'/', |
60
|
|
|
null, |
61
|
|
|
false, |
62
|
|
|
false |
63
|
|
|
); |
64
|
|
|
$this->client->getCookieJar()->set($cookie); |
65
|
|
|
$this->client->request( |
66
|
|
|
'GET', //method |
67
|
|
|
'/', //uri |
68
|
|
|
array(), //parameters |
69
|
|
|
array(), //files |
70
|
|
|
array() //server |
71
|
|
|
); |
72
|
|
|
|
73
|
|
|
$this->assertSame($fieldValue, $this->strategy->apply($this->client->getRequest())); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array<string> |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function stringProvider() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
return array( |
82
|
|
|
'plain string, no special chars' => array('exampleAuthenticationHeader'), |
83
|
|
|
'string with special chars' => array("$-_.+!*'(),{}|\\^~[]`<>#%;/?:@&=."), |
84
|
|
|
'string with octal chars' => array("a: \141, A: \101"), |
85
|
|
|
'string with hex chars' => array("a: \x61, A: \x41") |
86
|
|
|
); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Todo, find a way to have also to client id set in request stack. |
91
|
|
|
* |
92
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::apply |
93
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::extractAdUsername |
94
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\CookieFieldStrategy::extractCoreId |
95
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo |
96
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField |
97
|
|
|
* |
98
|
|
|
* @dataProvider stringExtractProvider |
99
|
|
|
* |
100
|
|
|
* @param string $fieldValue value to check |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
View Code Duplication |
public function testApplyExtract($fieldValue) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$cookie = new Cookie( |
107
|
|
|
$this->propertyKey, |
108
|
|
|
$fieldValue, |
109
|
|
|
time() + 3600 * 24 * 7, |
110
|
|
|
'/', |
111
|
|
|
null, |
112
|
|
|
false, |
113
|
|
|
false |
114
|
|
|
); |
115
|
|
|
$this->client->getCookieJar()->set($cookie); |
116
|
|
|
$this->client->request( |
117
|
|
|
'GET', //method |
118
|
|
|
'/', //uri |
119
|
|
|
array(), //parameters |
120
|
|
|
array(), //files |
121
|
|
|
array() //server |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
$this->assertSame('testUser', $this->strategy->apply($this->client->getRequest())); |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @return array<string> |
129
|
|
|
*/ |
130
|
|
|
public function stringExtractProvider() |
131
|
|
|
{ |
132
|
|
|
return array( |
133
|
|
|
'testing extract username' => array("username=testUser,finnova_id=someId123"), |
134
|
|
|
'testing extract rev username' => array("finnova_id=someId123,username=testUser"), |
135
|
|
|
); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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.