Files
Toiminnanohjaus/tests/Unit/Modules/Users/Application/UserAccountUseCasesTest.php
T
petriandClaude Opus 5.5 c9a7f970ab Fix review findings in login and user management
- Load stored users without re-checking role rules, so an intern whose
  supervisor was deleted no longer gets a 500 on every request; the
  rules apply again on the next change.
- Refuse to deactivate or demote a supervisor of active interns
  (SupervisorHasInterns).
- Lock active admin rows in the last-admin check so concurrent changes
  cannot remove every admin.
- Creating a user survives mail failures: the error is reported and
  the admin is asked to resend the link (CreatedUserAccount result).
- Session-ended redirect passes the reason in the URL
  (/login?access=ended), so Livewire requests also show it.
- Same login message for a wrong password and a disabled account.
- Forgot-password sends links only to users who may sign in today
  (RequestPasswordReset), with the same neutral message.
- Use the default callout instead of the low-contrast warning variant.
- Document make:class/make:test for module files in CLAUDE.md.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-22 11:18:22 +02:00

131 lines
6.6 KiB
PHP

<?php
use App\Modules\Users\Application\DTOs\UserAccountData;
use App\Modules\Users\Application\UseCases\CheckLoginAllowed;
use App\Modules\Users\Application\UseCases\CreateUserAccount;
use App\Modules\Users\Application\UseCases\SendPasswordSetupLink;
use App\Modules\Users\Application\UseCases\UpdateUserAccount;
use App\Modules\Users\Domain\Entities\UserAccount;
use App\Modules\Users\Domain\Enums\Role;
use App\Modules\Users\Domain\Exceptions\EmailAlreadyInUse;
use App\Modules\Users\Domain\Exceptions\InvalidSupervisor;
use App\Modules\Users\Domain\Exceptions\LastAdminCannotBeRemoved;
use App\Modules\Users\Domain\Exceptions\SupervisorHasInterns;
use App\Modules\Users\Domain\Exceptions\UserAccountNotFound;
use App\Modules\Users\Domain\Services\SupervisorRule;
use App\Modules\Users\Domain\ValueObjects\EmailAddress;
use App\Modules\Users\Domain\ValueObjects\PersonName;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakePasswordLinkSender;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryUserAccountRepository;
beforeEach(function () {
$this->users = new InMemoryUserAccountRepository;
$this->passwordLinks = new FakePasswordLinkSender;
$supervisorRule = new SupervisorRule($this->users);
$transactions = new FakeTransactionManager;
$this->createUser = new CreateUserAccount($this->users, $supervisorRule, $transactions, $this->passwordLinks);
$this->updateUser = new UpdateUserAccount($this->users, $supervisorRule, $transactions);
$this->adminId = $this->users->save(UserAccount::register(new PersonName('Anna Admin'), new EmailAddress('[email protected]'), Role::Admin))->id();
$this->employeeId = $this->users->save(UserAccount::register(new PersonName('Eero Työntekijä'), new EmailAddress('[email protected]'), Role::Employee))->id();
});
it('creates a user and sends a link for setting the password', function () {
$userId = $this->createUser->execute(new UserAccountData('Maija Meikäläinen', '[email protected]', Role::Employee))->userId;
expect($this->users->findById($userId)?->email()->value)->toBe('[email protected]')
->and($this->passwordLinks->sentTo)->toBe(['[email protected]']);
});
it('creates an intern with an end date and a supervisor', function () {
$userId = $this->createUser->execute(new UserAccountData('Ida', '[email protected]', Role::Intern, true, '2027-05-31', $this->employeeId))->userId;
$intern = $this->users->findById($userId);
expect($intern?->employmentEndsOn()?->format('Y-m-d'))->toBe('2027-05-31')
->and($intern?->supervisorId())->toBe($this->employeeId);
});
it('rejects a supervisor who cannot supervise', function (string $case) {
$supervisorId = match ($case) {
'admin' => $this->adminId,
'unknown' => 999,
'inactive' => $this->users->save(UserAccount::reconstitute(50, new PersonName('Pois'), new EmailAddress('[email protected]'), Role::Employee, false, null, null))->id(),
};
$this->createUser->execute(new UserAccountData('Ida', '[email protected]', Role::Intern, true, '2027-05-31', $supervisorId));
})->throws(InvalidSupervisor::class)->with(['admin', 'unknown', 'inactive']);
it('rejects an email address that another user has', function () {
expect(fn () => $this->createUser->execute(new UserAccountData('Toinen Eero', '[email protected]', Role::Employee)))
->toThrow(EmailAlreadyInUse::class)
->and($this->passwordLinks->sentTo)->toBeEmpty();
});
it('does not let the last active admin be deactivated or demoted', function (bool $active, Role $role) {
$this->updateUser->execute($this->adminId, new UserAccountData('Anna Admin', '[email protected]', $role, $active));
})->throws(LastAdminCannotBeRemoved::class)->with([
'deactivated' => [false, Role::Admin],
'demoted' => [true, Role::Management],
]);
it('lets an admin be demoted when another active admin remains', function () {
$this->users->save(UserAccount::register(new PersonName('Toinen Admin'), new EmailAddress('[email protected]'), Role::Admin));
$this->updateUser->execute($this->adminId, new UserAccountData('Anna Admin', '[email protected]', Role::Management));
expect($this->users->findById($this->adminId)?->role())->toBe(Role::Management);
});
it('does not let a supervisor of active interns stop supervising', function (bool $active, Role $role) {
$this->createUser->execute(new UserAccountData('Ida', '[email protected]', Role::Intern, true, '2027-05-31', $this->employeeId));
$this->updateUser->execute($this->employeeId, new UserAccountData('Eero Työntekijä', '[email protected]', $role, $active));
})->throws(SupervisorHasInterns::class)->with([
'deactivated' => [false, Role::Employee],
'made a subcontractor' => [true, Role::Subcontractor],
]);
it('lets a supervisor without interns change role, and one with interns move between supervising roles', function () {
$this->createUser->execute(new UserAccountData('Ida', '[email protected]', Role::Intern, true, '2027-05-31', $this->employeeId));
$this->updateUser->execute($this->employeeId, new UserAccountData('Eero Työntekijä', '[email protected]', Role::Management));
$this->updateUser->execute($this->adminId, new UserAccountData('Anna Admin', '[email protected]', Role::Admin));
expect($this->users->findById($this->employeeId)?->role())->toBe(Role::Management);
});
it('fails to update an unknown user', function () {
$this->updateUser->execute(999, new UserAccountData('X', '[email protected]', Role::Employee));
})->throws(UserAccountNotFound::class);
it('sends a password link to a stored user', function () {
(new SendPasswordSetupLink($this->users, $this->passwordLinks))->execute($this->employeeId);
expect($this->passwordLinks->sentTo)->toBe(['[email protected]']);
});
it('checks whether a user may log in today', function () {
$internId = $this->createUser->execute(new UserAccountData('Ida', '[email protected]', Role::Intern, true, '2027-05-31', $this->employeeId))->userId;
$clock = new FakeClock(new DateTimeImmutable('2027-05-31'));
$checkLogin = new CheckLoginAllowed($this->users, $clock);
expect($checkLogin->execute($internId))->toBeTrue()
->and($checkLogin->execute(999))->toBeFalse();
$clock->today = new DateTimeImmutable('2027-06-01');
expect($checkLogin->execute($internId))->toBeFalse();
});
it('builds input from form data', function () {
$data = UserAccountData::fromArray(['name' => 'Ida', 'email' => '[email protected]', 'role' => 'intern', 'active' => true, 'employment_ends_on' => '', 'supervisor_id' => '3']);
expect($data->role)->toBe(Role::Intern)
->and($data->employmentEndsOn)->toBeNull()
->and($data->supervisorId)->toBe(3);
});