|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace codecept\authorization; |
|
4
|
|
|
|
|
5
|
|
|
use codecept\ApiTester; |
|
6
|
|
|
use Codeception\Util\HttpCode; |
|
7
|
|
|
use SlayerBirden\DataFlowServer\Authorization\Entities\History; |
|
8
|
|
|
use SlayerBirden\DataFlowServer\Authorization\Entities\Permission; |
|
9
|
|
|
use SlayerBirden\DataFlowServer\Domain\Entities\User; |
|
10
|
|
|
|
|
11
|
|
|
class SavePermissionsCest |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var int |
|
15
|
|
|
*/ |
|
16
|
|
|
private $userId; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param ApiTester $I |
|
20
|
|
|
* @throws \Exception |
|
21
|
|
|
*/ |
|
22
|
|
|
public function _before(ApiTester $I) |
|
|
|
|
|
|
23
|
|
|
{ |
|
24
|
|
|
$this->userId = $I->haveInRepository(User::class, [ |
|
25
|
|
|
'first' => 'Tester2', |
|
26
|
|
|
'last' => 'Tester2', |
|
27
|
|
|
'email' => '[email protected]', |
|
28
|
|
|
]); |
|
29
|
|
|
|
|
30
|
|
|
$user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]); |
|
31
|
|
|
$resources = [ |
|
32
|
|
|
'get_tmp_token', |
|
33
|
|
|
'save_permissions', |
|
34
|
|
|
]; |
|
35
|
|
|
foreach ($resources as $key => $resource) { |
|
36
|
|
|
$I->haveInRepository(Permission::class, [ |
|
37
|
|
|
'id' => ++$key, |
|
38
|
|
|
'user' => $user, |
|
39
|
|
|
'resource' => $resource, |
|
40
|
|
|
]); |
|
41
|
|
|
} |
|
42
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
43
|
|
|
$I->sendPOST('/gettmptoken/' . $this->userId, [ |
|
44
|
|
|
'resources' => [ |
|
45
|
|
|
'save_permissions' |
|
46
|
|
|
], |
|
47
|
|
|
]); |
|
48
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
|
49
|
|
|
$tmpToken = $I->grabDataFromResponseByJsonPath('data.token.token')[0]; |
|
50
|
|
|
|
|
51
|
|
|
$I->amBearerAuthenticated($tmpToken); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function savePermissionsSuccess(ApiTester $I) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
$I->wantTo('successfully save permissions'); |
|
57
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
58
|
|
|
$I->sendPut('/permissions/' . $this->userId, [ |
|
59
|
|
|
'resources' => [ |
|
60
|
|
|
'save_permissions', |
|
61
|
|
|
'get_tmp_token', |
|
62
|
|
|
'create_password', |
|
63
|
|
|
], |
|
64
|
|
|
]); |
|
65
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
|
66
|
|
|
$I->seeResponseContainsJson(['resource' => 'create_password']); |
|
67
|
|
|
$I->seeResponseContainsJson(['resource' => 'get_tmp_token']); |
|
68
|
|
|
$I->seeResponseContainsJson(['resource' => 'create_password']); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function savePermissionsNoChanges(ApiTester $I) |
|
|
|
|
|
|
72
|
|
|
{ |
|
73
|
|
|
$I->wantTo('re-save existing permissions'); |
|
74
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
75
|
|
|
$I->sendPut('/permissions/' . $this->userId, [ |
|
76
|
|
|
'resources' => [ |
|
77
|
|
|
'get_tmp_token', |
|
78
|
|
|
'save_permissions', |
|
79
|
|
|
], |
|
80
|
|
|
]); |
|
81
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
|
82
|
|
|
$I->seeResponseContainsJson(['permissions' => []]); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function setForNonExistingResources(ApiTester $I) |
|
|
|
|
|
|
86
|
|
|
{ |
|
87
|
|
|
$I->wantTo('set permissions for non existing resources'); |
|
88
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
89
|
|
|
$I->sendPut('/permissions/' . $this->userId, [ |
|
90
|
|
|
'resources' => [ |
|
91
|
|
|
'i_do_not_exist', |
|
92
|
|
|
], |
|
93
|
|
|
]); |
|
94
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
|
95
|
|
|
$I->seeResponseContainsJson(['validation' => [ |
|
96
|
|
|
'field' => 'resources' |
|
97
|
|
|
]]); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
public function tryToSendWithNoResource(ApiTester $I) |
|
|
|
|
|
|
101
|
|
|
{ |
|
102
|
|
|
$I->wantTo('send request with no resources'); |
|
103
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
104
|
|
|
$I->sendPut('/permissions/' . $this->userId, []); |
|
105
|
|
|
$I->seeResponseCodeIs(HttpCode::BAD_REQUEST); |
|
106
|
|
|
$I->seeResponseContainsJson(['validation' => [ |
|
107
|
|
|
'field' => 'resources' |
|
108
|
|
|
]]); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function countHistoryChanges(ApiTester $I) |
|
|
|
|
|
|
112
|
|
|
{ |
|
113
|
|
|
$I->wantTo('count history changes'); |
|
114
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
115
|
|
|
$I->sendPut('/permissions/' . $this->userId, [ |
|
116
|
|
|
'resources' => [], |
|
117
|
|
|
]); |
|
118
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
|
119
|
|
|
// remove 2 |
|
120
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
121
|
|
|
$I->sendPut('/permissions/' . $this->userId, [ |
|
122
|
|
|
'resources' => [ |
|
123
|
|
|
'save_permissions', |
|
124
|
|
|
'get_tmp_token', |
|
125
|
|
|
'create_password', |
|
126
|
|
|
], |
|
127
|
|
|
]); |
|
128
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
|
129
|
|
|
// add 3 |
|
130
|
|
|
$historyRecords = $I->grabEntitiesFromRepository(History::class); |
|
131
|
|
|
$I->assertCount(5, $historyRecords); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
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.