|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the U2F Security bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Michael Barbey <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Mbarbey\U2fSecurityBundle\Tests\DependencyInjection; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Bundle\FrameworkBundle\Tests\TestCase; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
16
|
|
|
use Mbarbey\U2fSecurityBundle\DependencyInjection\MbarbeyU2fSecurityExtension; |
|
17
|
|
|
use Symfony\Component\Yaml\Parser; |
|
18
|
|
|
|
|
19
|
|
|
class MbarbeyU2fSecurityExtensionTest extends TestCase |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var ContainerBuilder */ |
|
22
|
|
|
protected $configuration; |
|
23
|
|
|
|
|
24
|
|
|
protected function tearDown() |
|
25
|
|
|
{ |
|
26
|
|
|
$this->configuration = null; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function testConfigLoadThrowsExceptionWhenNoAuthenticationRoute() |
|
33
|
|
|
{ |
|
34
|
|
|
$loader = new MbarbeyU2fSecurityExtension(); |
|
35
|
|
|
$config = $this->getEmptyConfig(); |
|
36
|
|
|
$loader->load(array($config), new ContainerBuilder()); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function testConfigLoadWithoutWhitelist() |
|
40
|
|
|
{ |
|
41
|
|
|
$this->configuration = new ContainerBuilder(); |
|
42
|
|
|
$loader = new MbarbeyU2fSecurityExtension(); |
|
43
|
|
|
$config = $this->getOnlyAuthenticationRoute(); |
|
44
|
|
|
$loader->load(array($config), $this->configuration); |
|
45
|
|
|
|
|
46
|
|
|
$this->assertHasDefinition('mbarbey_u2f_security.subscriber'); |
|
47
|
|
|
$this->assertEquals($this->getDefinitionArgument('mbarbey_u2f_security.subscriber', 0), 'test'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function testFullConfig() |
|
51
|
|
|
{ |
|
52
|
|
|
$this->configuration = new ContainerBuilder(); |
|
53
|
|
|
$loader = new MbarbeyU2fSecurityExtension(); |
|
54
|
|
|
$config = $this->getFullConfig(); |
|
55
|
|
|
$loader->load(array($config), $this->configuration); |
|
56
|
|
|
|
|
57
|
|
|
$this->assertHasDefinition('mbarbey_u2f_security.subscriber'); |
|
58
|
|
|
$this->assertEquals($this->getDefinitionArgument('mbarbey_u2f_security.subscriber', 0), 'test2'); |
|
59
|
|
|
$this->assertEquals($this->getDefinitionArgument('mbarbey_u2f_security.subscriber', 1), ['route1', 'route2']); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function getEmptyConfig() |
|
63
|
|
|
{ |
|
64
|
|
|
$yaml = <<<EOF |
|
65
|
|
|
authentication_route: ~ |
|
66
|
|
|
whitelist_routes: ~ |
|
67
|
|
|
EOF; |
|
68
|
|
|
$parser = new Parser(); |
|
69
|
|
|
return $parser->parse($yaml); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function getOnlyAuthenticationRoute() |
|
73
|
|
|
{ |
|
74
|
|
|
$yaml = <<<EOF |
|
75
|
|
|
authentication_route: 'test' |
|
76
|
|
|
EOF; |
|
77
|
|
|
$parser = new Parser(); |
|
78
|
|
|
return $parser->parse($yaml); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected function getFullConfig() |
|
82
|
|
|
{ |
|
83
|
|
|
$yaml = <<<EOF |
|
84
|
|
|
authentication_route: test2 |
|
85
|
|
|
whitelist_routes: |
|
86
|
|
|
- route1 |
|
87
|
|
|
- route2 |
|
88
|
|
|
EOF; |
|
89
|
|
|
$parser = new Parser(); |
|
90
|
|
|
return $parser->parse($yaml); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function assertHasDefinition($id) |
|
94
|
|
|
{ |
|
95
|
|
|
$this->assertTrue($this->configuration->hasDefinition($id)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function getDefinitionArgument($id, $index) |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->configuration->getDefinition($id)->getArgument($index); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|