Files
petriandClaude Sonnet 5.5 6692396a63 Fix console hours, sign-in landing and counter usage races
- Hours the counter reading cannot parse (",5") no longer fail after
  they are saved; they just get no hour counter suggestion.
- The console's sign-in falls back to a console machine list
  (machines::console-picker at /console/machines) instead of /machines,
  so it stays in the console's own session.
- Recording counter usage locks the counter first, so usage saved at
  the same time from two devices is not lost.

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

116 lines
4.8 KiB
PHP

<?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.console-picker'));
});
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('goes to the console\'s machine list, in the console\'s own session, when no machine asked for the sign-in', function () {
$this->user->forceFill(['pin_hash' => Hash::make('2468')])->save();
Livewire::test('users::console-login')
->set('userId', (string) $this->user->id)
->set('pin', '2468')
->call('loginWithPin')
->assertRedirect(route('machines.console-picker'));
expect(UseConsoleSession::isConsoleUrl(route('machines.console-picker')))->toBeTrue();
$this->actingAs($this->user, 'console')
->get(route('machines.console-picker'))
->assertOk()
->assertSee('Suurkuvatulostin')
->assertSee($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();
});