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();
218 ->visit('/settings/users')
219 ->click('Add New User')
220 ->type($user->name, '#name')
221 ->type($user->email, '#email')
222 ->check('roles[admin]')
223 ->type($user->password, '#password')
224 ->type($user->password, '#password-confirm')
226 ->seePageIs('/settings/users')
227 ->seeInDatabase('users', $user->toArray())
231 public function test_user_updating()
233 $user = $this->getNormalUser();
234 $password = $user->password;
236 ->visit('/settings/users')
238 ->seePageIs('/settings/users/' . $user->id)
240 ->type('Barry Scott', '#name')
242 ->seePageIs('/settings/users')
243 ->seeInDatabase('users', ['id' => $user->id, 'name' => 'Barry Scott', 'password' => $password])
244 ->notSeeInDatabase('users', ['name' => $user->name]);
247 public function test_user_password_update()
249 $user = $this->getNormalUser();
250 $userProfilePage = '/settings/users/' . $user->id;
252 ->visit($userProfilePage)
253 ->type('newpassword', '#password')
255 ->seePageIs($userProfilePage)
256 ->see('Password confirmation required')
258 ->type('newpassword', '#password')
259 ->type('newpassword', '#password-confirm')
261 ->seePageIs('/settings/users');
263 $userPassword = User::find($user->id)->password;
264 $this->assertTrue(Hash::check('newpassword', $userPassword));
267 public function test_user_deletion()
269 $userDetails = factory(User::class)->make();
270 $user = $this->getEditor($userDetails->toArray());
273 ->visit('/settings/users/' . $user->id)
274 ->click('Delete User')
277 ->seePageIs('/settings/users')
278 ->notSeeInDatabase('users', ['name' => $user->name]);
281 public function test_user_cannot_be_deleted_if_last_admin()
283 $adminRole = Role::getRole('admin');
285 // Delete all but one admin user if there are more than one
286 $adminUsers = $adminRole->users;
287 if (count($adminUsers) > 1) {
288 foreach ($adminUsers->splice(1) as $user) {
293 // Ensure we currently only have 1 admin user
294 $this->assertEquals(1, $adminRole->users()->count());
295 $user = $adminRole->users->first();
297 $this->asAdmin()->visit('/settings/users/' . $user->id)
298 ->click('Delete User')
300 ->seePageIs('/settings/users/' . $user->id)
301 ->see('You cannot delete the only admin');
304 public function test_logout()
311 ->seePageIs('/login');
314 public function test_reset_password_flow()
316 Notification::fake();
318 $this->visit('/login')->click('Forgot Password?')
319 ->seePageIs('/password/email')
321 ->press('Send Reset Link')
322 ->see('A password reset link will be sent to
[email protected] if that email address is found in the system.');
324 $this->seeInDatabase('password_resets', [
330 Notification::assertSentTo($user, ResetPassword::class);
331 $n = Notification::sent($user, ResetPassword::class);
333 $this->visit('/password/reset/' . $n->first()->token)
334 ->see('Reset Password')
335 ->submitForm('Reset Password', [
337 'password' => 'randompass',
338 'password_confirmation' => 'randompass'
340 ->see('Your password has been successfully reset');
343 public function test_reset_password_flow_shows_success_message_even_if_wrong_password_to_prevent_user_discovery()
345 $this->visit('/login')->click('Forgot Password?')
346 ->seePageIs('/password/email')
348 ->press('Send Reset Link')
349 ->see('A password reset link will be sent to
[email protected] if that email address is found in the system.')
350 ->dontSee('We can\'t find a user');
353 $this->visit('/password/reset/arandometokenvalue')
354 ->see('Reset Password')
355 ->submitForm('Reset Password', [
357 'password' => 'randompass',
358 'password_confirmation' => 'randompass'
359 ])->followRedirects()
360 ->seePageIs('/password/reset/arandometokenvalue')
361 ->dontSee('We can\'t find a user')
362 ->see('The password reset token is invalid for this email address.');
365 public function test_reset_password_page_shows_sign_links()
367 $this->setSettings(['registration-enabled' => 'true']);
368 $this->visit('/password/email')
370 ->seeLink('Sign up');
373 public function test_login_redirects_to_initially_requested_url_correctly()
375 config()->set('app.url', 'http://localhost');
376 $page = Page::query()->first();
378 $this->visit($page->getUrl())
379 ->seePageUrlIs(url('/login'));
381 ->seePageUrlIs($page->getUrl());
384 public function test_login_authenticates_admins_on_all_guards()
387 $this->assertTrue(auth()->check());
388 $this->assertTrue(auth('ldap')->check());
389 $this->assertTrue(auth('saml2')->check());
390 $this->assertTrue(auth('openid')->check());
393 public function test_login_authenticates_nonadmins_on_default_guard_only()
395 $editor = $this->getEditor();
396 $editor->password = bcrypt('password');
399 $this->post('/login', ['email' => $editor->email, 'password' => 'password']);
400 $this->assertTrue(auth()->check());
401 $this->assertFalse(auth('ldap')->check());
402 $this->assertFalse(auth('saml2')->check());
403 $this->assertFalse(auth('openid')->check());
409 protected function login(string $email, string $password): AuthTest
411 return $this->visit('/login')
412 ->type($email, '#email')
413 ->type($password, '#password')