|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Tactician\Bundle\Tests\Integration; |
|
4
|
|
|
|
|
5
|
|
|
use League\Tactician\Bundle\DependencyInjection\Compiler\UnknownMiddlewareException; |
|
6
|
|
|
use League\Tactician\Bundle\Tests\Fake\FakeCommand; |
|
7
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; |
|
8
|
|
|
use Symfony\Component\Security\Core\Exception\AccessDeniedException; |
|
9
|
|
|
use Symfony\Component\Security\Core\Role\Role; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Integration test for security middleware. |
|
13
|
|
|
* |
|
14
|
|
|
* @author Ron Rademaker |
|
15
|
|
|
*/ |
|
16
|
|
|
class SecurityTest extends IntegrationTest |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Tests if the kernel is bootable with security middleware. |
|
20
|
|
|
* |
|
21
|
|
|
* @return void |
|
22
|
|
|
*/ |
|
23
|
|
|
public function testCanBootKernelWithSecurityMiddleware() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->loadSecurityConfiguration(); |
|
26
|
|
|
|
|
27
|
|
|
$this->givenConfig('tactician', <<<'EOF' |
|
28
|
|
|
commandbus: |
|
29
|
|
|
default: |
|
30
|
|
|
middleware: |
|
31
|
|
|
- tactician.middleware.security |
|
32
|
|
|
EOF |
|
33
|
|
|
); |
|
34
|
|
|
static::$kernel->boot(); |
|
35
|
|
|
$this->assertTrue(true); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Tests if the kernel is not bootable without security settings (but with security middleware). |
|
40
|
|
|
* |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testCanNotBootKernelWithoutSecurity() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->expectException(UnknownMiddlewareException::class); |
|
46
|
|
|
$this->givenConfig('tactician', <<<'EOF' |
|
47
|
|
|
commandbus: |
|
48
|
|
|
default: |
|
49
|
|
|
middleware: |
|
50
|
|
|
- tactician.middleware.security |
|
51
|
|
|
EOF |
|
52
|
|
|
); |
|
53
|
|
|
static::$kernel->boot(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Tests if the kernel is bootable without security middleware and without security settings. |
|
58
|
|
|
*/ |
|
59
|
|
|
public function testCanBootKernelWithoutSecurity() |
|
60
|
|
|
{ |
|
61
|
|
|
static::$kernel->boot(); |
|
62
|
|
|
$this->assertTrue(true); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Tests security middleware. |
|
67
|
|
|
* |
|
68
|
|
|
* @dataProvider provideTestData |
|
69
|
|
|
* |
|
70
|
|
|
* @param string $role |
|
71
|
|
|
* @param bool $allowed |
|
72
|
|
|
*/ |
|
73
|
|
|
public function testSecurityMiddleware(string $role, bool $allowed) |
|
74
|
|
|
{ |
|
75
|
|
|
if (false === $allowed) { |
|
76
|
|
|
$this->expectException(AccessDeniedException::class); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$this->loadSecurityConfiguration(); |
|
80
|
|
|
$this->givenConfig('tactician', <<<'EOF' |
|
81
|
|
|
commandbus: |
|
82
|
|
|
default: |
|
83
|
|
|
middleware: |
|
84
|
|
|
- tactician.middleware.security |
|
85
|
|
|
security: |
|
86
|
|
|
League\Tactician\Bundle\Tests\Fake\FakeCommand: |
|
87
|
|
|
- 'ROLE_ADMIN' |
|
88
|
|
|
EOF |
|
89
|
|
|
); |
|
90
|
|
|
|
|
91
|
|
|
static::$kernel->boot(); |
|
92
|
|
|
static::$kernel->getContainer()->get('security.token_storage')->setToken(new AnonymousToken('test', 'anon', [new Role($role)])); |
|
93
|
|
|
static::$kernel->getContainer()->get('tactician.commandbus.default')->handle(new FakeCommand()); |
|
94
|
|
|
|
|
95
|
|
|
$this->assertTrue($allowed); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Gets test data for security middleware integration test. |
|
100
|
|
|
* |
|
101
|
|
|
* @return array |
|
102
|
|
|
*/ |
|
103
|
|
|
public function provideTestData(): array |
|
104
|
|
|
{ |
|
105
|
|
|
return [ |
|
106
|
|
|
'Role may handle the command' => ['ROLE_ADMIN', true], |
|
107
|
|
|
'Test role hierarchy' => ['ROLE_SUPER_ADMIN', true], |
|
108
|
|
|
'Role may not handle the command' => ['ROLE_USER', false], |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Security configuration. |
|
114
|
|
|
*/ |
|
115
|
|
|
private function loadSecurityConfiguration() |
|
116
|
|
|
{ |
|
117
|
|
|
$this->givenConfig('security', <<< 'EOF' |
|
118
|
|
|
access_denied_url: / |
|
119
|
|
|
|
|
120
|
|
|
role_hierarchy: |
|
121
|
|
|
ROLE_ADMIN: ROLE_USER |
|
122
|
|
|
ROLE_SUPER_ADMIN: ROLE_ADMIN |
|
123
|
|
|
|
|
124
|
|
|
providers: |
|
125
|
|
|
my_in_memory_provider: |
|
126
|
|
|
memory: |
|
127
|
|
|
|
|
128
|
|
|
firewalls: |
|
129
|
|
|
main: |
|
130
|
|
|
anonymous: ~ |
|
131
|
|
|
http_basic: ~ |
|
132
|
|
|
EOF |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|