Skip to content

Commit

Permalink
Command handlers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
galeaspablo committed Sep 17, 2024
1 parent a263e78 commit f8121fc
Show file tree
Hide file tree
Showing 7 changed files with 274 additions and 237 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Galeas\Api\BoundedContext\Security\Session\CommandHandler\RefreshToken;

use Galeas\Api\Common\ExceptionBase\AccessDeniedException;

class AlreadySignedOut extends AccessDeniedException
{
public static function getErrorIdentifier(): string
{
return 'Security_Session_SignOut_AlreadySignedOut';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function handle(RefreshToken $command): array

$this->eventStore->beginTransaction();

$aggregateAndEventIds = $this->eventStore->find($command->authenticatedUserId);
$aggregateAndEventIds = $this->eventStore->find($sessionId);

if (null === $aggregateAndEventIds) {
throw new NoSessionFound();
Expand All @@ -57,6 +57,10 @@ public function handle(RefreshToken $command): array
throw new NoSessionFound();
}

if (null !== $session->sessionIsSignedOut()) {
throw new AlreadySignedOut();
}

if ($command->authenticatedUserId !== $session->sessionDetails()->asUser()->id()) {
throw new SessionUserDoesNotMatch();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
use Galeas\Api\Common\ExceptionBase\EventStoreCannotRead;
use Galeas\Api\Common\ExceptionBase\EventStoreCannotWrite;
use Galeas\Api\Common\ExceptionBase\ProjectionCannotRead;
use Galeas\Api\Common\ExceptionBase\QueuingFailure;
use Galeas\Api\Common\Id\Id;
use Galeas\Api\Common\Id\InvalidId;
use Galeas\Api\Service\EventStore\EventStore;
use Galeas\Api\Service\Queue\Queue;

class SignOutHandler
{
Expand Down Expand Up @@ -52,8 +50,13 @@ public function handle(SignOut $command): void

$this->eventStore->beginTransaction();

$session = $this->eventStore->find($sessionId);
$aggregateAndEventIds = $this->eventStore->find($sessionId);

if (null === $aggregateAndEventIds) {
throw new NoSessionFound();
}

$session = $aggregateAndEventIds->aggregate();
if (!($session instanceof Session)) {
throw new NoSessionFound();
}
Expand All @@ -66,9 +69,13 @@ public function handle(SignOut $command): void
throw new SessionTokenDoesNotMatch();
}

$event = SignedOut::fromProperties(
$event = SignedOut::new(
Id::createNew(),
$session->aggregateId(),
Id::fromId($command->authenticatedUserId),
$session->aggregateVersion() + 1,
$aggregateAndEventIds->lastEventId(),
$aggregateAndEventIds->firstEventId(),
new \DateTimeImmutable("now"),
$command->metadata,
$command->withIp,
$command->withSessionToken
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,42 +66,49 @@ function (string $username): bool {
/** @var SignedUp $storedEvent */
$storedEvent = $this->getInMemoryEventStore()->storedEvents()[0];

Assert::assertEquals(
$command->primaryEmail,
$storedEvent->primaryEmail()
);
Assert::assertTrue(
password_verify(
$command->password,
$storedEvent->hashedPassword()
)
);
Assert::assertEquals(
$command->username,
$storedEvent->username()
);
Assert::assertEquals(
$command->termsOfUseAccepted,
$storedEvent->termsOfUseAccepted()
);
Assert::assertEquals(
$command->metadata,
$storedEvent->metadata()
);
Assert::assertEquals(
$storedEvent->eventId(),
$storedEvent->correlationId()
);
Assert::assertEquals(
$storedEvent->eventId(),
$storedEvent->causationId()
);
Assert::assertEquals(
[
'userId' => $storedEvent->aggregateId()->id(),
],
$response
);

Assert::assertEquals(
[
$storedEvent->eventId(),
$storedEvent->aggregateId(),
1,
$storedEvent->eventId(),
$storedEvent->eventId(),
$storedEvent->recordedOn(),
$command->metadata,
$storedEvent->primaryEmail(),
$storedEvent->primaryEmailVerificationCode(),
$storedEvent->hashedPassword(),
$command->username,
$command->termsOfUseAccepted
],
[
$storedEvent->eventId(),
$storedEvent->aggregateId(),
$storedEvent->aggregateVersion(),
$storedEvent->causationId(),
$storedEvent->correlationId(),
$storedEvent->recordedOn(),
$storedEvent->metadata(),
$storedEvent->primaryEmail(),
$storedEvent->primaryEmailVerificationCode(),
$storedEvent->hashedPassword(),
$storedEvent->username(),
$storedEvent->termsOfUseAccepted(),
]
);
}

public function testInvalidEmail(): void
Expand Down
Loading

0 comments on commit f8121fc

Please sign in to comment.