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