]> BookStack Code Mirror - bookstack/blob - tests/Auth/AuthTest.php
Cleaned tests up, Started LDAP tests, Created LDAP wrapper
[bookstack] / tests / Auth / AuthTest.php
1 <?php
2
3 use BookStack\EmailConfirmation;
4
5 class AuthTest extends TestCase
6 {
7
8     public function test_auth_working()
9     {
10         $this->visit('/')
11             ->seePageIs('/login');
12     }
13
14     public function test_login()
15     {
16         $this->login('[email protected]', 'password')
17             ->seePageIs('/');
18     }
19
20     public function test_public_viewing()
21     {
22         $settings = app('BookStack\Services\SettingService');
23         $settings->put('app-public', 'true');
24         $this->visit('/')
25             ->seePageIs('/')
26             ->see('Sign In');
27     }
28
29     public function test_registration_showing()
30     {
31         // Ensure registration form is showing
32         $this->setSettings(['registration-enabled' => 'true']);
33         $this->visit('/login')
34             ->see('Sign up')
35             ->click('Sign up')
36             ->seePageIs('/register');
37     }
38
39     public function test_normal_registration()
40     {
41         // Set settings and get user instance
42         $this->setSettings(['registration-enabled' => 'true']);
43         $user = factory(\BookStack\User::class)->make();
44
45         // Test form and ensure user is created
46         $this->visit('/register')
47             ->see('Sign Up')
48             ->type($user->name, '#name')
49             ->type($user->email, '#email')
50             ->type($user->password, '#password')
51             ->press('Create Account')
52             ->seePageIs('/')
53             ->see($user->name)
54             ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email]);
55     }
56
57
58     public function test_confirmed_registration()
59     {
60         // Set settings and get user instance
61         $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true']);
62         $user = factory(\BookStack\User::class)->make();
63
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);
68
69         // Go through registration process
70         $this->visit('/register')
71             ->see('Sign Up')
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]);
78
79         // Test access and resend confirmation email
80         $this->login($user->email, $user->password)
81             ->seePageIs('/register/confirm/awaiting')
82             ->see('Resend')
83             ->visit('/books')
84             ->seePageIs('/register/confirm/awaiting')
85             ->press('Resend Confirmation Email');
86
87         // Get confirmation
88         $user = $user->where('email', '=', $user->email)->first();
89         $emailConfirmation = EmailConfirmation::where('user_id', '=', $user->id)->first();
90
91
92         // Check confirmation email button and confirmation activation.
93         $this->visit('/register/confirm/' . $emailConfirmation->token . '/email')
94             ->see('Email Confirmation')
95             ->click('Confirm Email')
96             ->seePageIs('/')
97             ->see($user->name)
98             ->notSeeInDatabase('email_confirmations', ['token' => $emailConfirmation->token])
99             ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
100     }
101
102     public function test_restricted_registration()
103     {
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');
115
116         $user->email = '[email protected]';
117
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]);
125     }
126
127     public function test_user_creation()
128     {
129         $user = factory(\BookStack\User::class)->make();
130
131         $this->asAdmin()
132             ->visit('/users')
133             ->click('Add new user')
134             ->type($user->name, '#name')
135             ->type($user->email, '#email')
136             ->select(2, '#role')
137             ->type($user->password, '#password')
138             ->type($user->password, '#password-confirm')
139             ->press('Save')
140             ->seeInDatabase('users', $user->toArray())
141             ->seePageIs('/users')
142             ->see($user->name);
143     }
144
145     public function test_user_updating()
146     {
147         $user = \BookStack\User::all()->last();
148         $password = $user->password;
149         $this->asAdmin()
150             ->visit('/users')
151             ->click($user->name)
152             ->seePageIs('/users/' . $user->id)
153             ->see($user->email)
154             ->type('Barry Scott', '#name')
155             ->press('Save')
156             ->seePageIs('/users')
157             ->seeInDatabase('users', ['id' => $user->id, 'name' => 'Barry Scott', 'password' => $password])
158             ->notSeeInDatabase('users', ['name' => $user->name]);
159     }
160
161     public function test_user_password_update()
162     {
163         $user = \BookStack\User::all()->last();
164         $userProfilePage = '/users/' . $user->id;
165         $this->asAdmin()
166             ->visit($userProfilePage)
167             ->type('newpassword', '#password')
168             ->press('Save')
169             ->seePageIs($userProfilePage)
170             ->see('Password confirmation required')
171
172             ->type('newpassword', '#password')
173             ->type('newpassword', '#password-confirm')
174             ->press('Save')
175             ->seePageIs('/users');
176
177             $userPassword = \BookStack\User::find($user->id)->password;
178             $this->assertTrue(Hash::check('newpassword', $userPassword));
179     }
180
181     public function test_user_deletion()
182     {
183         $userDetails = factory(\BookStack\User::class)->make();
184         $user = $this->getNewUser($userDetails->toArray());
185
186         $this->asAdmin()
187             ->visit('/users/' . $user->id)
188             ->click('Delete User')
189             ->see($user->name)
190             ->press('Confirm')
191             ->seePageIs('/users')
192             ->notSeeInDatabase('users', ['name' => $user->name]);
193     }
194
195     public function test_user_cannot_be_deleted_if_last_admin()
196     {
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();
201
202         $this->asAdmin()->visit('/users/' . $user->id)
203             ->click('Delete User')
204             ->press('Confirm')
205             ->seePageIs('/users/' . $user->id)
206             ->see('You cannot delete the only admin');
207     }
208
209     public function test_logout()
210     {
211         $this->asAdmin()
212             ->visit('/')
213             ->seePageIs('/')
214             ->visit('/logout')
215             ->visit('/')
216             ->seePageIs('/login');
217     }
218
219     /**
220      * Perform a login
221      * @param string $email
222      * @param string $password
223      * @return $this
224      */
225     protected function login($email, $password)
226     {
227         return $this->visit('/login')
228             ->type($email, '#email')
229             ->type($password, '#password')
230             ->press('Sign In');
231     }
232 }