1 <?php namespace Tests\Auth;
3 use BookStack\Auth\Role;
4 use BookStack\Auth\User;
5 use BookStack\Entities\Models\Page;
6 use BookStack\Notifications\ConfirmEmail;
7 use BookStack\Notifications\ResetPassword;
8 use BookStack\Settings\SettingService;
11 use Illuminate\Support\Facades\Notification;
12 use Illuminate\Support\Str;
13 use Tests\BrowserKitTest;
15 class AuthTest extends BrowserKitTest
18 public function test_auth_working()
21 ->seePageIs('/login');
24 public function test_login()
30 public function test_public_viewing()
32 $settings = app(SettingService::class);
33 $settings->put('app-public', 'true');
39 public function test_registration_showing()
41 // Ensure registration form is showing
42 $this->setSettings(['registration-enabled' => 'true']);
43 $this->visit('/login')
46 ->seePageIs('/register');
49 public function test_normal_registration()
51 // Set settings and get user instance
52 $this->setSettings(['registration-enabled' => 'true']);
53 $user = factory(User::class)->make();
55 // Test form and ensure user is created
56 $this->visit('/register')
58 ->type($user->name, '#name')
59 ->type($user->email, '#email')
60 ->type($user->password, '#password')
61 ->press('Create Account')
64 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email]);
67 public function test_empty_registration_redirects_back_with_errors()
69 // Set settings and get user instance
70 $this->setSettings(['registration-enabled' => 'true']);
72 // Test form and ensure user is created
73 $this->visit('/register')
74 ->press('Create Account')
75 ->see('The name field is required')
76 ->seePageIs('/register');
79 public function test_registration_validation()
81 $this->setSettings(['registration-enabled' => 'true']);
83 $this->visit('/register')
86 ->type('1', '#password')
87 ->press('Create Account')
88 ->see('The name must be at least 2 characters.')
89 ->see('The email must be a valid email address.')
90 ->see('The password must be at least 8 characters.')
91 ->seePageIs('/register');
94 public function test_sign_up_link_on_login()
96 $this->visit('/login')
99 $this->setSettings(['registration-enabled' => 'true']);
101 $this->visit('/login')
105 public function test_confirmed_registration()
107 // Fake notifications
108 Notification::fake();
110 // Set settings and get user instance
111 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true']);
112 $user = factory(User::class)->make();
114 // Go through registration process
115 $this->visit('/register')
117 ->type($user->name, '#name')
118 ->type($user->email, '#email')
119 ->type($user->password, '#password')
120 ->press('Create Account')
121 ->seePageIs('/register/confirm')
122 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
124 // Ensure notification sent
125 $dbUser = User::where('email', '=', $user->email)->first();
126 Notification::assertSentTo($dbUser, ConfirmEmail::class);
128 // Test access and resend confirmation email
129 $this->login($user->email, $user->password)
130 ->seePageIs('/register/confirm/awaiting')
133 ->seePageIs('/register/confirm/awaiting')
134 ->press('Resend Confirmation Email');
136 // Get confirmation and confirm notification matches
137 $emailConfirmation = DB::table('email_confirmations')->where('user_id', '=', $dbUser->id)->first();
138 Notification::assertSentTo($dbUser, ConfirmEmail::class, function($notification, $channels) use ($emailConfirmation) {
139 return $notification->token === $emailConfirmation->token;
142 // Check confirmation email confirmation activation.
143 $this->visit('/register/confirm/' . $emailConfirmation->token)
146 ->notSeeInDatabase('email_confirmations', ['token' => $emailConfirmation->token])
147 ->seeInDatabase('users', ['name' => $dbUser->name, 'email' => $dbUser->email, 'email_confirmed' => true]);
150 public function test_restricted_registration()
152 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true', 'registration-restrict' => 'example.com']);
153 $user = factory(User::class)->make();
154 // Go through registration process
155 $this->visit('/register')
156 ->type($user->name, '#name')
157 ->type($user->email, '#email')
158 ->type($user->password, '#password')
159 ->press('Create Account')
160 ->seePageIs('/register')
161 ->dontSeeInDatabase('users', ['email' => $user->email])
162 ->see('That email domain does not have access to this application');
166 $this->visit('/register')
167 ->type($user->name, '#name')
168 ->type($user->email, '#email')
169 ->type($user->password, '#password')
170 ->press('Create Account')
171 ->seePageIs('/register/confirm')
172 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
175 ->seePageIs('/register/confirm/awaiting');
179 $this->visit('/')->seePageIs('/login')
180 ->type($user->email, '#email')
181 ->type($user->password, '#password')
183 ->seePageIs('/register/confirm/awaiting')
184 ->seeText('Email Address Not Confirmed');
187 public function test_restricted_registration_with_confirmation_disabled()
189 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'false', 'registration-restrict' => 'example.com']);
190 $user = factory(User::class)->make();
191 // Go through registration process
192 $this->visit('/register')
193 ->type($user->name, '#name')
194 ->type($user->email, '#email')
195 ->type($user->password, '#password')
196 ->press('Create Account')
197 ->seePageIs('/register')
198 ->dontSeeInDatabase('users', ['email' => $user->email])
199 ->see('That email domain does not have access to this application');
203 $this->visit('/register')
204 ->type($user->name, '#name')
205 ->type($user->email, '#email')
206 ->type($user->password, '#password')
207 ->press('Create Account')
208 ->seePageIs('/register/confirm')
209 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
212 ->seePageIs('/register/confirm/awaiting');
215 $this->visit('/')->seePageIs('/login')
216 ->type($user->email, '#email')
217 ->type($user->password, '#password')
219 ->seePageIs('/register/confirm/awaiting')
220 ->seeText('Email Address Not Confirmed');
223 public function test_user_creation()
225 /** @var User $user */
226 $user = factory(User::class)->make();
227 $adminRole = Role::getRole('admin');
230 ->visit('/settings/users')
231 ->click('Add New User')
232 ->type($user->name, '#name')
233 ->type($user->email, '#email')
234 ->check("roles[{$adminRole->id}]")
235 ->type($user->password, '#password')
236 ->type($user->password, '#password-confirm')
238 ->seePageIs('/settings/users')
239 ->seeInDatabase('users', $user->only(['name', 'email']))
243 $this->assertStringStartsWith(Str::slug($user->name), $user->slug);
246 public function test_user_updating()
248 $user = $this->getNormalUser();
249 $password = $user->password;
251 ->visit('/settings/users')
253 ->seePageIs('/settings/users/' . $user->id)
255 ->type('Barry Scott', '#name')
257 ->seePageIs('/settings/users')
258 ->seeInDatabase('users', ['id' => $user->id, 'name' => 'Barry Scott', 'password' => $password])
259 ->notSeeInDatabase('users', ['name' => $user->name]);
262 $this->assertStringStartsWith(Str::slug($user->name), $user->slug);
265 public function test_user_password_update()
267 $user = $this->getNormalUser();
268 $userProfilePage = '/settings/users/' . $user->id;
270 ->visit($userProfilePage)
271 ->type('newpassword', '#password')
273 ->seePageIs($userProfilePage)
274 ->see('Password confirmation required')
276 ->type('newpassword', '#password')
277 ->type('newpassword', '#password-confirm')
279 ->seePageIs('/settings/users');
281 $userPassword = User::find($user->id)->password;
282 $this->assertTrue(Hash::check('newpassword', $userPassword));
285 public function test_user_deletion()
287 $userDetails = factory(User::class)->make();
288 $user = $this->getEditor($userDetails->toArray());
291 ->visit('/settings/users/' . $user->id)
292 ->click('Delete User')
295 ->seePageIs('/settings/users')
296 ->notSeeInDatabase('users', ['name' => $user->name]);
299 public function test_user_cannot_be_deleted_if_last_admin()
301 $adminRole = Role::getRole('admin');
303 // Delete all but one admin user if there are more than one
304 $adminUsers = $adminRole->users;
305 if (count($adminUsers) > 1) {
306 foreach ($adminUsers->splice(1) as $user) {
311 // Ensure we currently only have 1 admin user
312 $this->assertEquals(1, $adminRole->users()->count());
313 $user = $adminRole->users->first();
315 $this->asAdmin()->visit('/settings/users/' . $user->id)
316 ->click('Delete User')
318 ->seePageIs('/settings/users/' . $user->id)
319 ->see('You cannot delete the only admin');
322 public function test_logout()
329 ->seePageIs('/login');
332 public function test_reset_password_flow()
334 Notification::fake();
336 $this->visit('/login')->click('Forgot Password?')
337 ->seePageIs('/password/email')
339 ->press('Send Reset Link')
340 ->see('A password reset link will be sent to
[email protected] if that email address is found in the system.');
342 $this->seeInDatabase('password_resets', [
348 Notification::assertSentTo($user, ResetPassword::class);
349 $n = Notification::sent($user, ResetPassword::class);
351 $this->visit('/password/reset/' . $n->first()->token)
352 ->see('Reset Password')
353 ->submitForm('Reset Password', [
355 'password' => 'randompass',
356 'password_confirmation' => 'randompass'
358 ->see('Your password has been successfully reset');
361 public function test_reset_password_flow_shows_success_message_even_if_wrong_password_to_prevent_user_discovery()
363 $this->visit('/login')->click('Forgot Password?')
364 ->seePageIs('/password/email')
366 ->press('Send Reset Link')
367 ->see('A password reset link will be sent to
[email protected] if that email address is found in the system.')
368 ->dontSee('We can\'t find a user');
371 $this->visit('/password/reset/arandometokenvalue')
372 ->see('Reset Password')
373 ->submitForm('Reset Password', [
375 'password' => 'randompass',
376 'password_confirmation' => 'randompass'
377 ])->followRedirects()
378 ->seePageIs('/password/reset/arandometokenvalue')
379 ->dontSee('We can\'t find a user')
380 ->see('The password reset token is invalid for this email address.');
383 public function test_reset_password_page_shows_sign_links()
385 $this->setSettings(['registration-enabled' => 'true']);
386 $this->visit('/password/email')
388 ->seeLink('Sign up');
391 public function test_login_redirects_to_initially_requested_url_correctly()
393 config()->set('app.url', 'http://localhost');
394 $page = Page::query()->first();
396 $this->visit($page->getUrl())
397 ->seePageUrlIs(url('/login'));
399 ->seePageUrlIs($page->getUrl());
402 public function test_login_intended_redirect_does_not_redirect_to_external_pages()
404 config()->set('app.url', 'http://localhost');
405 $this->setSettings(['app-public' => true]);
407 $this->get('/login', ['referer' => 'https://example.com']);
410 $login->assertRedirectedTo('http://localhost');
413 public function test_login_authenticates_admins_on_all_guards()
416 $this->assertTrue(auth()->check());
417 $this->assertTrue(auth('ldap')->check());
418 $this->assertTrue(auth('saml2')->check());
421 public function test_login_authenticates_nonadmins_on_default_guard_only()
423 $editor = $this->getEditor();
424 $editor->password = bcrypt('password');
427 $this->post('/login', ['email' => $editor->email, 'password' => 'password']);
428 $this->assertTrue(auth()->check());
429 $this->assertFalse(auth('ldap')->check());
430 $this->assertFalse(auth('saml2')->check());
433 public function test_failed_logins_are_logged_when_message_configured()
435 $log = $this->withTestLogger();
436 config()->set(['logging.failed_login.message' => 'Failed login for %u']);
448 protected function login(string $email, string $password): AuthTest
450 return $this->visit('/login')
451 ->type($email, '#email')
452 ->type($password, '#password')