Commit 7d84b3ba authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Add ApiGuard tests

parent dd53f515
......@@ -64,13 +64,9 @@ class ApiGuard implements Guard
return $this->user;
}
$user = null;
$token = $this->getTokenForRequest();
if (!empty($token)) {
$user = $this->retrieveByToken($token);
}
$user = $this->retrieveByToken($token);
return $this->user = $user;
}
......@@ -80,13 +76,9 @@ class ApiGuard implements Guard
*
* @return string|null
*/
public function getTokenForRequest(): ?string
protected function getTokenForRequest(): ?string
{
$token = $this->request->query($this->inputKey);
if (empty($token)) {
$token = $this->request->input($this->inputKey);
}
$token = $this->request->input($this->inputKey);
if (empty($token)) {
$token = $this->request->bearerToken();
......@@ -111,36 +103,19 @@ class ApiGuard implements Guard
*/
public function validate(array $credentials = [])
{
if (empty($credentials[$this->inputKey])) {
if (!isset($credentials[$this->inputKey])) {
return false;
}
if ($this->retrieveByToken($credentials[$this->inputKey])) {
return true;
}
return false;
}
/**
* Set the current request instance.
*
* @param Request $request
* @return $this
*/
public function setRequest(Request $request): self
{
$this->request = $request;
return $this;
return (bool) $this->retrieveByToken($credentials[$this->inputKey]);
}
/**
* @param string $token
* @param string|null $token
*
* @return Authenticatable|null
*/
public function retrieveByToken(string $token): ?Authenticatable
protected function retrieveByToken(?string $token): ?Authenticatable
{
$presenter = new TranslationPresenter();
......
......@@ -7,7 +7,6 @@ use Illuminate\Http\Request;
use Illuminate\Auth\GuardHelpers;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\Authenticatable;
use Source\Exceptions\AuthenticationException;
use Source\UseCases\Doors\Authenticate\AuthenticateUseCase;
use Source\UseCases\Doors\Authenticate\TranslationPresenter;
......@@ -98,7 +97,7 @@ class DoorGuard implements Guard
}
/**
* @param string $token
* @param string|null $token
*
* @return Authenticatable|null
*/
......
......@@ -12,7 +12,6 @@ use Illuminate\Auth\Middleware\RequirePassword;
use Illuminate\Http\Middleware\SetCacheHeaders;
use Illuminate\Session\Middleware\StartSession;
use App\Http\Middleware\CheckForMaintenanceMode;
use App\Http\Middleware\RedirectIfAuthenticated;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
use Illuminate\Routing\Middleware\ValidateSignature;
......
......@@ -27,6 +27,10 @@ class Authenticate implements AuthenticateUseCase
*/
public function check(Presenter $presenter, ?string $token): void
{
$response = new ResponseModel();
$response->setUser(null);
$presenter->present($response);
if (!$token) {
return;
}
......@@ -37,8 +41,6 @@ class Authenticate implements AuthenticateUseCase
return;
}
$response = new ResponseModel();
$user = $this->users->get($found->getUserId());
$response->setUser($user);
......
......@@ -3,7 +3,6 @@
namespace Tests\Doubles;
use Illuminate\Contracts\Auth\Authenticatable;
class AuthenticatableStub implements Authenticatable
......
<?php
namespace Tests\Unit\Guards;
use App\Guards\ApiGuard;
use Source\Entities\User;
use Illuminate\Http\Request;
use PHPUnit\Framework\TestCase;
use Tests\Doubles\AuthenticatableStub;
class ApiGuardTest extends TestCase
{
public const INPUT_KEY = 'input_key';
/**
* @var \Illuminate\Http\Request
*/
protected Request $request;
/**
* @var \App\Guards\ApiGuard
*/
protected ApiGuard $guard;
/**
* @var \Tests\Unit\Guards\TokenAuthenticateUseCaseStub
*/
protected TokenAuthenticateUseCaseStub $useCase;
/**
* @var \Tests\Doubles\AuthenticatableStub
*/
protected AuthenticatableStub $user;
/**
* @var \Tests\Unit\Guards\TokenPresenterStub
*/
protected TokenPresenterStub $presenter;
public function setUp(): void
{
parent::setUp();
$this->useCase = new TokenAuthenticateUseCaseStub();
$this->request = new Request([self::INPUT_KEY => 'token']);
$this->user = new AuthenticatableStub();
$this->guard = new ApiGuard($this->useCase, $this->request, self::INPUT_KEY);
}
/**
* @test
*/
public function it_validates_credentials(): void
{
$this->useCase->setUserToReturn(new User(0, '', '', '', '', '', '', ''));
$this->assertTrue($this->guard->validate([self::INPUT_KEY => 'ree']));
}
/**
* @test
*/
public function it_wont_validate_empty_credentials(): void
{
$this->useCase->setUserToReturn(null);
$this->assertFalse($this->guard->validate([]));
$this->assertFalse($this->guard->validate([self::INPUT_KEY => '']));
}
/**
* @test
*/
public function it_gets_a_null_user(): void
{
$this->useCase->setUserToReturn(null);
$this->assertNull($this->guard->user());
}
/**
* @test
*/
public function it_gets_an_already_retrieved_user(): void
{
$user = new User(0, '', '', '', '', '', '', '');
$this->useCase->setUserToReturn($user);
$this->guard->user();
$this->useCase->setUserToReturn(null);
$this->assertNotNull($this->guard->user());
}
}
<?php
namespace Tests\Unit\Source\Authorizer;
namespace Tests\Unit\Guards;
use Source\Entities\Door;
use Source\UseCases\Doors\Authenticate\Presenter;
use Source\UseCases\Doors\Authenticate\ResponseModel;
use Source\UseCases\Doors\Authenticate\AuthenticateUseCase;
class AuthenticateUseCaseStub implements AuthenticateUseCase
class DoorAuthenticateUseCaseStub implements AuthenticateUseCase
{
protected ?Door $door;
......
......@@ -8,8 +8,6 @@ use Source\Entities\Door;
use Illuminate\Http\Request;
use PHPUnit\Framework\TestCase;
use Tests\Doubles\AuthenticatableStub;
use Illuminate\Contracts\Auth\Authenticatable;
use Tests\Unit\Source\Authorizer\AuthenticateUseCaseStub;
class DoorGuardTest extends TestCase
{
......@@ -26,25 +24,25 @@ class DoorGuardTest extends TestCase
protected DoorGuard $guard;
/**
* @var \Tests\Unit\Source\Authorizer\AuthenticateUseCaseStub
* @var \Tests\Unit\Guards\DoorAuthenticateUseCaseStub
*/
protected AuthenticateUseCaseStub $useCase;
protected DoorAuthenticateUseCaseStub $useCase;
/**
* @var \Illuminate\Contracts\Auth\Authenticatable
* @var \Tests\Doubles\AuthenticatableStub
*/
protected Authenticatable $user;
protected AuthenticatableStub $user;
/**
* @var \Tests\Unit\Guards\PresenterStub
* @var \Tests\Unit\Guards\DoorPresenterStub
*/
protected PresenterStub $presenter;
protected DoorPresenterStub $presenter;
public function setUp(): void
{
parent::setUp();
$this->useCase = new AuthenticateUseCaseStub();
$this->useCase = new DoorAuthenticateUseCaseStub();
$this->user = new AuthenticatableStub();
$this->request = new Request([self::INPUT_KEY => 'token']);
......
......@@ -7,7 +7,7 @@ use App\Door;
use Source\UseCases\Doors\Authenticate\Presenter;
use Source\UseCases\Doors\Authenticate\ResponseModel;
class PresenterStub implements Presenter
class DoorPresenterStub implements Presenter
{
public ResponseModel $response;
......
<?php
namespace Tests\Unit\Guards;
use Source\Entities\User;
use Source\UseCases\Token\Authenticate\Presenter;
use Source\UseCases\Token\Authenticate\ResponseModel;
use Source\UseCases\Token\Authenticate\AuthenticateUseCase;
class TokenAuthenticateUseCaseStub implements AuthenticateUseCase
{
/**
* @var \Source\Entities\User|null
*/
protected ?User $user = null;
/**
* @param \Source\Entities\User|null $user
*/
public function setUserToReturn(?User $user): void
{
$this->user = $user;
}
/**
* @inheritDoc
*/
public function check(Presenter $presenter, ?string $token): void
{
$response = new ResponseModel();
$response->setUser($this->user);
$presenter->present($response);
}
}
<?php
namespace Tests\Unit\Guards;
use App\User;
use Source\UseCases\Token\Authenticate\Presenter;
use Source\UseCases\Token\Authenticate\ResponseModel;
class TokenPresenterStub implements Presenter
{
/**
* @var \Source\UseCases\Token\Authenticate\ResponseModel
*/
public ResponseModel $response;
protected bool $presenterCalled = false;
/**
* @param \Source\UseCases\Token\Authenticate\ResponseModel $responseModel
*/
public function present(ResponseModel $responseModel): void
{
$this->presenterCalled = true;
$this->response = $responseModel;
}
public function wasPresenterCalled(): bool
{
return $this->presenterCalled;
}
public function getViewModel(): ?User
{
return null;
}
}
......@@ -96,7 +96,7 @@ class AuthorizerTest extends TestCase
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_denies_unauthoirzed_peeps(): void
public function it_denies_unauthorized_peeps(): void
{
$this->createUserWithPerms(['what-i-have']);
......
......@@ -3,10 +3,7 @@
namespace Tests\Unit\Source\UseCases\Doors\Authenticate;
use Carbon\Carbon;
use Source\Entities\User;
use Source\Entities\Door;
use Source\Entities\Token;
use PHPUnit\Framework\TestCase;
use Source\UseCases\Doors\Authenticate\ResponseModel;
use Source\UseCases\Doors\Authenticate\TranslationPresenter;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment