presseportale/tests/Feature/Admin/UserImpersonationTest.php
Kevin Adametz 5b8bdf4182
Some checks are pending
linter / quality (push) Waiting to run
tests / ci (push) Waiting to run
12-05-2026 Frontend dev
2026-05-12 18:32:33 +02:00

132 lines
4.2 KiB
PHP

<?php
use App\Actions\Admin\UserImpersonation;
use App\Models\User;
use Database\Seeders\RolesAndPermissionsSeeder;
use Livewire\Volt\Volt as LivewireVolt;
use Tests\TestCase;
test('admin can impersonate a customer from user management', function () {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
$admin = User::factory()->create();
$admin->assignRole('admin');
$customer = User::factory()->create([
'name' => 'Test Kunde',
'email' => 'kunde@example.com',
]);
$customer->assignRole('customer');
$this->actingAs($admin);
LivewireVolt::test('admin.users')
->assertSee('Login als User')
->call('loginAsUser', $customer->id)
->assertRedirect(route('me.dashboard'));
$this->assertAuthenticatedAs($customer);
expect(session(UserImpersonation::SessionKey))->toBe($admin->id);
$this->get(route('dashboard'))->assertRedirect(route('me.dashboard'));
$this->get(route('me.dashboard'))->assertSuccessful();
});
test('users without manage permission cannot start impersonation', function () {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
$editor = User::factory()->create();
$editor->assignRole('editor');
$customer = User::factory()->create();
$customer->assignRole('customer');
$this->actingAs($editor);
LivewireVolt::test('admin.users')
->assertDontSee('Login als User')
->call('loginAsUser', $customer->id)
->assertForbidden();
$this->assertAuthenticatedAs($editor);
$this->assertFalse(session()->has(UserImpersonation::SessionKey));
});
test('admin can impersonate accounts with admin access', function () {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
$admin = User::factory()->create();
$admin->assignRole('admin');
$targetAdmin = User::factory()->create([
'email' => 'target-admin@example.com',
]);
$targetAdmin->assignRole('admin');
$targetEditor = User::factory()->create([
'email' => 'target-editor@example.com',
]);
$targetEditor->assignRole('editor');
$this->actingAs($admin);
LivewireVolt::test('admin.users')
->set('search', 'target-admin@example.com')
->call('loginAsUser', $targetAdmin->id)
->assertRedirect(route('me.dashboard'));
$this->assertAuthenticatedAs($targetAdmin);
expect(session(UserImpersonation::SessionKey))->toBe($admin->id);
$this->get(route('dashboard'))->assertRedirect(route('me.dashboard'));
$this->actingAs($admin);
session()->forget(UserImpersonation::SessionKey);
LivewireVolt::test('admin.users')
->set('search', 'target-editor@example.com')
->call('loginAsUser', $targetEditor->id)
->assertRedirect(route('me.dashboard'));
$this->assertAuthenticatedAs($targetEditor);
expect(session(UserImpersonation::SessionKey))->toBe($admin->id);
$this->get(route('dashboard'))->assertRedirect(route('me.dashboard'));
});
test('impersonated user can return to the admin account', function () {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
$admin = User::factory()->create();
$admin->assignRole('admin');
$customer = User::factory()->create();
$customer->assignRole('customer');
$this->actingAs($customer)
->withSession([UserImpersonation::SessionKey => $admin->id])
->post(route('admin.impersonate.leave'))
->assertRedirect(route('admin.users.index'))
->assertSessionHas('status', 'Erfolgreich zurück zum Admin-Account gewechselt.')
->assertSessionMissing(UserImpersonation::SessionKey);
$this->assertAuthenticatedAs($admin);
});
test('invalid impersonation session is cleared on leave', function () {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
$customer = User::factory()->create();
$customer->assignRole('customer');
$this->actingAs($customer)
->withSession([UserImpersonation::SessionKey => 999999])
->post(route('admin.impersonate.leave'))
->assertRedirect(route('me.dashboard'))
->assertSessionMissing(UserImpersonation::SessionKey);
$this->assertAuthenticatedAs($customer);
});