Files
ppeasy_vg12/tests/Feature/Web/Profile/SessionsControllerTest.php
T
2026-10-09 08:21:06 +02:00

64 lines
1.6 KiB
PHP

<?php
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Inertia\Testing\AssertableInertia as Assert;
uses(RefreshDatabase::class);
beforeEach(function () {
config(['session.driver' => 'database']);
$this->artisan('db:seed');
});
test('user can see their active sessions', function () {
$user = User::factory()->create();
$sessionId = insertProfileSession($user);
$this->actingAs($user)
->get('profile/sessions')
->assertOk()
->assertInertia(fn (Assert $page) => $page
->component('users/Sessions')
->where('adminView', false)
->where('user.id', $user->id)
->has('sessions', 1)
->where('sessions.0.id', $sessionId)
);
});
test('user can invalidate their session', function () {
$user = User::factory()->create();
$sessionId = insertProfileSession($user);
$this->actingAs($user)
->delete("profile/sessions/{$sessionId}/invalidate")
->assertRedirect(route('profile.sessions'));
$this->assertDatabaseMissing('sessions', ['id' => $sessionId]);
});
test('guest cannot access profile sessions', function () {
$this->get('profile/sessions')->assertRedirect('login');
});
function insertProfileSession(User $user): string
{
$id = Str::random();
DB::table('sessions')->insert([
'id' => $id,
'user_id' => $user->id,
'ip_address' => fake()->ipv4,
'user_agent' => fake()->userAgent,
'payload' => 'foo',
'last_activity' => Carbon::now()->timestamp,
]);
return $id;
}