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