Add login, password reset and user management (Users module)
New Users module with the four layers: - Domain: Role enum, UserAccount entity (intern needs end date and supervisor, login allowed until the end of the last day), email and name value objects, SupervisorRule, last-admin protection. - Application: CreateUserAccount (emails a set-password link), UpdateUserAccount, SendPasswordSetupLink, CheckLoginAllowed, query contract and Clock (business time zone Europe/Helsinki). - Infrastructure: users table gets role, is_active, employment_ends_on and supervisor_id; Eloquent repository and queries; Password broker link sender with a Finnish email; Gate users.manage. - Presentation: Livewire login (rate limited), forgot and reset password pages, logout, EnsureAccountIsUsable middleware that ends sessions of deactivated users, admin-only user list and form, and users:create-admin command. Also: - Layout gets a labelled main menu, the signed-in user and logout. - Remove the local /dev/login; guests are redirected to /login. - Customers: customers.use ability (no subcontractors), authorization in mount and save, locked customerId, 404 for a missing customer, empty-state check uses the total count. - Architecture test lets module providers register their own Presentation classes; docs and CLAUDE.md updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,143 @@
|
||||
<?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 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();
|
||||
});
|
||||
Reference in New Issue
Block a user