1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace codecept; |
5
|
|
|
|
6
|
|
|
use codecept\Helper\CleanDoctrine2; |
7
|
|
|
use Codeception\Util\HttpCode; |
8
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Entities\Grant; |
9
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Entities\Password; |
10
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Entities\Token; |
11
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Repository\PasswordRepository; |
12
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Service\PasswordManager; |
13
|
|
|
use SlayerBirden\DataFlowServer\Domain\Entities\User; |
14
|
|
|
|
15
|
|
|
class UpdatePasswordCest |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var CleanDoctrine2 |
19
|
|
|
*/ |
20
|
|
|
private $doctrine; |
21
|
|
|
/** |
22
|
|
|
* @var int |
23
|
|
|
*/ |
24
|
|
|
private $userId; |
25
|
|
|
/** |
26
|
|
|
* @var \Psr\Log\LoggerInterface |
27
|
|
|
*/ |
28
|
|
|
private $logger; |
29
|
|
|
/** |
30
|
|
|
* @var PasswordManager |
31
|
|
|
*/ |
32
|
|
|
private $passwordManager; |
33
|
|
|
|
34
|
|
|
public function _inject(CleanDoctrine2 $cleanDoctrine2) |
35
|
|
|
{ |
36
|
|
|
$this->doctrine = $cleanDoctrine2; |
37
|
|
|
$this->logger = new \Monolog\Logger('log', [ |
38
|
|
|
new \Monolog\Handler\NoopHandler() |
39
|
|
|
]); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function _before(ApiTester $I) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$this->userId = $I->haveInRepository(User::class, [ |
45
|
|
|
'first' => 'Tester2', |
46
|
|
|
'last' => 'Tester2', |
47
|
|
|
'email' => '[email protected]', |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
$user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]); |
51
|
|
|
$this->passwordManager = new PasswordManager( |
52
|
|
|
new PasswordRepository($this->doctrine->registry), |
53
|
|
|
$this->logger |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
$I->haveInRepository(Password::class, [ |
57
|
|
|
'owner' => $user, |
58
|
|
|
'hash' => $this->passwordManager->getHash('test123'), |
59
|
|
|
'createdAt' => new \DateTime(), |
60
|
|
|
'due' => new \DateTime('+1 year'), |
61
|
|
|
'active' => true, |
62
|
|
|
]); |
63
|
|
|
|
64
|
|
|
$tokenId = $I->haveInRepository(Token::class, [ |
65
|
|
|
'owner' => $user, |
66
|
|
|
'active' => true, |
67
|
|
|
'token' => 'yyy', |
68
|
|
|
'due' => new \DateTime('+1 year'), |
69
|
|
|
'createdAt' => new \DateTime(), |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
$token = $I->grabEntityFromRepository(Token::class, ['id' => $tokenId]); |
73
|
|
|
|
74
|
|
|
$I->haveInRepository(Grant::class, [ |
75
|
|
|
'token' => $token, |
76
|
|
|
'resource' => 'update_password', |
77
|
|
|
]); |
78
|
|
|
|
79
|
|
|
$I->amBearerAuthenticated('yyy'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function updatePasswordSuccess(ApiTester $I) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$I->wantTo('update my password'); |
85
|
|
|
|
86
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
87
|
|
|
$I->sendPost('/updatepassword', [ |
88
|
|
|
'password' => 'test123', |
89
|
|
|
'new_password' => 'there is a clown on a wing', |
90
|
|
|
]); |
91
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
92
|
|
|
$I->seeResponseContainsJson([ |
93
|
|
|
'success' => true, |
94
|
|
|
]); |
95
|
|
|
|
96
|
|
|
// check that new password works |
97
|
|
|
/** @var User $user */ |
98
|
|
|
$user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]); |
99
|
|
|
$valid = $this->passwordManager->isValidForUser('there is a clown on a wing', $user); |
100
|
|
|
|
101
|
|
|
$I->assertSame(true, $valid); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function updatePasswordValidationError(ApiTester $I) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
$I->wantTo('update my password but providing invalid data'); |
107
|
|
|
|
108
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
109
|
|
|
$I->sendPost('/updatepassword', [ |
110
|
|
|
'password' => 'test123', |
111
|
|
|
'new_password' => 'short', |
112
|
|
|
]); |
113
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
114
|
|
|
$I->seeResponseContainsJson([ |
115
|
|
|
'success' => false, |
116
|
|
|
'data' => [ |
117
|
|
|
'validation' => [ |
118
|
|
|
[ |
119
|
|
|
'field' => 'new_password', |
120
|
|
|
] |
121
|
|
|
] |
122
|
|
|
] |
123
|
|
|
]); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function updatePasswordOldPasswordWrong(ApiTester $I) |
|
|
|
|
127
|
|
|
{ |
128
|
|
|
$I->wantTo('update my password but providing wrong old pw'); |
129
|
|
|
|
130
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
131
|
|
|
$I->sendPost('/updatepassword', [ |
132
|
|
|
'password' => 'forgot', |
133
|
|
|
'new_password' => 'cool new password', |
134
|
|
|
]); |
135
|
|
|
$I->seeResponseCodeIs(HttpCode::PRECONDITION_FAILED); |
136
|
|
|
$I->seeResponseContainsJson([ |
137
|
|
|
'success' => false, |
138
|
|
|
]); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function updatePasswordWithoutProvidingOld(ApiTester $I) |
|
|
|
|
142
|
|
|
{ |
143
|
|
|
$I->wantTo('update my password without providing old one'); |
144
|
|
|
|
145
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
146
|
|
|
$I->sendPost('/updatepassword', [ |
147
|
|
|
'new_password' => 'cool new password', |
148
|
|
|
]); |
149
|
|
|
$I->seeResponseCodeIs(HttpCode::PRECONDITION_FAILED); |
150
|
|
|
$I->seeResponseContainsJson([ |
151
|
|
|
'success' => false, |
152
|
|
|
]); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function updatePasswordUseUsedPw(ApiTester $I) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$I->wantTo('update my password using old password which is already in the system'); |
158
|
|
|
|
159
|
|
|
$user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]); |
160
|
|
|
$I->haveInRepository(Password::class, [ |
161
|
|
|
'owner' => $user, |
162
|
|
|
'hash' => $this->passwordManager->getHash('old cool long password'), |
163
|
|
|
'createdAt' => new \DateTime('-1 year'), |
164
|
|
|
'due' => new \DateTime('-1 month'), |
165
|
|
|
'active' => false, |
166
|
|
|
]); |
167
|
|
|
|
168
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
169
|
|
|
$I->sendPost('/updatepassword', [ |
170
|
|
|
'password' => 'test123', |
171
|
|
|
'new_password' => 'old cool long password', |
172
|
|
|
]); |
173
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
174
|
|
|
$I->seeResponseContainsJson([ |
175
|
|
|
'success' => false, |
176
|
|
|
]); |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|