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

122 lines
3.7 KiB
PHP

<?php
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\RateLimiter;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->travelTo(now('Europe/Helsinki')->setDate(2027, 5, 31)->setTime(12, 0));
});
it('sends guests to the login page and back where they were going', function () {
$user = User::factory()->create(['email' => '[email protected]']);
$this->get(route('customers.index'))->assertRedirect(route('login'));
$this->get(route('login'))->assertOk()->assertSee('Kirjaudu sisään');
Livewire::test('users::login')
->set('email', ' [email protected] ')
->set('password', 'password')
->call('login')
->assertHasNoErrors()
->assertRedirect(route('customers.index'));
$this->assertAuthenticatedAs($user);
});
it('rejects a wrong password', function () {
User::factory()->create(['email' => '[email protected]']);
Livewire::test('users::login')
->set('email', '[email protected]')
->set('password', 'väärä')
->call('login')
->assertHasErrors(['email'])
->assertSee('Kirjautuminen ei onnistunut.')
->assertSet('password', '');
$this->assertGuest();
});
it('does not let a deactivated user or an intern past the last day log in, with the same message as a wrong password', function (string $state) {
$factory = User::factory();
$user = match ($state) {
'inactive' => $factory->inactive()->create(),
'intern ended yesterday' => $factory->intern('2027-05-30')->create(),
};
Livewire::test('users::login')
->set('email', $user->email)
->set('password', 'password')
->call('login')
->assertHasErrors(['email'])
->assertSee('Kirjautuminen ei onnistunut.')
->assertDontSee('Käyttöoikeutesi on päättynyt.');
$this->assertGuest();
})->with(['inactive', 'intern ended yesterday']);
it('lets an intern log in on the last day', function () {
$intern = User::factory()->intern('2027-05-31')->create();
Livewire::test('users::login')
->set('email', $intern->email)
->set('password', 'password')
->call('login')
->assertHasNoErrors();
$this->assertAuthenticatedAs($intern);
});
it('blocks login after five failed attempts', function () {
RateLimiter::clear('[email protected]|127.0.0.1');
User::factory()->create(['email' => '[email protected]']);
$login = Livewire::test('users::login')->set('email', '[email protected]');
foreach (range(1, 5) as $attempt) {
$login->set('password', 'väärä')->call('login');
}
$login->set('password', 'password')
->call('login')
->assertHasErrors(['email'])
->assertSee('Liian monta kirjautumisyritystä.');
$this->assertGuest();
});
it('ends the session of a user who is deactivated while signed in', function () {
$user = User::factory()->create();
$this->actingAs($user)->get(route('customers.index'))->assertOk();
$user->update(['is_active' => false]);
$this->get(route('customers.index'))->assertRedirect(route('login', ['access' => 'ended']));
$this->assertGuest();
$this->get(route('login', ['access' => 'ended']))
->assertOk()
->assertSee('Käyttöoikeutesi on päättynyt. Ota yhteyttä pääkäyttäjään.');
});
it('logs out', function () {
$this->actingAs(User::factory()->create())
->post(route('logout'))
->assertRedirect(route('login'));
$this->assertGuest();
});
it('sends signed-in users away from the login page', function () {
$this->actingAs(User::factory()->create())
->get(route('login'))
->assertRedirect(route('home'));
});