Skip to content

Commit 8b8bdfd

Browse files
committed
Add test for the resending of a new email confirmation token
1 parent 157749f commit 8b8bdfd

7 files changed

Lines changed: 37 additions & 10 deletions

File tree

features/bootstrap/DoctrineContext.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public function createForm(string $type)
216216
}
217217

218218
/**
219-
* @Given /^there is a user with the username "([^"]*)" password "([^"]*)" and role "([^"]*)"(?: and the email address "([^"]*)"|)$/i
219+
* @Given /^there is a user with the username "([^" ]*)" password "([^" ]*)" and role "([^" ]*)"(?: and the email address "([^" ]*)"|)$/i
220220
*/
221221
public function thereIsAUserWithUsernamePasswordAndRole(string $username, string $password, string $role, string $emailAddress = 'test.user@example.com'): void
222222
{

features/bootstrap/ProfilerContext.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public function iShouldNotReceiveAnyEmails()
199199
}
200200

201201
/**
202-
* @Then /^I should get a(?:n|) "(.+)" email sent(?:| to the email address "(.+)")$/i
202+
* @Then /^I should get a(?:n|) "([^" ]*)" email sent(?:| to the email address "([^" ]*)")$/i
203203
*/
204204
public function iShouldGetAnEmail(string $emailType, string $emailAddress = 'user@example.com')
205205
{
@@ -242,7 +242,7 @@ public function iShouldGetAnEmail(string $emailType, string $emailAddress = 'use
242242
$this->validateChangeEmailVerification($context, $headers, true);
243243
break;
244244
case 'change_email_confirmation':
245-
$this->validateChangeEmailVerification($context, $headers);
245+
$this->validateChangeEmailVerification($context, $headers, false, $context['user']->getUsername());
246246
break;
247247
case 'change_password_notification':
248248
$this->validateChangePasswordNotification($headers);
@@ -283,13 +283,16 @@ private function validateEnabledNotification(Headers $headers): void
283283
Assert::assertStringStartsWith(UserEnabledEmailFactory::MESSAGE_ID_PREFIX, $headers->get('x-message-id')->getBodyAsString());
284284
}
285285

286-
private function validateChangeEmailVerification(array $context, Headers $headers, bool $customPath = false): void
286+
private function validateChangeEmailVerification(array $context, Headers $headers, bool $customPath = false, ?string $username = null): void
287287
{
288+
if (!$username) {
289+
$username = 'new_user';
290+
}
288291
$pathInsert = $customPath ? 'another-path' : 'confirm-new-email';
289292
Assert::assertEquals('Please confirm your new email address', $headers->get('subject')->getBodyAsString());
290293
Assert::assertStringStartsWith(ChangeEmailConfirmationEmailFactory::MESSAGE_ID_PREFIX, $headers->get('x-message-id')->getBodyAsString());
291294
Assert::assertIsString($context['user']->getNewEmailConfirmationToken());
292-
Assert::assertMatchesRegularExpression('/^http:\/\/www.website.com\/' . $pathInsert . '\/new_user\/new%40example.com\/([a-z0-9]+)$/i', $context['redirect_url']);
295+
Assert::assertMatchesRegularExpression('/^http:\/\/www\.website\.com\/' . $pathInsert . '\/' . $username . '\/new%40example.com\/([a-z0-9]+)$/i', $context['redirect_url']);
293296
}
294297

295298
private function validateChangePasswordNotification(Headers $headers): void

features/user/new_email_address.feature

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ Feature: Register process via a form
164164
@loginUser
165165
Scenario: I can resend a new email address confirmation email with a new token
166166
Given there is a "new_email" form
167-
And there is a user with the username "another_user" password "password" and role "ROLE_USER"
167+
And there is a user with the username "my_username" password "password" and role "ROLE_USER" and the email address "user@example.com"
168+
And the user has a new email address "new@example.com" and confirmation token abc123
168169
And I add "referer" header equal to "http://www.website.com"
169-
When I send a "GET" request to "/confirm-email/my_username/new@email.com/abc123"
170-
Then the response status code should be 201
171-
And the JSON node "newEmailConfirmationToken" should not exist
172-
And I should not receive any emails
170+
When I send a "GET" request to "/resend-verify-new-email/my_username"
171+
Then the response status code should be 200
172+
And I should get a "change_email_confirmation" email sent to the email address "user@example.com"
173173

174174
Scenario: I can verify my new email address
175175
Given there is a user with the username "my_username" password "password" and role "ROLE_USER" and the email address "old@email.com"

src/Action/User/ResendVerifyEmailAddressAction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function __invoke(string $username): Response
3838
's_maxage' => 0,
3939
'max_age' => 0,
4040
]);
41+
4142
return $response;
4243
}
4344

src/Action/User/ResendVerifyNewEmailAddressAction.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public function __invoke(string $username): Response
3838
's_maxage' => 0,
3939
'max_age' => 0,
4040
]);
41+
4142
return $response;
4243
}
4344
$emailSuccess = $this->userMailer->sendChangeEmailConfirmationEmail($user);

src/Helper/User/UserDataProcessor.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ private function findUserByUsername(string $usernameQuery): ?AbstractUser
109109
if (!$user) {
110110
throw new InvalidArgumentException('Username not found');
111111
}
112+
112113
return $user;
113114
}
114115

@@ -119,6 +120,7 @@ public function updateNewEmailToken(string $usernameQuery): ?AbstractUser
119120
return null;
120121
}
121122
$this->setNewEmailConfirmationToken($user);
123+
122124
return $user;
123125
}
124126

@@ -129,6 +131,7 @@ public function updateVerifyEmailToken(string $usernameQuery): ?AbstractUser
129131
return null;
130132
}
131133
$this->setEmailAddressVerifyToken($user);
134+
132135
return $user;
133136
}
134137

src/Resources/config/services.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
use Silverback\ApiComponentsBundle\Action\Uploadable\UploadAction;
2727
use Silverback\ApiComponentsBundle\Action\User\EmailAddressConfirmAction;
2828
use Silverback\ApiComponentsBundle\Action\User\PasswordRequestAction;
29+
use Silverback\ApiComponentsBundle\Action\User\ResendVerifyEmailAddressAction;
30+
use Silverback\ApiComponentsBundle\Action\User\ResendVerifyNewEmailAddressAction;
2931
use Silverback\ApiComponentsBundle\Action\User\VerifyEmailAddressAction;
3032
use Silverback\ApiComponentsBundle\ApiPlatform\Api\IriConverter;
3133
use Silverback\ApiComponentsBundle\ApiPlatform\Api\MercureIriConverter;
@@ -1269,6 +1271,23 @@
12691271
)
12701272
->tag('controller.service_arguments');
12711273

1274+
$services
1275+
->set(ResendVerifyEmailAddressAction::class)
1276+
->args([
1277+
new Reference(UserMailer::class),
1278+
new Reference(UserDataProcessor::class),
1279+
])
1280+
->tag('controller.service_arguments');
1281+
1282+
$services
1283+
->set(ResendVerifyNewEmailAddressAction::class)
1284+
->args([
1285+
new Reference(UserMailer::class),
1286+
new Reference(UserDataProcessor::class),
1287+
])
1288+
->tag('controller.service_arguments');
1289+
1290+
12721291
$services
12731292
->set(VerifyEmailFactory::class)
12741293
->parent(AbstractUserEmailFactory::class);

0 commit comments

Comments
 (0)