Files
Toiminnanohjaus/tests/Feature/Modules/Users/UserManagementTest.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

158 lines
5.6 KiB
PHP

<?php
use App\Models\User;
use App\Modules\Users\Domain\Enums\Role;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
Notification::fake();
$this->admin = User::factory()->admin()->create(['name' => 'Anna Admin']);
});
it('only lets admins manage users', function (string $state, int $status) {
$user = User::factory()->{$state}()->create();
$this->actingAs($user)->get(route('users.index'))->assertStatus($status);
$this->actingAs($user)->get(route('users.create'))->assertStatus($status);
})->with([
'admin' => ['admin', 200],
'management' => ['management', 403],
'employee' => ['employee', 403],
'intern' => ['intern', 403],
'subcontractor' => ['subcontractor', 403],
]);
it('lists users with their role and status', function () {
$supervisor = User::factory()->create(['name' => 'Eero Työntekijä']);
User::factory()->intern('2027-05-31', $supervisor)->create(['name' => 'Ida Harjoittelija']);
User::factory()->inactive()->create(['name' => 'Pekka Poissa']);
$this->actingAs($this->admin)->get(route('users.index'))
->assertOk()
->assertSee('Ida Harjoittelija')
->assertSee('Harjoittelija')
->assertSee('Vastuuhenkilö: Eero Työntekijä')
->assertSee('Päättyy 31.5.2027')
->assertSee('Ei käytössä');
});
it('creates an intern and emails a set-password link', function () {
$supervisor = User::factory()->create();
Livewire::actingAs($this->admin)
->test('users::form')
->set('form.name', 'Ida Harjoittelija')
->set('form.email', '[email protected]')
->set('form.role', 'intern')
->set('form.employment_ends_on', '2027-05-31')
->set('form.supervisor_id', (string) $supervisor->id)
->call('save')
->assertHasNoErrors()
->assertRedirect(route('users.index'));
$intern = User::query()->where('email', '[email protected]')->sole();
expect($intern->role)->toBe(Role::Intern)
->and($intern->employment_ends_on?->toDateString())->toBe('2027-05-31')
->and($intern->supervisor_id)->toBe($supervisor->id);
Notification::assertSentTo($intern, ResetPassword::class);
});
it('requires an end date and a supervisor for an intern', function () {
Livewire::actingAs($this->admin)
->test('users::form')
->set('form.name', 'Ida')
->set('form.email', '[email protected]')
->set('form.role', 'intern')
->call('save')
->assertHasErrors(['form.employment_ends_on', 'form.supervisor_id'])
->assertSee('Anna harjoittelun viimeinen päivä.');
});
it('rejects an admin as an intern supervisor', function () {
Livewire::actingAs($this->admin)
->test('users::form')
->set('form.name', 'Ida')
->set('form.email', '[email protected]')
->set('form.role', 'intern')
->set('form.employment_ends_on', '2027-05-31')
->set('form.supervisor_id', (string) $this->admin->id)
->call('save')
->assertHasErrors(['form.supervisor_id']);
});
it('rejects an email address that is in use', function () {
Livewire::actingAs($this->admin)
->test('users::form')
->set('form.name', 'Toinen Anna')
->set('form.email', strtoupper($this->admin->email))
->call('save')
->assertHasErrors(['form.email'])
->assertSee('Tällä sähköpostiosoitteella on jo käyttäjä.');
});
it('deactivates a user', function () {
$user = User::factory()->create();
Livewire::actingAs($this->admin)
->test('users::form', ['user' => $user->id])
->set('form.active', false)
->call('save')
->assertHasNoErrors();
expect($user->fresh()->is_active)->toBeFalse();
});
it('does not deactivate a supervisor who still has active interns', function () {
$supervisor = User::factory()->create();
User::factory()->intern('2027-05-31', $supervisor)->create();
Livewire::actingAs($this->admin)
->test('users::form', ['user' => $supervisor->id])
->set('form.active', false)
->call('save')
->assertHasErrors(['form.role'])
->assertSee('Vaihda harjoittelijoille ensin uusi vastuuhenkilö.');
expect($supervisor->fresh()->is_active)->toBeTrue();
});
it('does not let the last admin deactivate themselves', function () {
Livewire::actingAs($this->admin)
->test('users::form', ['user' => $this->admin->id])
->set('form.active', false)
->call('save')
->assertHasErrors(['form.role'])
->assertSee('Viimeistä aktiivista pääkäyttäjää ei voi poistaa käytöstä');
expect($this->admin->fresh()->is_active)->toBeTrue();
});
it('sends a set-password link from the edit page', function () {
$user = User::factory()->create(['email' => '[email protected]']);
Livewire::actingAs($this->admin)
->test('users::form', ['user' => $user->id])
->call('sendPasswordLink')
->assertSee('Lähetimme linkin salasanan asettamiseen osoitteeseen [email protected].');
Notification::assertSentTo($user, ResetPassword::class);
});
it('does not let the user id be changed in the browser', function () {
Livewire::actingAs($this->admin)
->test('users::form')
->set('userId', 1);
})->throws(CannotUpdateLockedPropertyException::class);
it('returns 404 for an unknown user', function () {
$this->actingAs($this->admin)->get(route('users.edit', 999))->assertNotFound();
});