forked from Codeception/module-symfony
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurityAssertionsTrait.php
More file actions
173 lines (154 loc) · 4.96 KB
/
SecurityAssertionsTrait.php
File metadata and controls
173 lines (154 loc) · 4.96 KB
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
namespace Codeception\Module\Symfony;
use PHPUnit\Framework\Assert;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use function sprintf;
trait SecurityAssertionsTrait
{
/**
* Check that user is not authenticated.
*
* ```php
* <?php
* $I->dontSeeAuthentication();
* ```
*/
public function dontSeeAuthentication(): void
{
$security = $this->grabSecurityService();
$this->assertFalse(
$security->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY),
'There is an user authenticated.'
);
}
/**
* Check that user is not authenticated with the 'remember me' option.
*
* ```php
* <?php
* $I->dontSeeRememberedAuthentication();
* ```
*/
public function dontSeeRememberedAuthentication(): void
{
$security = $this->grabSecurityService();
$client = $this->getClient();
$hasCookie = $client->getCookieJar()->get('REMEMBERME') !== null;
$hasRole = $security->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED);
$this->assertFalse($hasCookie && $hasRole, 'User does have remembered authentication.');
}
/**
* Checks that a user is authenticated.
*
* ```php
* <?php
* $I->seeAuthentication();
* ```
*/
public function seeAuthentication(): void
{
$security = $this->grabSecurityService();
$this->assertTrue(
$security->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_FULLY),
'There is no authenticated user.'
);
}
/**
* Checks that a user is authenticated with the 'remember me' option.
*
* ```php
* <?php
* $I->seeRememberedAuthentication();
* ```
*/
public function seeRememberedAuthentication(): void
{
$security = $this->grabSecurityService();
$client = $this->getClient();
$hasCookie = $client->getCookieJar()->get('REMEMBERME') !== null;
$hasRole = $security->isGranted(AuthenticatedVoter::IS_AUTHENTICATED_REMEMBERED);
$this->assertTrue($hasCookie && $hasRole, 'User does not have remembered authentication.');
}
/**
* Check that the current user has a role
*
* ```php
* <?php
* $I->seeUserHasRole('ROLE_ADMIN');
* ```
*/
public function seeUserHasRole(string $role): void
{
$user = $this->getAuthenticatedUser();
$identifier = $user->getUserIdentifier();
$this->assertTrue(
$this->grabSecurityService()->isGranted($role),
sprintf('User %s has no role %s', $identifier, $role)
);
}
/**
* Verifies that the current user has multiple roles
*
* ```php
* <?php
* $I->seeUserHasRoles(['ROLE_USER', 'ROLE_ADMIN']);
* ```
*
* @param string[] $roles
*/
public function seeUserHasRoles(array $roles): void
{
foreach ($roles as $role) {
$this->seeUserHasRole($role);
}
}
/**
* Checks that the user's password would not benefit from rehashing.
* If the user is not provided, it is taken from the current session.
*
* You might use this function after performing tasks like registering a user or submitting a password update form.
*
* ```php
* <?php
* $I->seeUserPasswordDoesNotNeedRehash();
* $I->seeUserPasswordDoesNotNeedRehash($user);
* ```
*
* @param UserInterface|null $user
*/
public function seeUserPasswordDoesNotNeedRehash(?UserInterface $user = null): void
{
$userToValidate = $user ?? $this->getAuthenticatedUser();
if (!$userToValidate instanceof PasswordAuthenticatedUserInterface) {
Assert::fail('Provided user does not implement PasswordAuthenticatedUserInterface.');
}
$hasher = $this->grabPasswordHasherService();
$this->assertFalse($hasher->needsRehash($userToValidate), 'User password needs rehash.');
}
private function getAuthenticatedUser(): UserInterface
{
$user = $this->grabSecurityService()->getUser();
if ($user === null) {
Assert::fail('No user found in session to perform this check.');
}
return $user;
}
/** @return Security */
protected function grabSecurityService()
{
/** @var Security $security */
$security = $this->grabService('security.helper');
return $security;
}
protected function grabPasswordHasherService(): UserPasswordHasherInterface
{
/** @var UserPasswordHasherInterface $hasher */
$hasher = $this->getService('security.password_hasher');
return $hasher;
}
}