5 use BookStack\Auth\Role;
6 use BookStack\Auth\User;
7 use BookStack\Notifications\ConfirmEmail;
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, ConfirmEmail::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, ConfirmEmail::class, function ($notification, $channels) use ($emailConfirmation) {
46 return $notification->token === $emailConfirmation->token;
49 // Check confirmation email confirmation activation.
50 $this->get('/register/confirm/' . $emailConfirmation->token)->assertRedirect('/login');
51 $this->get('/login')->assertSee('Your email has been confirmed! You should now be able to login using this email address.');
52 $this->assertDatabaseMissing('email_confirmations', ['token' => $emailConfirmation->token]);
53 $this->assertDatabaseHas('users', ['name' => $dbUser->name, 'email' => $dbUser->email, 'email_confirmed' => true]);
56 public function test_restricted_registration()
58 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true', 'registration-restrict' => 'example.com']);
59 $user = User::factory()->make();
61 // Go through registration process
62 $this->post('/register', $user->only('name', 'email', 'password'))
63 ->assertRedirect('/register');
64 $resp = $this->get('/register');
65 $resp->assertSee('That email domain does not have access to this application');
66 $this->assertDatabaseMissing('users', $user->only('email'));
70 $this->post('/register', $user->only('name', 'email', 'password'))
71 ->assertRedirect('/register/confirm');
72 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
74 $this->assertNull(auth()->user());
76 $this->get('/')->assertRedirect('/login');
77 $resp = $this->followingRedirects()->post('/login', $user->only('email', 'password'));
78 $resp->assertSee('Email Address Not Confirmed');
79 $this->assertNull(auth()->user());
82 public function test_restricted_registration_with_confirmation_disabled()
84 $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'false', 'registration-restrict' => 'example.com']);
85 $user = User::factory()->make();
87 // Go through registration process
88 $this->post('/register', $user->only('name', 'email', 'password'))
89 ->assertRedirect('/register');
90 $this->assertDatabaseMissing('users', $user->only('email'));
91 $this->get('/register')->assertSee('That email domain does not have access to this application');
95 $this->post('/register', $user->only('name', 'email', 'password'))
96 ->assertRedirect('/register/confirm');
97 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
99 $this->assertNull(auth()->user());
101 $this->get('/')->assertRedirect('/login');
102 $resp = $this->post('/login', $user->only('email', 'password'));
103 $resp->assertRedirect('/register/confirm/awaiting');
104 $this->get('/register/confirm/awaiting')->assertSee('Email Address Not Confirmed');
105 $this->assertNull(auth()->user());
108 public function test_registration_role_unset_by_default()
110 $this->assertFalse(setting('registration-role'));
112 $resp = $this->asAdmin()->get('/settings/registration');
113 $this->withHtml($resp)->assertElementContains('select[name="setting-registration-role"] option[value="0"][selected]', '-- None --');
116 public function test_registration_showing()
118 // Ensure registration form is showing
119 $this->setSettings(['registration-enabled' => 'true']);
120 $resp = $this->get('/login');
121 $this->withHtml($resp)->assertElementContains('a[href="' . url('/register') . '"]', 'Sign up');
124 public function test_normal_registration()
126 // Set settings and get user instance
127 /** @var Role $registrationRole */
128 $registrationRole = Role::query()->first();
129 $this->setSettings(['registration-enabled' => 'true', 'registration-role' => $registrationRole->id]);
130 /** @var User $user */
131 $user = User::factory()->make();
133 // Test form and ensure user is created
134 $resp = $this->get('/register')
135 ->assertSee('Sign Up');
136 $this->withHtml($resp)->assertElementContains('form[action="' . url('/register') . '"]', 'Create Account');
138 $resp = $this->post('/register', $user->only('password', 'name', 'email'));
139 $resp->assertRedirect('/');
141 $resp = $this->get('/');
143 $resp->assertSee($user->name);
145 $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email]);
147 $user = User::query()->where('email', '=', $user->email)->first();
148 $this->assertEquals(1, $user->roles()->count());
149 $this->assertEquals($registrationRole->id, $user->roles()->first()->id);
152 public function test_empty_registration_redirects_back_with_errors()
154 // Set settings and get user instance
155 $this->setSettings(['registration-enabled' => 'true']);
157 // Test form and ensure user is created
158 $this->get('/register');
159 $this->post('/register', [])->assertRedirect('/register');
160 $this->get('/register')->assertSee('The name field is required');
163 public function test_registration_validation()
165 $this->setSettings(['registration-enabled' => 'true']);
167 $this->get('/register');
168 $resp = $this->followingRedirects()->post('/register', [
173 $resp->assertSee('The name must be at least 2 characters.');
174 $resp->assertSee('The email must be a valid email address.');
175 $resp->assertSee('The password must be at least 8 characters.');