5 use BookStack\Access\Mfa\MfaSession;
6 use Illuminate\Testing\TestResponse;
9 class AuthTest extends TestCase
11 public function test_auth_working()
13 $this->get('/')->assertRedirect('/login');
16 public function test_login()
21 public function test_public_viewing()
23 $this->setSettings(['app-public' => 'true']);
26 ->assertSee('Log in');
29 public function test_sign_up_link_on_login()
31 $this->get('/login')->assertDontSee('Sign up');
33 $this->setSettings(['registration-enabled' => 'true']);
35 $this->get('/login')->assertSee('Sign up');
38 public function test_logout()
40 $this->asAdmin()->get('/')->assertOk();
41 $this->post('/logout')->assertRedirect('/');
42 $this->get('/')->assertRedirect('/login');
45 public function test_mfa_session_cleared_on_logout()
47 $user = $this->users->editor();
48 $mfaSession = $this->app->make(MfaSession::class);
50 $mfaSession->markVerifiedForUser($user);
51 $this->assertTrue($mfaSession->isVerifiedForUser($user));
53 $this->asAdmin()->post('/logout');
54 $this->assertFalse($mfaSession->isVerifiedForUser($user));
57 public function test_login_redirects_to_initially_requested_url_correctly()
59 config()->set('app.url', 'http://localhost');
60 $page = $this->entities->page();
62 $this->get($page->getUrl())->assertRedirect(url('/login'));
64 ->assertRedirect($page->getUrl());
67 public function test_login_intended_redirect_does_not_redirect_to_external_pages()
69 config()->set('app.url', 'http://localhost');
70 $this->setSettings(['app-public' => true]);
72 $this->get('/login', ['referer' => 'https://example.com']);
75 $login->assertRedirect('http://localhost');
78 public function test_login_intended_redirect_does_not_factor_mfa_routes()
80 $this->get('/books')->assertRedirect('/login');
81 $this->get('/mfa/setup')->assertRedirect('/login');
83 $login->assertRedirect('/books');
86 public function test_login_authenticates_admins_on_all_guards()
89 $this->assertTrue(auth()->check());
90 $this->assertTrue(auth('ldap')->check());
91 $this->assertTrue(auth('saml2')->check());
92 $this->assertTrue(auth('oidc')->check());
95 public function test_login_authenticates_nonadmins_on_default_guard_only()
97 $editor = $this->users->editor();
98 $editor->password = bcrypt('password');
101 $this->post('/login', ['email' => $editor->email, 'password' => 'password']);
102 $this->assertTrue(auth()->check());
103 $this->assertFalse(auth('ldap')->check());
104 $this->assertFalse(auth('saml2')->check());
105 $this->assertFalse(auth('oidc')->check());
108 public function test_failed_logins_are_logged_when_message_configured()
110 $log = $this->withTestLogger();
111 config()->set(['logging.failed_login.message' => 'Failed login for %u']);
120 public function test_logged_in_user_with_unconfirmed_email_is_logged_out()
122 $this->setSettings(['registration-confirmation' => 'true']);
123 $user = $this->users->editor();
124 $user->email_confirmed = false;
127 auth()->login($user);
128 $this->assertTrue(auth()->check());
130 $this->get('/books')->assertRedirect('/');
131 $this->assertFalse(auth()->check());
134 public function test_login_attempts_are_rate_limited()
136 for ($i = 0; $i < 5; $i++) {
139 $resp = $this->followRedirects($resp);
140 $resp->assertSee('These credentials do not match our records.');
142 // Check the fifth attempt provides a lockout response
144 $resp->assertSee('Too many login attempts. Please try again in');
150 protected function login(string $email, string $password): TestResponse
152 return $this->post('/login', compact('email', 'password'));