|
| 1 | +<?php |
| 2 | + |
| 3 | +use Eclipse\Core\Models\User; |
| 4 | +use Illuminate\Auth\Access\AuthorizationException; |
| 5 | +use Illuminate\Support\Facades\Auth; |
| 6 | +use Illuminate\Support\Facades\Gate; |
| 7 | +use STS\FilamentImpersonate\Pages\Actions\Impersonate as ImpersonatePageAction; |
| 8 | +use STS\FilamentImpersonate\Tables\Actions\Impersonate as ImpersonateTableAction; |
| 9 | + |
| 10 | +beforeEach(function () { |
| 11 | + $this->set_up_super_admin_and_tenant(); |
| 12 | + |
| 13 | + // Create a target user to impersonate |
| 14 | + $this->targetUser = User::factory()->create([ |
| 15 | + 'first_name' => 'Target', |
| 16 | + 'last_name' => 'User', |
| 17 | + 'email' => 'target@example.com', |
| 18 | + ]); |
| 19 | + |
| 20 | + // Create a user with impersonate permission |
| 21 | + $this->authorizedUser = User::factory()->create([ |
| 22 | + 'first_name' => 'Authorized', |
| 23 | + 'last_name' => 'User', |
| 24 | + 'email' => 'authorized@example.com', |
| 25 | + ]); |
| 26 | + $this->authorizedUser->givePermissionTo('impersonate_user'); |
| 27 | + |
| 28 | + // Create a user without impersonate permission |
| 29 | + $this->unauthorizedUser = User::factory()->create([ |
| 30 | + 'first_name' => 'Unauthorized', |
| 31 | + 'last_name' => 'User', |
| 32 | + 'email' => 'unauthorized@example.com', |
| 33 | + ]); |
| 34 | +}); |
| 35 | + |
| 36 | +test('non-authorized user cannot impersonate other users', function () { |
| 37 | + // Login as unauthorized user |
| 38 | + Auth::login($this->unauthorizedUser); |
| 39 | + |
| 40 | + // Assert user doesn't have impersonate permission |
| 41 | + $this->assertFalse($this->unauthorizedUser->hasPermissionTo('impersonate_user')); |
| 42 | + $this->assertFalse($this->unauthorizedUser->can('impersonate', User::class)); |
| 43 | + $this->assertFalse($this->unauthorizedUser->canImpersonate()); |
| 44 | + |
| 45 | + // Test authorization gate |
| 46 | + $this->expectException(AuthorizationException::class); |
| 47 | + Gate::authorize('impersonate', User::class); |
| 48 | +}); |
| 49 | + |
| 50 | +test('non-authorized user cannot see and trigger the impersonate table and page action', function () { |
| 51 | + // Login as unauthorized user |
| 52 | + Auth::login($this->unauthorizedUser); |
| 53 | + |
| 54 | + // Assert user doesn't have impersonate permission |
| 55 | + $this->assertFalse($this->unauthorizedUser->hasPermissionTo('impersonate_user')); |
| 56 | + |
| 57 | + // Create an instance of the Impersonate action |
| 58 | + $action = ImpersonateTableAction::make('impersonate') |
| 59 | + ->record($this->targetUser); |
| 60 | + |
| 61 | + // Assert the action is not authorized |
| 62 | + $this->assertFalse($action->isVisible()); |
| 63 | + $this->assertFalse($action->isEnabled()); |
| 64 | + |
| 65 | + // Create an instance of the Impersonate page action |
| 66 | + $action = ImpersonatePageAction::make('impersonate') |
| 67 | + ->record($this->targetUser); |
| 68 | + |
| 69 | + // Assert the action is not authorized |
| 70 | + $this->assertFalse($action->isVisible()); |
| 71 | + $this->assertFalse($action->isEnabled()); |
| 72 | +}); |
| 73 | + |
| 74 | +test('authorized user can impersonate other users', function () { |
| 75 | + // Login as authorized user |
| 76 | + Auth::login($this->authorizedUser); |
| 77 | + |
| 78 | + // Assert user has impersonate permission |
| 79 | + $this->assertTrue($this->authorizedUser->hasPermissionTo('impersonate_user')); |
| 80 | + $this->assertTrue($this->authorizedUser->can('impersonate', User::class)); |
| 81 | + $this->assertTrue($this->authorizedUser->canImpersonate()); |
| 82 | + |
| 83 | + // Test authorization gate |
| 84 | + $this->assertTrue(Gate::allows('impersonate', User::class)); |
| 85 | +}); |
| 86 | + |
| 87 | +test('authorized user can see and trigger the impersonate table and page action', function () { |
| 88 | + // Login as authorized user |
| 89 | + Auth::login($this->authorizedUser); |
| 90 | + |
| 91 | + // Assert user has impersonate permission |
| 92 | + $this->assertTrue($this->authorizedUser->hasPermissionTo('impersonate_user')); |
| 93 | + |
| 94 | + // Create an instance of the Impersonate action |
| 95 | + $action = ImpersonateTableAction::make('impersonate') |
| 96 | + ->record($this->targetUser); |
| 97 | + |
| 98 | + // Assert the action is authorized |
| 99 | + $this->assertTrue($action->isVisible()); |
| 100 | + $this->assertTrue($action->isEnabled()); |
| 101 | + |
| 102 | + // Create an instance of the Impersonate page action |
| 103 | + $action = ImpersonatePageAction::make('impersonate') |
| 104 | + ->record($this->targetUser); |
| 105 | + |
| 106 | + // Assert the action is authorized |
| 107 | + $this->assertTrue($action->isVisible()); |
| 108 | + $this->assertTrue($action->isEnabled()); |
| 109 | +}); |
0 commit comments