1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace codecept\authentication; |
5
|
|
|
|
6
|
|
|
use codecept\ApiTester; |
7
|
|
|
use codecept\Helper\CleanDoctrine2; |
8
|
|
|
use codecept\Helper\ZendExpressive3; |
9
|
|
|
use Codeception\Util\HttpCode; |
10
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Entities\Password; |
11
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Service\PasswordManager; |
12
|
|
|
use SlayerBirden\DataFlowServer\Authorization\Entities\Permission; |
13
|
|
|
use SlayerBirden\DataFlowServer\Domain\Entities\User; |
14
|
|
|
|
15
|
|
|
class GetTokenCest |
|
|
|
|
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var CleanDoctrine2 |
19
|
|
|
*/ |
20
|
|
|
private $doctrine; |
21
|
|
|
/** |
22
|
|
|
* @var ZendExpressive3 |
23
|
|
|
*/ |
24
|
|
|
private $expressive; |
25
|
|
|
|
26
|
|
|
public function _inject(CleanDoctrine2 $cleanDoctrine2, ZendExpressive3 $expressive) |
27
|
|
|
{ |
28
|
|
|
$this->doctrine = $cleanDoctrine2; |
29
|
|
|
$this->expressive = $expressive; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function _before(ApiTester $I) |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$userId = $I->haveInRepository(User::class, [ |
35
|
|
|
'first' => 'Tester2', |
36
|
|
|
'last' => 'Tester2', |
37
|
|
|
'email' => '[email protected]', |
38
|
|
|
]); |
39
|
|
|
|
40
|
|
|
$user = $I->grabEntityFromRepository(User::class, ['id' => $userId]); |
41
|
|
|
|
42
|
|
|
$logger = new \Monolog\Logger('log', [ |
43
|
|
|
new \Monolog\Handler\NoopHandler() |
44
|
|
|
]); |
45
|
|
|
$passwordManager = new PasswordManager( |
46
|
|
|
$this->expressive->container->get('PasswordRepository'), |
47
|
|
|
$logger |
48
|
|
|
); |
49
|
|
|
$I->haveInRepository(Password::class, [ |
50
|
|
|
'owner' => $user, |
51
|
|
|
'hash' => $passwordManager->getHash('test123'), |
52
|
|
|
'createdAt' => new \DateTime(), |
53
|
|
|
'due' => new \DateTime('+1 year'), |
54
|
|
|
'active' => true, |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$resources = [ |
58
|
|
|
'create_password', |
59
|
|
|
]; |
60
|
|
|
foreach ($resources as $key => $resource) { |
61
|
|
|
$I->haveInRepository(Permission::class, [ |
62
|
|
|
'id' => ++$key, |
63
|
|
|
'user' => $user, |
64
|
|
|
'resource' => $resource, |
65
|
|
|
]); |
66
|
|
|
} |
67
|
|
|
// cancel current Auth header |
68
|
|
|
$I->deleteHeader('Authorization'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function createTokenSuccess(ApiTester $I) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$I->wantTo('get token for performing operations with the app'); |
74
|
|
|
|
75
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
76
|
|
|
$I->sendPOST('/gettoken', [ |
77
|
|
|
'user' => '[email protected]', |
78
|
|
|
'password' => 'test123', |
79
|
|
|
'resources' => [ |
80
|
|
|
'create_password', |
81
|
|
|
], |
82
|
|
|
]); |
83
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
84
|
|
|
$I->seeResponseContainsJson([ |
85
|
|
|
'data' => [ |
86
|
|
|
'token' => [ |
87
|
|
|
'owner' => [ |
88
|
|
|
'email' => '[email protected]', |
89
|
|
|
], |
90
|
|
|
'active' => 1, |
91
|
|
|
], |
92
|
|
|
], |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function createTokenWrongPassword(ApiTester $I) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
$I->wantTo('attempt to get token, but specify wrong password'); |
99
|
|
|
|
100
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
101
|
|
|
$I->sendPOST('/gettoken', [ |
102
|
|
|
'user' => '[email protected]', |
103
|
|
|
'password' => 'abracadabra111', |
104
|
|
|
'resources' => [ |
105
|
|
|
'create_password', |
106
|
|
|
], |
107
|
|
|
]); |
108
|
|
|
$I->seeResponseCodeIs(HttpCode::UNAUTHORIZED); |
109
|
|
|
$I->seeResponseContainsJson([ |
110
|
|
|
'data' => [ |
111
|
|
|
'token' => null, |
112
|
|
|
], |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function createTokenWrongNoPermissions(ApiTester $I) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$I->wantTo('attempt to get token for resource that is not permitted'); |
119
|
|
|
|
120
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
121
|
|
|
$I->sendPOST('/gettoken', [ |
122
|
|
|
'user' => '[email protected]', |
123
|
|
|
'password' => 'test123', |
124
|
|
|
'resources' => [ |
125
|
|
|
'get_tmp_token', |
126
|
|
|
], |
127
|
|
|
]); |
128
|
|
|
$I->seeResponseCodeIs(HttpCode::FORBIDDEN); |
129
|
|
|
$I->seeResponseContainsJson([ |
130
|
|
|
'data' => [ |
131
|
|
|
'token' => null, |
132
|
|
|
], |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function createTokenValidationError(ApiTester $I) |
|
|
|
|
137
|
|
|
{ |
138
|
|
|
$I->wantTo('attempt to get token with wrong parameters'); |
139
|
|
|
|
140
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
141
|
|
|
$I->sendPOST('/gettoken', [ |
142
|
|
|
'user' => '[email protected]', |
143
|
|
|
'resources' => [ |
144
|
|
|
'create_password', |
145
|
|
|
], |
146
|
|
|
]); |
147
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
148
|
|
|
$I->seeResponseContainsJson([ |
149
|
|
|
'data' => [ |
150
|
|
|
'token' => null, |
151
|
|
|
'validation' => [ |
152
|
|
|
[ |
153
|
|
|
'field' => 'password', |
154
|
|
|
], |
155
|
|
|
] |
156
|
|
|
], |
157
|
|
|
]); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function createTokenEmptyResources(ApiTester $I) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$I->wantTo('attempt to get token with empty resources'); |
163
|
|
|
|
164
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
165
|
|
|
$I->sendPOST('/gettoken', [ |
166
|
|
|
'user' => '[email protected]', |
167
|
|
|
'password' => 'test123', |
168
|
|
|
]); |
169
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
170
|
|
|
$I->seeResponseContainsJson(['validation' => [ |
171
|
|
|
[ |
172
|
|
|
'field' => 'resources', |
173
|
|
|
], |
174
|
|
|
]]); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|