|
2 | 2 |
|
3 | 3 | namespace Tests\Actions; |
4 | 4 |
|
| 5 | +use Illuminate\Support\Facades\Gate; |
5 | 6 | use PHPUnit\Framework\Attributes\Group; |
6 | 7 | use PHPUnit\Framework\Attributes\Test; |
| 8 | +use Statamic\Actions\Impersonate as Action; |
7 | 9 | use Statamic\Facades\User; |
| 10 | +use Statamic\Policies\UserPolicy; |
8 | 11 | use Tests\ElevatesSessions; |
| 12 | +use Tests\FakesRoles; |
9 | 13 | use Tests\PreventSavingStacheItemsToDisk; |
10 | 14 | use Tests\TestCase; |
11 | 15 |
|
12 | 16 | #[Group('elevated-session')] |
13 | 17 | class ImpersonateTest extends TestCase |
14 | 18 | { |
15 | 19 | use ElevatesSessions; |
| 20 | + use FakesRoles; |
16 | 21 | use PreventSavingStacheItemsToDisk; |
17 | 22 |
|
18 | 23 | private function impersonate($user) |
@@ -41,4 +46,87 @@ public function it_authenticates_as_another_user_and_clears_elevated_session() |
41 | 46 | $this->assertEquals($impersonated->id(), auth()->id()); |
42 | 47 | $this->assertFalse(request()->hasElevatedSession()); |
43 | 48 | } |
| 49 | + |
| 50 | + #[Test] |
| 51 | + public function it_is_visible_to_a_valid_target_user() |
| 52 | + { |
| 53 | + $impersonator = tap(User::make()->email('admin@example.com')->makeSuper())->save(); |
| 54 | + $impersonated = tap(User::make()->email('user@example.com'))->save(); |
| 55 | + |
| 56 | + $this->actingAs($impersonator); |
| 57 | + |
| 58 | + $this->assertTrue((new Action)->visibleTo($impersonated)); |
| 59 | + } |
| 60 | + |
| 61 | + #[Test] |
| 62 | + public function it_is_not_visible_when_policy_denies_impersonation() |
| 63 | + { |
| 64 | + $this->setTestRoles(['impersonator' => ['impersonate users']]); |
| 65 | + |
| 66 | + $impersonator = tap(User::make()->email('admin@example.com')->assignRole('impersonator'))->save(); |
| 67 | + $impersonated = tap(User::make()->email('user@example.com'))->save(); |
| 68 | + |
| 69 | + Gate::policy(get_class($impersonated), DenyImpersonationPolicy::class); |
| 70 | + |
| 71 | + $this->actingAs($impersonator); |
| 72 | + |
| 73 | + $this->assertFalse((new Action)->visibleTo($impersonated)); |
| 74 | + } |
| 75 | + |
| 76 | + #[Test] |
| 77 | + public function it_is_authorized_with_the_default_policy() |
| 78 | + { |
| 79 | + $this->setTestRoles(['impersonator' => ['impersonate users']]); |
| 80 | + |
| 81 | + $impersonator = tap(User::make()->email('admin@example.com')->assignRole('impersonator'))->save(); |
| 82 | + $impersonated = tap(User::make()->email('user@example.com'))->save(); |
| 83 | + |
| 84 | + $this->assertTrue((new Action)->authorize($impersonator, $impersonated)); |
| 85 | + } |
| 86 | + |
| 87 | + #[Test] |
| 88 | + public function it_is_not_authorized_when_policy_denies_impersonation() |
| 89 | + { |
| 90 | + $this->setTestRoles(['impersonator' => ['impersonate users']]); |
| 91 | + |
| 92 | + $impersonator = tap(User::make()->email('admin@example.com')->assignRole('impersonator'))->save(); |
| 93 | + $impersonated = tap(User::make()->email('user@example.com'))->save(); |
| 94 | + |
| 95 | + Gate::policy(get_class($impersonated), DenyImpersonationPolicy::class); |
| 96 | + |
| 97 | + $this->assertFalse((new Action)->authorize($impersonator, $impersonated)); |
| 98 | + } |
| 99 | + |
| 100 | + #[Test] |
| 101 | + public function it_is_not_authorized_without_permission() |
| 102 | + { |
| 103 | + $this->setTestRoles(['editor' => ['edit users']]); |
| 104 | + |
| 105 | + $impersonator = tap(User::make()->email('admin@example.com')->assignRole('editor'))->save(); |
| 106 | + $impersonated = tap(User::make()->email('user@example.com'))->save(); |
| 107 | + |
| 108 | + $this->assertFalse((new Action)->authorize($impersonator, $impersonated)); |
| 109 | + } |
| 110 | + |
| 111 | + #[Test] |
| 112 | + public function super_users_bypass_the_policy_check() |
| 113 | + { |
| 114 | + $impersonator = tap(User::make()->email('admin@example.com')->makeSuper())->save(); |
| 115 | + $impersonated = tap(User::make()->email('user@example.com'))->save(); |
| 116 | + |
| 117 | + Gate::policy(get_class($impersonated), DenyImpersonationPolicy::class); |
| 118 | + |
| 119 | + $this->actingAs($impersonator); |
| 120 | + |
| 121 | + $this->assertTrue((new Action)->visibleTo($impersonated)); |
| 122 | + $this->assertTrue((new Action)->authorize($impersonator, $impersonated)); |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +class DenyImpersonationPolicy extends UserPolicy |
| 127 | +{ |
| 128 | + public function impersonate($authed, $user) |
| 129 | + { |
| 130 | + return false; |
| 131 | + } |
44 | 132 | } |
0 commit comments