1 <?php namespace Tests\Auth;
3 use BookStack\Auth\Role;
4 use BookStack\Auth\User;
5 use BookStack\Entities\Page;
6 use BookStack\Notifications\ConfirmEmail;
7 use BookStack\Notifications\ResetPassword;
8 use BookStack\Settings\SettingService;
11 use Illuminate\Support\Facades\Notification;
12 use Tests\BrowserKitTest;
14 class AuthTest extends BrowserKitTest
17 public function test_auth_working()
20 ->seePageIs('/login');
23 public function test_login()
29 public function test_public_viewing()
31 $settings = app(SettingService::class);
32 $settings->put('app-public', 'true');
38 public function test_registration_showing()
40 // Ensure registration form is showing
41 $this->setSettings(['registration-enabled' => 'true']);
42 $this->visit('/login')
45 ->seePageIs('/register');
48 public function test_normal_registration()
50 // Set settings and get user instance
51 $this->setSettings(['registration-enabled' => 'true']);
52 $user = factory(User::class)->make();
54 // Test form and ensure user is created
55 $this->visit('/register')
57 ->type($user->name, '#name')
58 ->type($user->email, '#email')
59 ->type($user->password, '#password')
60 ->press('Create Account')
63 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email]);
66 public function test_empty_registration_redirects_back_with_errors()
68 // Set settings and get user instance
69 $this->setSettings(['registration-enabled' => 'true']);
71 // Test form and ensure user is created
72 $this->visit('/register')
73 ->press('Create Account')
74 ->see('The name field is required')
75 ->seePageIs('/register');
78 public function test_registration_validation()
80 $this->setSettings(['registration-enabled' => 'true']);
82 $this->visit('/register')
85 ->type('1', '#password')
86 ->press('Create Account')
87 ->see('The name must be at least 2 characters.')
88 ->see('The email must be a valid email address.')
89 ->see('The password must be at least 8 characters.')
90 ->seePageIs('/register');
93 public function test_sign_up_link_on_login()
95 $this->visit('/login')
98 $this->setSettings(['registration-enabled' => 'true']);
100 $this->visit('/login')
104 public function test_confirmed_registration()
106 // Fake notifications
107 Notification::fake();
109 // Set settings and get user instance
110 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true']);
111 $user = factory(User::class)->make();
113 // Go through registration process
114 $this->visit('/register')
116 ->type($user->name, '#name')
117 ->type($user->email, '#email')
118 ->type($user->password, '#password')
119 ->press('Create Account')
120 ->seePageIs('/register/confirm')
121 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
123 // Ensure notification sent
124 $dbUser = User::where('email', '=', $user->email)->first();
125 Notification::assertSentTo($dbUser, ConfirmEmail::class);
127 // Test access and resend confirmation email
128 $this->login($user->email, $user->password)
129 ->seePageIs('/register/confirm/awaiting')
132 ->seePageIs('/register/confirm/awaiting')
133 ->press('Resend Confirmation Email');
135 // Get confirmation and confirm notification matches
136 $emailConfirmation = DB::table('email_confirmations')->where('user_id', '=', $dbUser->id)->first();
137 Notification::assertSentTo($dbUser, ConfirmEmail::class, function($notification, $channels) use ($emailConfirmation) {
138 return $notification->token === $emailConfirmation->token;
141 // Check confirmation email confirmation activation.
142 $this->visit('/register/confirm/' . $emailConfirmation->token)
145 ->notSeeInDatabase('email_confirmations', ['token' => $emailConfirmation->token])
146 ->seeInDatabase('users', ['name' => $dbUser->name, 'email' => $dbUser->email, 'email_confirmed' => true]);
149 public function test_restricted_registration()
151 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true', 'registration-restrict' => 'example.com']);
152 $user = factory(User::class)->make();
153 // Go through registration process
154 $this->visit('/register')
155 ->type($user->name, '#name')
156 ->type($user->email, '#email')
157 ->type($user->password, '#password')
158 ->press('Create Account')
159 ->seePageIs('/register')
160 ->dontSeeInDatabase('users', ['email' => $user->email])
161 ->see('That email domain does not have access to this application');
165 $this->visit('/register')
166 ->type($user->name, '#name')
167 ->type($user->email, '#email')
168 ->type($user->password, '#password')
169 ->press('Create Account')
170 ->seePageIs('/register/confirm')
171 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
173 $this->visit('/')->seePageIs('/login')
174 ->type($user->email, '#email')
175 ->type($user->password, '#password')
177 ->seePageIs('/register/confirm/awaiting')
178 ->seeText('Email Address Not Confirmed');
181 public function test_restricted_registration_with_confirmation_disabled()
183 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'false', 'registration-restrict' => 'example.com']);
184 $user = factory(User::class)->make();
185 // Go through registration process
186 $this->visit('/register')
187 ->type($user->name, '#name')
188 ->type($user->email, '#email')
189 ->type($user->password, '#password')
190 ->press('Create Account')
191 ->seePageIs('/register')
192 ->dontSeeInDatabase('users', ['email' => $user->email])
193 ->see('That email domain does not have access to this application');
197 $this->visit('/register')
198 ->type($user->name, '#name')
199 ->type($user->email, '#email')
200 ->type($user->password, '#password')
201 ->press('Create Account')
202 ->seePageIs('/register/confirm')
203 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
205 $this->visit('/')->seePageIs('/login')
206 ->type($user->email, '#email')
207 ->type($user->password, '#password')
209 ->seePageIs('/register/confirm/awaiting')
210 ->seeText('Email Address Not Confirmed');
213 public function test_user_creation()
215 $user = factory(User::class)->make();
216 $adminRole = Role::getRole('admin');
219 ->visit('/settings/users')
220 ->click('Add New User')
221 ->type($user->name, '#name')
222 ->type($user->email, '#email')
223 ->check("roles[{$adminRole->id}]")
224 ->type($user->password, '#password')
225 ->type($user->password, '#password-confirm')
227 ->seePageIs('/settings/users')
228 ->seeInDatabase('users', $user->toArray())
232 public function test_user_updating()
234 $user = $this->getNormalUser();
235 $password = $user->password;
237 ->visit('/settings/users')
239 ->seePageIs('/settings/users/' . $user->id)
241 ->type('Barry Scott', '#name')
243 ->seePageIs('/settings/users')
244 ->seeInDatabase('users', ['id' => $user->id, 'name' => 'Barry Scott', 'password' => $password])
245 ->notSeeInDatabase('users', ['name' => $user->name]);
248 public function test_user_password_update()
250 $user = $this->getNormalUser();
251 $userProfilePage = '/settings/users/' . $user->id;
253 ->visit($userProfilePage)
254 ->type('newpassword', '#password')
256 ->seePageIs($userProfilePage)
257 ->see('Password confirmation required')
259 ->type('newpassword', '#password')
260 ->type('newpassword', '#password-confirm')
262 ->seePageIs('/settings/users');
264 $userPassword = User::find($user->id)->password;
265 $this->assertTrue(Hash::check('newpassword', $userPassword));
268 public function test_user_deletion()
270 $userDetails = factory(User::class)->make();
271 $user = $this->getEditor($userDetails->toArray());
274 ->visit('/settings/users/' . $user->id)
275 ->click('Delete User')
278 ->seePageIs('/settings/users')
279 ->notSeeInDatabase('users', ['name' => $user->name]);
282 public function test_user_cannot_be_deleted_if_last_admin()
284 $adminRole = Role::getRole('admin');
286 // Delete all but one admin user if there are more than one
287 $adminUsers = $adminRole->users;
288 if (count($adminUsers) > 1) {
289 foreach ($adminUsers->splice(1) as $user) {
294 // Ensure we currently only have 1 admin user
295 $this->assertEquals(1, $adminRole->users()->count());
296 $user = $adminRole->users->first();
298 $this->asAdmin()->visit('/settings/users/' . $user->id)
299 ->click('Delete User')
301 ->seePageIs('/settings/users/' . $user->id)
302 ->see('You cannot delete the only admin');
305 public function test_logout()
312 ->seePageIs('/login');
315 public function test_reset_password_flow()
317 Notification::fake();
319 $this->visit('/login')->click('Forgot Password?')
320 ->seePageIs('/password/email')
322 ->press('Send Reset Link')
323 ->see('A password reset link will be sent to
[email protected] if that email address is found in the system.');
325 $this->seeInDatabase('password_resets', [
331 Notification::assertSentTo($user, ResetPassword::class);
332 $n = Notification::sent($user, ResetPassword::class);
334 $this->visit('/password/reset/' . $n->first()->token)
335 ->see('Reset Password')
336 ->submitForm('Reset Password', [
338 'password' => 'randompass',
339 'password_confirmation' => 'randompass'
341 ->see('Your password has been successfully reset');
344 public function test_reset_password_flow_shows_success_message_even_if_wrong_password_to_prevent_user_discovery()
346 $this->visit('/login')->click('Forgot Password?')
347 ->seePageIs('/password/email')
349 ->press('Send Reset Link')
350 ->see('A password reset link will be sent to
[email protected] if that email address is found in the system.')
351 ->dontSee('We can\'t find a user');
354 $this->visit('/password/reset/arandometokenvalue')
355 ->see('Reset Password')
356 ->submitForm('Reset Password', [
358 'password' => 'randompass',
359 'password_confirmation' => 'randompass'
360 ])->followRedirects()
361 ->seePageIs('/password/reset/arandometokenvalue')
362 ->dontSee('We can\'t find a user')
363 ->see('The password reset token is invalid for this email address.');
366 public function test_reset_password_page_shows_sign_links()
368 $this->setSettings(['registration-enabled' => 'true']);
369 $this->visit('/password/email')
371 ->seeLink('Sign up');
374 public function test_login_redirects_to_initially_requested_url_correctly()
376 config()->set('app.url', 'http://localhost');
377 $page = Page::query()->first();
379 $this->visit($page->getUrl())
380 ->seePageUrlIs(url('/login'));
382 ->seePageUrlIs($page->getUrl());
385 public function test_login_intended_redirect_does_not_redirect_to_external_pages()
387 config()->set('app.url', 'http://localhost');
388 $this->setSettings(['app-public' => true]);
390 $this->get('/login', ['referer' => 'https://example.com']);
393 $login->assertRedirectedTo('http://localhost');
396 public function test_login_authenticates_admins_on_all_guards()
399 $this->assertTrue(auth()->check());
400 $this->assertTrue(auth('ldap')->check());
401 $this->assertTrue(auth('saml2')->check());
404 public function test_login_authenticates_nonadmins_on_default_guard_only()
406 $editor = $this->getEditor();
407 $editor->password = bcrypt('password');
410 $this->post('/login', ['email' => $editor->email, 'password' => 'password']);
411 $this->assertTrue(auth()->check());
412 $this->assertFalse(auth('ldap')->check());
413 $this->assertFalse(auth('saml2')->check());
416 public function test_failed_logins_are_logged_when_message_configured()
418 $log = $this->withTestLogger();
419 config()->set(['logging.failed_login.message' => 'Failed login for %u']);
431 protected function login(string $email, string $password): AuthTest
433 return $this->visit('/login')
434 ->type($email, '#email')
435 ->type($password, '#password')