|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Handling authentication for clients in the same network. |
|
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 MultiStrategyTest |
|
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 MultiStrategyTest 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
|
|
|
$propertyKey = $this->client->getKernel() |
|
35
|
|
|
->getContainer() |
|
36
|
|
|
->getParameter('graviton.security.authentication.strategy.subnet.key'); |
|
37
|
|
|
$sameSubnetStrategy = new SameSubnetStrategy($propertyKey); |
|
38
|
|
|
$this->propertyKey = $this->client->getKernel() |
|
39
|
|
|
->getContainer() |
|
40
|
|
|
->getParameter('graviton.security.authentication.strategy.cookie.key'); |
|
41
|
|
|
$cookieFieldStrategy = new CookieFieldStrategy($this->propertyKey); |
|
42
|
|
|
|
|
43
|
|
|
$this->strategy = new MultiStrategy( |
|
44
|
|
|
[ |
|
45
|
|
|
$sameSubnetStrategy, |
|
46
|
|
|
$cookieFieldStrategy, |
|
47
|
|
|
] |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\MultiStrategy::apply |
|
53
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo |
|
54
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
View Code Duplication |
public function testApply() |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
$fieldValue = 'exampleAuthenticationHeader'; |
|
61
|
|
|
|
|
62
|
|
|
$cookie = new Cookie( |
|
63
|
|
|
$this->propertyKey, |
|
64
|
|
|
$fieldValue, |
|
65
|
|
|
time() + 3600 * 24 * 7, |
|
66
|
|
|
'/', |
|
67
|
|
|
null, |
|
68
|
|
|
false, |
|
69
|
|
|
false |
|
70
|
|
|
); |
|
71
|
|
|
$this->client->getCookieJar()->set($cookie); |
|
72
|
|
|
$this->client->request( |
|
73
|
|
|
'GET', //method |
|
74
|
|
|
'/', //uri |
|
75
|
|
|
array(), //parameters |
|
76
|
|
|
array(), //files |
|
77
|
|
|
array() //server |
|
78
|
|
|
); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertSame($fieldValue, $this->strategy->apply($this->client->getRequest())); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
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.