Files
Toiminnanohjaus/tests/Feature/Modules/Users/LoginTest.php
T
petriandClaude Opus 5.5 1acdaa5b19 Harden the app: block project files, add rate limits and security headers
- Root .htaccess refuses everything outside public/, because local Apache
  serves the whole project folder (.env, logs with set-password links, .git).
- Login allows 20 failed attempts a minute per IP across accounts, besides
  5 per email and IP; the forgot-password page 5 requests a minute per IP.
- A global middleware sends X-Frame-Options, frame-ancestors, nosniff,
  Referrer-Policy, Permissions-Policy and HSTS over HTTPS, and drops
  X-Powered-By.
- The weekly time sheet locks the shown week and selected day, so the
  browser cannot set them.
- Users who may not see personal identity codes can save a new code at
  most 10 times an hour, so the duplicate check cannot be used to test codes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-24 06:25:26 +02:00

139 lines
4.3 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('blocks one address that tries a password on many accounts', function () {
RateLimiter::clear('login-ip|127.0.0.1');
User::factory()->create(['email' => '[email protected]']);
$login = Livewire::test('users::login');
foreach (range(1, 20) as $attempt) {
$login->set('email', "tuntematon{$attempt}@example.test")->set('password', 'Kesä2027!')->call('login');
}
$login->set('email', '[email protected]')
->set('password', 'password')
->call('login')
->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'));
});