From 52cb94e532424319ac841f754d03fb22e860fb3c Mon Sep 17 00:00:00 2001 From: dakriy Date: Wed, 4 Mar 2020 13:42:51 -0800 Subject: [PATCH] Door use case tests. --- src/web/backend/app/Door.php | 6 +- src/web/backend/app/Guards/DoorGuard.php | 15 ++-- src/web/backend/app/Http/Kernel.php | 1 - .../Middleware/RedirectIfAuthenticated.php | 27 ------- .../Gateways/Saml/InMemorySamlRepository.php | 8 +- .../Doors/Authenticate/Authenticate.php | 15 +++- .../Doors/Authenticate/PresenterStub.php | 31 +++++++ .../Doors/Authenticate/PresenterTest.php | 78 ++++++++++++++++++ .../Doors/Authenticate/UseCaseTest.php | 81 +++++++++++++++++++ 9 files changed, 215 insertions(+), 47 deletions(-) delete mode 100644 src/web/backend/app/Http/Middleware/RedirectIfAuthenticated.php create mode 100644 src/web/backend/tests/Unit/Source/UseCases/Doors/Authenticate/PresenterStub.php create mode 100644 src/web/backend/tests/Unit/Source/UseCases/Doors/Authenticate/PresenterTest.php create mode 100644 src/web/backend/tests/Unit/Source/UseCases/Doors/Authenticate/UseCaseTest.php diff --git a/src/web/backend/app/Door.php b/src/web/backend/app/Door.php index ee35fac2..a613e599 100644 --- a/src/web/backend/app/Door.php +++ b/src/web/backend/app/Door.php @@ -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 = []; } diff --git a/src/web/backend/app/Guards/DoorGuard.php b/src/web/backend/app/Guards/DoorGuard.php index f0db5269..d056eb7e 100644 --- a/src/web/backend/app/Guards/DoorGuard.php +++ b/src/web/backend/app/Guards/DoorGuard.php @@ -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(); diff --git a/src/web/backend/app/Http/Kernel.php b/src/web/backend/app/Http/Kernel.php index dde5b237..c2790c4c 100644 --- a/src/web/backend/app/Http/Kernel.php +++ b/src/web/backend/app/Http/Kernel.php @@ -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, diff --git a/src/web/backend/app/Http/Middleware/RedirectIfAuthenticated.php b/src/web/backend/app/Http/Middleware/RedirectIfAuthenticated.php deleted file mode 100644 index d38fc2e9..00000000 --- a/src/web/backend/app/Http/Middleware/RedirectIfAuthenticated.php +++ /dev/null @@ -1,27 +0,0 @@ -check()) { - return redirect(RouteServiceProvider::HOME); - } - - return $next($request); - } -} diff --git a/src/web/backend/src/Gateways/Saml/InMemorySamlRepository.php b/src/web/backend/src/Gateways/Saml/InMemorySamlRepository.php index 8afbdec6..72232c44 100644 --- a/src/web/backend/src/Gateways/Saml/InMemorySamlRepository.php +++ b/src/web/backend/src/Gateways/Saml/InMemorySamlRepository.php @@ -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; } /** diff --git a/src/web/backend/src/UseCases/Doors/Authenticate/Authenticate.php b/src/web/backend/src/UseCases/Doors/Authenticate/Authenticate.php index 207812c7..2c211bd9 100644 --- a/src/web/backend/src/UseCases/Doors/Authenticate/Authenticate.php +++ b/src/web/backend/src/UseCases/Doors/Authenticate/Authenticate.php @@ -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(); diff --git a/src/web/backend/tests/Unit/Source/UseCases/Doors/Authenticate/PresenterStub.php b/src/web/backend/tests/Unit/Source/UseCases/Doors/Authenticate/PresenterStub.php new file mode 100644 index 00000000..6019baa0 --- /dev/null +++ b/src/web/backend/tests/Unit/Source/UseCases/Doors/Authenticate/PresenterStub.php @@ -0,0 +1,31 @@ +presenterCalled = true; + $this->response = $responseModel; + } + + public function wasPresenterCalled(): bool + { + return $this->presenterCalled; + } + + public function getViewModel(): ?Door + { + return null; + } +} diff --git a/src/web/backend/tests/Unit/Source/UseCases/Doors/Authenticate/PresenterTest.php b/src/web/backend/tests/Unit/Source/UseCases/Doors/Authenticate/PresenterTest.php new file mode 100644 index 00000000..833703f2 --- /dev/null +++ b/src/web/backend/tests/Unit/Source/UseCases/Doors/Authenticate/PresenterTest.php @@ -0,0 +1,78 @@ +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); + } +} diff --git a/src/web/backend/tests/Unit/Source/UseCases/Doors/Authenticate/UseCaseTest.php b/src/web/backend/tests/Unit/Source/UseCases/Doors/Authenticate/UseCaseTest.php new file mode 100644 index 00000000..85f93a03 --- /dev/null +++ b/src/web/backend/tests/Unit/Source/UseCases/Doors/Authenticate/UseCaseTest.php @@ -0,0 +1,81 @@ +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()); + } +} -- GitLab