3 use BookStack\EmailConfirmation;
5 class AuthTest extends TestCase
8 public function test_auth_working()
11 ->seePageIs('/login');
14 public function test_login()
20 public function test_public_viewing()
22 $settings = app('BookStack\Services\SettingService');
23 $settings->put('app-public', 'true');
29 public function test_registration_showing()
31 // Ensure registration form is showing
32 $this->setSettings(['registration-enabled' => 'true']);
33 $this->visit('/login')
36 ->seePageIs('/register');
39 public function test_normal_registration()
41 // Set settings and get user instance
42 $this->setSettings(['registration-enabled' => 'true']);
43 $user = factory(\BookStack\User::class)->make();
45 // Test form and ensure user is created
46 $this->visit('/register')
48 ->type($user->name, '#name')
49 ->type($user->email, '#email')
50 ->type($user->password, '#password')
51 ->press('Create Account')
54 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email]);
58 public function test_confirmed_registration()
60 // Set settings and get user instance
61 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true']);
62 $user = factory(\BookStack\User::class)->make();
64 // Mock Mailer to ensure mail is being sent
65 $mockMailer = Mockery::mock('Illuminate\Contracts\Mail\Mailer');
66 $mockMailer->shouldReceive('send')->with('emails/email-confirmation', Mockery::type('array'), Mockery::type('callable'))->twice();
67 $this->app->instance('mailer', $mockMailer);
69 // Go through registration process
70 $this->visit('/register')
72 ->type($user->name, '#name')
73 ->type($user->email, '#email')
74 ->type($user->password, '#password')
75 ->press('Create Account')
76 ->seePageIs('/register/confirm')
77 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
79 // Test access and resend confirmation email
80 $this->login($user->email, $user->password)
81 ->seePageIs('/register/confirm/awaiting')
84 ->seePageIs('/register/confirm/awaiting')
85 ->press('Resend Confirmation Email');
88 $user = $user->where('email', '=', $user->email)->first();
89 $emailConfirmation = EmailConfirmation::where('user_id', '=', $user->id)->first();
92 // Check confirmation email button and confirmation activation.
93 $this->visit('/register/confirm/' . $emailConfirmation->token . '/email')
94 ->see('Email Confirmation')
95 ->click('Confirm Email')
98 ->notSeeInDatabase('email_confirmations', ['token' => $emailConfirmation->token])
99 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
102 public function test_restricted_registration()
104 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true', 'registration-restrict' => 'example.com']);
105 $user = factory(\BookStack\User::class)->make();
106 // Go through registration process
107 $this->visit('/register')
108 ->type($user->name, '#name')
109 ->type($user->email, '#email')
110 ->type($user->password, '#password')
111 ->press('Create Account')
112 ->seePageIs('/register')
113 ->dontSeeInDatabase('users', ['email' => $user->email])
114 ->see('That email domain does not have access to this application');
118 $this->visit('/register')
119 ->type($user->name, '#name')
120 ->type($user->email, '#email')
121 ->type($user->password, '#password')
122 ->press('Create Account')
123 ->seePageIs('/register/confirm')
124 ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
127 public function test_user_creation()
129 $user = factory(\BookStack\User::class)->make();
133 ->click('Add new user')
134 ->type($user->name, '#name')
135 ->type($user->email, '#email')
137 ->type($user->password, '#password')
138 ->type($user->password, '#password-confirm')
140 ->seeInDatabase('users', $user->toArray())
141 ->seePageIs('/users')
145 public function test_user_updating()
147 $user = \BookStack\User::all()->last();
148 $password = $user->password;
152 ->seePageIs('/users/' . $user->id)
154 ->type('Barry Scott', '#name')
156 ->seePageIs('/users')
157 ->seeInDatabase('users', ['id' => $user->id, 'name' => 'Barry Scott', 'password' => $password])
158 ->notSeeInDatabase('users', ['name' => $user->name]);
161 public function test_user_password_update()
163 $user = \BookStack\User::all()->last();
164 $userProfilePage = '/users/' . $user->id;
166 ->visit($userProfilePage)
167 ->type('newpassword', '#password')
169 ->seePageIs($userProfilePage)
170 ->see('Password confirmation required')
172 ->type('newpassword', '#password')
173 ->type('newpassword', '#password-confirm')
175 ->seePageIs('/users');
177 $userPassword = \BookStack\User::find($user->id)->password;
178 $this->assertTrue(Hash::check('newpassword', $userPassword));
181 public function test_user_deletion()
183 $userDetails = factory(\BookStack\User::class)->make();
184 $user = $this->getNewUser($userDetails->toArray());
187 ->visit('/users/' . $user->id)
188 ->click('Delete User')
191 ->seePageIs('/users')
192 ->notSeeInDatabase('users', ['name' => $user->name]);
195 public function test_user_cannot_be_deleted_if_last_admin()
197 $adminRole = \BookStack\Role::getRole('admin');
198 // Ensure we currently only have 1 admin user
199 $this->assertEquals(1, $adminRole->users()->count());
200 $user = $adminRole->users->first();
202 $this->asAdmin()->visit('/users/' . $user->id)
203 ->click('Delete User')
205 ->seePageIs('/users/' . $user->id)
206 ->see('You cannot delete the only admin');
209 public function test_logout()
216 ->seePageIs('/login');
221 * @param string $email
222 * @param string $password
225 protected function login($email, $password)
227 return $this->visit('/login')
228 ->type($email, '#email')
229 ->type($password, '#password')