Commit 617136ac authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

Merge branch '17-write-doors-tests' into 'master'

Resolve "Write Doors Tests"

Closes #17

See merge request kretschmar/doorcode!20
parents d79af452 1eac0e78
Pipeline #2762 passed with stages
in 1 minute and 52 seconds
......@@ -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 = [];
}
......@@ -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();
......
......@@ -64,13 +64,9 @@ class DoorGuard 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,23 +76,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);
}
/**
......@@ -107,36 +89,19 @@ 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;
}
/**
* 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();
......
......@@ -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;
......@@ -83,7 +82,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);
}
}
......@@ -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;
......
......@@ -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;
}
/**
......
......@@ -6,8 +6,14 @@ use Source\Gateways\Doors\DoorsRepository;
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 +24,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();
......
......@@ -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);
......
<?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\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\Guards;
use Source\Entities\Door;
use Source\UseCases\Doors\Authenticate\Presenter;
use Source\UseCases\Doors\Authenticate\ResponseModel;
use Source\UseCases\Doors\Authenticate\AuthenticateUseCase;
class DoorAuthenticateUseCaseStub 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);
}
}
<?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;
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\Guards\DoorAuthenticateUseCaseStub
*/
protected DoorAuthenticateUseCaseStub $useCase;
/**
* @var \Tests\Doubles\AuthenticatableStub
*/
protected AuthenticatableStub $user;
/**
* @var \Tests\Unit\Guards\DoorPresenterStub
*/
protected DoorPresenterStub $presenter;
public function setUp(): void
{
parent::setUp();
$this->useCase = new DoorAuthenticateUseCaseStub();
$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 DoorPresenterStub implements Presenter
{
public ResponseModel $response;
protected bool $presenterCalled = false;
public function present(ResponseModel $responseModel): void
{