Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jenky committed Jun 20, 2023
1 parent 0b4a2fd commit 4079140
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Middleware/Auth/TokenAuthentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ final class TokenAuthentication
*/
private $token;

public function __construct(string $token, string $tokenPrefix = 'Bearer ')
public function __construct(string $token, string $tokenPrefix = 'Bearer')
{
$this->token = $tokenPrefix.trim($token);
$this->token = $tokenPrefix.' '.trim($token);
}

public function __invoke(RequestInterface $request, callable $next): ResponseInterface
Expand Down
83 changes: 83 additions & 0 deletions tests/AuthTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

namespace Jenky\Atlas\Tests;

use Jenky\Atlas\Middleware\Auth\BasicAuthentication;
use Jenky\Atlas\Middleware\Auth\TokenAuthentication;
use Jenky\Atlas\Mock\MockClient;
use Jenky\Atlas\Mock\MockResponse;
use Jenky\Atlas\NullConnector;
use Jenky\Atlas\Tests\Services\DummyRequest;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

final class AuthTest extends TestCase
{
private function fakeAuthentication(string $credential): callable
{
return static function (RequestInterface $request, callable $next) use ($credential): ResponseInterface {
if (! $request->hasHeader('Authorization')) {
return $next($request)->withStatus(401);
}

$authorization = $request->getHeaderLine('Authorization');

if ($authorization !== $credential) {
return $next($request)->withStatus(401);
}

return $next($request);
};
}

private function fakeBasicAuthentication(string $username, string $password): callable
{
$credential = base64_encode("$username:$password");

return $this->fakeAuthentication('Basic '.$credential);
}

private function fakeTokenAuthentication(string $token): callable
{
return $this->fakeAuthentication('Bearer '.$token);
}

public function test_basic_auth(): void
{
$client = new MockClient(
MockResponse::create(['authenticated' => true])
);
$connector = (new NullConnector())->withClient($client);
$connector->middleware()->push($this->fakeBasicAuthentication('foo', 'password'), 'fake_auth');

$response = $connector->send(new DummyRequest('http://localhost'));

$this->assertTrue($response->unauthorized());

$connector->middleware()->before('fake_auth', new BasicAuthentication('foo', 'password'));

$response = $connector->send(new DummyRequest('http://localhost'));

$this->assertTrue($response->ok());
$this->assertTrue($response['authenticated']);
}

public function test_token_auth(): void
{
$client = new MockClient();
$connector = (new NullConnector())->withClient($client);
$connector->middleware()->push($this->fakeTokenAuthentication('#zKh#4KNu$Bq4^b97KJ6'), 'fake_auth');

$response = $connector->send(new DummyRequest('http://localhost'));

$this->assertTrue($response->unauthorized());

$connector->middleware()->before('fake_auth', new TokenAuthentication('#zKh#4KNu$Bq4^b97KJ6'));

$response = $connector->send(new DummyRequest('http://localhost'));

$this->assertTrue($response->ok());
}
}

0 comments on commit 4079140

Please sign in to comment.