Skip to content

Commit

Permalink
test: Add IPAddressACLTest and UserAgentACLTest
Browse files Browse the repository at this point in the history
  • Loading branch information
secondtruth committed May 17, 2024
1 parent f60ae5a commit f9f05d0
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/ACL/IPAddressACLTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
* Gatekeeper
* Copyright (C) 2024 Christian Neff
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all copies.
*/

namespace Secondtruth\Gatekeeper\Tests\ACL;

use PHPUnit\Framework\TestCase;
use Secondtruth\Gatekeeper\Visitor;
use Secondtruth\Gatekeeper\ACL\IPAddressACL;
use Secondtruth\Gatekeeper\Listing\IPList;
use Symfony\Component\HttpFoundation\Request;

class IPAddressACLTest extends TestCase
{
private $acl;

protected function setUp(): void
{
$allowed = new IPList(['127.0.0.1']);
$denied = new IPList(['192.168.1.1']);
$this->acl = new IPAddressACL($allowed, $denied);
}

public function testIsAllowed()
{
$request = Request::create('/', 'GET', [], [], [], ['REMOTE_ADDR' => '127.0.0.1']);
$this->assertTrue($this->acl->isAllowed(Visitor::fromSymfonyRequest($request)));
}

public function testIsDenied()
{
$request = Request::create('/', 'GET', [], [], [], ['REMOTE_ADDR' => '192.168.1.1']);
$this->assertTrue($this->acl->isDenied(Visitor::fromSymfonyRequest($request)));
}
}
41 changes: 41 additions & 0 deletions tests/ACL/UserAgentACLTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
* Gatekeeper
* Copyright (C) 2024 Christian Neff
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted, provided that the
* above copyright notice and this permission notice appear in all copies.
*/

namespace Secondtruth\Gatekeeper\Tests\ACL;

use PHPUnit\Framework\TestCase;
use Secondtruth\Gatekeeper\Visitor;
use Secondtruth\Gatekeeper\ACL\UserAgentACL;
use Secondtruth\Gatekeeper\Listing\StringList;
use Symfony\Component\HttpFoundation\Request;

class UserAgentACLTest extends TestCase
{
private $acl;

protected function setUp(): void
{
$allowed = new StringList(['Mozilla/5.0']);
$denied = new StringList(['Googlebot']);
$this->acl = new UserAgentACL($allowed, $denied);
}

public function testIsAllowed()
{
$request = Request::create('/', 'GET', [], [], [], ['HTTP_USER_AGENT' => 'Mozilla/5.0']);
$this->assertTrue($this->acl->isAllowed(Visitor::fromSymfonyRequest($request)));
}

public function testIsDenied()
{
$request = Request::create('/', 'GET', [], [], [], ['HTTP_USER_AGENT' => 'Googlebot']);
$this->assertTrue($this->acl->isDenied(Visitor::fromSymfonyRequest($request)));
}
}

0 comments on commit f9f05d0

Please sign in to comment.