- AuthenticateSession in the web group logs out a user's other sessions when their password changes. - Set-password links made by an admin use a new admin_links broker with its own password_setup_tokens table and a 72-hour expiry, so links handed over by hand don't expire in an hour. Forgot-password links keep 60 minutes. The reset page accepts both, and issuing or using a link ends the user's earlier links. - Admin links are emailed with PasswordSetupLinkNotification; the email body is shared with the forgot-password email via PasswordLinkMail. - Spec chapter 3 and CLAUDE.md describe the link lifetimes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
176 lines
6.6 KiB
PHP
176 lines
6.6 KiB
PHP
<?php
|
|
|
|
use App\Models\User;
|
|
use App\Modules\Users\Domain\Enums\Role;
|
|
use App\Modules\Users\Infrastructure\Auth\PasswordSetupLinkNotification;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Notification;
|
|
use Illuminate\Support\Facades\Password;
|
|
use Illuminate\Support\Str;
|
|
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, emails a set-password link and shows it to copy', function () {
|
|
$supervisor = User::factory()->create();
|
|
|
|
$component = 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()
|
|
->assertNoRedirect()
|
|
->assertSee('Käyttäjä Ida Harjoittelija lisätty.')
|
|
->assertSee('Salasanan asetuslinkki');
|
|
|
|
$intern = User::query()->where('email', '[email protected]')->sole();
|
|
|
|
expect($component->get('userId'))->toBe($intern->id);
|
|
|
|
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, PasswordSetupLinkNotification::class, function (PasswordSetupLinkNotification $notification) use ($component) {
|
|
return str_contains($component->get('setupLink'), '/reset-password/'.$notification->token);
|
|
});
|
|
});
|
|
|
|
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('makes a new set-password link on the edit page, also right after an earlier one', function () {
|
|
$user = User::factory()->create(['email' => '[email protected]']);
|
|
|
|
$component = Livewire::actingAs($this->admin)
|
|
->test('users::form', ['user' => $user->id])
|
|
->call('sendPasswordLink')
|
|
->call('sendPasswordLink')
|
|
->assertSee('Uusi linkki luotu ja lähetetty osoitteeseen [email protected].');
|
|
|
|
$token = Str::between($component->get('setupLink'), '/reset-password/', '?');
|
|
|
|
Notification::assertSentToTimes($user, PasswordSetupLinkNotification::class, 2);
|
|
expect(Password::broker('admin_links')->tokenExists($user, $token))->toBeTrue();
|
|
});
|
|
|
|
it('does not let the set-password link be changed in the browser', function () {
|
|
Livewire::actingAs($this->admin)
|
|
->test('users::form')
|
|
->set('setupLink', 'https://evil.example/');
|
|
})->throws(CannotUpdateLockedPropertyException::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();
|
|
});
|