|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace codecept\authentication; |
|
5
|
|
|
|
|
6
|
|
|
use codecept\ApiTester; |
|
7
|
|
|
use Codeception\Util\HttpCode; |
|
8
|
|
|
use SlayerBirden\DataFlowServer\Authorization\Entities\Permission; |
|
9
|
|
|
use SlayerBirden\DataFlowServer\Domain\Entities\User; |
|
10
|
|
|
|
|
11
|
|
|
class GenerateTmpTokenCest |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var int |
|
15
|
|
|
*/ |
|
16
|
|
|
private $userId; |
|
17
|
|
|
|
|
18
|
|
|
public function _before(ApiTester $I) |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
$this->userId = $I->haveInRepository(User::class, [ |
|
21
|
|
|
'first' => 'Tester2', |
|
22
|
|
|
'last' => 'Tester2', |
|
23
|
|
|
'email' => '[email protected]', |
|
24
|
|
|
]); |
|
25
|
|
|
|
|
26
|
|
|
$user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]); |
|
27
|
|
|
$resources = [ |
|
28
|
|
|
'create_password', |
|
29
|
|
|
'get_tmp_token', |
|
30
|
|
|
]; |
|
31
|
|
|
foreach ($resources as $key => $resource) { |
|
32
|
|
|
$I->haveInRepository(Permission::class, [ |
|
33
|
|
|
'id' => ++$key, |
|
34
|
|
|
'user' => $user, |
|
35
|
|
|
'resource' => $resource, |
|
36
|
|
|
]); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param ApiTester $I |
|
42
|
|
|
* @throws \Exception |
|
43
|
|
|
*/ |
|
44
|
|
|
public function createTmpTokenSuccess(ApiTester $I) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
$I->wantTo('create tmp token'); |
|
47
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
48
|
|
|
$I->sendPOST('/gettmptoken/' . (string)$this->userId, [ |
|
49
|
|
|
'resources' => [ |
|
50
|
|
|
'create_password' |
|
51
|
|
|
], |
|
52
|
|
|
]); |
|
53
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
|
54
|
|
|
$I->seeResponseContainsJson([ |
|
55
|
|
|
'success' => true, |
|
56
|
|
|
'data' => [ |
|
57
|
|
|
'token' => [ |
|
58
|
|
|
'owner' => [ |
|
59
|
|
|
'email' => '[email protected]', |
|
60
|
|
|
], |
|
61
|
|
|
'active' => 1, |
|
62
|
|
|
], |
|
63
|
|
|
], |
|
64
|
|
|
]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param ApiTester $I |
|
69
|
|
|
* @throws \Exception |
|
70
|
|
|
*/ |
|
71
|
|
|
public function createTmpTokenForNonExistingUser(ApiTester $I) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$I->wantTo('create tmp token for non existing user'); |
|
74
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
75
|
|
|
$I->sendPOST('/gettmptoken/' . (string)($this->userId + 100), [ |
|
76
|
|
|
'resources' => [ |
|
77
|
|
|
'create_password' |
|
78
|
|
|
], |
|
79
|
|
|
]); |
|
80
|
|
|
$I->seeResponseCodeIs(HttpCode::NOT_FOUND); |
|
81
|
|
|
$I->seeResponseContainsJson([ |
|
82
|
|
|
'success' => false, |
|
83
|
|
|
]); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param ApiTester $I |
|
88
|
|
|
* @throws \Exception |
|
89
|
|
|
*/ |
|
90
|
|
|
public function createTmpTokenNotPermitted(ApiTester $I) |
|
|
|
|
|
|
91
|
|
|
{ |
|
92
|
|
|
$I->wantTo('create tmp token for resource without granted permission'); |
|
93
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
94
|
|
|
$I->sendPOST('/gettmptoken/' . (string)$this->userId, [ |
|
95
|
|
|
'resources' => [ |
|
96
|
|
|
'update_password' |
|
97
|
|
|
], |
|
98
|
|
|
]); |
|
99
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
|
100
|
|
|
$I->seeResponseContainsJson([ |
|
101
|
|
|
'success' => false, |
|
102
|
|
|
]); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param ApiTester $I |
|
107
|
|
|
* @throws \Exception |
|
108
|
|
|
*/ |
|
109
|
|
|
public function createTmpTokenValidationError(ApiTester $I) |
|
|
|
|
|
|
110
|
|
|
{ |
|
111
|
|
|
$I->wantTo('create tmp token wrong input'); |
|
112
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
113
|
|
|
$I->sendPOST('/gettmptoken/' . (string)$this->userId, [ |
|
114
|
|
|
'bar' => 'baz', |
|
115
|
|
|
]); |
|
116
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
|
117
|
|
|
$I->seeResponseContainsJson([ |
|
118
|
|
|
'success' => false, |
|
119
|
|
|
'data' => [ |
|
120
|
|
|
'validation' => [ |
|
121
|
|
|
[ |
|
122
|
|
|
'field' => 'resources' |
|
123
|
|
|
] |
|
124
|
|
|
] |
|
125
|
|
|
] |
|
126
|
|
|
]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param ApiTester $I |
|
131
|
|
|
* @throws \Exception |
|
132
|
|
|
*/ |
|
133
|
|
|
public function createTmpTokenValidationErrorNonExistingResource(ApiTester $I) |
|
|
|
|
|
|
134
|
|
|
{ |
|
135
|
|
|
$I->wantTo('create tmp token for non existing resource'); |
|
136
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
137
|
|
|
$I->sendPOST('/gettmptoken/' . (string)$this->userId, [ |
|
138
|
|
|
'resources' => [ |
|
139
|
|
|
'bar' |
|
140
|
|
|
], |
|
141
|
|
|
]); |
|
142
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
|
143
|
|
|
$I->seeResponseContainsJson([ |
|
144
|
|
|
'success' => false, |
|
145
|
|
|
]); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.