From 920cb06d1f9079c7738a4471baab4be81b9ba997 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Mon, 13 Apr 2026 10:23:30 +0200 Subject: [PATCH] Test that stopImpersonating only has an effect on the original guard Starting impersonation using 'web', then using a different guard and calling UserImpersonation::stopImpersonating() should log out the user from the 'web' guard stored in `session('tenancy_impersonation_guard')`. --- tests/TenantUserImpersonationTest.php | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/tests/TenantUserImpersonationTest.php b/tests/TenantUserImpersonationTest.php index f9fed8ae..2bb3b910 100644 --- a/tests/TenantUserImpersonationTest.php +++ b/tests/TenantUserImpersonationTest.php @@ -191,6 +191,50 @@ test('stopImpersonating can keep the user authenticated', function() { ->assertSee('You are logged in as Joe'); }); +test('stopImpersonating logs out the user from the guard used while starting impersonation', function() { + Route::middleware(InitializeTenancyByPath::class)->prefix('/{tenant}')->group(getRoutes(false)); + + $tenant = Tenant::create([ + 'id' => 'acme', + 'tenancy_db_name' => 'db' . Str::random(16), + ]); + + migrateTenants(); + + $user = $tenant->run(function () { + return ImpersonationUser::create([ + 'name' => 'Joe', + 'email' => 'joe@local', + 'password' => bcrypt('secret'), + ]); + }); + + // Impersonate the user + $token = tenancy()->impersonate($tenant, $user->id, '/acme/dashboard'); + + pest()->get('/acme/impersonate/' . $token->token) + ->assertRedirect('/acme/dashboard'); + + expect(session('tenancy_impersonation_guard'))->toBe('web'); + + // Impersonation logged in the user using the current guard ('web') + expect(auth('web')->check())->toBeTrue(); + + config(['auth.guards.test' => [ + 'driver' => 'session', + 'provider' => 'users', + ]]); + + // Manually log in the user using a different guard + auth('test')->loginUsingId($user->id); + + // Should log out the user from the guard used for impersonation ('web') + UserImpersonation::stopImpersonating(); + + expect(auth('web')->check())->toBeFalse(); + expect(auth('test')->check())->toBeTrue(); +}); + test('tokens have a limited ttl', function () { Route::middleware(InitializeTenancyByDomain::class)->group(getRoutes());