lernschiff/tests/Feature/Auth/LogoutTest.php

75 lines
2.1 KiB
PHP

<?php
use App\Models\License;
use App\Models\Role;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Support\Facades\DB;
uses(\Illuminate\Foundation\Testing\RefreshDatabase::class);
beforeEach(function () {
$this->seed(\Database\Seeders\RolesSeeder::class);
$tenant = Tenant::firstOrCreate(
['name' => 'Test Schule'],
['type' => 'school', 'active' => true]
);
$this->user = User::withoutGlobalScopes()->create([
'name' => 'Logout Tester',
'email' => 'logout@test.example',
'tenant_id' => $tenant->id,
'password' => bcrypt('password'),
]);
$role = Role::where('name', 'child')->first();
DB::table('role_user')->insert([
'user_id' => $this->user->id,
'role_id' => $role->id,
'created_at' => now(),
'updated_at' => now(),
]);
$license = License::withoutGlobalScopes()->create([
'tenant_id' => $tenant->id,
'type' => 'school_flat',
'expires_at' => now()->addYear(),
'active' => true,
]);
DB::table('license_assignments')->insert([
'license_id' => $license->id,
'user_id' => $this->user->id,
'created_at' => now(),
'updated_at' => now(),
]);
});
it('logout route is registered as POST', function () {
expect(\Illuminate\Support\Facades\Route::has('logout'))->toBeTrue();
});
it('POST /logout signs out authenticated user', function () {
$this->actingAs($this->user);
expect(auth()->check())->toBeTrue();
$this->post('/logout')
->assertRedirect(route('login'));
expect(auth()->check())->toBeFalse();
});
it('GET /logout returns 405 (only POST allowed)', function () {
$this->actingAs($this->user);
$this->get('/logout')->assertStatus(405);
});
it('dashboard renders logout button in sidebar', function () {
$this->actingAs($this->user);
$response = $this->get(route('dashboard'));
$response->assertOk();
$response->assertSee('data-testid="sidebar-logout"', false);
$response->assertSee('action="' . route('logout') . '"', false);
});