Skip to content
This repository has been archived by the owner on Aug 20, 2020. It is now read-only.

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Vaidas Bagdonas committed Nov 21, 2019
1 parent fcef394 commit 18c2a9c
Showing 1 changed file with 63 additions and 3 deletions.
66 changes: 63 additions & 3 deletions tests/Unit/Services/AccessManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 18c2a9c

Please sign in to comment.