5 use BookStack\Auth\Access\Mfa\MfaSession;
6 use BookStack\Entities\Models\Page;
7 use Illuminate\Testing\TestResponse;
10 class AuthTest extends TestCase
12 public function test_auth_working()
14 $this->get('/')->assertRedirect('/login');
17 public function test_login()
22 public function test_public_viewing()
24 $this->setSettings(['app-public' => 'true']);
27 ->assertSee('Log in');
30 public function test_sign_up_link_on_login()
32 $this->get('/login')->assertDontSee('Sign up');
34 $this->setSettings(['registration-enabled' => 'true']);
36 $this->get('/login')->assertSee('Sign up');
39 public function test_logout()
41 $this->asAdmin()->get('/')->assertOk();
42 $this->post('/logout')->assertRedirect('/');
43 $this->get('/')->assertRedirect('/login');
46 public function test_mfa_session_cleared_on_logout()
48 $user = $this->getEditor();
49 $mfaSession = $this->app->make(MfaSession::class);
51 $mfaSession->markVerifiedForUser($user);
52 $this->assertTrue($mfaSession->isVerifiedForUser($user));
54 $this->asAdmin()->post('/logout');
55 $this->assertFalse($mfaSession->isVerifiedForUser($user));
58 public function test_login_redirects_to_initially_requested_url_correctly()
60 config()->set('app.url', 'http://localhost');
61 /** @var Page $page */
62 $page = Page::query()->first();
64 $this->get($page->getUrl())->assertRedirect(url('/login'));
66 ->assertRedirect($page->getUrl());
69 public function test_login_intended_redirect_does_not_redirect_to_external_pages()
71 config()->set('app.url', 'http://localhost');
72 $this->setSettings(['app-public' => true]);
74 $this->get('/login', ['referer' => 'https://example.com']);
77 $login->assertRedirect('http://localhost');
80 public function test_login_intended_redirect_does_not_factor_mfa_routes()
82 $this->get('/books')->assertRedirect('/login');
83 $this->get('/mfa/setup')->assertRedirect('/login');
85 $login->assertRedirect('/books');
88 public function test_login_authenticates_admins_on_all_guards()
91 $this->assertTrue(auth()->check());
92 $this->assertTrue(auth('ldap')->check());
93 $this->assertTrue(auth('saml2')->check());
94 $this->assertTrue(auth('oidc')->check());
97 public function test_login_authenticates_nonadmins_on_default_guard_only()
99 $editor = $this->getEditor();
100 $editor->password = bcrypt('password');
103 $this->post('/login', ['email' => $editor->email, 'password' => 'password']);
104 $this->assertTrue(auth()->check());
105 $this->assertFalse(auth('ldap')->check());
106 $this->assertFalse(auth('saml2')->check());
107 $this->assertFalse(auth('oidc')->check());
110 public function test_failed_logins_are_logged_when_message_configured()
112 $log = $this->withTestLogger();
113 config()->set(['logging.failed_login.message' => 'Failed login for %u']);
122 public function test_logged_in_user_with_unconfirmed_email_is_logged_out()
124 $this->setSettings(['registration-confirmation' => 'true']);
125 $user = $this->getEditor();
126 $user->email_confirmed = false;
129 auth()->login($user);
130 $this->assertTrue(auth()->check());
132 $this->get('/books')->assertRedirect('/');
133 $this->assertFalse(auth()->check());
136 public function test_login_attempts_are_rate_limited()
138 for ($i = 0; $i < 5; $i++) {
141 $resp = $this->followRedirects($resp);
142 $resp->assertSee('These credentials do not match our records.');
144 // Check the fifth attempt provides a lockout response
146 $resp->assertSee('Too many login attempts. Please try again in');
152 protected function login(string $email, string $password): TestResponse
154 return $this->post('/login', compact('email', 'password'));