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]);
174 ->seePageIs('/register/confirm/awaiting');
178 $this->visit('/')->seePageIs('/login')
179 ->type($user->email, '#email')
180 ->type($user->password, '#password')
182 ->seePageIs('/register/confirm/awaiting')
183 ->seeText('Email Address Not Confirmed');
186 public function test_restricted_registration_with_confirmation_disabled()
188 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'false', 'registration-restrict' => 'example.com']);
189 $user = factory(User::class)->make();
190 // Go through registration process
191 $this->visit('/register')
192 ->type($user->name, '#name')
193 ->type($user->email, '#email')
194 ->type($user->password, '#password')
195 ->press('Create Account')
196 ->seePageIs('/register')
197 ->dontSeeInDatabase('users', ['email' => $user->email])
198 ->see('That email domain does not have access to this application');
202 $this->visit('/register')
203 ->type($user->name, '#name')
204 ->type($user->email, '#email')
205 ->type($user->password, '#password')
206 ->press('Create Account')
207 ->seePageIs('/register/confirm')
208 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
211 ->seePageIs('/register/confirm/awaiting');
214 $this->visit('/')->seePageIs('/login')
215 ->type($user->email, '#email')
216 ->type($user->password, '#password')
218 ->seePageIs('/register/confirm/awaiting')
219 ->seeText('Email Address Not Confirmed');
222 public function test_user_creation()
224 $user = factory(User::class)->make();
225 $adminRole = Role::getRole('admin');
228 ->visit('/settings/users')
229 ->click('Add New User')
230 ->type($user->name, '#name')
231 ->type($user->email, '#email')
232 ->check("roles[{$adminRole->id}]")
233 ->type($user->password, '#password')
234 ->type($user->password, '#password-confirm')
236 ->seePageIs('/settings/users')
237 ->seeInDatabase('users', $user->toArray())
241 public function test_user_updating()
243 $user = $this->getNormalUser();
244 $password = $user->password;
246 ->visit('/settings/users')
248 ->seePageIs('/settings/users/' . $user->id)
250 ->type('Barry Scott', '#name')
252 ->seePageIs('/settings/users')
253 ->seeInDatabase('users', ['id' => $user->id, 'name' => 'Barry Scott', 'password' => $password])
254 ->notSeeInDatabase('users', ['name' => $user->name]);
257 public function test_user_password_update()
259 $user = $this->getNormalUser();
260 $userProfilePage = '/settings/users/' . $user->id;
262 ->visit($userProfilePage)
263 ->type('newpassword', '#password')
265 ->seePageIs($userProfilePage)
266 ->see('Password confirmation required')
268 ->type('newpassword', '#password')
269 ->type('newpassword', '#password-confirm')
271 ->seePageIs('/settings/users');
273 $userPassword = User::find($user->id)->password;
274 $this->assertTrue(Hash::check('newpassword', $userPassword));
277 public function test_user_deletion()
279 $userDetails = factory(User::class)->make();
280 $user = $this->getEditor($userDetails->toArray());
283 ->visit('/settings/users/' . $user->id)
284 ->click('Delete User')
287 ->seePageIs('/settings/users')
288 ->notSeeInDatabase('users', ['name' => $user->name]);
291 public function test_user_cannot_be_deleted_if_last_admin()
293 $adminRole = Role::getRole('admin');
295 // Delete all but one admin user if there are more than one
296 $adminUsers = $adminRole->users;
297 if (count($adminUsers) > 1) {
298 foreach ($adminUsers->splice(1) as $user) {
303 // Ensure we currently only have 1 admin user
304 $this->assertEquals(1, $adminRole->users()->count());
305 $user = $adminRole->users->first();
307 $this->asAdmin()->visit('/settings/users/' . $user->id)
308 ->click('Delete User')
310 ->seePageIs('/settings/users/' . $user->id)
311 ->see('You cannot delete the only admin');
314 public function test_logout()
321 ->seePageIs('/login');
324 public function test_reset_password_flow()
326 Notification::fake();
328 $this->visit('/login')->click('Forgot Password?')
329 ->seePageIs('/password/email')
331 ->press('Send Reset Link')
332 ->see('A password reset link will be sent to
[email protected] if that email address is found in the system.');
334 $this->seeInDatabase('password_resets', [
340 Notification::assertSentTo($user, ResetPassword::class);
341 $n = Notification::sent($user, ResetPassword::class);
343 $this->visit('/password/reset/' . $n->first()->token)
344 ->see('Reset Password')
345 ->submitForm('Reset Password', [
347 'password' => 'randompass',
348 'password_confirmation' => 'randompass'
350 ->see('Your password has been successfully reset');
353 public function test_reset_password_flow_shows_success_message_even_if_wrong_password_to_prevent_user_discovery()
355 $this->visit('/login')->click('Forgot Password?')
356 ->seePageIs('/password/email')
358 ->press('Send Reset Link')
359 ->see('A password reset link will be sent to
[email protected] if that email address is found in the system.')
360 ->dontSee('We can\'t find a user');
363 $this->visit('/password/reset/arandometokenvalue')
364 ->see('Reset Password')
365 ->submitForm('Reset Password', [
367 'password' => 'randompass',
368 'password_confirmation' => 'randompass'
369 ])->followRedirects()
370 ->seePageIs('/password/reset/arandometokenvalue')
371 ->dontSee('We can\'t find a user')
372 ->see('The password reset token is invalid for this email address.');
375 public function test_reset_password_page_shows_sign_links()
377 $this->setSettings(['registration-enabled' => 'true']);
378 $this->visit('/password/email')
380 ->seeLink('Sign up');
383 public function test_login_redirects_to_initially_requested_url_correctly()
385 config()->set('app.url', 'http://localhost');
386 $page = Page::query()->first();
388 $this->visit($page->getUrl())
389 ->seePageUrlIs(url('/login'));
391 ->seePageUrlIs($page->getUrl());
394 public function test_login_intended_redirect_does_not_redirect_to_external_pages()
396 config()->set('app.url', 'http://localhost');
397 $this->setSettings(['app-public' => true]);
399 $this->get('/login', ['referer' => 'https://example.com']);
402 $login->assertRedirectedTo('http://localhost');
405 public function test_login_authenticates_admins_on_all_guards()
408 $this->assertTrue(auth()->check());
409 $this->assertTrue(auth('ldap')->check());
410 $this->assertTrue(auth('saml2')->check());
413 public function test_login_authenticates_nonadmins_on_default_guard_only()
415 $editor = $this->getEditor();
416 $editor->password = bcrypt('password');
419 $this->post('/login', ['email' => $editor->email, 'password' => 'password']);
420 $this->assertTrue(auth()->check());
421 $this->assertFalse(auth('ldap')->check());
422 $this->assertFalse(auth('saml2')->check());
425 public function test_failed_logins_are_logged_when_message_configured()
427 $log = $this->withTestLogger();
428 config()->set(['logging.failed_login.message' => 'Failed login for %u']);
440 protected function login(string $email, string $password): AuthTest
442 return $this->visit('/login')
443 ->type($email, '#email')
444 ->type($password, '#password')