Commit 8e3b920b authored by Jacob Priddy's avatar Jacob Priddy 👌
Browse files

add command authorizer test

parent d16541d1
......@@ -32,6 +32,11 @@ class ApiAuthorizer implements Authorizer
*/
protected ?string $currentUserId;
/**
* @var string[]
*/
protected array $userGroups;
public function __construct(
?string $currentUserId,
UsersRepository $users,
......@@ -49,7 +54,10 @@ class ApiAuthorizer implements Authorizer
*/
public function setCurrentUserId(?string $id): void
{
$this->currentUserId = $id;
if ($this->currentUserId !== $id) {
$this->userGroups = [];
$this->currentUserId = $id;
}
}
/**
......@@ -114,7 +122,11 @@ class ApiAuthorizer implements Authorizer
*/
protected function getGroupsForUser(string $userId): array
{
return static::groupNames($this->groupUserRepository->getGroupsForUser($userId));
if (!$this->userGroups) {
$this->userGroups = static::groupNames($this->groupUserRepository->getGroupsForUser($userId));
}
return $this->userGroups;
}
/**
......
<?php
namespace Tests\Doubles;
use Source\Entities\User;
use Source\UseCases\Door\Commands\CommandHandler;
class CommandHandlerStub implements CommandHandler
{
public bool $handleReturn = true;
/**
* @inheritDoc
*/
public function handle(User $user, string $doorId, string $commandString): bool
{
return $this->handleReturn;
}
}
<?php
namespace Tests\Unit\Source\UseCases\Door\Access\Authorizers;
use Carbon\Carbon;
use Source\Entities\User;
use PHPUnit\Framework\TestCase;
use Tests\Doubles\AuthorizerStub;
use Tests\Doubles\CommandHandlerStub;
use Source\UseCases\Door\Access\AccessAuthorizer;
use Source\UseCases\Door\Access\Authorizers\CommandAuthorizer;
class CommandAuthorizerTest extends TestCase
{
/**
* @var \Tests\Doubles\AuthorizerStub
*/
protected AuthorizerStub $auth;
/**
* @var \Tests\Doubles\CommandHandlerStub
*/
protected CommandHandlerStub $handler;
/**
* @var \Source\UseCases\Door\Access\Authorizers\CommandAuthorizer
*/
protected CommandAuthorizer $authorizer;
protected int $status;
public function setUp(): void
{
parent::setUp();
$this->auth = new AuthorizerStub();
$this->handler = new CommandHandlerStub();
$this->authorizer = new CommandAuthorizer($this->auth, $this->handler);
}
protected function handleTest(bool $commandSuccess, ?User $user, ?string $command = ''): void
{
$this->handler->handleReturn = $commandSuccess;
$this->status = $this->authorizer->check($user, Carbon::now(), '', '', $command);
}
protected function createUser(): User
{
return new User(0, '', '', '', '');
}
/**
* @test
*/
public function it_does_nothing_when_command_is_not_set(): void
{
$this->handleTest(true, $this->createUser(), null);
$this->assertEquals(AccessAuthorizer::CONTINUE, $this->status);
}
/**
* @test
*/
public function it_does_nothing_when_user_is_not_set(): void
{
$this->handleTest(true, null, '');
$this->assertEquals(AccessAuthorizer::CONTINUE, $this->status);
}
/**
* @test
*/
public function it_denies_if_user_is_not_authorized(): void
{
$this->auth->setNext(false);
$this->handleTest(true, $this->createUser(), 'command');
$this->assertEquals(AccessAuthorizer::DENY, $this->status);
}
/**
* @test
*/
public function it_denies_if_command_fails(): void
{
$this->handleTest(false, $this->createUser(), 'command');
$this->assertEquals(AccessAuthorizer::DENY, $this->status);
}
/**
* @test
*/
public function it_allows_if_command_succeeds(): void
{
$this->handleTest(true, $this->createUser(), 'command');
$this->assertEquals(AccessAuthorizer::ALLOW, $this->status);
}
/**
* @test
*/
public function it_does_nothing_if_command_is_empty(): void
{
$this->handleTest(true, $this->createUser(), '');
$this->assertEquals(AccessAuthorizer::CONTINUE, $this->status);
}
}
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