Commit dd53f515 authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

add door guard unit tests

parent 52cb94e5
......@@ -65,8 +65,6 @@ class DoorGuard implements Guard
return $this->user;
}
$user = null;
$token = $this->getTokenForRequest();
$user = $this->retrieveByToken($token);
......@@ -79,23 +77,9 @@ class DoorGuard 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);
}
if (empty($token)) {
$token = $this->request->bearerToken();
}
if (empty($token)) {
$token = $this->request->getPassword();
}
return $token;
return $this->request->input($this->inputKey);
}
/**
......@@ -113,25 +97,12 @@ class DoorGuard implements Guard
return (bool) $this->retrieveByToken($credentials[$this->inputKey]);
}
/**
* Set the current request instance.
*
* @param Request $request
* @return $this
*/
public function setRequest(Request $request): self
{
$this->request = $request;
return $this;
}
/**
* @param string $token
*
* @return Authenticatable|null
*/
public function retrieveByToken(?string $token): ?Authenticatable
protected function retrieveByToken(?string $token): ?Authenticatable
{
$presenter = new TranslationPresenter();
......
......@@ -4,8 +4,8 @@
namespace Source\Authorization;
use App\Guards\ApiGuard;
use Carbon\Laravel\ServiceProvider;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Source\Gateways\GroupUser\GroupUserRepository;
use Illuminate\Contracts\Support\DeferrableProvider;
......
......@@ -3,7 +3,6 @@
namespace Source\UseCases\Doors\Authenticate;
use Source\Gateways\Doors\DoorsRepository;
use Source\Exceptions\AuthenticationException;
class Authenticate implements AuthenticateUseCase
{
......
<?php
namespace Tests\Doubles;
use Illuminate\Contracts\Auth\Authenticatable;
class AuthenticatableStub implements Authenticatable
{
/**
* @inheritDoc
*/
public function getAuthIdentifierName()
{
// Nada
}
/**
* @inheritDoc
*/
public function getAuthIdentifier()
{
// Nada
}
/**
* @inheritDoc
*/
public function getAuthPassword()
{
// Nada
}
/**
* @inheritDoc
*/
public function getRememberToken()
{
// Nada
}
/**
* @inheritDoc
*/
public function setRememberToken($value)
{
// Nada
}
/**
* @inheritDoc
*/
public function getRememberTokenName()
{
// Nada
}
}
<?php
namespace Tests\Unit\Guards;
use App\Guards\DoorGuard;
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
{
public const INPUT_KEY = 'input_key';
/**
* @var \Illuminate\Http\Request
*/
protected Request $request;
/**
* @var \App\Guards\DoorGuard
*/
protected DoorGuard $guard;
/**
* @var \Tests\Unit\Source\Authorizer\AuthenticateUseCaseStub
*/
protected AuthenticateUseCaseStub $useCase;
/**
* @var \Illuminate\Contracts\Auth\Authenticatable
*/
protected Authenticatable $user;
/**
* @var \Tests\Unit\Guards\PresenterStub
*/
protected PresenterStub $presenter;
public function setUp(): void
{
parent::setUp();
$this->useCase = new AuthenticateUseCaseStub();
$this->user = new AuthenticatableStub();
$this->request = new Request([self::INPUT_KEY => 'token']);
$this->guard = new DoorGuard($this->useCase, $this->request, self::INPUT_KEY);
}
/**
* @test
*/
public function it_validates_credentials(): void
{
$this->useCase->setDoorToReturn(new Door(0, '', '', ''));
$this->assertTrue($this->guard->validate([self::INPUT_KEY => '']));
}
/**
* @test
*/
public function it_wont_validate_empty_credentials(): void
{
$this->useCase->setDoorToReturn(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->setDoorToReturn(null);
$this->assertNull($this->guard->user());
}
/**
* @test
*/
public function it_gets_an_already_retrieved_user(): void
{
$door = new Door(0, '', '', '');
$this->useCase->setDoorToReturn($door);
$this->guard->user();
$this->useCase->setDoorToReturn(null);
$this->assertNotNull($this->guard->user());
}
}
<?php
namespace Tests\Unit\Guards;
use App\Door;
use Source\UseCases\Doors\Authenticate\Presenter;
use Source\UseCases\Doors\Authenticate\ResponseModel;
class PresenterStub implements Presenter
{
public ResponseModel $response;
protected bool $presenterCalled = false;
public function present(ResponseModel $responseModel): void
{
$this->presenterCalled = true;
$this->response = $responseModel;
}
public function wasPresenterCalled(): bool
{
return $this->presenterCalled;
}
public function getViewModel(): ?Door
{
return null;
}
}
<?php
namespace Tests\Unit\Source\Authorizer;
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
{
protected ?Door $door;
/**
* @param \Source\Entities\Door|null $door
*/
public function setDoorToReturn(?Door $door): void
{
$this->door = $door;
}
/**
* @inheritDoc
*/
public function check(Presenter $presenter, ?string $token): void
{
$response = new ResponseModel();
$response->setDoor($this->door);
$presenter->present($response);
}
}
......@@ -3,9 +3,9 @@
namespace Tests\Unit\Source\Authorizer;
use Tests\TestCase;
use Source\Entities\User;
use Source\Entities\Group;
use PHPUnit\Framework\TestCase;
use Source\Authorization\Permissions;
use Source\Authorization\ApiAuthorizer;
use Source\Exceptions\AuthorizationException;
......@@ -15,14 +15,29 @@ use Source\Gateways\GroupUser\InMemoryGroupUserRepository;
class AuthorizerTest extends TestCase
{
/**
* @var \Source\Gateways\GroupUser\InMemoryGroupUserRepository
*/
protected InMemoryGroupUserRepository $groupUser;
/**
* @var \Source\Gateways\Users\InMemoryUsersRepository
*/
protected InMemoryUsersRepository $users;
/**
* @var \Source\Gateways\Groups\InMemoryGroupsRepository
*/
protected InMemoryGroupsRepository $groups;
/**
* @var \Source\Authorization\ApiAuthorizer
*/
protected ApiAuthorizer $authorizer;
/**
* @var \Tests\Unit\Source\Authorizer\GuardStub
*/
protected GuardStub $guard;
public function setUp(): void
......
......@@ -13,6 +13,8 @@ class PresenterStub implements Presenter
protected bool $presenterCalled = false;
public function present(ResponseModel $responseModel): void
{
$this->presenterCalled = true;
......
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