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