Skip to content

Commit 99d55fc

Browse files
committed
Add tests
1 parent cf5a4e5 commit 99d55fc

File tree

7 files changed

+195
-1
lines changed

7 files changed

+195
-1
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@
33

44
# Composer
55
composer.phar
6+
/composer.lock
67
/vendor/
8+
9+
# PHPUnit
10+
.phpunit.result.cache
11+

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"require": {
2424
"php": "^7.2",
25+
"bloatless/endocore-http": "~0.0.1"
2526
},
2627
"require-dev": {
2728
"phpunit/phpunit": "^8.3"

phpunit.xml.dist

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<phpunit beStrictAboutTestsThatDoNotTestAnything="true"
2+
beStrictAboutChangesToGlobalState="true"
3+
beStrictAboutOutputDuringTests="true"
4+
colors="true"
5+
stopOnFailure="false"
6+
bootstrap="./tests/bootstrap.php">
7+
<testsuites>
8+
<testsuite name="Endocore Basic Auth Test Suite">
9+
<directory>./tests/</directory>
10+
</testsuite>
11+
</testsuites>
12+
13+
<filter>
14+
<whitelist processUncoveredFilesFromWhitelist="true">
15+
<directory>./src/</directory>
16+
</whitelist>
17+
</filter>
18+
</phpunit>

src/BasicAuth.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Bloatless\Endocore\Http\Request;
66
use Bloatless\Endocore\Http\Response;
77

8-
class BasicAuth
8+
class BasicAuth
99
{
1010
/**
1111
* Valid/known users.

tests/Unit/BasicAuthTest.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
3+
namespace Bloatless\Endocore\Components\BasicAuth\Tests\Unit;
4+
5+
use Bloatless\Endocore\Components\BasicAuth\BasicAuth;
6+
use Bloatless\Endocore\Http\Request;
7+
use Bloatless\Endocore\Http\Response;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class BasicAuthTest extends TestCase
11+
{
12+
protected $config;
13+
14+
public function setUp(): void
15+
{
16+
$this->config = [
17+
'auth' => [
18+
'users' => [
19+
'foo' => '$2y$10$hJpespHOJUYzFtHIQk57OusBdwIOXz.8tUdbb9j545Meh2wmeshMm',
20+
]
21+
]
22+
];
23+
}
24+
25+
public function testGetSetUsers()
26+
{
27+
// set via config
28+
$auth = new BasicAuth($this->config['auth']['users']);
29+
$users = $auth->getUsers();
30+
$this->assertIsArray($users);
31+
$this->assertArrayHasKey('foo', $users);
32+
$this->assertEquals($this->config['auth']['users']['foo'], $users['foo']);
33+
34+
// set via setter
35+
$auth = new BasicAuth;
36+
$this->assertEquals([], $auth->getUsers());
37+
$auth->setUsers($this->config['auth']['users']);
38+
$users = $auth->getUsers();
39+
$this->assertArrayHasKey('foo', $users);
40+
$this->assertEquals($this->config['auth']['users']['foo'], $users['foo']);
41+
}
42+
43+
public function testIsAuthenticatedWithoutHTTPHeader()
44+
{
45+
// test without auth header
46+
$auth = new BasicAuth;
47+
$request = new Request;
48+
$result = $auth->isAuthenticated($request);
49+
$this->assertFalse($result);
50+
}
51+
52+
public function testIsAuthenticatedWithValidHttpHeader()
53+
{
54+
$request = new Request([], [], [
55+
'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('foo:bar'),
56+
]);
57+
$auth = new BasicAuth;
58+
$auth->setUsers($this->config['auth']['users']);
59+
$result = $auth->isAuthenticated($request);
60+
$this->assertTrue($result);
61+
}
62+
63+
public function testIsAuthenticatedWithInvalidHttpHeader()
64+
{
65+
$auth = new BasicAuth;
66+
67+
// Header is missing "Basic" keyword
68+
$request = new Request([], [], [
69+
'HTTP_AUTHORIZATION' => 'cisaB ' . base64_encode('foo:bar'),
70+
]);
71+
$result = $auth->isAuthenticated($request);
72+
$this->assertFalse($result);
73+
74+
// Header with invalid base64 encoding
75+
$request = new Request([], [], [
76+
'HTTP_AUTHORIZATION' => 'Basic FooBarBz',
77+
]);
78+
$result = $auth->isAuthenticated($request);
79+
$this->assertFalse($result);
80+
}
81+
82+
public function testIsAuthenticatedWithNoUsersSet()
83+
{
84+
$request = new Request([], [], [
85+
'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('foo:bar'),
86+
]);
87+
$auth = new BasicAuth;
88+
$result = $auth->isAuthenticated($request);
89+
$this->assertFalse($result);
90+
91+
}
92+
93+
public function testIsAuthenticatedWithInvalidUsername()
94+
{
95+
$request = new Request([], [], [
96+
'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('invalid:bar'),
97+
]);
98+
$auth = new BasicAuth;
99+
$auth->setUsers($this->config['auth']['users']);
100+
$result = $auth->isAuthenticated($request);
101+
$this->assertFalse($result);
102+
}
103+
104+
public function testIsAuthenticatedWithInvalidPassword()
105+
{
106+
$request = new Request([], [], [
107+
'HTTP_AUTHORIZATION' => 'Basic ' . base64_encode('foo:naa'),
108+
]);
109+
$auth = new BasicAuth;
110+
$auth->setUsers($this->config['auth']['users']);
111+
$result = $auth->isAuthenticated($request);
112+
$this->assertFalse($result);
113+
}
114+
115+
public function testRequestAuthorization()
116+
{
117+
$auth = new BasicAuth;
118+
$response = $auth->requestAuthorization();
119+
$this->assertInstanceOf(Response::class, $response);
120+
$this->assertEquals(401, $response->getStatus());
121+
122+
$headers = $response->getHeaders();
123+
$this->assertIsArray($headers);
124+
$this->assertEquals([
125+
'WWW-Authenticate' => 'Basic realm="Restricted access"',
126+
], $headers);
127+
}
128+
}

tests/Unit/FactoryTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Bloatless\Endocore\Components\BasicAuth\Tests\Unit;
4+
5+
use Bloatless\Endocore\Components\BasicAuth\BasicAuth;
6+
use Bloatless\Endocore\Components\BasicAuth\Factory;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class FactoryTest extends TestCase
10+
{
11+
public function testFactoryWithValidConfig()
12+
{
13+
$config = [
14+
'auth' => [
15+
'users' => [
16+
'foo' => '$2y$10$hJpespHOJUYzFtHIQk57OusBdwIOXz.8tUdbb9j545Meh2wmeshMm',
17+
]
18+
]
19+
];
20+
21+
$factory = new Factory($config);
22+
$auth = $factory->makeAuth();
23+
$this->assertInstanceOf(BasicAuth::class, $auth);
24+
}
25+
26+
public function testFactoryWithInvalidConfig()
27+
{
28+
$config = [];
29+
$factory = new Factory($config);
30+
$auth = $factory->makeAuth();
31+
$this->assertInstanceOf(BasicAuth::class, $auth);
32+
}
33+
}

tests/bootstrap.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
define('TESTS_ROOT', __DIR__);
4+
5+
/** @var \Composer\Autoload\ClassLoader $autoloader */
6+
$autoloader = require TESTS_ROOT . '/../vendor/autoload.php';
7+
8+
// Register test classes
9+
$autoloader->addPsr4('Bloatless\Endocore\Components\BasicAuth\Tests\\', TESTS_ROOT);

0 commit comments

Comments
 (0)