-
Notifications
You must be signed in to change notification settings - Fork 7
/
PasswordResetContext.php
156 lines (131 loc) · 4.8 KB
/
PasswordResetContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
/**
* (c) FSi sp. z o.o. <info@fsi.pl>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace FSi\Bundle\AdminSecurityBundle\Behat\Context;
use Assert\Assertion;
use Behat\Mink\Session;
use DateInterval;
use DateTimeImmutable;
use Doctrine\ORM\EntityManagerInterface;
use FriendsOfBehat\SymfonyExtension\Mink\MinkParameters;
use FSi\Bundle\AdminSecurityBundle\Behat\Page\PasswordResetChangePassword;
use FSi\Bundle\AdminSecurityBundle\Behat\Page\PasswordResetRequest;
use FSi\Bundle\AdminSecurityBundle\Security\Token\Token;
use FSi\FixturesBundle\Entity\User;
use FSi\FixturesBundle\Time\Clock;
final class PasswordResetContext extends AbstractContext
{
private Clock $clock;
public function __construct(
Session $session,
MinkParameters $minkParameters,
EntityManagerInterface $entityManager,
Clock $clock
) {
parent::__construct($session, $minkParameters, $entityManager);
$this->clock = $clock;
}
/**
* @Given /^user "([^"]*)" has confirmation token "([^"]*)"$/
*/
public function userHasConfirmationToken(string $username, string $confirmationToken): void
{
$user = $this->findUserByUsername($username);
$user->setPasswordResetToken($this->createToken($confirmationToken, new DateInterval('PT3600S')));
$this->getEntityManager()->flush();
}
/**
* @Given /^user "([^"]*)" should still have confirmation token "([^"]*)"$/
*/
public function userShouldStillHaveConfirmationToken(string $username, string $expectedConfirmationToken): void
{
$user = $this->findUserByUsername($username);
$passwordResetToken = $user->getPasswordResetToken();
Assertion::notNull(
$passwordResetToken,
"User {$username} has no password reset token"
);
Assertion::same(
$passwordResetToken->getToken(),
$expectedConfirmationToken
);
}
/**
* @Given /^user "([^"]*)" has expired confirmation token "([^"]*)"$/
*/
public function userHasConfirmationTokenWithTtl(string $username, string $confirmationToken): void
{
$this->clock->freeze((new DateTimeImmutable())->sub(new DateInterval('P2D')));
$user = $this->findUserByUsername($username);
$user->setPasswordResetToken($this->createToken($confirmationToken, new DateInterval('P1D')));
$this->getEntityManager()->flush();
$this->clock->return();
}
/**
* @When /^I fill form with non-existent email address$/
*/
public function iFillFormWithNonExistentEmailAddress(): void
{
$this->getPasswordResetRequestPage()->fillField('Email', 'nonexistent@fsi.pl');
}
/**
* @When /^I fill form with correct email address$/
*/
public function iFillFormWithCorrectEmailAddress(): void
{
$this->getPasswordResetRequestPage()->fillField('Email', 'admin@fsi.pl');
}
/**
* @When /^I try open password change page with token "([^"]*)"$/
*/
public function iTryOpenPasswordChangePageWithToken(string $confirmationToken): void
{
$this->getPasswordResetChangePasswordPage()->openWithoutVerification([
'confirmationToken' => $confirmationToken
]);
}
/**
* @When /^I open password change page with token "([^"]*)"$/
*/
public function iOpenPasswordChangePageWithToken(string $confirmationToken): void
{
$this->getPasswordResetChangePasswordPage()->open(['confirmationToken' => $confirmationToken]);
}
/**
* @Given /^I fill in new password with confirmation$/
*/
public function iFillInNewPasswordWithConfirmation(): void
{
$this->getPasswordResetChangePasswordPage()->fillForm();
}
/**
* @Given /^I fill in new password with invalid confirmation$/
*/
public function iFillInNewPasswordWithInvalidConfirmation(): void
{
$this->getPasswordResetChangePasswordPage()->fillFormWithInvalidData();
}
private function findUserByUsername(string $username): User
{
$user = $this->getRepository(User::class)->findOneBy(['username' => $username]);
Assertion::notNull($user, "No user for username \"{$username}\".");
return $user;
}
private function createToken(string $confirmationToken, DateInterval $ttl): Token
{
return new Token($confirmationToken, $this->clock, $ttl);
}
private function getPasswordResetRequestPage(): PasswordResetRequest
{
return $this->getPageObject(PasswordResetRequest::class);
}
private function getPasswordResetChangePasswordPage(): PasswordResetChangePassword
{
return $this->getPageObject(PasswordResetChangePassword::class);
}
}