3 use BookStack\Notifications\ConfirmEmail;
4 use BookStack\Auth\User;
5 use BookStack\Settings\SettingService;
6 use Illuminate\Support\Facades\Notification;
8 class AuthTest extends BrowserKitTest
11 public function test_auth_working()
14 ->seePageIs('/login');
17 public function test_login()
23 public function test_public_viewing()
25 $settings = app(SettingService::class);
26 $settings->put('app-public', 'true');
32 public function test_registration_showing()
34 // Ensure registration form is showing
35 $this->setSettings(['registration-enabled' => 'true']);
36 $this->visit('/login')
39 ->seePageIs('/register');
42 public function test_normal_registration()
44 // Set settings and get user instance
45 $this->setSettings(['registration-enabled' => 'true']);
46 $user = factory(User::class)->make();
48 // Test form and ensure user is created
49 $this->visit('/register')
51 ->type($user->name, '#name')
52 ->type($user->email, '#email')
53 ->type($user->password, '#password')
54 ->press('Create Account')
57 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email]);
60 public function test_empty_registration_redirects_back_with_errors()
62 // Set settings and get user instance
63 $this->setSettings(['registration-enabled' => 'true']);
65 // Test form and ensure user is created
66 $this->visit('/register')
67 ->press('Create Account')
68 ->see('The name field is required')
69 ->seePageIs('/register');
73 public function test_confirmed_registration()
78 // Set settings and get user instance
79 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true']);
80 $user = factory(User::class)->make();
82 // Go through registration process
83 $this->visit('/register')
85 ->type($user->name, '#name')
86 ->type($user->email, '#email')
87 ->type($user->password, '#password')
88 ->press('Create Account')
89 ->seePageIs('/register/confirm')
90 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
92 // Ensure notification sent
93 $dbUser = User::where('email', '=', $user->email)->first();
94 Notification::assertSentTo($dbUser, ConfirmEmail::class);
96 // Test access and resend confirmation email
97 $this->login($user->email, $user->password)
98 ->seePageIs('/register/confirm/awaiting')
101 ->seePageIs('/register/confirm/awaiting')
102 ->press('Resend Confirmation Email');
104 // Get confirmation and confirm notification matches
105 $emailConfirmation = \DB::table('email_confirmations')->where('user_id', '=', $dbUser->id)->first();
106 Notification::assertSentTo($dbUser, ConfirmEmail::class, function($notification, $channels) use ($emailConfirmation) {
107 return $notification->token === $emailConfirmation->token;
110 // Check confirmation email confirmation activation.
111 $this->visit('/register/confirm/' . $emailConfirmation->token)
114 ->notSeeInDatabase('email_confirmations', ['token' => $emailConfirmation->token])
115 ->seeInDatabase('users', ['name' => $dbUser->name, 'email' => $dbUser->email, 'email_confirmed' => true]);
118 public function test_restricted_registration()
120 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true', 'registration-restrict' => 'example.com']);
121 $user = factory(User::class)->make();
122 // Go through registration process
123 $this->visit('/register')
124 ->type($user->name, '#name')
125 ->type($user->email, '#email')
126 ->type($user->password, '#password')
127 ->press('Create Account')
128 ->seePageIs('/register')
129 ->dontSeeInDatabase('users', ['email' => $user->email])
130 ->see('That email domain does not have access to this application');
134 $this->visit('/register')
135 ->type($user->name, '#name')
136 ->type($user->email, '#email')
137 ->type($user->password, '#password')
138 ->press('Create Account')
139 ->seePageIs('/register/confirm')
140 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
142 $this->visit('/')->seePageIs('/login')
143 ->type($user->email, '#email')
144 ->type($user->password, '#password')
146 ->seePageIs('/register/confirm/awaiting')
147 ->seeText('Email Address Not Confirmed');
150 public function test_restricted_registration_with_confirmation_disabled()
152 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'false', 'registration-restrict' => 'example.com']);
153 $user = factory(User::class)->make();
154 // Go through registration process
155 $this->visit('/register')
156 ->type($user->name, '#name')
157 ->type($user->email, '#email')
158 ->type($user->password, '#password')
159 ->press('Create Account')
160 ->seePageIs('/register')
161 ->dontSeeInDatabase('users', ['email' => $user->email])
162 ->see('That email domain does not have access to this application');
166 $this->visit('/register')
167 ->type($user->name, '#name')
168 ->type($user->email, '#email')
169 ->type($user->password, '#password')
170 ->press('Create Account')
171 ->seePageIs('/register/confirm')
172 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
174 $this->visit('/')->seePageIs('/login')
175 ->type($user->email, '#email')
176 ->type($user->password, '#password')
178 ->seePageIs('/register/confirm/awaiting')
179 ->seeText('Email Address Not Confirmed');
182 public function test_user_creation()
184 $user = factory(User::class)->make();
187 ->visit('/settings/users')
188 ->click('Add New User')
189 ->type($user->name, '#name')
190 ->type($user->email, '#email')
191 ->check('roles[admin]')
192 ->type($user->password, '#password')
193 ->type($user->password, '#password-confirm')
195 ->seePageIs('/settings/users')
196 ->seeInDatabase('users', $user->toArray())
200 public function test_user_updating()
202 $user = $this->getNormalUser();
203 $password = $user->password;
205 ->visit('/settings/users')
207 ->seePageIs('/settings/users/' . $user->id)
209 ->type('Barry Scott', '#name')
211 ->seePageIs('/settings/users')
212 ->seeInDatabase('users', ['id' => $user->id, 'name' => 'Barry Scott', 'password' => $password])
213 ->notSeeInDatabase('users', ['name' => $user->name]);
216 public function test_user_password_update()
218 $user = $this->getNormalUser();
219 $userProfilePage = '/settings/users/' . $user->id;
221 ->visit($userProfilePage)
222 ->type('newpassword', '#password')
224 ->seePageIs($userProfilePage)
225 ->see('Password confirmation required')
227 ->type('newpassword', '#password')
228 ->type('newpassword', '#password-confirm')
230 ->seePageIs('/settings/users');
232 $userPassword = User::find($user->id)->password;
233 $this->assertTrue(\Hash::check('newpassword', $userPassword));
236 public function test_user_deletion()
238 $userDetails = factory(User::class)->make();
239 $user = $this->getEditor($userDetails->toArray());
242 ->visit('/settings/users/' . $user->id)
243 ->click('Delete User')
246 ->seePageIs('/settings/users')
247 ->notSeeInDatabase('users', ['name' => $user->name]);
250 public function test_user_cannot_be_deleted_if_last_admin()
252 $adminRole = \BookStack\Auth\Role::getRole('admin');
253 // Ensure we currently only have 1 admin user
254 $this->assertEquals(1, $adminRole->users()->count());
255 $user = $adminRole->users->first();
257 $this->asAdmin()->visit('/settings/users/' . $user->id)
258 ->click('Delete User')
260 ->seePageIs('/settings/users/' . $user->id)
261 ->see('You cannot delete the only admin');
264 public function test_logout()
271 ->seePageIs('/login');
274 public function test_reset_password_flow()
277 Notification::fake();
279 $this->visit('/login')->click('Forgot Password?')
280 ->seePageIs('/password/email')
282 ->press('Send Reset Link')
285 $this->seeInDatabase('password_resets', [
291 Notification::assertSentTo($user, \BookStack\Notifications\ResetPassword::class);
292 $n = Notification::sent($user, \BookStack\Notifications\ResetPassword::class);
294 $this->visit('/password/reset/' . $n->first()->token)
295 ->see('Reset Password')
296 ->submitForm('Reset Password', [
298 'password' => 'randompass',
299 'password_confirmation' => 'randompass'
301 ->see('Your password has been successfully reset');
304 public function test_reset_password_page_shows_sign_links()
306 $this->setSettings(['registration-enabled' => 'true']);
307 $this->visit('/password/email')
309 ->seeLink('Sign up');
314 * @param string $email
315 * @param string $password
318 protected function login($email, $password)
320 return $this->visit('/login')
321 ->type($email, '#email')
322 ->type($password, '#password')