Give the machine console its own session with PIN sign-in, make counters cumulative
The console under console/ has its own session cookie and console guard (UseConsoleSession), so signing out or changing user there leaves the normal interface alone. Links open it in a new window through a one-time handoff key; a signed-out console asks for a PIN code or password on its own login page. Users set their 4-6 digit PIN on the profile's PIN page, and the console's change user dialog takes it on a keypad; five wrong PINs lock PIN sign-in for 15 minutes. Machine counters are cumulative: users record how much a job used and it is added to the latest reading. A maintenance record restarts the whole counter interval from the reading at the record. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -10,13 +10,17 @@ use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MaintenanceItemModel;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->user = User::factory()->management()->create(['name' => 'Johtaja']);
|
||||
$this->actingAs($this->user);
|
||||
// The machine console has its own session and guard.
|
||||
$this->actingAs($this->user, 'console');
|
||||
|
||||
$print = ServiceUnitModel::query()->where('name', 'Painotuotanto')->sole();
|
||||
$print->update(['uses_stock' => true]);
|
||||
@@ -115,3 +119,68 @@ it('refreshes the console on a tablet and keeps focus on the button', function (
|
||||
->assertSee(__('machines.console.selected', ['title' => 'Banderolli']))
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
});
|
||||
|
||||
it('changes user with a PIN code in a dialog on the console', function (string $device, string $colorScheme) {
|
||||
User::factory()->employee()->create(['name' => 'Olli Operaattori', 'pin_hash' => Hash::make('2468')]);
|
||||
|
||||
$visit = visit(route('machines.console', $this->machine->id));
|
||||
$visit = $device === 'mobile' ? $visit->on()->mobile() : $visit->on()->desktop();
|
||||
$visit = $colorScheme === 'dark' ? $visit->inDarkMode() : $visit->inLightMode();
|
||||
|
||||
$visit->click('#pin-switch-open')
|
||||
->assertScript("document.getElementById('pin-switch-dialog').open", true)
|
||||
->assertScript("document.getElementById('pin-switch-dialog').contains(document.activeElement)", true)
|
||||
->assertSee('Olli Operaattori')
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
})->with([
|
||||
'desktop, light' => ['desktop', 'light'],
|
||||
'desktop, dark' => ['desktop', 'dark'],
|
||||
'mobile, light' => ['mobile', 'light'],
|
||||
'mobile, dark' => ['mobile', 'dark'],
|
||||
]);
|
||||
|
||||
it('signs in with a PIN code typed on the keypad and stays on the console', function () {
|
||||
User::factory()->employee()->create(['name' => 'Olli Operaattori', 'pin_hash' => Hash::make('2468')]);
|
||||
|
||||
visit(route('machines.console', $this->machine->id))
|
||||
->click('#pin-switch-open')
|
||||
->click('Olli Operaattori')
|
||||
->click('2')->click('4')->click('6')->click('8')
|
||||
->click(__('users.pin_switch.submit'))
|
||||
->assertSee(__('machines.console.user', ['name' => 'Olli Operaattori']))
|
||||
->assertNoJavaScriptErrors();
|
||||
});
|
||||
|
||||
it('has no accessibility issues on the console\'s sign-in page', function (string $device, string $colorScheme) {
|
||||
User::factory()->employee()->create(['name' => 'Olli Operaattori', 'pin_hash' => Hash::make('2468')]);
|
||||
Auth::guard('console')->logout();
|
||||
|
||||
$visit = visit(route('console.login'));
|
||||
$visit = $device === 'mobile' ? $visit->on()->mobile() : $visit->on()->desktop();
|
||||
$visit = $colorScheme === 'dark' ? $visit->inDarkMode() : $visit->inLightMode();
|
||||
|
||||
$visit->assertSee(__('users.console_login.title'))
|
||||
->assertSee('Olli Operaattori')
|
||||
->assertNoJavaScriptErrors()
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
})->with([
|
||||
'desktop, light' => ['desktop', 'light'],
|
||||
'desktop, dark' => ['desktop', 'dark'],
|
||||
'mobile, light' => ['mobile', 'light'],
|
||||
'mobile, dark' => ['mobile', 'dark'],
|
||||
]);
|
||||
|
||||
it('has no accessibility issues on the PIN code page', function (string $device, string $colorScheme) {
|
||||
$visit = visit(route('profile.pin'));
|
||||
$visit = $device === 'mobile' ? $visit->on()->mobile() : $visit->on()->desktop();
|
||||
$visit = $colorScheme === 'dark' ? $visit->inDarkMode() : $visit->inLightMode();
|
||||
|
||||
$visit->assertSee(__('users.pin.title'))
|
||||
->assertNoJavaScriptErrors()
|
||||
->assertNoAccessibilityIssues(level: 2);
|
||||
})->with([
|
||||
'desktop, light' => ['desktop', 'light'],
|
||||
'desktop, dark' => ['desktop', 'dark'],
|
||||
'mobile, light' => ['mobile', 'light'],
|
||||
'mobile, dark' => ['mobile', 'dark'],
|
||||
]);
|
||||
|
||||
@@ -17,6 +17,8 @@ uses(RefreshDatabase::class);
|
||||
beforeEach(function () {
|
||||
$this->user = User::factory()->management()->create(['name' => 'Johtaja']);
|
||||
$this->actingAs($this->user);
|
||||
// The machine console has its own session and guard.
|
||||
$this->actingAs($this->user, 'console');
|
||||
|
||||
$print = ServiceUnitModel::query()->where('name', 'Painotuotanto')->sole();
|
||||
$print->update(['uses_stock' => true]);
|
||||
|
||||
@@ -32,6 +32,11 @@ class FakeUserAccountQueries implements UserAccountQueries
|
||||
throw new LogicException('Not needed in unit tests.');
|
||||
}
|
||||
|
||||
public function pinLoginOptions(): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
public function supervisorOptions(?int $exceptUserId = null): array
|
||||
{
|
||||
return [];
|
||||
|
||||
@@ -9,11 +9,14 @@ use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\CustomerJobModel;
|
||||
use App\Modules\Jobs\Infrastructure\Persistence\Eloquent\TaskModel;
|
||||
use App\Modules\Machines\Application\UseCases\MachineWorkList;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterReadingModel;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineQueueEntryModel;
|
||||
use App\Modules\TimeTracking\Infrastructure\Persistence\Eloquent\TimeEntryModel;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
@@ -46,6 +49,8 @@ function console(object $test): mixed
|
||||
}
|
||||
|
||||
it('opens without the main menu and shows today\'s work list', function () {
|
||||
$this->actingAs($this->employee, 'console');
|
||||
|
||||
$this->get(route('machines.console', $this->machine->id))
|
||||
->assertOk()
|
||||
->assertSee('Suurkuvatulostin')
|
||||
@@ -53,7 +58,7 @@ it('opens without the main menu and shows today\'s work list', function () {
|
||||
->assertSee(__('machines.console.refresh'))
|
||||
->assertDontSee(__('layout.main_menu'));
|
||||
|
||||
$this->actingAs(User::factory()->subcontractor()->create());
|
||||
$this->actingAs(User::factory()->subcontractor()->create(), 'console');
|
||||
$this->get(route('machines.console', $this->machine->id))->assertForbidden();
|
||||
});
|
||||
|
||||
@@ -122,11 +127,77 @@ it('announces new rows when refreshed', function () {
|
||||
->assertDontSee('Juliste');
|
||||
});
|
||||
|
||||
it('returns to the console after changing user', function () {
|
||||
$this->post(route('logout'), ['return_to' => route('machines.console', $this->machine->id)])->assertRedirect(route('login'));
|
||||
it('returns to the console after signing out of the console to sign in with a password', function () {
|
||||
$this->actingAs($this->employee, 'console');
|
||||
$this->post(route('console.logout'), ['return_to' => route('machines.console', $this->machine->id)])->assertRedirect(route('console.login'));
|
||||
expect(session('url.intended'))->toBe(route('machines.console', $this->machine->id));
|
||||
|
||||
$this->actingAs($this->employee);
|
||||
$this->post(route('logout'), ['return_to' => 'https://example.com/phish']);
|
||||
$this->actingAs($this->employee, 'console');
|
||||
$this->post(route('console.logout'), ['return_to' => 'https://example.com/phish']);
|
||||
expect(session('url.intended'))->toBeNull();
|
||||
});
|
||||
|
||||
it('suggests the hours booked as the usage of the machine\'s hour counter and leaves out counters and maintenance when there are none', function () {
|
||||
console($this)->assertDontSee(__('machines.console.counters_and_maintenance'));
|
||||
|
||||
$hours = MachineCounterModel::query()->create(['machine_id' => $this->machine->id, 'name' => 'Käyttötunnit', 'unit' => 'hour', 'sort_order' => 1]);
|
||||
|
||||
console($this)
|
||||
->assertSee(__('machines.console.counters_and_maintenance'))
|
||||
->call('selectRow', MachineQueueEntryModel::query()->sole()->id)
|
||||
->set('hours', '1,5')
|
||||
->call('saveHours')
|
||||
->assertSet('usage.'.$hours->id, '1,5')
|
||||
->assertSet('message', fn (string $message) => str_contains($message, 'Ehdotus: kirjaa 1,5 h laskuriin Käyttötunnit.'))
|
||||
->call('saveUsage', $hours->id)
|
||||
->assertHasNoErrors();
|
||||
|
||||
expect(MachineCounterReadingModel::query()->sole()->reading_milli)->toBe(1_500);
|
||||
});
|
||||
|
||||
it('changes the console\'s user with a PIN code', function () {
|
||||
$console = route('machines.console', $this->machine->id);
|
||||
$other = User::factory()->employee()->create(['name' => 'Olli Operaattori', 'pin_hash' => Hash::make('2468')]);
|
||||
User::factory()->employee()->create(['name' => 'Ei PINiä']);
|
||||
|
||||
Livewire::test('users::pin-switch', ['returnTo' => $console])
|
||||
->assertSee('Olli Operaattori')
|
||||
->assertDontSee('Ei PINiä')
|
||||
->set('userId', (string) $other->id)
|
||||
->set('pin', '1111')
|
||||
->call('switchUser')
|
||||
->assertHasErrors('pin')
|
||||
->set('pin', '2468')
|
||||
->call('switchUser')
|
||||
->assertRedirect($console);
|
||||
|
||||
expect(auth()->id())->toBe($other->id);
|
||||
});
|
||||
|
||||
it('locks a user\'s PIN code after five wrong attempts', function () {
|
||||
$other = User::factory()->employee()->create(['pin_hash' => Hash::make('2468')]);
|
||||
$component = Livewire::test('users::pin-switch', ['returnTo' => route('machines.console', $this->machine->id)])->set('userId', (string) $other->id);
|
||||
|
||||
foreach (range(1, 5) as $attempt) {
|
||||
$component->set('pin', '0000')->call('switchUser');
|
||||
}
|
||||
|
||||
$component->set('pin', '2468')->call('switchUser')
|
||||
->assertHasErrors('pin')
|
||||
->assertSee('Liian monta väärää yritystä');
|
||||
|
||||
expect(auth()->id())->toBe($this->employee->id);
|
||||
});
|
||||
|
||||
it('does not let a deactivated user in with a PIN code or return to another site', function () {
|
||||
$other = User::factory()->employee()->inactive()->create(['pin_hash' => Hash::make('2468')]);
|
||||
|
||||
Livewire::test('users::pin-switch', ['returnTo' => route('machines.console', $this->machine->id)])
|
||||
->assertDontSee($other->name)
|
||||
->set('userId', (string) $other->id)
|
||||
->set('pin', '2468')
|
||||
->call('switchUser')
|
||||
->assertHasErrors('userId');
|
||||
|
||||
Livewire::test('users::pin-switch', ['returnTo' => 'https://example.com/phish'])->assertNotFound();
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockItemModel;
|
||||
use App\Modules\Inventory\Infrastructure\Persistence\Eloquent\StockMovementModel;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterModel;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineCounterReadingModel;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MaintenanceItemModel;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MaintenanceRecordModel;
|
||||
@@ -54,7 +55,41 @@ it('adds maintenance items with a counter interval, time intervals or both, and
|
||||
$this->get(route('machines.maintenance.create', $this->machine->id))->assertForbidden();
|
||||
});
|
||||
|
||||
it('shows a maintenance request when due and starts the interval again with a maintenance record that uses the stock item', function () {
|
||||
it('adds the usage of a job to the cumulative counter and refuses an empty or zero usage', function () {
|
||||
$this->actingAs($this->employee);
|
||||
|
||||
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
|
||||
->set('usage.'.$this->prints->id, '350')
|
||||
->call('saveUsage', $this->prints->id)
|
||||
->assertHasNoErrors()
|
||||
->set('usage.'.$this->prints->id, '1 000')
|
||||
->call('saveUsage', $this->prints->id)
|
||||
->assertSet('message', "Laskuriin Tulosteet lisätty 1\u{00A0}000 kpl, laskuri nyt 1\u{00A0}350 kpl.")
|
||||
->set('usage.'.$this->prints->id, '0')
|
||||
->call('saveUsage', $this->prints->id)
|
||||
->assertHasErrors('usage.'.$this->prints->id)
|
||||
->set('usage.'.$this->prints->id, '')
|
||||
->call('saveUsage', $this->prints->id)
|
||||
->assertHasErrors('usage.'.$this->prints->id);
|
||||
|
||||
expect(MachineCounterReadingModel::query()->orderBy('id')->pluck('reading_milli')->all())->toBe([350_000, 1_350_000]);
|
||||
});
|
||||
|
||||
it('hides the maintenance sections of a machine without counters and maintenance items from users', function () {
|
||||
$this->prints->delete();
|
||||
$this->actingAs($this->employee);
|
||||
|
||||
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
|
||||
->assertDontSee(__('machines.maintenance.items'))
|
||||
->assertDontSee(__('machines.maintenance.history'));
|
||||
|
||||
$this->actingAs($this->manager);
|
||||
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
|
||||
->assertSee(__('machines.maintenance.add'))
|
||||
->assertDontSee(__('machines.maintenance.history'));
|
||||
});
|
||||
|
||||
it('shows a maintenance request when due and resets the interval with a maintenance record that uses the stock item', function () {
|
||||
$item = MaintenanceItemModel::query()->create([
|
||||
'machine_id' => $this->machine->id, 'name' => 'Rummun vaihto', 'machine_counter_id' => $this->prints->id, 'interval_milli' => 20_000_000,
|
||||
'counter_baseline_milli' => 0, 'period' => 'week', 'period_count' => 1, 'starts_on' => now()->toDateString(), 'warning_percent' => 90,
|
||||
@@ -64,8 +99,8 @@ it('shows a maintenance request when due and starts the interval again with a ma
|
||||
|
||||
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
|
||||
->assertSee('Kunnossa')
|
||||
->set('readings.'.$this->prints->id, '19 000')
|
||||
->call('saveReading', $this->prints->id)
|
||||
->set('usage.'.$this->prints->id, '19 000')
|
||||
->call('saveUsage', $this->prints->id)
|
||||
->assertSee('Huolto lähestyy')
|
||||
->assertSee("jäljellä 1\u{00A0}000 kpl", false);
|
||||
|
||||
@@ -74,18 +109,17 @@ it('shows a maintenance request when due and starts the interval again with a ma
|
||||
Livewire::test('machines::maintenance', ['machineId' => $this->machine->id])
|
||||
->assertSee('Huolto myöhässä')
|
||||
->call('startRecord', $item->id)
|
||||
->assertSet('recordReading', '19000')
|
||||
->set('recordReading', '19500')
|
||||
->set('recordNote', 'Rumpu vaihdettu')
|
||||
->call('record')
|
||||
->assertSet('message', 'Huolto Rummun vaihto kirjattu.')
|
||||
->assertSee('Kunnossa')
|
||||
->assertSee("jäljellä 20\u{00A0}000 kpl", false)
|
||||
->assertSee('Rumpu vaihdettu');
|
||||
|
||||
$record = MaintenanceRecordModel::query()->sole();
|
||||
$use = StockMovementModel::query()->sole();
|
||||
|
||||
expect($record->counter_reading_milli)->toBe(19_500_000)
|
||||
expect($record->counter_reading_milli)->toBe(19_000_000)
|
||||
->and($use->only(['stock_item_id', 'machine_id', 'maintenance_record_id', 'quantity_milli']))
|
||||
->toBe(['stock_item_id' => $this->drum->id, 'machine_id' => $this->machine->id, 'maintenance_record_id' => $record->id, 'quantity_milli' => -1_000]);
|
||||
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Machines\Infrastructure\Persistence\Eloquent\MachineModel;
|
||||
use App\Modules\Users\Presentation\Http\Middleware\UseConsoleSession;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->user = User::factory()->employee()->create(['name' => 'Eero Työntekijä', 'password' => 'Salasana-123!']);
|
||||
$this->machine = MachineModel::factory()->create(['name' => 'Suurkuvatulostin']);
|
||||
$this->console = route('machines.console', $this->machine->id);
|
||||
});
|
||||
|
||||
it('opens the console signed in as the same user with a one-time key', function () {
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$accept = $this->get(route('console.handoff', ['to' => $this->console]))->assertRedirect()->headers->get('Location');
|
||||
|
||||
expect($accept)->toStartWith(url('console/handoff/'));
|
||||
|
||||
$this->get($accept)->assertRedirect($this->console);
|
||||
expect(Auth::guard('console')->id())->toBe($this->user->id);
|
||||
|
||||
Auth::guard('console')->logout();
|
||||
$this->get($accept)->assertRedirect(route('console.login'));
|
||||
expect(Auth::guard('console')->check())->toBeFalse();
|
||||
});
|
||||
|
||||
it('lets a console key expire after a minute and refuses other addresses', function () {
|
||||
$this->actingAs($this->user);
|
||||
|
||||
$accept = $this->get(route('console.handoff', ['to' => $this->console]))->headers->get('Location');
|
||||
$this->travel(61)->seconds();
|
||||
$this->get($accept)->assertRedirect(route('console.login'));
|
||||
|
||||
$this->get(route('console.handoff', ['to' => 'https://example.com/console/x']))->assertNotFound();
|
||||
$this->get(route('console.handoff', ['to' => route('customers.index')]))->assertNotFound();
|
||||
});
|
||||
|
||||
it('asks a signed-out console to sign in on the console\'s own page and keeps /login for the normal user interface', function () {
|
||||
$this->get($this->console)->assertRedirect(route('console.login'));
|
||||
expect(session('url.intended'))->toBe($this->console);
|
||||
|
||||
$this->actingAs($this->user, 'console');
|
||||
$this->get(route('login'))->assertOk();
|
||||
$this->get(route('console.login'))->assertRedirect(route('machines.index'));
|
||||
});
|
||||
|
||||
it('signs out of the console without signing out of the normal user interface', function () {
|
||||
$this->actingAs($this->user);
|
||||
$this->actingAs($this->user, 'console');
|
||||
|
||||
$this->post(route('console.logout'))->assertRedirect(route('console.login'));
|
||||
|
||||
expect(Auth::guard('console')->check())->toBeFalse()
|
||||
->and(Auth::guard('web')->id())->toBe($this->user->id);
|
||||
});
|
||||
|
||||
it('signs in to the console with a PIN code or a password', function () {
|
||||
$this->user->forceFill(['pin_hash' => Hash::make('2468')])->save();
|
||||
session()->put('url.intended', $this->console);
|
||||
|
||||
Livewire::test('users::console-login')
|
||||
->assertSee('Eero Työntekijä')
|
||||
->set('userId', (string) $this->user->id)
|
||||
->set('pin', '2468')
|
||||
->call('loginWithPin')
|
||||
->assertRedirect($this->console);
|
||||
|
||||
Auth::logout();
|
||||
session()->put('url.intended', $this->console);
|
||||
|
||||
Livewire::test('users::console-login')
|
||||
->set('email', $this->user->email)
|
||||
->set('password', 'Salasana-123!')
|
||||
->call('loginWithPassword')
|
||||
->assertRedirect($this->console);
|
||||
});
|
||||
|
||||
it('tells console requests by their path or by where their Livewire component was rendered', function () {
|
||||
$livewire = fn (string $path): Request => tap(
|
||||
Request::create('/livewire-1234/update', 'POST', ['components' => [['snapshot' => json_encode(['memo' => ['path' => $path]])]]]),
|
||||
fn (Request $request) => $request->headers->set('X-Livewire', '1'),
|
||||
);
|
||||
|
||||
expect(UseConsoleSession::isConsole(Request::create('/console/machines/1')))->toBeTrue()
|
||||
->and(UseConsoleSession::isConsole(Request::create('/console-handoff')))->toBeFalse()
|
||||
->and(UseConsoleSession::isConsole(Request::create('/machines/1')))->toBeFalse()
|
||||
->and(UseConsoleSession::isConsole($livewire('console/machines/1')))->toBeTrue()
|
||||
->and(UseConsoleSession::isConsole($livewire('machines/1')))->toBeFalse();
|
||||
});
|
||||
@@ -0,0 +1,60 @@
|
||||
<?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('is not offered to subcontractors', function () {
|
||||
$this->actingAs(User::factory()->subcontractor()->create());
|
||||
|
||||
$this->get(route('profile.pin'))->assertForbidden();
|
||||
});
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
|
||||
use App\Modules\Users\Application\Contracts\SecretHasher;
|
||||
use App\Modules\Users\Application\UseCases\CheckLoginAllowed;
|
||||
use App\Modules\Users\Application\UseCases\LoginPins;
|
||||
use App\Modules\Users\Domain\Contracts\LoginSecrets;
|
||||
use App\Modules\Users\Domain\Entities\UserAccount;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
use App\Modules\Users\Domain\Exceptions\WrongPassword;
|
||||
use App\Modules\Users\Domain\ValueObjects\PersonName;
|
||||
use Tests\Fakes\FakeClock;
|
||||
use Tests\Fakes\FakeTransactionManager;
|
||||
use Tests\Fakes\InMemoryUserAccountRepository;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->users = new InMemoryUserAccountRepository;
|
||||
$this->userId = $this->users->save(UserAccount::register(new PersonName('Olli'), new EmailAddress('[email protected]'), Role::Employee))->id();
|
||||
|
||||
$this->secrets = new class implements LoginSecrets
|
||||
{
|
||||
/** @var array<int, string> */
|
||||
public array $passwords = [];
|
||||
|
||||
/** @var array<int, string> */
|
||||
public array $pins = [];
|
||||
|
||||
public function passwordHashOf(int $userId): ?string
|
||||
{
|
||||
return $this->passwords[$userId] ?? null;
|
||||
}
|
||||
|
||||
public function pinHashOf(int $userId): ?string
|
||||
{
|
||||
return $this->pins[$userId] ?? null;
|
||||
}
|
||||
|
||||
public function storePinHash(int $userId, ?string $hash): void
|
||||
{
|
||||
if ($hash === null) {
|
||||
unset($this->pins[$userId]);
|
||||
} else {
|
||||
$this->pins[$userId] = $hash;
|
||||
}
|
||||
}
|
||||
};
|
||||
$this->secrets->passwords[$this->userId] = 'hashed:Salasana-123!';
|
||||
|
||||
$hasher = new class implements SecretHasher
|
||||
{
|
||||
public function hash(string $secret): string
|
||||
{
|
||||
return 'hashed:'.$secret;
|
||||
}
|
||||
|
||||
public function check(string $secret, string $hash): bool
|
||||
{
|
||||
return $hash === 'hashed:'.$secret;
|
||||
}
|
||||
};
|
||||
|
||||
$this->clock = new FakeClock(new DateTimeImmutable('2027-05-31'));
|
||||
$this->pins = new LoginPins($this->secrets, $hasher, new CheckLoginAllowed($this->users, $this->clock), new FakeTransactionManager);
|
||||
});
|
||||
|
||||
it('sets a PIN code only with the right password and verifies it', function () {
|
||||
expect(fn () => $this->pins->set($this->userId, 'väärä', '2468'))->toThrow(WrongPassword::class);
|
||||
|
||||
$this->pins->set($this->userId, 'Salasana-123!', '2468');
|
||||
|
||||
expect($this->secrets->pins[$this->userId])->toBe('hashed:2468')
|
||||
->and($this->pins->verify($this->userId, '2468'))->toBeTrue()
|
||||
->and($this->pins->verify($this->userId, '1111'))->toBeFalse()
|
||||
->and($this->pins->verify(999, '2468'))->toBeFalse();
|
||||
});
|
||||
|
||||
it('refuses a right PIN code of an intern whose last day has passed', function () {
|
||||
$internId = $this->users->save(UserAccount::register(new PersonName('Ida'), new EmailAddress('[email protected]'), Role::Intern, new DateTimeImmutable('2027-05-31'), $this->userId))->id();
|
||||
$this->secrets->passwords[$internId] = 'hashed:Salasana-123!';
|
||||
$this->pins->set($internId, 'Salasana-123!', '1357');
|
||||
|
||||
expect($this->pins->verify($internId, '1357'))->toBeTrue();
|
||||
|
||||
$this->clock->today = new DateTimeImmutable('2027-06-01');
|
||||
|
||||
expect($this->pins->verify($internId, '1357'))->toBeFalse();
|
||||
});
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Users\Domain\Exceptions\InvalidLoginPin;
|
||||
use App\Modules\Users\Domain\ValueObjects\LoginPin;
|
||||
|
||||
it('accepts 4–6 digits as a PIN code', function (string $pin) {
|
||||
expect((new LoginPin($pin))->value)->toBe(trim($pin));
|
||||
})->with(['1234', '123456', ' 0042 ']);
|
||||
|
||||
it('refuses other PIN codes', function (string $pin) {
|
||||
new LoginPin($pin);
|
||||
})->with(['123', '1234567', '12a4', ''])->throws(InvalidLoginPin::class);
|
||||
Reference in New Issue
Block a user