Files
Toiminnanohjaus/tests/Feature/Modules/Users/LoginPinTest.php
T
petriandClaude Sonnet 5.5 ef400054b0 Return stock of cancelled maintenance, throttle the PIN page's password
Cancelling a maintenance record now reverses the stock it used, through
Inventory's new ReverseMaintenanceStockUse behind the MaintenanceMaterials
port, in the same transaction as the cancellation.

The PIN page allows 5 wrong current passwords a minute per user, as the
login form does, so a signed-in session cannot be used to guess the
password.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
2026-11-29 10:10:18 +02:00

84 lines
2.7 KiB
PHP

<?php
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Livewire\Livewire;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->user = User::factory()->employee()->create(['password' => 'Salasana-123!']);
$this->actingAs($this->user);
});
it('sets the console PIN code with the user\'s password and stores only its hash', function () {
$this->get(route('profile.pin'))->assertOk()->assertSee(__('users.pin.not_set'));
Livewire::test('users::pin')
->set('currentPassword', 'väärä')
->set('pin', '2468')
->set('pinConfirmation', '2468')
->call('save')
->assertHasErrors('currentPassword')
->set('currentPassword', 'Salasana-123!')
->set('pin', '24a8')
->set('pinConfirmation', '24a8')
->call('save')
->assertHasErrors('pin')
->set('pin', '2468')
->set('pinConfirmation', '2469')
->call('save')
->assertHasErrors('pinConfirmation')
->set('pinConfirmation', '2468')
->call('save')
->assertHasNoErrors()
->assertSet('message', __('users.pin.saved'));
$hash = $this->user->fresh()->pin_hash;
expect($hash)->not->toBe('2468')
->and(Hash::check('2468', $hash))->toBeTrue()
->and($this->user->fresh()->toArray())->not->toHaveKey('pin_hash');
});
it('removes the PIN code with the user\'s password', function () {
$this->user->forceFill(['pin_hash' => Hash::make('2468')])->save();
Livewire::test('users::pin')
->set('currentPassword', 'Salasana-123!')
->call('remove')
->assertSet('message', __('users.pin.removed'));
expect($this->user->fresh()->pin_hash)->toBeNull();
});
it('stops checking the password after 5 wrong ones a minute', function () {
$component = Livewire::test('users::pin');
foreach (range(1, 5) as $attempt) {
$component->set('currentPassword', 'väärä')->call('remove')->assertHasErrors(['currentPassword' => __('users.pin.errors.password')]);
}
$this->user->forceFill(['pin_hash' => Hash::make('2468')])->save();
$component->set('currentPassword', 'Salasana-123!')
->call('remove')
->assertHasErrors('currentPassword')
->assertSet('message', '');
expect($this->user->fresh()->pin_hash)->not->toBeNull();
$this->travel(61)->seconds();
$component->set('currentPassword', 'Salasana-123!')
->call('remove')
->assertSet('message', __('users.pin.removed'));
});
it('is not offered to subcontractors', function () {
$this->actingAs(User::factory()->subcontractor()->create());
$this->get(route('profile.pin'))->assertForbidden();
});