1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Tests\Security; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Description; |
12
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Operation; |
13
|
|
|
use KleijnWeb\PhpApi\Descriptions\Description\Repository; |
14
|
|
|
use KleijnWeb\PhpApi\RoutingBundle\Routing\RequestMeta; |
15
|
|
|
use KleijnWeb\SwaggerBundle\Security\RbacRequestVoter; |
16
|
|
|
use KleijnWeb\SwaggerBundle\Security\RequestAuthorizationListener; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
21
|
|
|
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; |
22
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author John Kleijn <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class RbacRequestVoterTestTest extends TestCase |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var AccessDecisionManagerInterface |
31
|
|
|
*/ |
32
|
|
|
private $accessDecisionManager; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var \PHPUnit_Framework_MockObject_MockObject |
36
|
|
|
*/ |
37
|
|
|
private $repositoryMock; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var RbacRequestVoter |
41
|
|
|
*/ |
42
|
|
|
private $voter; |
43
|
|
|
|
44
|
|
|
protected function setUp() |
45
|
|
|
{ |
46
|
|
|
$this->accessDecisionManager = $this->getMockForAbstractClass(AccessDecisionManagerInterface::class); |
|
|
|
|
47
|
|
|
|
48
|
|
|
/** @var Repository $repository */ |
49
|
|
|
$this->repositoryMock = $repository = $this |
|
|
|
|
50
|
|
|
->getMockBuilder(Repository::class) |
51
|
|
|
->disableOriginalConstructor() |
52
|
|
|
->getMock(); |
53
|
|
|
|
54
|
|
|
$this->voter = new RbacRequestVoter($this->repositoryMock, $this->accessDecisionManager); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @test |
59
|
|
|
*/ |
60
|
|
|
public function legacySupportsClassMethodReturnsFalse() |
61
|
|
|
{ |
62
|
|
|
$this->assertFalse($this->voter->supportsClass('Foo')); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @test |
67
|
|
|
*/ |
68
|
|
|
public function willAbstainWhenNotPassedRequest() |
69
|
|
|
{ |
70
|
|
|
/** @var TokenInterface $token */ |
71
|
|
|
$token = $this->getMockForAbstractClass(TokenInterface::class); |
72
|
|
|
|
73
|
|
|
$this->assertEquals(VoterInterface::ACCESS_ABSTAIN, $this->voter->vote($token, new \stdClass(), [])); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @test |
78
|
|
|
*/ |
79
|
|
View Code Duplication |
public function willAbstainWhenRequestHasNoSwaggerPath() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
/** @var TokenInterface $token */ |
82
|
|
|
$token = $this->getMockForAbstractClass(TokenInterface::class); |
83
|
|
|
|
84
|
|
|
$this->assertEquals( |
85
|
|
|
VoterInterface::ACCESS_ABSTAIN, |
86
|
|
|
$this->voter->vote($token, $this->createRequest([]), [RequestAuthorizationListener::ATTRIBUTE]) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @test |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function willAbstainWhenAttributesNotFromListener() |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
/** @var TokenInterface $token */ |
96
|
|
|
$token = $this->getMockForAbstractClass(TokenInterface::class); |
97
|
|
|
|
98
|
|
|
$this->assertEquals( |
99
|
|
|
VoterInterface::ACCESS_ABSTAIN, |
100
|
|
|
$this->voter->vote($token, $this->createRequest([]), ['something else']) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @test |
106
|
|
|
*/ |
107
|
|
|
public function willNotAbstainOneAttributesFromListener() |
108
|
|
|
{ |
109
|
|
|
/** @var TokenInterface $token */ |
110
|
|
|
$token = $this->getMockForAbstractClass(TokenInterface::class); |
111
|
|
|
|
112
|
|
|
$this->repositoryMock->expects($this->once())->method('get'); |
113
|
|
|
|
114
|
|
|
$this->voter->vote( |
115
|
|
|
$token, |
116
|
|
|
$this->createRequest([ |
117
|
|
|
RequestMeta::ATTRIBUTE_URI => '/', |
118
|
|
|
RequestMeta::ATTRIBUTE_PATH => '/', |
119
|
|
|
]), |
120
|
|
|
['something else', RequestAuthorizationListener::ATTRIBUTE] |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @test |
126
|
|
|
*/ |
127
|
|
|
public function willRequireIsAuthenticatedFullyWhenOperationSecured() |
128
|
|
|
{ |
129
|
|
|
/** @var TokenInterface $token */ |
130
|
|
|
$token = $this->getMockForAbstractClass(TokenInterface::class); |
131
|
|
|
|
132
|
|
|
/** @var Operation $operation */ |
133
|
|
|
$operationMock = $operation = $this->getMockBuilder(Operation::class)->disableOriginalConstructor()->getMock(); |
134
|
|
|
|
135
|
|
|
/** @var Description $description */ |
136
|
|
|
$description = $this->getMockBuilder(Description::class)->disableOriginalConstructor()->getMock(); |
137
|
|
|
|
138
|
|
|
$request = $this->createRequest([RequestMeta::ATTRIBUTE => new RequestMeta($description, $operation)]); |
|
|
|
|
139
|
|
|
|
140
|
|
|
$this->assertEquals( |
141
|
|
|
VoterInterface::ACCESS_DENIED, |
142
|
|
|
$this->voter->vote($token, $request, [RequestAuthorizationListener::ATTRIBUTE]) |
143
|
|
|
); |
144
|
|
|
|
145
|
|
|
$operationMock->expects($this->once())->method('isSecured')->willReturn(true); |
146
|
|
|
|
147
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject $mock */ |
148
|
|
|
$mock = $this->accessDecisionManager; |
149
|
|
|
$mock |
150
|
|
|
->expects($this->once()) |
151
|
|
|
->method('decide') |
152
|
|
|
->with($token, ['IS_AUTHENTICATED_FULLY']) |
153
|
|
|
->willReturn(true); |
154
|
|
|
|
155
|
|
|
$this->assertEquals( |
156
|
|
|
VoterInterface::ACCESS_GRANTED, |
157
|
|
|
$this->voter->vote($token, $request, [RequestAuthorizationListener::ATTRIBUTE]) |
158
|
|
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param array $attributes |
163
|
|
|
* |
164
|
|
|
* @param string $content |
|
|
|
|
165
|
|
|
* |
166
|
|
|
* @return Request |
167
|
|
|
*/ |
168
|
|
|
private function createRequest(array $attributes): Request |
169
|
|
|
{ |
170
|
|
|
return new class($attributes) extends Request |
171
|
|
|
{ |
172
|
|
|
/** |
173
|
|
|
* @param array $attributes |
174
|
|
|
* @param array $content |
|
|
|
|
175
|
|
|
*/ |
176
|
|
|
public function __construct(array $attributes) |
177
|
|
|
{ |
178
|
|
|
parent::__construct(); |
179
|
|
|
$this->attributes = new ParameterBag($attributes); |
|
|
|
|
180
|
|
|
} |
181
|
|
|
}; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..