|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Yiisoft\Yii\Web\Tests\Middleware; |
|
5
|
|
|
|
|
6
|
|
|
use Http\Message\ResponseFactory; |
|
7
|
|
|
use Nyholm\Psr7\Response; |
|
8
|
|
|
use PHPUnit\Framework\TestCase; |
|
9
|
|
|
use Psr\Http\Message\ResponseFactoryInterface; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
11
|
|
|
use Psr\Http\Server\RequestHandlerInterface; |
|
12
|
|
|
use Yiisoft\Validator\Rule\Ip; |
|
13
|
|
|
use Yiisoft\Yii\Web\Middleware\IpFilter; |
|
14
|
|
|
|
|
15
|
|
|
class IpFilterTest extends TestCase |
|
16
|
|
|
{ |
|
17
|
|
|
private const REQUEST_PARAMS = [ |
|
18
|
|
|
'REMOTE_ADDR' => '8.8.8.8', |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
private const ALLOWED_IP = '1.1.1.1'; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var ResponseFactory|\PHPUnit\Framework\MockObject\MockObject |
|
25
|
|
|
*/ |
|
26
|
|
|
private $responseFactoryMock; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var RequestHandlerInterface|\PHPUnit\Framework\MockObject\MockObject |
|
30
|
|
|
*/ |
|
31
|
|
|
private $requestHandlerMock; |
|
32
|
|
|
|
|
33
|
|
|
private IpFilter $ipFilter; |
|
34
|
|
|
|
|
35
|
|
|
public function setUp(): void |
|
36
|
|
|
{ |
|
37
|
|
|
parent::setUp(); |
|
38
|
|
|
$this->responseFactoryMock = $this->createMock(ResponseFactoryInterface::class); |
|
39
|
|
|
$this->requestHandlerMock = $this->createMock(RequestHandlerInterface::class); |
|
40
|
|
|
$this->ipFilter = new IpFilter((new Ip())->ranges([self::ALLOWED_IP]), $this->responseFactoryMock); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function testProcessReturnsAccessDeniedResponseWhenIpIsNotAllowed(): void |
|
44
|
|
|
{ |
|
45
|
|
|
$this->setUpResponseFactory(); |
|
46
|
|
|
$requestMock = $this->createMock(ServerRequestInterface::class); |
|
47
|
|
|
$requestMock |
|
48
|
|
|
->expects($this->once()) |
|
49
|
|
|
->method('getServerParams') |
|
50
|
|
|
->willReturn(self::REQUEST_PARAMS); |
|
51
|
|
|
|
|
52
|
|
|
$this->requestHandlerMock |
|
53
|
|
|
->expects($this->never()) |
|
54
|
|
|
->method('handle') |
|
55
|
|
|
->with($requestMock); |
|
56
|
|
|
|
|
57
|
|
|
$response = $this->ipFilter->process($requestMock, $this->requestHandlerMock); |
|
58
|
|
|
$this->assertEquals(403, $response->getStatusCode()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function testProcessCallsRequestHandlerWhenRemoteAddressIsAllowed(): void |
|
62
|
|
|
{ |
|
63
|
|
|
$requestParams = [ |
|
64
|
|
|
'REMOTE_ADDR' => self::ALLOWED_IP, |
|
65
|
|
|
]; |
|
66
|
|
|
$requestMock = $this->createMock(ServerRequestInterface::class); |
|
67
|
|
|
$requestMock |
|
68
|
|
|
->expects($this->once()) |
|
69
|
|
|
->method('getServerParams') |
|
70
|
|
|
->willReturn($requestParams); |
|
71
|
|
|
|
|
72
|
|
|
$this->requestHandlerMock |
|
73
|
|
|
->expects($this->once()) |
|
74
|
|
|
->method('handle') |
|
75
|
|
|
->with($requestMock); |
|
76
|
|
|
|
|
77
|
|
|
$this->ipFilter->process($requestMock, $this->requestHandlerMock); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function setUpResponseFactory(): void |
|
81
|
|
|
{ |
|
82
|
|
|
$response = new Response(403); |
|
83
|
|
|
$this->responseFactoryMock |
|
84
|
|
|
->expects($this->once()) |
|
85
|
|
|
->method('createResponse') |
|
86
|
|
|
->willReturn($response); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|