|
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 SameSubnetStrategyTest |
|
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 SameSubnetStrategyTest 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
|
|
View Code Duplication |
public function setUp() |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
|
|
parent::setUp(); |
|
31
|
|
|
|
|
32
|
|
|
/** @var \Symfony\Bundle\FrameworkBundle\Client client */ |
|
33
|
|
|
$this->client = static::createClient(); |
|
34
|
|
|
$this->propertyKey = $this->client->getKernel() |
|
35
|
|
|
->getContainer() |
|
36
|
|
|
->getParameter('graviton.security.authentication.strategy.subnet.key'); |
|
37
|
|
|
$this->strategy = new SameSubnetStrategy( |
|
38
|
|
|
$this->propertyKey |
|
39
|
|
|
); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\SameSubnetStrategy::apply |
|
44
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo |
|
45
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField |
|
46
|
|
|
* |
|
47
|
|
|
* @return void |
|
48
|
|
|
*/ |
|
49
|
|
|
public function testApply() |
|
50
|
|
|
{ |
|
51
|
|
|
$this->client->request( |
|
52
|
|
|
'GET', //method |
|
53
|
|
|
'/', //uri |
|
54
|
|
|
array(), //parameters |
|
55
|
|
|
array(), //files |
|
56
|
|
|
array() //server |
|
57
|
|
|
); |
|
58
|
|
|
|
|
59
|
|
|
$this->assertSame('graviton_subnet_user', $this->strategy->apply($this->client->getRequest())); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\SameSubnetStrategy::apply |
|
64
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::extractFieldInfo |
|
65
|
|
|
* @covers \Graviton\SecurityBundle\Authentication\Strategies\AbstractHttpStrategy::validateField |
|
66
|
|
|
* |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testApplyExpectingInvalidArgumentException() |
|
70
|
|
|
{ |
|
71
|
|
|
$this->client->request( |
|
72
|
|
|
'GET', //method |
|
73
|
|
|
'/', //uri |
|
74
|
|
|
array(), //parameters |
|
75
|
|
|
array(), //files |
|
76
|
|
|
array() //server |
|
77
|
|
|
); |
|
78
|
|
|
|
|
79
|
|
|
$strategy = new SameSubnetStrategy('10.2.0.2'); |
|
80
|
|
|
|
|
81
|
|
|
$this->setExpectedException('\InvalidArgumentException'); |
|
|
|
|
|
|
82
|
|
|
$strategy->apply($this->client->getRequest()); |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
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.