|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Tarantool Client package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Eugene Leonovich <[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
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Tarantool\Client\Tests\Unit\Middleware; |
|
15
|
|
|
|
|
16
|
|
|
use PHPUnit\Framework\MockObject\MockObject; |
|
17
|
|
|
use PHPUnit\Framework\TestCase; |
|
18
|
|
|
use Tarantool\Client\Exception\RequestDenied; |
|
19
|
|
|
use Tarantool\Client\Handler\Handler; |
|
20
|
|
|
use Tarantool\Client\Middleware\FirewallMiddleware; |
|
21
|
|
|
use Tarantool\Client\Middleware\Middleware; |
|
22
|
|
|
use Tarantool\Client\Request\AuthenticateRequest; |
|
23
|
|
|
use Tarantool\Client\Request\CallRequest; |
|
24
|
|
|
use Tarantool\Client\Request\EvaluateRequest; |
|
25
|
|
|
use Tarantool\Client\Request\PingRequest; |
|
26
|
|
|
use Tarantool\Client\Request\SelectRequest; |
|
27
|
|
|
use Tarantool\Client\RequestTypes; |
|
28
|
|
|
use Tarantool\Client\Schema\IteratorTypes; |
|
29
|
|
|
use Tarantool\Client\Tests\Unit\ResponseFactory; |
|
30
|
|
|
|
|
31
|
|
|
final class FirewallMiddlewareTest extends TestCase |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var Handler|MockObject |
|
35
|
|
|
*/ |
|
36
|
|
|
private $handler; |
|
37
|
|
|
|
|
38
|
|
|
protected function setUp() : void |
|
39
|
|
|
{ |
|
40
|
|
|
$this->handler = $this->createMock(Handler::class); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testEmptyAllowList() : void |
|
44
|
|
|
{ |
|
45
|
|
|
$this->handler->expects($this->once())->method('handle') |
|
46
|
|
|
->willReturn(ResponseFactory::create()); |
|
47
|
|
|
|
|
48
|
|
|
$middleware = FirewallMiddleware::deny(RequestTypes::CALL); |
|
49
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function testAllow() : void |
|
53
|
|
|
{ |
|
54
|
|
|
$this->handler->expects($this->once())->method('handle') |
|
55
|
|
|
->willReturn(ResponseFactory::create()); |
|
56
|
|
|
|
|
57
|
|
|
$middleware = FirewallMiddleware::allow(RequestTypes::PING); |
|
58
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testAllowForbids() : void |
|
62
|
|
|
{ |
|
63
|
|
|
$middleware = FirewallMiddleware::allow(RequestTypes::PING); |
|
64
|
|
|
|
|
65
|
|
|
$this->expectException(RequestDenied::class); |
|
66
|
|
|
$this->expectExceptionMessage(sprintf('Request "%s" is denied.', CallRequest::class)); |
|
67
|
|
|
|
|
68
|
|
|
$middleware->process(new CallRequest('foo'), $this->handler); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testAndAllow() : void |
|
72
|
|
|
{ |
|
73
|
|
|
$this->handler->expects($this->once())->method('handle') |
|
74
|
|
|
->willReturn(ResponseFactory::create()); |
|
75
|
|
|
|
|
76
|
|
|
$middleware = FirewallMiddleware::deny(RequestTypes::CALL)->andAllow(RequestTypes::PING); |
|
77
|
|
|
|
|
78
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function testAndAllowForbids() : void |
|
82
|
|
|
{ |
|
83
|
|
|
$middleware = FirewallMiddleware::deny(RequestTypes::CALL)->andAllow(RequestTypes::PING); |
|
84
|
|
|
|
|
85
|
|
|
$this->expectException(RequestDenied::class); |
|
86
|
|
|
$this->expectExceptionMessage(sprintf('Request "%s" is denied.', EvaluateRequest::class)); |
|
87
|
|
|
|
|
88
|
|
|
$middleware->process(new EvaluateRequest('return 42'), $this->handler); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function testAndAllowOnly() : void |
|
92
|
|
|
{ |
|
93
|
|
|
$this->handler->expects($this->once())->method('handle') |
|
94
|
|
|
->willReturn(ResponseFactory::create()); |
|
95
|
|
|
|
|
96
|
|
|
$middleware = FirewallMiddleware::allow(RequestTypes::CALL)->andAllowOnly(RequestTypes::PING); |
|
97
|
|
|
|
|
98
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function testAndAllowOnlyForbids() : void |
|
102
|
|
|
{ |
|
103
|
|
|
$middleware = FirewallMiddleware::allow(RequestTypes::CALL)->andAllowOnly(RequestTypes::PING); |
|
104
|
|
|
|
|
105
|
|
|
$this->expectException(RequestDenied::class); |
|
106
|
|
|
$this->expectExceptionMessage(sprintf('Request "%s" is denied.', CallRequest::class)); |
|
107
|
|
|
|
|
108
|
|
|
$middleware->process(new CallRequest('foo'), $this->handler); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function testDeny() : void |
|
112
|
|
|
{ |
|
113
|
|
|
$this->handler->expects($this->once())->method('handle') |
|
114
|
|
|
->willReturn(ResponseFactory::create()); |
|
115
|
|
|
|
|
116
|
|
|
$middleware = FirewallMiddleware::deny(RequestTypes::PING); |
|
117
|
|
|
|
|
118
|
|
|
$middleware->process(new CallRequest('foo'), $this->handler); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
public function testDenyForbids() : void |
|
122
|
|
|
{ |
|
123
|
|
|
$middleware = FirewallMiddleware::deny(RequestTypes::PING); |
|
124
|
|
|
|
|
125
|
|
|
$this->expectException(RequestDenied::class); |
|
126
|
|
|
$this->expectExceptionMessage(sprintf('Request "%s" is denied.', PingRequest::class)); |
|
127
|
|
|
|
|
128
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function testAndDeny() : void |
|
132
|
|
|
{ |
|
133
|
|
|
$this->handler->expects($this->once())->method('handle') |
|
134
|
|
|
->willReturn(ResponseFactory::create()); |
|
135
|
|
|
|
|
136
|
|
|
$middleware = FirewallMiddleware::allow(RequestTypes::PING)->andDeny(RequestTypes::CALL); |
|
137
|
|
|
|
|
138
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public function testAndDenyForbids() : void |
|
142
|
|
|
{ |
|
143
|
|
|
$middleware = FirewallMiddleware::allow(RequestTypes::PING)->andDeny(RequestTypes::CALL); |
|
144
|
|
|
|
|
145
|
|
|
$this->expectException(RequestDenied::class); |
|
146
|
|
|
$this->expectExceptionMessage(sprintf('Request "%s" is denied.', CallRequest::class)); |
|
147
|
|
|
|
|
148
|
|
|
$middleware->process(new CallRequest('foo'), $this->handler); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function testAndDenyOnly() : void |
|
152
|
|
|
{ |
|
153
|
|
|
$this->handler->expects($this->once())->method('handle') |
|
154
|
|
|
->willReturn(ResponseFactory::create()); |
|
155
|
|
|
|
|
156
|
|
|
$middleware = FirewallMiddleware::deny(RequestTypes::PING)->andDenyOnly(RequestTypes::CALL); |
|
157
|
|
|
|
|
158
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
public function testAndDenyOnlyForbids() : void |
|
162
|
|
|
{ |
|
163
|
|
|
$middleware = FirewallMiddleware::deny(RequestTypes::PING)->andDenyOnly(RequestTypes::CALL); |
|
164
|
|
|
|
|
165
|
|
|
$this->expectException(RequestDenied::class); |
|
166
|
|
|
$this->expectExceptionMessage(sprintf('Request "%s" is denied.', CallRequest::class)); |
|
167
|
|
|
|
|
168
|
|
|
$middleware->process(new CallRequest('foo'), $this->handler); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @dataProvider provideBlacklistPriorityData |
|
173
|
|
|
*/ |
|
174
|
|
|
public function testDenyHasPriority(Middleware $middleware) : void |
|
175
|
|
|
{ |
|
176
|
|
|
$this->expectException(RequestDenied::class); |
|
177
|
|
|
$this->expectExceptionMessage(sprintf('Request "%s" is denied.', PingRequest::class)); |
|
178
|
|
|
|
|
179
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function provideBlacklistPriorityData() : iterable |
|
183
|
|
|
{ |
|
184
|
|
|
yield [FirewallMiddleware::allow(RequestTypes::PING)->andDeny(RequestTypes::PING)]; |
|
185
|
|
|
yield [FirewallMiddleware::deny(RequestTypes::PING)->andAllow(RequestTypes::PING)]; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
public function testAllowReadOnly() : void |
|
189
|
|
|
{ |
|
190
|
|
|
$this->handler->expects($this->exactly(3))->method('handle') |
|
191
|
|
|
->willReturn(ResponseFactory::create()); |
|
192
|
|
|
|
|
193
|
|
|
$middleware = FirewallMiddleware::allowReadOnly(); |
|
194
|
|
|
|
|
195
|
|
|
$middleware->process(new AuthenticateRequest('12345678901234567890', 'guest'), $this->handler); |
|
196
|
|
|
$middleware->process(new PingRequest(), $this->handler); |
|
197
|
|
|
$middleware->process(new SelectRequest(1, 1, [], 0, 1, IteratorTypes::ALL), $this->handler); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
public function testAllowReadOnlyForbids() : void |
|
201
|
|
|
{ |
|
202
|
|
|
$this->expectException(RequestDenied::class); |
|
203
|
|
|
$this->expectExceptionMessage(sprintf('Request "%s" is denied.', EvaluateRequest::class)); |
|
204
|
|
|
|
|
205
|
|
|
FirewallMiddleware::allowReadOnly()->process(new EvaluateRequest('return 42'), $this->handler); |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|