Skip to content

Commit 08481b0

Browse files
committed
Add Tests
1 parent b76e632 commit 08481b0

File tree

1 file changed

+188
-0
lines changed

1 file changed

+188
-0
lines changed
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
3+
namespace Tests\User;
4+
5+
use Tests\TestCase;
6+
7+
class UserImpersonationTest extends TestCase
8+
{
9+
public function test_impersonate_button_shown_on_edit_page_for_admin()
10+
{
11+
$viewer = $this->users->viewer();
12+
13+
$resp = $this->asAdmin()->get("/settings/users/{$viewer->id}");
14+
15+
$this->withHtml($resp)->assertElementExists("form[action$=\"/settings/users/{$viewer->id}/impersonate\"]");
16+
$resp->assertSee('Impersonate User');
17+
}
18+
19+
public function test_impersonate_button_not_shown_for_non_admin()
20+
{
21+
$viewer = $this->users->viewer();
22+
$editor = $this->users->editor();
23+
24+
$resp = $this->actingAs($editor)->get("/settings/users/{$viewer->id}");
25+
26+
$resp->assertDontSee('Impersonate User');
27+
$this->withHtml($resp)->assertElementNotExists("form[action$=\"/settings/users/{$viewer->id}/impersonate\"]");
28+
}
29+
30+
public function test_impersonate_button_not_shown_for_own_user()
31+
{
32+
$admin = $this->users->admin();
33+
34+
$resp = $this->actingAs($admin)->get("/settings/users/{$admin->id}");
35+
36+
$resp->assertDontSee('Impersonate User');
37+
$this->withHtml($resp)->assertElementNotExists("form[action$=\"/settings/users/{$admin->id}/impersonate\"]");
38+
}
39+
40+
public function test_impersonate_button_not_shown_for_guest_user()
41+
{
42+
$guest = $this->users->guest();
43+
44+
$resp = $this->asAdmin()->get("/settings/users/{$guest->id}");
45+
46+
$resp->assertDontSee('Impersonate User');
47+
$this->withHtml($resp)->assertElementNotExists("form[action$=\"/settings/users/{$guest->id}/impersonate\"]");
48+
}
49+
50+
public function test_impersonate_button_not_shown_when_already_impersonating()
51+
{
52+
$viewer = $this->users->viewer();
53+
$editor = $this->users->editor();
54+
55+
$this->asAdmin()->post("/settings/users/{$viewer->id}/impersonate");
56+
57+
$resp = $this->get("/settings/users/{$editor->id}");
58+
$resp->assertDontSee('Impersonate User');
59+
}
60+
61+
public function test_impersonate_sets_session_and_redirects_to_home()
62+
{
63+
$viewer = $this->users->viewer();
64+
65+
$resp = $this->asAdmin()->post("/settings/users/{$viewer->id}/impersonate");
66+
67+
$resp->assertRedirect('/');
68+
$this->assertSessionHas('impersonate', $viewer->id);
69+
}
70+
71+
public function test_impersonate_requires_users_manage_permission()
72+
{
73+
$viewer = $this->users->viewer();
74+
$editor = $this->users->editor();
75+
76+
$resp = $this->actingAs($editor)->post("/settings/users/{$viewer->id}/impersonate");
77+
78+
$resp->assertRedirect('/');
79+
$this->assertSessionMissing('impersonate');
80+
}
81+
82+
public function test_cannot_impersonate_guest_user()
83+
{
84+
$guest = $this->users->guest();
85+
86+
$resp = $this->asAdmin()->post("/settings/users/{$guest->id}/impersonate");
87+
88+
$resp->assertRedirect("/settings/users/{$guest->id}");
89+
$this->assertSessionError('You cannot impersonate this user');
90+
$this->assertSessionMissing('impersonate');
91+
}
92+
93+
public function test_cannot_impersonate_self()
94+
{
95+
$admin = $this->users->admin();
96+
97+
$resp = $this->actingAs($admin)->post("/settings/users/{$admin->id}/impersonate");
98+
99+
$resp->assertRedirect("/settings/users/{$admin->id}");
100+
$this->assertSessionError('You cannot impersonate this user');
101+
$this->assertSessionMissing('impersonate');
102+
}
103+
104+
public function test_impersonation_banner_shown_while_impersonating()
105+
{
106+
$viewer = $this->users->viewer();
107+
108+
$this->asAdmin()->post("/settings/users/{$viewer->id}/impersonate");
109+
$resp = $this->get('/');
110+
111+
$resp->assertSee('Impersonating: ' . $viewer->name);
112+
$resp->assertSee('Stop Impersonating');
113+
}
114+
115+
public function test_impersonation_banner_not_shown_when_not_impersonating()
116+
{
117+
$resp = $this->asAdmin()->get('/');
118+
119+
$resp->assertDontSee('Impersonating:');
120+
$resp->assertDontSee('Stop Impersonating');
121+
}
122+
123+
public function test_requests_are_performed_as_impersonated_user()
124+
{
125+
$viewer = $this->users->viewer();
126+
$admin = $this->users->admin();
127+
128+
$this->actingAs($admin)->post("/settings/users/{$viewer->id}/impersonate");
129+
130+
$resp = $this->get('/');
131+
$resp->assertSee('Impersonating: ' . $viewer->name);
132+
}
133+
134+
public function test_stop_impersonate_clears_session_and_redirects_to_user_edit()
135+
{
136+
$viewer = $this->users->viewer();
137+
138+
$this->asAdmin()->post("/settings/users/{$viewer->id}/impersonate");
139+
$this->assertSessionHas('impersonate', $viewer->id);
140+
141+
$resp = $this->get('/impersonate/stop');
142+
143+
$resp->assertRedirect("/settings/users/{$viewer->id}");
144+
$this->assertSessionMissing('impersonate');
145+
}
146+
147+
public function test_stop_impersonate_banner_gone_after_stopping()
148+
{
149+
$viewer = $this->users->viewer();
150+
151+
$this->asAdmin()->post("/settings/users/{$viewer->id}/impersonate");
152+
$this->get('/impersonate/stop');
153+
154+
$resp = $this->get('/');
155+
$resp->assertDontSee('Impersonating:');
156+
}
157+
158+
public function test_middleware_does_not_switch_user_without_impersonate_session()
159+
{
160+
$admin = $this->users->admin();
161+
162+
$resp = $this->actingAs($admin)->get('/');
163+
164+
$resp->assertDontSee('Impersonating:');
165+
}
166+
167+
public function test_middleware_does_not_switch_user_if_actor_lacks_users_manage()
168+
{
169+
$viewer = $this->users->viewer();
170+
$editor = $this->users->editor();
171+
172+
$this->actingAs($editor)->withSession(['impersonate' => $viewer->id]);
173+
174+
$resp = $this->get('/');
175+
176+
$resp->assertDontSee('Impersonating: ' . $viewer->name);
177+
}
178+
179+
public function test_stop_impersonate_link_shown_in_user_menu_while_impersonating()
180+
{
181+
$viewer = $this->users->viewer();
182+
183+
$this->asAdmin()->post("/settings/users/{$viewer->id}/impersonate");
184+
$resp = $this->get('/');
185+
186+
$this->withHtml($resp)->assertElementContains('a[href="' . url('/impersonate/stop') . '"]', 'Stop Impersonating');
187+
}
188+
}

0 commit comments

Comments
 (0)