50 lines
1.7 KiB
PHP
50 lines
1.7 KiB
PHP
<?php
|
|
|
|
use App\Models\Customer;
|
|
use App\Models\User;
|
|
|
|
// admin() helper is defined in HostManagementTest.
|
|
|
|
it('lets an admin impersonate a customer and returns to the admin session', function () {
|
|
$admin = admin();
|
|
$customer = Customer::factory()->create(['email' => 'c@imp.test', 'name' => 'Imp Kunde']);
|
|
|
|
$this->actingAs($admin)
|
|
->get(route('admin.impersonate', $customer->uuid))
|
|
->assertRedirect(route('dashboard'));
|
|
|
|
$customer->refresh();
|
|
expect($customer->user_id)->not->toBeNull()
|
|
->and(auth()->id())->toBe($customer->user_id)
|
|
->and(session('impersonator_id'))->toBe($admin->id);
|
|
|
|
// Return to the admin session.
|
|
$this->get(route('impersonate.leave'))->assertRedirect(route('admin.overview'));
|
|
|
|
expect(auth()->id())->toBe($admin->id)
|
|
->and(session()->has('impersonator_id'))->toBeFalse();
|
|
});
|
|
|
|
it('reuses an existing portal user with the same email', function () {
|
|
$existing = User::factory()->create(['email' => 'shared@imp.test', 'is_admin' => false]);
|
|
$customer = Customer::factory()->create(['email' => 'shared@imp.test']);
|
|
|
|
expect($customer->ensureUser()->id)->toBe($existing->id);
|
|
expect($customer->fresh()->user_id)->toBe($existing->id);
|
|
});
|
|
|
|
it('forbids a non-admin from impersonating', function () {
|
|
$user = User::factory()->create(['is_admin' => false]);
|
|
$customer = Customer::factory()->create();
|
|
|
|
$this->actingAs($user)
|
|
->get(route('admin.impersonate', $customer->uuid))
|
|
->assertForbidden();
|
|
});
|
|
|
|
it('gates impersonation start to authenticated users', function () {
|
|
$customer = Customer::factory()->create();
|
|
|
|
$this->get(route('admin.impersonate', $customer->uuid))->assertRedirect('/login');
|
|
});
|