5 use BookStack\Auth\Access\Mfa\MfaSession;
6 use BookStack\Auth\User;
7 use BookStack\Entities\Models\Page;
8 use BookStack\Notifications\ConfirmEmail;
9 use BookStack\Notifications\ResetPassword;
10 use Illuminate\Support\Facades\DB;
11 use Illuminate\Support\Facades\Notification;
13 use Tests\TestResponse;
15 class AuthTest extends TestCase
17 public function test_auth_working()
19 $this->get('/')->assertRedirect('/login');
22 public function test_login()
27 public function test_public_viewing()
29 $this->setSettings(['app-public' => 'true']);
32 ->assertSee('Log in');
35 public function test_registration_showing()
37 // Ensure registration form is showing
38 $this->setSettings(['registration-enabled' => 'true']);
40 ->assertElementContains('a[href="' . url('/register') . '"]', 'Sign up');
43 public function test_normal_registration()
45 // Set settings and get user instance
46 $this->setSettings(['registration-enabled' => 'true']);
47 $user = factory(User::class)->make();
49 // Test form and ensure user is created
50 $this->get('/register')
51 ->assertSee('Sign Up')
52 ->assertElementContains('form[action="' . url('/register') . '"]', 'Create Account');
54 $resp = $this->post('/register', $user->only('password', 'name', 'email'));
55 $resp->assertRedirect('/');
57 $resp = $this->get('/');
59 $resp->assertSee($user->name);
60 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email]);
63 public function test_empty_registration_redirects_back_with_errors()
65 // Set settings and get user instance
66 $this->setSettings(['registration-enabled' => 'true']);
68 // Test form and ensure user is created
69 $this->get('/register');
70 $this->post('/register', [])->assertRedirect('/register');
71 $this->get('/register')->assertSee('The name field is required');
74 public function test_registration_validation()
76 $this->setSettings(['registration-enabled' => 'true']);
78 $this->get('/register');
79 $resp = $this->followingRedirects()->post('/register', [
84 $resp->assertSee('The name must be at least 2 characters.');
85 $resp->assertSee('The email must be a valid email address.');
86 $resp->assertSee('The password must be at least 8 characters.');
89 public function test_sign_up_link_on_login()
91 $this->get('/login')->assertDontSee('Sign up');
93 $this->setSettings(['registration-enabled' => 'true']);
95 $this->get('/login')->assertSee('Sign up');
98 public function test_confirmed_registration()
100 // Fake notifications
101 Notification::fake();
103 // Set settings and get user instance
104 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true']);
105 $user = factory(User::class)->make();
107 // Go through registration process
108 $resp = $this->post('/register', $user->only('name', 'email', 'password'));
109 $resp->assertRedirect('/register/confirm');
110 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
112 // Ensure notification sent
113 /** @var User $dbUser */
114 $dbUser = User::query()->where('email', '=', $user->email)->first();
115 Notification::assertSentTo($dbUser, ConfirmEmail::class);
117 // Test access and resend confirmation email
118 $resp = $this->login($user->email, $user->password);
119 $resp->assertRedirect('/register/confirm/awaiting');
121 $resp = $this->get('/register/confirm/awaiting');
122 $resp->assertElementContains('form[action="' . url('/register/confirm/resend') . '"]', 'Resend');
124 $this->get('/books')->assertRedirect('/login');
125 $this->post('/register/confirm/resend', $user->only('email'));
127 // Get confirmation and confirm notification matches
128 $emailConfirmation = DB::table('email_confirmations')->where('user_id', '=', $dbUser->id)->first();
129 Notification::assertSentTo($dbUser, ConfirmEmail::class, function ($notification, $channels) use ($emailConfirmation) {
130 return $notification->token === $emailConfirmation->token;
133 // Check confirmation email confirmation activation.
134 $this->get('/register/confirm/' . $emailConfirmation->token)->assertRedirect('/');
135 $this->get('/')->assertSee($user->name);
136 $this->assertDatabaseMissing('email_confirmations', ['token' => $emailConfirmation->token]);
137 $this->assertDatabaseHas('users', ['name' => $dbUser->name, 'email' => $dbUser->email, 'email_confirmed' => true]);
140 public function test_restricted_registration()
142 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true', 'registration-restrict' => 'example.com']);
143 $user = factory(User::class)->make();
145 // Go through registration process
146 $this->post('/register', $user->only('name', 'email', 'password'))
147 ->assertRedirect('/register');
148 $resp = $this->get('/register');
149 $resp->assertSee('That email domain does not have access to this application');
150 $this->assertDatabaseMissing('users', $user->only('email'));
154 $this->post('/register', $user->only('name', 'email', 'password'))
155 ->assertRedirect('/register/confirm');
156 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
158 $this->assertNull(auth()->user());
160 $this->get('/')->assertRedirect('/login');
161 $resp = $this->followingRedirects()->post('/login', $user->only('email', 'password'));
162 $resp->assertSee('Email Address Not Confirmed');
163 $this->assertNull(auth()->user());
166 public function test_restricted_registration_with_confirmation_disabled()
168 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'false', 'registration-restrict' => 'example.com']);
169 $user = factory(User::class)->make();
171 // Go through registration process
172 $this->post('/register', $user->only('name', 'email', 'password'))
173 ->assertRedirect('/register');
174 $this->assertDatabaseMissing('users', $user->only('email'));
175 $this->get('/register')->assertSee('That email domain does not have access to this application');
179 $this->post('/register', $user->only('name', 'email', 'password'))
180 ->assertRedirect('/register/confirm');
181 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
183 $this->assertNull(auth()->user());
185 $this->get('/')->assertRedirect('/login');
186 $resp = $this->post('/login', $user->only('email', 'password'));
187 $resp->assertRedirect('/register/confirm/awaiting');
188 $this->get('/register/confirm/awaiting')->assertSee('Email Address Not Confirmed');
189 $this->assertNull(auth()->user());
192 public function test_logout()
194 $this->asAdmin()->get('/')->assertOk();
195 $this->get('/logout')->assertRedirect('/');
196 $this->get('/')->assertRedirect('/login');
199 public function test_mfa_session_cleared_on_logout()
201 $user = $this->getEditor();
202 $mfaSession = $this->app->make(MfaSession::class);
204 $mfaSession->markVerifiedForUser($user);
205 $this->assertTrue($mfaSession->isVerifiedForUser($user));
207 $this->asAdmin()->get('/logout');
208 $this->assertFalse($mfaSession->isVerifiedForUser($user));
211 public function test_reset_password_flow()
213 Notification::fake();
216 ->assertElementContains('a[href="' . url('/password/email') . '"]', 'Forgot Password?');
218 $this->get('/password/email')
219 ->assertElementContains('form[action="' . url('/password/email') . '"]', 'Send Reset Link');
221 $resp = $this->post('/password/email', [
224 $resp->assertRedirect('/password/email');
226 $resp = $this->get('/password/email');
227 $resp->assertSee('A password reset link will be sent to
[email protected] if that email address is found in the system.');
229 $this->assertDatabaseHas('password_resets', [
233 /** @var User $user */
236 Notification::assertSentTo($user, ResetPassword::class);
237 $n = Notification::sent($user, ResetPassword::class);
239 $this->get('/password/reset/' . $n->first()->token)
241 ->assertSee('Reset Password');
243 $resp = $this->post('/password/reset', [
245 'password' => 'randompass',
246 'password_confirmation' => 'randompass',
247 'token' => $n->first()->token,
249 $resp->assertRedirect('/');
251 $this->get('/')->assertSee('Your password has been successfully reset');
254 public function test_reset_password_flow_shows_success_message_even_if_wrong_password_to_prevent_user_discovery()
256 $this->get('/password/email');
257 $resp = $this->followingRedirects()->post('/password/email', [
260 $resp->assertSee('A password reset link will be sent to
[email protected] if that email address is found in the system.');
261 $resp->assertDontSee('We can\'t find a user');
263 $this->get('/password/reset/arandometokenvalue')->assertSee('Reset Password');
264 $resp = $this->post('/password/reset', [
266 'password' => 'randompass',
267 'password_confirmation' => 'randompass',
268 'token' => 'arandometokenvalue',
270 $resp->assertRedirect('/password/reset/arandometokenvalue');
272 $this->get('/password/reset/arandometokenvalue')
273 ->assertDontSee('We can\'t find a user')
274 ->assertSee('The password reset token is invalid for this email address.');
277 public function test_reset_password_page_shows_sign_links()
279 $this->setSettings(['registration-enabled' => 'true']);
280 $this->get('/password/email')
281 ->assertElementContains('a', 'Log in')
282 ->assertElementContains('a', 'Sign up');
285 public function test_login_redirects_to_initially_requested_url_correctly()
287 config()->set('app.url', 'http://localhost');
288 /** @var Page $page */
289 $page = Page::query()->first();
291 $this->get($page->getUrl())->assertRedirect(url('/login'));
293 ->assertRedirect($page->getUrl());
296 public function test_login_intended_redirect_does_not_redirect_to_external_pages()
298 config()->set('app.url', 'http://localhost');
299 $this->setSettings(['app-public' => true]);
301 $this->get('/login', ['referer' => 'https://example.com']);
304 $login->assertRedirect('http://localhost');
307 public function test_login_intended_redirect_does_not_factor_mfa_routes()
309 $this->get('/books')->assertRedirect('/login');
310 $this->get('/mfa/setup')->assertRedirect('/login');
312 $login->assertRedirect('/books');
315 public function test_login_authenticates_admins_on_all_guards()
318 $this->assertTrue(auth()->check());
319 $this->assertTrue(auth('ldap')->check());
320 $this->assertTrue(auth('saml2')->check());
323 public function test_login_authenticates_nonadmins_on_default_guard_only()
325 $editor = $this->getEditor();
326 $editor->password = bcrypt('password');
329 $this->post('/login', ['email' => $editor->email, 'password' => 'password']);
330 $this->assertTrue(auth()->check());
331 $this->assertFalse(auth('ldap')->check());
332 $this->assertFalse(auth('saml2')->check());
335 public function test_failed_logins_are_logged_when_message_configured()
337 $log = $this->withTestLogger();
338 config()->set(['logging.failed_login.message' => 'Failed login for %u']);
347 public function test_logged_in_user_with_unconfirmed_email_is_logged_out()
349 $this->setSettings(['registration-confirmation' => 'true']);
350 $user = $this->getEditor();
351 $user->email_confirmed = false;
354 auth()->login($user);
355 $this->assertTrue(auth()->check());
357 $this->get('/books')->assertRedirect('/');
358 $this->assertFalse(auth()->check());
364 protected function login(string $email, string $password): TestResponse
366 return $this->post('/login', compact('email', 'password'));