Skip to content

Commit 1b736ac

Browse files
committed
Added tests for confirmed registration
1 parent e8dd7fd commit 1b736ac

7 files changed

Lines changed: 96 additions & 12 deletions

File tree

app/Http/Controllers/Auth/AuthController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,18 @@ public function getRegisterConfirmation()
170170
return view('auth/register-confirm');
171171
}
172172

173+
/**
174+
* View the confirmation email as a standard web page.
175+
* @param $token
176+
* @return \Illuminate\View\View
177+
* @throws UserRegistrationException
178+
*/
179+
public function viewConfirmEmail($token)
180+
{
181+
$confirmation = $this->emailConfirmationService->getEmailConfirmationFromToken($token);
182+
return view('emails/email-confirmation', ['token' => $confirmation->token]);
183+
}
184+
173185
/**
174186
* Confirms an email via a token and logs the user into the system.
175187
* @param $token

app/Http/routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@
9494
Route::get('/register/confirm/awaiting', 'Auth\AuthController@showAwaitingConfirmation');
9595
Route::post('/register/confirm/resend', 'Auth\AuthController@resendConfirmation');
9696
Route::get('/register/confirm/{token}', 'Auth\AuthController@confirmEmail');
97+
Route::get('/register/confirm/{token}/email', 'Auth\AuthController@viewConfirmEmail');
9798
Route::get('/register/service/{socialDriver}', 'Auth\AuthController@socialRegister');
9899
Route::post('/register', 'Auth\AuthController@postRegister');
99100

app/User.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ public function getAvatar($size = 50)
134134
return '//www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon';
135135
}
136136

137+
/**
138+
* Get the url for editing this user.
139+
* @return string
140+
*/
137141
public function getEditUrl()
138142
{
139143
return '/users/' . $this->id;

config/mail.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,6 @@
119119
|
120120
*/
121121

122-
'pretend' => false,
122+
'pretend' => env('MAIL_PRETEND', false),
123123

124124
];

phpunit.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@
2525
<env name="SESSION_DRIVER" value="array"/>
2626
<env name="QUEUE_DRIVER" value="sync"/>
2727
<env name="DB_CONNECTION" value="mysql_testing"/>
28+
<env name="MAIL_PRETEND" value="true"/>
2829
</php>
2930
</phpunit>

resources/views/users/edit.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242

4343
<hr class="margin-top large">
4444

45-
@if($currentUser->id === $user->id)
45+
@if($currentUser->id === $user->id && count($activeSocialDrivers) > 0)
4646
<h3>Social Accounts</h3>
4747
<p class="text-muted">
4848
Here you can connect your other accounts for quicker and easier login. <br>

tests/AuthTest.php

Lines changed: 76 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use BookStack\EmailConfirmation;
4+
35
class AuthTest extends TestCase
46
{
57

@@ -12,10 +14,9 @@ public function testAuthWorking()
1214
public function testLogin()
1315
{
1416
$this->visit('/')
15-
->seePageIs('/login')
16-
->type('admin@admin.com', '#email')
17-
->type('password', '#password')
18-
->press('Sign In')
17+
->seePageIs('/login');
18+
19+
$this->login('admin@admin.com', 'password')
1920
->seePageIs('/')
2021
->see('BookStack');
2122
}
@@ -41,25 +42,64 @@ public function testRegistrationShowing()
4142

4243
public function testNormalRegistration()
4344
{
45+
// Set settings and get user instance
4446
$this->setSettings(['registration-enabled' => 'true']);
4547
$user = factory(\BookStack\User::class)->make();
4648

49+
// Test form and ensure user is created
4750
$this->visit('/register')
4851
->see('Sign Up')
4952
->type($user->name, '#name')
5053
->type($user->email, '#email')
5154
->type($user->password, '#password')
5255
->press('Create Account')
5356
->seePageIs('/')
54-
->see($user->name);
57+
->see($user->name)
58+
->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email]);
5559
}
5660

57-
private function setSettings($settingsArray)
61+
public function testConfirmedRegistration()
5862
{
59-
$settings = app('BookStack\Services\SettingService');
60-
foreach($settingsArray as $key => $value) {
61-
$settings->put($key, $value);
62-
}
63+
// Set settings and get user instance
64+
$this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true']);
65+
$user = factory(\BookStack\User::class)->make();
66+
67+
// Mock Mailer to ensure mail is being sent
68+
$mockMailer = Mockery::mock('Illuminate\Contracts\Mail\Mailer');
69+
$mockMailer->shouldReceive('send')->with('emails/email-confirmation', Mockery::type('array'), Mockery::type('callable'))->twice();
70+
$this->app->instance('mailer', $mockMailer);
71+
72+
// Go through registration process
73+
$this->visit('/register')
74+
->see('Sign Up')
75+
->type($user->name, '#name')
76+
->type($user->email, '#email')
77+
->type($user->password, '#password')
78+
->press('Create Account')
79+
->seePageIs('/register/confirm')
80+
->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
81+
82+
// Test access and resend confirmation email
83+
$this->login($user->email, $user->password)
84+
->seePageIs('/register/confirm/awaiting')
85+
->see('Resend')
86+
->visit('/books')
87+
->seePageIs('/register/confirm/awaiting')
88+
->press('Resend Confirmation Email');
89+
90+
// Get confirmation
91+
$user = $user->where('email', '=', $user->email)->first();
92+
$emailConfirmation = EmailConfirmation::where('user_id', '=', $user->id)->first();
93+
94+
95+
// Check confirmation email button and confirmation activation.
96+
$this->visit('/register/confirm/' . $emailConfirmation->token . '/email')
97+
->see('Email Confirmation')
98+
->click('Confirm Email')
99+
->seePageIs('/')
100+
->see($user->name)
101+
->notSeeInDatabase('email_confirmations', ['token' => $emailConfirmation->token])
102+
->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
63103
}
64104

65105
public function testLogout()
@@ -71,4 +111,30 @@ public function testLogout()
71111
->visit('/')
72112
->seePageIs('/login');
73113
}
114+
115+
/**
116+
* Quickly sets an array of settings.
117+
* @param $settingsArray
118+
*/
119+
private function setSettings($settingsArray)
120+
{
121+
$settings = app('BookStack\Services\SettingService');
122+
foreach ($settingsArray as $key => $value) {
123+
$settings->put($key, $value);
124+
}
125+
}
126+
127+
/**
128+
* Perform a login
129+
* @param string $email
130+
* @param string $password
131+
* @return $this
132+
*/
133+
private function login($email, $password)
134+
{
135+
return $this->visit('/login')
136+
->type($email, '#email')
137+
->type($password, '#password')
138+
->press('Sign In');
139+
}
74140
}

0 commit comments

Comments
 (0)