Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nekudo committed Nov 16, 2019
1 parent cf5a4e5 commit 99d55fc
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@

# Composer
composer.phar
/composer.lock
/vendor/

# PHPUnit
.phpunit.result.cache

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"require": {
"php": "^7.2",
"bloatless/endocore-http": "~0.0.1"
},
"require-dev": {
"phpunit/phpunit": "^8.3"
Expand Down
18 changes: 18 additions & 0 deletions phpunit.xml.dist
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>
2 changes: 1 addition & 1 deletion src/BasicAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Bloatless\Endocore\Http\Request;
use Bloatless\Endocore\Http\Response;

class BasicAuth
class BasicAuth
{
/**
* Valid/known users.
Expand Down
128 changes: 128 additions & 0 deletions tests/Unit/BasicAuthTest.php
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);
}
}
33 changes: 33 additions & 0 deletions tests/Unit/FactoryTest.php
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);
}
}
9 changes: 9 additions & 0 deletions tests/bootstrap.php
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);

0 comments on commit 99d55fc

Please sign in to comment.