3 use BookStack\Notifications\ConfirmEmail;
4 use Illuminate\Support\Facades\Notification;
6 class AuthTest extends BrowserKitTest
9 public function test_auth_working()
12 ->seePageIs('/login');
15 public function test_login()
21 public function test_public_viewing()
23 $settings = app('BookStack\Services\SettingService');
24 $settings->put('app-public', 'true');
30 public function test_registration_showing()
32 // Ensure registration form is showing
33 $this->setSettings(['registration-enabled' => 'true']);
34 $this->visit('/login')
37 ->seePageIs('/register');
40 public function test_normal_registration()
42 // Set settings and get user instance
43 $this->setSettings(['registration-enabled' => 'true']);
44 $user = factory(\BookStack\User::class)->make();
46 // Test form and ensure user is created
47 $this->visit('/register')
49 ->type($user->name, '#name')
50 ->type($user->email, '#email')
51 ->type($user->password, '#password')
52 ->press('Create Account')
55 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email]);
59 public function test_confirmed_registration()
64 // Set settings and get user instance
65 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true']);
66 $user = factory(\BookStack\User::class)->make();
68 // Go through registration process
69 $this->visit('/register')
71 ->type($user->name, '#name')
72 ->type($user->email, '#email')
73 ->type($user->password, '#password')
74 ->press('Create Account')
75 ->seePageIs('/register/confirm')
76 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
78 // Ensure notification sent
79 $dbUser = \BookStack\User::where('email', '=', $user->email)->first();
80 Notification::assertSentTo($dbUser, ConfirmEmail::class);
82 // Test access and resend confirmation email
83 $this->login($user->email, $user->password)
84 ->seePageIs('/register/confirm/awaiting')
87 ->seePageIs('/register/confirm/awaiting')
88 ->press('Resend Confirmation Email');
90 // Get confirmation and confirm notification matches
91 $emailConfirmation = \DB::table('email_confirmations')->where('user_id', '=', $dbUser->id)->first();
92 Notification::assertSentTo($dbUser, ConfirmEmail::class, function($notification, $channels) use ($emailConfirmation) {
93 return $notification->token === $emailConfirmation->token;
96 // Check confirmation email confirmation activation.
97 $this->visit('/register/confirm/' . $emailConfirmation->token)
100 ->notSeeInDatabase('email_confirmations', ['token' => $emailConfirmation->token])
101 ->seeInDatabase('users', ['name' => $dbUser->name, 'email' => $dbUser->email, 'email_confirmed' => true]);
104 public function test_restricted_registration()
106 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true', 'registration-restrict' => 'example.com']);
107 $user = factory(\BookStack\User::class)->make();
108 // Go through registration process
109 $this->visit('/register')
110 ->type($user->name, '#name')
111 ->type($user->email, '#email')
112 ->type($user->password, '#password')
113 ->press('Create Account')
114 ->seePageIs('/register')
115 ->dontSeeInDatabase('users', ['email' => $user->email])
116 ->see('That email domain does not have access to this application');
120 $this->visit('/register')
121 ->type($user->name, '#name')
122 ->type($user->email, '#email')
123 ->type($user->password, '#password')
124 ->press('Create Account')
125 ->seePageIs('/register/confirm')
126 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
128 $this->visit('/')->seePageIs('/login')
129 ->type($user->email, '#email')
130 ->type($user->password, '#password')
132 ->seePageIs('/register/confirm/awaiting')
133 ->seeText('Email Address Not Confirmed');
136 public function test_restricted_registration_with_confirmation_disabled()
138 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'false', 'registration-restrict' => 'example.com']);
139 $user = factory(\BookStack\User::class)->make();
140 // Go through registration process
141 $this->visit('/register')
142 ->type($user->name, '#name')
143 ->type($user->email, '#email')
144 ->type($user->password, '#password')
145 ->press('Create Account')
146 ->seePageIs('/register')
147 ->dontSeeInDatabase('users', ['email' => $user->email])
148 ->see('That email domain does not have access to this application');
152 $this->visit('/register')
153 ->type($user->name, '#name')
154 ->type($user->email, '#email')
155 ->type($user->password, '#password')
156 ->press('Create Account')
157 ->seePageIs('/register/confirm')
158 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
160 $this->visit('/')->seePageIs('/login')
161 ->type($user->email, '#email')
162 ->type($user->password, '#password')
164 ->seePageIs('/register/confirm/awaiting')
165 ->seeText('Email Address Not Confirmed');
168 public function test_user_creation()
170 $user = factory(\BookStack\User::class)->make();
173 ->visit('/settings/users')
174 ->click('Add New User')
175 ->type($user->name, '#name')
176 ->type($user->email, '#email')
177 ->check('roles[admin]')
178 ->type($user->password, '#password')
179 ->type($user->password, '#password-confirm')
181 ->seePageIs('/settings/users')
182 ->seeInDatabase('users', $user->toArray())
186 public function test_user_updating()
188 $user = $this->getNormalUser();
189 $password = $user->password;
191 ->visit('/settings/users')
193 ->seePageIs('/settings/users/' . $user->id)
195 ->type('Barry Scott', '#name')
197 ->seePageIs('/settings/users')
198 ->seeInDatabase('users', ['id' => $user->id, 'name' => 'Barry Scott', 'password' => $password])
199 ->notSeeInDatabase('users', ['name' => $user->name]);
202 public function test_user_password_update()
204 $user = $this->getNormalUser();
205 $userProfilePage = '/settings/users/' . $user->id;
207 ->visit($userProfilePage)
208 ->type('newpassword', '#password')
210 ->seePageIs($userProfilePage)
211 ->see('Password confirmation required')
213 ->type('newpassword', '#password')
214 ->type('newpassword', '#password-confirm')
216 ->seePageIs('/settings/users');
218 $userPassword = \BookStack\User::find($user->id)->password;
219 $this->assertTrue(\Hash::check('newpassword', $userPassword));
222 public function test_user_deletion()
224 $userDetails = factory(\BookStack\User::class)->make();
225 $user = $this->getEditor($userDetails->toArray());
228 ->visit('/settings/users/' . $user->id)
229 ->click('Delete User')
232 ->seePageIs('/settings/users')
233 ->notSeeInDatabase('users', ['name' => $user->name]);
236 public function test_user_cannot_be_deleted_if_last_admin()
238 $adminRole = \BookStack\Role::getRole('admin');
239 // Ensure we currently only have 1 admin user
240 $this->assertEquals(1, $adminRole->users()->count());
241 $user = $adminRole->users->first();
243 $this->asAdmin()->visit('/settings/users/' . $user->id)
244 ->click('Delete User')
246 ->seePageIs('/settings/users/' . $user->id)
247 ->see('You cannot delete the only admin');
250 public function test_logout()
257 ->seePageIs('/login');
260 public function test_reset_password_flow()
263 Notification::fake();
265 $this->visit('/login')->click('Forgot Password?')
266 ->seePageIs('/password/email')
268 ->press('Send Reset Link')
271 $this->seeInDatabase('password_resets', [
277 Notification::assertSentTo($user, \BookStack\Notifications\ResetPassword::class);
278 $n = Notification::sent($user, \BookStack\Notifications\ResetPassword::class);
280 $this->visit('/password/reset/' . $n->first()->token)
281 ->see('Reset Password')
282 ->submitForm('Reset Password', [
284 'password' => 'randompass',
285 'password_confirmation' => 'randompass'
287 ->see('Your password has been successfully reset');
290 public function test_reset_password_page_shows_sign_links()
292 $this->setSettings(['registration-enabled' => 'true']);
293 $this->visit('/password/email')
295 ->seeLink('Sign up');
300 * @param string $email
301 * @param string $password
304 protected function login($email, $password)
306 return $this->visit('/login')
307 ->type($email, '#email')
308 ->type($password, '#password')