-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,9 @@ | |
|
||
# Composer | ||
composer.phar | ||
/composer.lock | ||
/vendor/ | ||
|
||
# PHPUnit | ||
.phpunit.result.cache | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<phpunit beStrictAboutTestsThatDoNotTestAnything="true" | ||
beStrictAboutChangesToGlobalState="true" | ||
beStrictAboutOutputDuringTests="true" | ||
colors="true" | ||
stopOnFailure="false" | ||
bootstrap="./tests/bootstrap.php"> | ||
<testsuites> | ||
<testsuite name="Endocore Basic Auth Test Suite"> | ||
<directory>./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory>./src/</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
|
||
namespace Bloatless\Endocore\Components\BasicAuth\Tests\Unit; | ||
|
||
use Bloatless\Endocore\Components\BasicAuth\BasicAuth; | ||
use Bloatless\Endocore\Http\Request; | ||
use Bloatless\Endocore\Http\Response; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class BasicAuthTest extends TestCase | ||
{ | ||
protected $config; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->config = [ | ||
'auth' => [ | ||
'users' => [ | ||
'foo' => '$2y$10$hJpespHOJUYzFtHIQk57OusBdwIOXz.8tUdbb9j545Meh2wmeshMm', | ||
] | ||
] | ||
]; | ||
} | ||
|
||
public function testGetSetUsers() | ||
{ | ||
// set via config | ||
$auth = new BasicAuth($this->config['auth']['users']); | ||
$users = $auth->getUsers(); | ||
$this->assertIsArray($users); | ||
$this->assertArrayHasKey('foo', $users); | ||
$this->assertEquals($this->config['auth']['users']['foo'], $users['foo']); | ||
|
||
// set via setter | ||
$auth = new BasicAuth; | ||
$this->assertEquals([], $auth->getUsers()); | ||
$auth->setUsers($this->config['auth']['users']); | ||
$users = $auth->getUsers(); | ||
$this->assertArrayHasKey('foo', $users); | ||
$this->assertEquals($this->config['auth']['users']['foo'], $users['foo']); | ||
} | ||
|
||
public function testIsAuthenticatedWithoutHTTPHeader() | ||
{ | ||
// test without auth header | ||
$auth = new BasicAuth; | ||
$request = new Request; | ||
$result = $auth->isAuthenticated($request); | ||
$this->assertFalse($result); | ||
} | ||
|
||
public function testIsAuthenticatedWithValidHttpHeader() | ||
{ | ||
$request = new Request([], [], [ | ||
'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('foo:bar'), | ||
]); | ||
$auth = new BasicAuth; | ||
$auth->setUsers($this->config['auth']['users']); | ||
$result = $auth->isAuthenticated($request); | ||
$this->assertTrue($result); | ||
} | ||
|
||
public function testIsAuthenticatedWithInvalidHttpHeader() | ||
{ | ||
$auth = new BasicAuth; | ||
|
||
// Header is missing "Basic" keyword | ||
$request = new Request([], [], [ | ||
'HTTP_AUTHORIZATION' => 'cisaB ' . base64_encode('foo:bar'), | ||
]); | ||
$result = $auth->isAuthenticated($request); | ||
$this->assertFalse($result); | ||
|
||
// Header with invalid base64 encoding | ||
$request = new Request([], [], [ | ||
'HTTP_AUTHORIZATION' => 'Basic FooBarBz', | ||
]); | ||
$result = $auth->isAuthenticated($request); | ||
$this->assertFalse($result); | ||
} | ||
|
||
public function testIsAuthenticatedWithNoUsersSet() | ||
{ | ||
$request = new Request([], [], [ | ||
'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('foo:bar'), | ||
]); | ||
$auth = new BasicAuth; | ||
$result = $auth->isAuthenticated($request); | ||
$this->assertFalse($result); | ||
|
||
} | ||
|
||
public function testIsAuthenticatedWithInvalidUsername() | ||
{ | ||
$request = new Request([], [], [ | ||
'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('invalid:bar'), | ||
]); | ||
$auth = new BasicAuth; | ||
$auth->setUsers($this->config['auth']['users']); | ||
$result = $auth->isAuthenticated($request); | ||
$this->assertFalse($result); | ||
} | ||
|
||
public function testIsAuthenticatedWithInvalidPassword() | ||
{ | ||
$request = new Request([], [], [ | ||
'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('foo:naa'), | ||
]); | ||
$auth = new BasicAuth; | ||
$auth->setUsers($this->config['auth']['users']); | ||
$result = $auth->isAuthenticated($request); | ||
$this->assertFalse($result); | ||
} | ||
|
||
public function testRequestAuthorization() | ||
{ | ||
$auth = new BasicAuth; | ||
$response = $auth->requestAuthorization(); | ||
$this->assertInstanceOf(Response::class, $response); | ||
$this->assertEquals(401, $response->getStatus()); | ||
|
||
$headers = $response->getHeaders(); | ||
$this->assertIsArray($headers); | ||
$this->assertEquals([ | ||
'WWW-Authenticate' => 'Basic realm="Restricted access"', | ||
], $headers); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace Bloatless\Endocore\Components\BasicAuth\Tests\Unit; | ||
|
||
use Bloatless\Endocore\Components\BasicAuth\BasicAuth; | ||
use Bloatless\Endocore\Components\BasicAuth\Factory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FactoryTest extends TestCase | ||
{ | ||
public function testFactoryWithValidConfig() | ||
{ | ||
$config = [ | ||
'auth' => [ | ||
'users' => [ | ||
'foo' => '$2y$10$hJpespHOJUYzFtHIQk57OusBdwIOXz.8tUdbb9j545Meh2wmeshMm', | ||
] | ||
] | ||
]; | ||
|
||
$factory = new Factory($config); | ||
$auth = $factory->makeAuth(); | ||
$this->assertInstanceOf(BasicAuth::class, $auth); | ||
} | ||
|
||
public function testFactoryWithInvalidConfig() | ||
{ | ||
$config = []; | ||
$factory = new Factory($config); | ||
$auth = $factory->makeAuth(); | ||
$this->assertInstanceOf(BasicAuth::class, $auth); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
define('TESTS_ROOT', __DIR__); | ||
|
||
/** @var \Composer\Autoload\ClassLoader $autoloader */ | ||
$autoloader = require TESTS_ROOT . '/../vendor/autoload.php'; | ||
|
||
// Register test classes | ||
$autoloader->addPsr4('Bloatless\Endocore\Components\BasicAuth\Tests\\', TESTS_ROOT); |