5 use BookStack\Access\Mfa\MfaValue;
6 use BookStack\Activity\Tools\UserEntityWatchOptions;
7 use BookStack\Activity\WatchLevels;
8 use BookStack\Api\ApiToken;
9 use BookStack\Uploads\Image;
10 use Illuminate\Support\Facades\Hash;
11 use Illuminate\Support\Str;
14 class UserMyAccountTest extends TestCase
16 public function test_index_view()
18 $resp = $this->asEditor()->get('/my-account');
19 $resp->assertRedirect('/my-account/profile');
22 public function test_views_not_accessible_to_guest_user()
24 $categories = ['profile', 'auth', 'shortcuts', 'notifications', ''];
25 $this->setSettings(['app-public' => 'true']);
27 $this->permissions->grantUserRolePermissions($this->users->guest(), ['receive-notifications']);
29 foreach ($categories as $category) {
30 $resp = $this->get('/my-account/' . $category);
31 $resp->assertRedirect('/');
35 public function test_profile_updating()
37 $editor = $this->users->editor();
39 $resp = $this->actingAs($editor)->get('/my-account/profile');
40 $resp->assertSee('Profile Details');
42 $html = $this->withHtml($resp);
43 $html->assertFieldHasValue('name', $editor->name);
44 $html->assertFieldHasValue('email', $editor->email);
46 $resp = $this->put('/my-account/profile', [
52 $resp->assertRedirect('/my-account/profile');
53 $this->assertDatabaseHas('users', [
55 'email' => $editor->email, // No email change due to not having permissions
57 $this->assertEquals(setting()->getUser($editor, 'language'), 'fr');
60 public function test_profile_user_avatar_update_and_reset()
62 $user = $this->users->viewer();
63 $avatarFile = $this->files->uploadedImage('avatar-icon.png');
65 $this->assertEquals(0, $user->image_id);
67 $upload = $this->actingAs($user)->call('PUT', "/my-account/profile", [
68 'name' => 'Barry Scott',
69 ], [], ['profile_image' => $avatarFile], []);
70 $upload->assertRedirect('/my-account/profile');
74 $this->assertNotEquals(0, $user->image_id);
75 /** @var Image $image */
76 $image = Image::query()->findOrFail($user->image_id);
77 $this->assertFileExists(public_path($image->path));
79 $reset = $this->put("/my-account/profile", [
80 'name' => 'Barry Scott',
81 'profile_image_reset' => 'true',
83 $upload->assertRedirect('/my-account/profile');
86 $this->assertFileDoesNotExist(public_path($image->path));
87 $this->assertEquals(0, $user->image_id);
90 public function test_profile_admin_options_link_shows_if_permissions_allow()
92 $editor = $this->users->editor();
94 $resp = $this->actingAs($editor)->get('/my-account/profile');
95 $resp->assertDontSee('Administrator Options');
96 $this->withHtml($resp)->assertLinkNotExists(url("/settings/users/{$editor->id}"));
98 $this->permissions->grantUserRolePermissions($editor, ['users-manage']);
100 $resp = $this->actingAs($editor)->get('/my-account/profile');
101 $resp->assertSee('Administrator Options');
102 $this->withHtml($resp)->assertLinkExists(url("/settings/users/{$editor->id}"));
105 public function test_profile_self_delete()
107 $editor = $this->users->editor();
109 $resp = $this->actingAs($editor)->get('/my-account/profile');
110 $this->withHtml($resp)->assertLinkExists(url('/my-account/delete'), 'Delete Account');
112 $resp = $this->get('/my-account/delete');
113 $resp->assertSee('Delete My Account');
114 $this->withHtml($resp)->assertElementContains('form[action$="/my-account"] button', 'Confirm');
116 $resp = $this->delete('/my-account');
117 $resp->assertRedirect('/');
119 $this->assertDatabaseMissing('users', ['id' => $editor->id]);
122 public function test_profile_self_delete_shows_ownership_migration_if_can_manage_users()
124 $editor = $this->users->editor();
126 $resp = $this->actingAs($editor)->get('/my-account/delete');
127 $resp->assertDontSee('Migrate Ownership');
129 $this->permissions->grantUserRolePermissions($editor, ['users-manage']);
131 $resp = $this->actingAs($editor)->get('/my-account/delete');
132 $resp->assertSee('Migrate Ownership');
135 public function test_auth_password_change()
137 $editor = $this->users->editor();
139 $resp = $this->actingAs($editor)->get('/my-account/auth');
140 $resp->assertSee('Change Password');
141 $this->withHtml($resp)->assertElementExists('form[action$="/my-account/auth/password"]');
143 $password = Str::random();
144 $resp = $this->put('/my-account/auth/password', [
145 'password' => $password,
146 'password-confirm' => $password,
148 $resp->assertRedirect('/my-account/auth');
151 $this->assertTrue(Hash::check($password, $editor->password));
154 public function test_auth_password_change_hides_if_not_using_email_auth()
156 $editor = $this->users->editor();
158 $resp = $this->actingAs($editor)->get('/my-account/auth');
159 $resp->assertSee('Change Password');
161 config()->set('auth.method', 'oidc');
163 $resp = $this->actingAs($editor)->get('/my-account/auth');
164 $resp->assertDontSee('Change Password');
167 public function test_auth_page_has_mfa_links()
169 $editor = $this->users->editor();
170 $resp = $this->actingAs($editor)->get('/my-account/auth');
171 $resp->assertSee('0 methods configured');
172 $this->withHtml($resp)->assertLinkExists(url('/mfa/setup'));
174 MfaValue::upsertWithValue($editor, 'totp', 'testval');
176 $resp = $this->get('/my-account/auth');
177 $resp->assertSee('1 method configured');
180 public function test_auth_page_api_tokens()
182 $editor = $this->users->editor();
183 $resp = $this->actingAs($editor)->get('/my-account/auth');
184 $resp->assertSee('API Tokens');
185 $this->withHtml($resp)->assertLinkExists(url("/api-tokens/{$editor->id}/create?context=my-account"));
187 ApiToken::factory()->create(['user_id' => $editor->id, 'name' => 'My great token']);
188 $editor->unsetRelations();
190 $resp = $this->get('/my-account/auth');
191 $resp->assertSee('My great token');
194 public function test_interface_shortcuts_updating()
198 // View preferences with defaults
199 $resp = $this->get('/my-account/shortcuts');
200 $resp->assertSee('UI Shortcut Preferences');
202 $html = $this->withHtml($resp);
203 $html->assertFieldHasValue('enabled', 'false');
204 $html->assertFieldHasValue('shortcut[home_view]', '1');
206 // Update preferences
207 $resp = $this->put('/my-account/shortcuts', [
209 'shortcut' => ['home_view' => 'Ctrl + 1'],
212 $resp->assertRedirect('/my-account/shortcuts');
213 $resp->assertSessionHas('success', 'Shortcut preferences have been updated!');
215 // View updates to preferences page
216 $resp = $this->get('/my-account/shortcuts');
217 $html = $this->withHtml($resp);
218 $html->assertFieldHasValue('enabled', 'true');
219 $html->assertFieldHasValue('shortcut[home_view]', 'Ctrl + 1');
222 public function test_body_has_shortcuts_component_when_active()
224 $editor = $this->users->editor();
225 $this->actingAs($editor);
227 $this->withHtml($this->get('/'))->assertElementNotExists('body[component="shortcuts"]');
229 setting()->putUser($editor, 'ui-shortcuts-enabled', 'true');
230 $this->withHtml($this->get('/'))->assertElementExists('body[component="shortcuts"]');
233 public function test_notification_routes_requires_notification_permission()
235 $viewer = $this->users->viewer();
236 $resp = $this->actingAs($viewer)->get('/my-account/notifications');
237 $this->assertPermissionError($resp);
239 $resp = $this->actingAs($viewer)->get('/my-account/profile');
240 $resp->assertDontSeeText('Notification Preferences');
242 $resp = $this->put('/my-account/notifications');
243 $this->assertPermissionError($resp);
245 $this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']);
246 $resp = $this->get('/my-account/notifications');
248 $resp->assertSee('Notification Preferences');
251 public function test_notification_preferences_updating()
253 $editor = $this->users->editor();
255 // View preferences with defaults
256 $resp = $this->actingAs($editor)->get('/my-account/notifications');
257 $resp->assertSee('Notification Preferences');
259 $html = $this->withHtml($resp);
260 $html->assertFieldHasValue('preferences[comment-replies]', 'false');
262 // Update preferences
263 $resp = $this->put('/my-account/notifications', [
264 'preferences' => ['comment-replies' => 'true'],
267 $resp->assertRedirect('/my-account/notifications');
268 $resp->assertSessionHas('success', 'Notification preferences have been updated!');
270 // View updates to preferences page
271 $resp = $this->get('/my-account/notifications');
272 $html = $this->withHtml($resp);
273 $html->assertFieldHasValue('preferences[comment-replies]', 'true');
276 public function test_notification_preferences_show_watches()
278 $editor = $this->users->editor();
279 $book = $this->entities->book();
281 $options = new UserEntityWatchOptions($editor, $book);
282 $options->updateLevelByValue(WatchLevels::COMMENTS);
284 $resp = $this->actingAs($editor)->get('/my-account/notifications');
285 $resp->assertSee($book->name);
286 $resp->assertSee('All Page Updates & Comments');
288 $options->updateLevelByValue(WatchLevels::DEFAULT);
290 $resp = $this->actingAs($editor)->get('/my-account/notifications');
291 $resp->assertDontSee($book->name);
292 $resp->assertDontSee('All Page Updates & Comments');
295 public function test_notification_preferences_dont_error_on_deleted_items()
297 $editor = $this->users->editor();
298 $book = $this->entities->book();
300 $options = new UserEntityWatchOptions($editor, $book);
301 $options->updateLevelByValue(WatchLevels::COMMENTS);
303 $this->actingAs($editor)->delete($book->getUrl());
305 $this->assertNotNull($book->deleted_at);
307 $resp = $this->actingAs($editor)->get('/my-account/notifications');
309 $resp->assertDontSee($book->name);
312 public function test_notification_preferences_not_accessible_to_guest()
314 $this->setSettings(['app-public' => 'true']);
315 $guest = $this->users->guest();
316 $this->permissions->grantUserRolePermissions($guest, ['receive-notifications']);
318 $resp = $this->get('/my-account/notifications');
319 $this->assertPermissionError($resp);
321 $resp = $this->put('/my-account/notifications', [
322 'preferences' => ['comment-replies' => 'true'],
324 $this->assertPermissionError($resp);
327 public function test_notification_comment_options_only_exist_if_comments_active()
329 $resp = $this->asEditor()->get('/my-account/notifications');
330 $resp->assertSee('Notify upon comments');
331 $resp->assertSee('Notify upon replies');
333 setting()->put('app-disable-comments', true);
335 $resp = $this->get('/my-account/notifications');
336 $resp->assertDontSee('Notify upon comments');
337 $resp->assertDontSee('Notify upon replies');