Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maintenance][Admin] Slight password reset cleanup #14181

Merged
merged 2 commits into from Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -168,7 +168,7 @@ public function iShouldBeNotifiedThatTheEnteredPasswordsDoNotMatch(): void
}

/**
* @Then /^I should be notified that the password should be ([^"]+)$/
* @Then I should be notified that the password should be :validationMessage
*/
public function iShouldBeNotifiedThatThePasswordShouldBe(string $validationMessage): void
{
Expand Down
18 changes: 12 additions & 6 deletions src/Sylius/Behat/Context/Ui/Admin/ResettingPasswordContext.php
Expand Up @@ -121,7 +121,10 @@ public function iShouldBeNotifiedThatTheEmailIsNotValid(): void
*/
public function iShouldBeNotifiedThatMyPasswordHasBeenSuccessfullyChanged(): void
{
$this->notificationChecker->checkNotification('has been changed successfully!', NotificationType::success());
$this->notificationChecker->checkNotification(
'Your password has been changed successfully!',
NotificationType::success(),
);
}

/**
Expand All @@ -139,7 +142,10 @@ public function iShouldNotBeAbleToChangeMyPasswordAgainWithTheSameToken(): void
*/
public function iShouldBeNotifiedThatThePasswordResetTokenHasExpired(): void
{
$this->notificationChecker->checkNotification('has expired', NotificationType::failure());
$this->notificationChecker->checkNotification(
'The password reset token has expired',
NotificationType::failure(),
);
}

/**
Expand All @@ -148,7 +154,7 @@ public function iShouldBeNotifiedThatThePasswordResetTokenHasExpired(): void
public function iShouldBeNotifiedThatTheNewPasswordIsRequired(): void
{
Assert::contains(
$this->resetPasswordPage->getValidationMessageFor('new_password'),
$this->resetPasswordPage->getValidationMessageForNewPassword(),
'Please enter the password.',
);
}
Expand All @@ -159,18 +165,18 @@ public function iShouldBeNotifiedThatTheNewPasswordIsRequired(): void
public function iShouldBeNotifiedThatTheEnteredPasswordsDoNotMatch(): void
{
Assert::contains(
$this->resetPasswordPage->getValidationMessageFor('new_password'),
$this->resetPasswordPage->getValidationMessageForNewPassword(),
'The entered passwords do not match.',
);
}

/**
* @Then /^I should be notified that the password should be ([^"]+)$/
* @Then I should be notified that the password should be :validationMessage
*/
public function iShouldBeNotifiedThatThePasswordShouldBe(string $validationMessage): void
{
Assert::contains(
$this->resetPasswordPage->getValidationMessageFor('new_password'),
$this->resetPasswordPage->getValidationMessageForNewPassword(),
$validationMessage,
);
}
Expand Down
9 changes: 8 additions & 1 deletion src/Sylius/Behat/Element/Admin/Account/ResetElement.php
Expand Up @@ -19,6 +19,13 @@ final class ResetElement extends Element implements ResetElementInterface
{
public function reset(): void
{
$this->getDocument()->find('css', 'button[type="submit"]:contains("Reset")')->click();
$this->getElement('reset')->click();
}

protected function getDefinedElements(): array
{
return array_merge(parent::getDefinedElements(), [
'reset' => 'button[type="submit"]:contains("Reset")',
]);
}
}
4 changes: 2 additions & 2 deletions src/Sylius/Behat/Page/Admin/Account/ResetPasswordPage.php
Expand Up @@ -28,9 +28,9 @@ public function specifyPasswordConfirmation(string $password): void
$this->getElement('confirm_new_password')->setValue($password);
}

public function getValidationMessageFor(string $element): string
public function getValidationMessageForNewPassword(): string
{
$errorLabel = $this->getElement($element)->getParent()->find('css', '[data-test-validation-error]');
$errorLabel = $this->getElement('new_password')->getParent()->find('css', '[data-test-validation-error]');

if (null === $errorLabel) {
throw new ElementNotFoundException($this->getSession(), 'Validation message', 'css', '[data-test-validation-error]');
Expand Down
Expand Up @@ -21,5 +21,5 @@ public function specifyNewPassword(string $password): void;

public function specifyPasswordConfirmation(string $password): void;

public function getValidationMessageFor(string $element): string;
public function getValidationMessageForNewPassword(): string;
}
Expand Up @@ -48,19 +48,7 @@ public function __invoke(Request $request, string $token): Response
$lifetime = new \DateInterval($this->tokenTtl);

if (!$admin->isPasswordRequestNonExpired($lifetime)) {
$this->flashBag->add('error', 'sylius.admin.password_reset.token_expired');

$attributes = $request->attributes->get('_sylius');
$redirect = $attributes['redirect'] ?? 'sylius_admin_login';

if (is_array($redirect)) {
return new RedirectResponse($this->router->generate(
$redirect['route'] ?? 'sylius_admin_login',
$redirect['params'] ?? [],
));
}

return new RedirectResponse($this->router->generate($redirect));
return $this->handleExpiredPasswordRequest($request);
}

$form = $this->formFactory->create(ResetPasswordType::class);
Expand All @@ -71,4 +59,21 @@ public function __invoke(Request $request, string $token): Response
]),
);
}

private function handleExpiredPasswordRequest(Request $request): RedirectResponse
{
$this->flashBag->add('error', 'sylius.admin.password_reset.token_expired');

$attributes = $request->attributes->get('_sylius');
$redirect = $attributes['redirect'] ?? 'sylius_admin_login';

if (is_array($redirect)) {
return new RedirectResponse($this->router->generate(
$redirect['route'] ?? 'sylius_admin_login',
$redirect['params'] ?? [],
));
}

return new RedirectResponse($this->router->generate($redirect));
}
}
Expand Up @@ -19,7 +19,7 @@
<services>
<defaults public="true" />

<service id="Sylius\Bundle\AdminBundle\Action\Account\RenderResetPasswordPageAction" public="true">
<service id="Sylius\Bundle\AdminBundle\Action\Account\RenderResetPasswordPageAction">
<argument type="service" id="sylius.repository.admin_user" />
<argument type="service" id="form.factory" />
<argument type="service" id="session.flash_bag" />
Expand Down
Expand Up @@ -15,6 +15,7 @@

use Symfony\Component\Validator\Constraint;

/** @experimental */
final class AdminResetPasswordTokenNonExpired extends Constraint
{
public string $message = 'sylius.admin.expired_password_reset_token';
Expand Down
Expand Up @@ -20,6 +20,7 @@
use Symfony\Component\Validator\ConstraintValidator;
use Webmozart\Assert\Assert;

/** @experimental */
final class AdminResetPasswordTokenNonExpiredValidator extends ConstraintValidator
{
public function __construct(
Expand All @@ -45,9 +46,7 @@ public function validate($value, Constraint $constraint): void
$lifetime = new \DateInterval($this->tokenTtl);

if (!$user->isPasswordRequestNonExpired($lifetime)) {
$this->context->addViolation(
$constraint->message,
);
$this->context->addViolation($constraint->message);
}
}
}