Files
ppeasy_vg12/tests/Browser/Users/SessionsTest.php
T
2026-10-09 08:21:06 +02:00

76 lines
2.1 KiB
PHP

<?php
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
/*
* Both session pages sit behind the session.database middleware, which 404s
* unless the database driver is in use. The suite otherwise runs on the array
* driver configured in phpunit.xml.
*/
beforeEach(function () {
config(['session.driver' => 'database']);
$this->artisan('db:seed');
});
it('lists another users sessions for an admin', function () {
browserLogin();
$user = User::factory()->create(['username' => 'watched']);
insertBrowserSession($user, '10.0.0.9');
visit("/users/{$user->id}/sessions")
->assertNoJavaScriptErrors()
->assertSee('Sessions')
->assertSee('IP Address')
->assertSee('10.0.0.9');
});
it('asks for confirmation before invalidating a session', function () {
browserLogin();
$user = User::factory()->create();
insertBrowserSession($user, '10.0.0.10');
visit("/users/{$user->id}/sessions")
->assertSee('10.0.0.10')
->click('[aria-label="Invalidate Session"]')
->assertSee('Are you sure that you want to invalidate this session?')
->assertSee('Yes, proceed!');
});
it('lists the signed in users own sessions', function () {
browserLogin();
visit('/profile/sessions')
->assertNoJavaScriptErrors()
->assertSee('Sessions')
->assertSee('Last Activity');
});
/**
* Insert a session row for a user, the way the server suite fakes one.
*
* Named apart from the Feature suite's insertUserSession: Pest loads every test
* file into one process, so a repeated global function name is a fatal
* redeclare even when the group is excluded from the run.
*/
function insertBrowserSession(User $user, string $ipAddress): string
{
$id = Str::random();
DB::table('sessions')->insert([
'id' => $id,
'user_id' => $user->id,
'ip_address' => $ipAddress,
'user_agent' => fake()->userAgent,
'payload' => 'foo',
'last_activity' => Carbon::now()->timestamp,
]);
return $id;
}