5 use BookStack\Notifications\ResetPassword;
6 use BookStack\Users\Models\User;
7 use Illuminate\Support\Facades\Notification;
10 class ResetPasswordTest extends TestCase
12 public function test_reset_flow()
16 $resp = $this->get('/login');
17 $this->withHtml($resp)->assertElementContains('a[href="' . url('/password/email') . '"]', 'Forgot Password?');
19 $resp = $this->get('/password/email');
20 $this->withHtml($resp)->assertElementContains('form[action="' . url('/password/email') . '"]', 'Send Reset Link');
22 $resp = $this->post('/password/email', [
25 $resp->assertRedirect('/password/email');
27 $resp = $this->get('/password/email');
28 $resp->assertSee('A password reset link will be sent to
[email protected] if that email address is found in the system.');
30 $this->assertDatabaseHas('password_resets', [
34 /** @var User $user */
37 Notification::assertSentTo($user, ResetPassword::class);
38 $n = Notification::sent($user, ResetPassword::class);
40 $this->get('/password/reset/' . $n->first()->token)
42 ->assertSee('Reset Password');
44 $resp = $this->post('/password/reset', [
46 'password' => 'randompass',
47 'password_confirmation' => 'randompass',
48 'token' => $n->first()->token,
50 $resp->assertRedirect('/');
52 $this->get('/')->assertSee('Your password has been successfully reset');
55 public function test_reset_flow_shows_success_message_even_if_wrong_password_to_prevent_user_discovery()
57 $this->get('/password/email');
58 $resp = $this->followingRedirects()->post('/password/email', [
61 $resp->assertSee('A password reset link will be sent to
[email protected] if that email address is found in the system.');
62 $resp->assertDontSee('We can\'t find a user');
64 $this->get('/password/reset/arandometokenvalue')->assertSee('Reset Password');
65 $resp = $this->post('/password/reset', [
67 'password' => 'randompass',
68 'password_confirmation' => 'randompass',
69 'token' => 'arandometokenvalue',
71 $resp->assertRedirect('/password/reset/arandometokenvalue');
73 $this->get('/password/reset/arandometokenvalue')
74 ->assertDontSee('We can\'t find a user')
75 ->assertSee('The password reset token is invalid for this email address.');
78 public function test_reset_page_shows_sign_links()
80 $this->setSettings(['registration-enabled' => 'true']);
81 $resp = $this->get('/password/email');
82 $this->withHtml($resp)->assertElementContains('a', 'Log in')
83 ->assertElementContains('a', 'Sign up');
86 public function test_reset_request_is_throttled()
88 $editor = $this->users->editor();
90 $this->get('/password/email');
91 $this->followingRedirects()->post('/password/email', [
92 'email' => $editor->email,
95 $resp = $this->followingRedirects()->post('/password/email', [
96 'email' => $editor->email,
98 Notification::assertTimesSent(1, ResetPassword::class);
99 $resp->assertSee('A password reset link will be sent to ' . $editor->email . ' if that email address is found in the system.');