From 18c2a9c631c10e53fb0c30fa7b1d7d14558d0ae0 Mon Sep 17 00:00:00 2001 From: Vaidas Bagdonas Date: Thu, 21 Nov 2019 15:44:26 +0100 Subject: [PATCH] add more tests --- tests/Unit/Services/AccessManagerTest.php | 66 +++++++++++++++++++++-- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/tests/Unit/Services/AccessManagerTest.php b/tests/Unit/Services/AccessManagerTest.php index cb58972..d6c3896 100644 --- a/tests/Unit/Services/AccessManagerTest.php +++ b/tests/Unit/Services/AccessManagerTest.php @@ -18,8 +18,8 @@ class AccessManagerTest extends TestCase */ public function testUserShouldBeAllowedIfNoIpRestrictionsAreSet(): void { - $this->markTestSkipped(); - $accessManager = new AccessManager('REMOTE_ADDR', null); + $userIpRequestHeaderName = 'REMOTE_ADDR'; + $accessManager = new AccessManager($userIpRequestHeaderName, null); /** @var Request|MockObject $requestMock */ $requestMock = $this->createMock(Request::class); @@ -30,12 +30,72 @@ public function testUserShouldBeAllowedIfNoIpRestrictionsAreSet(): void $accessManager->isAllowed($requestMock); } + /** + * @covers ::isAllowed() + * @expectedException \App\Exception\OutboundMessageTowerException + */ + public function testThrowsExceptionIfUserIpIsNotFound(): void + { + $userIpRequestHeaderName = 'REMOTE_ADDR'; + $accessManager = new AccessManager($userIpRequestHeaderName, '127.0.0.1'); + + /** @var Request|MockObject $requestMock */ + $requestMock = $this->createMock(Request::class); + $requestMock->headers = $this->createMock(HeaderBag::class); + $requestMock->headers->expects($this->once()) + ->method('get') + ->with($userIpRequestHeaderName) + ->willReturn(null); + + $accessManager->isAllowed($requestMock); + } + + /** + * @covers ::isAllowed() + * @expectedException \App\Exception\OutboundMessageTowerException + */ + public function testThrowsExceptionIfUserIpIsNotAllowed(): void + { + $userIpRequestHeaderName = 'REMOTE_ADDR'; + $accessManager = new AccessManager($userIpRequestHeaderName, '127.0.0.1'); + + /** @var Request|MockObject $requestMock */ + $requestMock = $this->createMock(Request::class); + $requestMock->headers = $this->createMock(HeaderBag::class); + $requestMock->headers->expects($this->once()) + ->method('get') + ->with($userIpRequestHeaderName) + ->willReturn('127.0.0.100'); + + $accessManager->isAllowed($requestMock); + } + + /** + * @covers ::isAllowed() + */ + public function testPassWithoutExceptionsIfUserIpIsAllowed(): void + { + $userIpRequestHeaderName = 'REMOTE_ADDR'; + $accessManager = new AccessManager($userIpRequestHeaderName, '127.0.0.1,127.0.0.100'); + + /** @var Request|MockObject $requestMock */ + $requestMock = $this->createMock(Request::class); + $requestMock->headers = $this->createMock(HeaderBag::class); + $requestMock->headers->expects($this->once()) + ->method('get') + ->with($userIpRequestHeaderName) + ->willReturn('127.0.0.100'); + + $accessManager->isAllowed($requestMock); + } + /** * @covers ::parseSubnetsToIpAddresses() */ public function testParsesSubnetIpAddressesCorrectly(): void { - $accessManager = new AccessManager('REMOTE_ADDR', '13.108.238.128/28'); + $userIpRequestHeaderName = 'REMOTE_ADDR'; + $accessManager = new AccessManager($userIpRequestHeaderName, '13.108.238.128/28'); /** @var Request|MockObject $requestMock */ $requestMock = $this->createMock(Request::class);