Commit 52cb94e5 authored by Jacob Priddy's avatar Jacob Priddy 👌

Door use case tests.

parent d79af452
......@@ -6,7 +6,7 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
class Door extends Authenticatable
{
protected $fillable = [
'*'
];
protected $fillable = ['id', 'name', 'location', 'created_at', 'updated_at'];
protected $guarded = [];
}
......@@ -7,6 +7,7 @@ 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;
......@@ -68,9 +69,7 @@ class DoorGuard implements Guard
$token = $this->getTokenForRequest();
if (!empty($token)) {
$user = $this->retrieveByToken($token);
}
$user = $this->retrieveByToken($token);
return $this->user = $user;
}
......@@ -107,15 +106,11 @@ class DoorGuard 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;
return (bool) $this->retrieveByToken($credentials[$this->inputKey]);
}
/**
......@@ -136,7 +131,7 @@ class DoorGuard implements Guard
*
* @return Authenticatable|null
*/
public function retrieveByToken(string $token): ?Authenticatable
public function retrieveByToken(?string $token): ?Authenticatable
{
$presenter = new TranslationPresenter();
......
......@@ -83,7 +83,6 @@ class Kernel extends HttpKernel
'bindings' => SubstituteBindings::class,
'cache.headers' => SetCacheHeaders::class,
'can' => Authorize::class,
'guest' => RedirectIfAuthenticated::class,
'password.confirm' => RequirePassword::class,
'signed' => ValidateSignature::class,
'throttle' => ThrottleRequests::class,
......
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use App\Providers\RouteServiceProvider;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
return $next($request);
}
}
......@@ -9,7 +9,7 @@ class InMemorySamlRepository implements SamlRepository
{
protected ?SamlUser $userToLogInAs = null;
protected ?SamlUser $loggedInUser = null;
protected ?SamlUser $loggedInUser;
protected string $loginUrl;
......@@ -43,7 +43,11 @@ class InMemorySamlRepository implements SamlRepository
*/
public function handleLogin(): ?SamlUser
{
return $this->loggedInUser;
if ($this->isAuthenticated()) {
return $this->loggedInUser;
}
return null;
}
/**
......
......@@ -3,11 +3,18 @@
namespace Source\UseCases\Doors\Authenticate;
use Source\Gateways\Doors\DoorsRepository;
use Source\Exceptions\AuthenticationException;
class Authenticate implements AuthenticateUseCase
{
/**
* @var \Source\Gateways\Doors\DoorsRepository
*/
protected DoorsRepository $doors;
/**
* @param \Source\Gateways\Doors\DoorsRepository $doors
*/
public function __construct(DoorsRepository $doors)
{
$this->doors = $doors;
......@@ -18,11 +25,11 @@ class Authenticate implements AuthenticateUseCase
*/
public function check(Presenter $presenter, ?string $token): void
{
if (!$token) {
return;
}
$found = null;
$found = $this->doors->getByToken($token);
if ($token) {
$found = $this->doors->getByToken($token);
}
$response = new ResponseModel();
......
<?php
namespace Tests\Unit\Source\UseCases\Doors\Authenticate;
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\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;
class PresenterTest extends TestCase
{
/**
* @var \Source\UseCases\Doors\Authenticate\TranslationPresenter
*/
protected TranslationPresenter $presenter;
/**
* @var \Source\UseCases\Doors\Authenticate\ResponseModel
*/
protected ResponseModel $model;
/**
* @var \App\Door|null
*/
protected ?\App\Door $response;
public function setUp(): void
{
parent::setUp();
$this->presenter = new TranslationPresenter();
$this->model = new ResponseModel();
}
public function handleTest(): void
{
$this->presenter->present($this->model);
$this->response = $this->presenter->getViewModel();
}
/**
* @test
*/
public function it_presents_a_null_door(): void
{
$this->model->setDoor(null);
$this->handleTest();
$this->assertNull($this->response);
}
/**
* @test
*/
public function it_presents_a_door(): void
{
$door = new Door(0, 'location', 'name', 'token');
$this->model->setDoor($door);
$this->handleTest();
$this->assertEquals(new \App\Door([
'id' => 0,
'name' => 'name',
'location' => 'location',
'created_at' => null,
'updated_at' => null,
]), $this->response);
}
}
<?php
namespace Tests\Unit\Source\UseCases\Doors\Authenticate;
use Source\Entities\Door;
use PHPUnit\Framework\TestCase;
use Source\Gateways\Doors\InMemoryDoorsRepository;
use Source\UseCases\Doors\Authenticate\Authenticate;
use Source\UseCases\Doors\Authenticate\ResponseModel;
class UseCaseTest extends TestCase
{
/**
* @var \Source\Gateways\Doors\InMemoryDoorsRepository
*/
protected InMemoryDoorsRepository $doors;
/**
* @var \Tests\Unit\Source\UseCases\Doors\Authenticate\PresenterStub
*/
protected PresenterStub $presenter;
/**
* @var \Source\UseCases\Doors\Authenticate\ResponseModel
*/
protected ResponseModel $response;
protected Authenticate $useCase;
public function setUp(): void
{
parent::setUp();
$this->doors = new InMemoryDoorsRepository();
$this->presenter = new PresenterStub();
$this->useCase = new Authenticate($this->doors);
}
/**
* @param string|null $token
*/
public function handleTest(?string $token): void
{
$this->useCase->check($this->presenter, $token);
$this->response = $this->presenter->response;
}
/**
* @test
*/
public function it_calls_present_on_presenter(): void
{
$this->handleTest(null);
$this->assertTrue($this->presenter->wasPresenterCalled());
}
/**
* @test
*/
public function it_doesnt_find_empty_token(): void
{
$this->handleTest(null);
$this->assertNull($this->response->getDoor());
}
/**
* @test
*/
public function it_finds_a_door(): void
{
$door = $this->doors->create(new Door(0, '', '', 'token'));
$this->handleTest('token');
$this->assertEquals($door, $this->response->getDoor());
}
}
Markdown is supported
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