Commit fad50f4a authored by Jacob Priddy's avatar Jacob Priddy 👌

add the rest of the authenticate tests

parent b49c6662
Pipeline #2606 canceled with stages
in 34 seconds
......@@ -124,7 +124,7 @@ class Token
}
/**
* @return Carbon|null
* @return \Carbon\Carbon|null
*/
public function getExpiresAt(): ?Carbon
{
......@@ -132,7 +132,7 @@ class Token
}
/**
* @return Carbon|null
* @return \Carbon\Carbon|null
*/
public function getCreatedAt(): ?Carbon
{
......@@ -140,13 +140,21 @@ class Token
}
/**
* @return Carbon|null
* @return \Carbon\Carbon|null
*/
public function getUpdatedAt(): ?Carbon
{
return $this->updatedAt;
}
/**
* @param \Carbon\Carbon|null $expires
*/
public function setExpiresAt(?Carbon $expires): void
{
$this->expiresAt = $expires;
}
/**
* @param Carbon $date
* @return bool
......@@ -155,4 +163,20 @@ class Token
{
return $this->expiresAt === null || $this->expiresAt->isAfter($date);
}
/**
* @return bool
*/
public function isValid(): bool
{
return $this->isValidAtTime(Carbon::now());
}
/**
* @return bool
*/
public function isInvalid(): bool
{
return !$this->isValid();
}
}
......@@ -7,9 +7,9 @@ use Source\Entities\SamlUser;
class InMemorySamlRepository implements SamlRepository
{
protected ?SamlUser $userToLogInAs;
protected ?SamlUser $userToLogInAs = null;
protected ?SamlUser $loggedInUser;
protected ?SamlUser $loggedInUser = null;
protected string $loginUrl;
......@@ -23,7 +23,7 @@ class InMemorySamlRepository implements SamlRepository
$this->logoutUrl = $logoutUrl;
}
public function setLoginUser(SamlUser $user): void
public function setLoginUser(?SamlUser $user): void
{
$this->userToLogInAs = $user;
}
......
......@@ -50,8 +50,10 @@ class InMemoryTokensRepository implements TokensRepository
*/
public function invalidateToken(string $token): void
{
$this->tokens = array_filter($this->tokens, static function (Token $t) use ($token) {
return !$t->matches($token);
});
$tok = $this->findValidToken($token);
if ($tok) {
$tok->setExpiresAt(Carbon::now());
}
}
}
......@@ -86,25 +86,25 @@ class Authenticate implements AuthenticateUseCase
*/
public function handleSamlLogin(Presenter $presenter): void
{
$user = $this->saml->handleLogin();
$samlUser = $this->saml->handleLogin();
if (!$user) {
if (!$samlUser) {
throw new UserCreationException();
}
// First check to see if the user exists in the database.
$user = $this->users->findByEmail($user->getEmail());
$user = $this->users->findByEmail($samlUser->getEmail());
// If the user does not exist, create them.
if (!$user) {
$user = $this->users->create(
new User(
0,
$user->getFirstName(),
$user->getLastName(),
$user->getDisplayName(),
$user->getEmplid(),
$user->getEmail(),
$samlUser->getFirstName(),
$samlUser->getLastName(),
$samlUser->getDisplayName(),
$samlUser->getEmplid(),
$samlUser->getEmail(),
null,
null
)
......
<?php
namespace Tests\Unit\Source\UseCases\Users\Authenticate;
use Carbon\Carbon;
use Source\Entities\User;
use Source\Entities\Token;
use Source\Entities\SamlUser;
use Tests\Doubles\InMemoryUsersRepositoryStub;
use Source\UseCases\Users\Authenticate\Authenticate;
use Source\UseCases\Users\Authenticate\UserCreationException;
class SamlUseCaseTest extends UseCaseBaseTest
{
protected const VALID_EMAIL = 'email';
/**
* @param \Source\Entities\SamlUser|null $samlUser
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\UseCases\Users\Authenticate\UserCreationException
*/
protected function handleLoginTest(?SamlUser $samlUser = null): void
{
$this->saml->setLoginUser($samlUser);
$this->saml->login();
$this->useCase->handleSamlLogin($this->presenter);
$this->response = $this->presenter->response;
}
/**
* @return \Source\Entities\User
*/
protected function createUser(): User
{
return $this->users->create(new User(0, '', '', '', '', self::VALID_EMAIL, '', ''));
}
/**
* @return \Source\Entities\SamlUser
*/
protected function createSamlUser(): SamlUser
{
return new SamlUser('first', 'last', 'emplid', strtoupper(self::VALID_EMAIL));
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\UseCases\Users\Authenticate\UserCreationException
*/
public function it_calls_present_on_presenter(): void
{
$samlUser = $this->createSamlUser();
$this->handleLoginTest($samlUser);
$this->assertTrue($this->presenter->wasPresenterCalled());
}
/**
* @test
*/
public function it_returns_saml_login_link(): void
{
$link = $this->useCase->handToSaml();
$this->assertEquals($this->loginUrl, $link);
}
/**
* @test
*/
public function it_returns_saml_logout_link(): void
{
$link = $this->useCase->samlLogout(null);
$this->assertEquals($this->logoutUrl, $link);
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
*/
public function it_invalidates_token_on_saml_logout(): void
{
$user = $this->createUser();
$this->tokens->create(new Token(0, $user->getId(), 'token'));
$this->useCase->samlLogout('token');
$tok = $this->tokens->all()[0];
$this->assertTrue($tok->isInvalid());
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\UseCases\Users\Authenticate\UserCreationException
*/
public function it_cannot_create_null_users(): void
{
$this->expectException(UserCreationException::class);
$this->handleLoginTest();
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\UseCases\Users\Authenticate\UserCreationException
*/
public function it_creates_a_user_if_they_do_not_exist_in_local_database(): void
{
$samlUser = $this->createSamlUser();
$this->handleLoginTest($samlUser);
$this->assertCount(1, $this->users->all());
$user = $this->users->all()[0];
$this->assertEquals('First', $user->getFirstName());
$this->assertEquals('Last', $user->getLastName());
$this->assertEquals('First Last', $user->getDisplayName());
$this->assertEquals('emplid', $user->getEmplid());
$this->assertEquals('email', $user->getEmail());
$this->assertNull($user->getPassword());
$this->assertNull($user->getDoorcode());
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\UseCases\Users\Authenticate\UserCreationException
*/
public function it_throws_an_exception_if_it_cannot_create_a_user(): void
{
$samlUser = $this->createSamlUser();
$users = new InMemoryUsersRepositoryStub();
$this->useCase = new Authenticate($users, $this->saml, $this->tokens);
$this->expectException(UserCreationException::class);
$this->handleLoginTest($samlUser);
}
/**
* @test
* @throws \Source\Exceptions\EntityNotFoundException
* @throws \Source\UseCases\Users\Authenticate\UserCreationException
*/
public function it_creates_a_token_for_the_user(): void
{
$user = $this->createUser();
$samlUser = $this->createSamlUser();
$this->handleLoginTest($samlUser);
$this->assertCount(1, $this->tokens->all());
$token = $this->tokens->all()[0];
$this->assertLessThan(Carbon::now()->addDays(2), $token->getExpiresAt());
$this->assertGreaterThan(Carbon::now()->addDays(1), $token->getExpiresAt());
$this->assertEquals($user->getId(), $token->getUserId());
$this->assertEquals(60, strlen($token->getTokenString()));
$this->assertEquals($token, $this->response->getToken());
}
}
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