5 use BookStack\Access\Notifications\ConfirmEmailNotification;
6 use BookStack\Users\Models\Role;
7 use BookStack\Users\Models\User;
8 use Illuminate\Support\Facades\DB;
9 use Illuminate\Support\Facades\Notification;
12 class RegistrationTest extends TestCase
14 public function test_confirmed_registration()
19 // Set settings and get user instance
20 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true']);
21 $user = User::factory()->make();
23 // Go through registration process
24 $resp = $this->post('/register', $user->only('name', 'email', 'password'));
25 $resp->assertRedirect('/register/confirm');
26 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
28 // Ensure notification sent
29 /** @var User $dbUser */
30 $dbUser = User::query()->where('email', '=', $user->email)->first();
31 Notification::assertSentTo($dbUser, ConfirmEmailNotification::class);
33 // Test access and resend confirmation email
34 $resp = $this->post('/login', ['email' => $user->email, 'password' => $user->password]);
35 $resp->assertRedirect('/register/confirm/awaiting');
37 $resp = $this->get('/register/confirm/awaiting');
38 $this->withHtml($resp)->assertElementContains('form[action="' . url('/register/confirm/resend') . '"]', 'Resend');
40 $this->get('/books')->assertRedirect('/login');
41 $this->post('/register/confirm/resend', $user->only('email'));
43 // Get confirmation and confirm notification matches
44 $emailConfirmation = DB::table('email_confirmations')->where('user_id', '=', $dbUser->id)->first();
45 Notification::assertSentTo($dbUser, ConfirmEmailNotification::class, function ($notification, $channels) use ($emailConfirmation) {
46 return $notification->token === $emailConfirmation->token;
49 // Check confirmation email confirmation accept page.
50 $resp = $this->get('/register/confirm/' . $emailConfirmation->token);
51 $acceptPage = $this->withHtml($resp);
53 $resp->assertSee('Thanks for confirming!');
54 $acceptPage->assertElementExists('form[method="post"][action$="/register/confirm/accept"][component="auto-submit"] button');
55 $acceptPage->assertFieldHasValue('token', $emailConfirmation->token);
57 // Check acceptance confirm
58 $this->post('/register/confirm/accept', ['token' => $emailConfirmation->token])->assertRedirect('/login');
60 // Check state on login redirect
61 $this->get('/login')->assertSee('Your email has been confirmed! You should now be able to login using this email address.');
62 $this->assertDatabaseMissing('email_confirmations', ['token' => $emailConfirmation->token]);
63 $this->assertDatabaseHas('users', ['name' => $dbUser->name, 'email' => $dbUser->email, 'email_confirmed' => true]);
66 public function test_restricted_registration()
68 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true', 'registration-restrict' => 'example.com']);
69 $user = User::factory()->make();
71 // Go through registration process
72 $this->post('/register', $user->only('name', 'email', 'password'))
73 ->assertRedirect('/register');
74 $resp = $this->get('/register');
75 $resp->assertSee('That email domain does not have access to this application');
76 $this->assertDatabaseMissing('users', $user->only('email'));
80 $this->post('/register', $user->only('name', 'email', 'password'))
81 ->assertRedirect('/register/confirm');
82 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
84 $this->assertNull(auth()->user());
86 $this->get('/')->assertRedirect('/login');
87 $resp = $this->followingRedirects()->post('/login', $user->only('email', 'password'));
88 $resp->assertSee('Email Address Not Confirmed');
89 $this->assertNull(auth()->user());
92 public function test_restricted_registration_with_confirmation_disabled()
94 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'false', 'registration-restrict' => 'example.com']);
95 $user = User::factory()->make();
97 // Go through registration process
98 $this->post('/register', $user->only('name', 'email', 'password'))
99 ->assertRedirect('/register');
100 $this->assertDatabaseMissing('users', $user->only('email'));
101 $this->get('/register')->assertSee('That email domain does not have access to this application');
105 $this->post('/register', $user->only('name', 'email', 'password'))
106 ->assertRedirect('/register/confirm');
107 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
109 $this->assertNull(auth()->user());
111 $this->get('/')->assertRedirect('/login');
112 $resp = $this->post('/login', $user->only('email', 'password'));
113 $resp->assertRedirect('/register/confirm/awaiting');
114 $this->get('/register/confirm/awaiting')->assertSee('Email Address Not Confirmed');
115 $this->assertNull(auth()->user());
118 public function test_registration_role_unset_by_default()
120 $this->assertFalse(setting('registration-role'));
122 $resp = $this->asAdmin()->get('/settings/registration');
123 $this->withHtml($resp)->assertElementContains('select[name="setting-registration-role"] option[value="0"][selected]', '-- None --');
126 public function test_registration_showing()
128 // Ensure registration form is showing
129 $this->setSettings(['registration-enabled' => 'true']);
130 $resp = $this->get('/login');
131 $this->withHtml($resp)->assertElementContains('a[href="' . url('/register') . '"]', 'Sign up');
134 public function test_normal_registration()
136 // Set settings and get user instance
137 /** @var Role $registrationRole */
138 $registrationRole = Role::query()->first();
139 $this->setSettings(['registration-enabled' => 'true', 'registration-role' => $registrationRole->id]);
140 /** @var User $user */
141 $user = User::factory()->make();
143 // Test form and ensure user is created
144 $resp = $this->get('/register')
145 ->assertSee('Sign Up');
146 $this->withHtml($resp)->assertElementContains('form[action="' . url('/register') . '"]', 'Create Account');
148 $resp = $this->post('/register', $user->only('password', 'name', 'email'));
149 $resp->assertRedirect('/');
151 $resp = $this->get('/');
153 $resp->assertSee($user->name);
155 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email]);
157 $user = User::query()->where('email', '=', $user->email)->first();
158 $this->assertEquals(1, $user->roles()->count());
159 $this->assertEquals($registrationRole->id, $user->roles()->first()->id);
162 public function test_empty_registration_redirects_back_with_errors()
164 // Set settings and get user instance
165 $this->setSettings(['registration-enabled' => 'true']);
167 // Test form and ensure user is created
168 $this->get('/register');
169 $this->post('/register', [])->assertRedirect('/register');
170 $this->get('/register')->assertSee('The name field is required');
173 public function test_registration_validation()
175 $this->setSettings(['registration-enabled' => 'true']);
177 $this->get('/register');
178 $resp = $this->followingRedirects()->post('/register', [
183 $resp->assertSee('The name must be at least 2 characters.');
184 $resp->assertSee('The email must be a valid email address.');
185 $resp->assertSee('The password must be at least 8 characters.');
188 public function test_registration_simple_honeypot_active()
190 $this->setSettings(['registration-enabled' => 'true']);
192 $resp = $this->get('/register');
193 $this->withHtml($resp)->assertElementExists('form input[name="username"]');
195 $resp = $this->post('/register', [
198 'password' => 'barryIsTheBestBot',
199 'username' => 'MyUsername'
201 $resp->assertRedirect('/register');
203 $resp = $this->followRedirects($resp);
204 $this->withHtml($resp)->assertElementExists('form input[name="username"].text-neg');