|
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\Authentication\Entities\Grant; |
|
9
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Entities\Token; |
|
10
|
|
|
use SlayerBirden\DataFlowServer\Domain\Entities\User; |
|
11
|
|
|
|
|
12
|
|
|
class InvalidateTokenCest |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var int |
|
16
|
|
|
*/ |
|
17
|
|
|
private $tokenId; |
|
18
|
|
|
|
|
19
|
|
|
public function _before(ApiTester $I) |
|
|
|
|
|
|
20
|
|
|
{ |
|
21
|
|
|
$userId = $I->haveInRepository(User::class, [ |
|
22
|
|
|
'first' => 'Tester2', |
|
23
|
|
|
'last' => 'Tester2', |
|
24
|
|
|
'email' => '[email protected]', |
|
25
|
|
|
]); |
|
26
|
|
|
|
|
27
|
|
|
$user = $I->grabEntityFromRepository(User::class, ['id' => $userId]); |
|
28
|
|
|
|
|
29
|
|
|
$this->tokenId = $I->haveInRepository(Token::class, [ |
|
30
|
|
|
'owner' => $user, |
|
31
|
|
|
'active' => true, |
|
32
|
|
|
'token' => 'yyy', |
|
33
|
|
|
'due' => new \DateTime('+1 year'), |
|
34
|
|
|
'createdAt' => new \DateTime(), |
|
35
|
|
|
]); |
|
36
|
|
|
|
|
37
|
|
|
$token = $I->grabEntityFromRepository(Token::class, ['id' => $this->tokenId]); |
|
38
|
|
|
|
|
39
|
|
|
$I->haveInRepository(Grant::class, [ |
|
40
|
|
|
'token' => $token, |
|
41
|
|
|
'resource' => 'invalidate_token', |
|
42
|
|
|
]); |
|
43
|
|
|
|
|
44
|
|
|
$I->amBearerAuthenticated('yyy'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function invalidateTokenSuccess(ApiTester $I) |
|
|
|
|
|
|
48
|
|
|
{ |
|
49
|
|
|
$I->wantTo('invalidate my token'); |
|
50
|
|
|
|
|
51
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
52
|
|
|
$I->sendPut('/invalidatetoken/' . (string)$this->tokenId); |
|
53
|
|
|
$I->seeResponseCodeIs(HttpCode::OK); |
|
54
|
|
|
$I->seeResponseContainsJson([ |
|
55
|
|
|
'success' => true, |
|
56
|
|
|
'data' => [ |
|
57
|
|
|
'token' => [ |
|
58
|
|
|
'owner' => [ |
|
59
|
|
|
'email' => '[email protected]', |
|
60
|
|
|
], |
|
61
|
|
|
'active' => 0, |
|
62
|
|
|
], |
|
63
|
|
|
], |
|
64
|
|
|
]); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function invalidateTokenNoPermissions(ApiTester $I) |
|
|
|
|
|
|
68
|
|
|
{ |
|
69
|
|
|
$I->wantTo('invalidate someone elses token'); |
|
70
|
|
|
|
|
71
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
72
|
|
|
$I->sendPut('/invalidatetoken/1'); |
|
73
|
|
|
$I->seeResponseCodeIs(HttpCode::FORBIDDEN); |
|
74
|
|
|
$I->seeResponseContainsJson([ |
|
75
|
|
|
'success' => false, |
|
76
|
|
|
'data' => [], |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function invalidateTokenCanNotFindToken(ApiTester $I) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
$I->wantTo('invalidate token that doesn\'t exist'); |
|
83
|
|
|
|
|
84
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json'); |
|
85
|
|
|
$I->sendPut('/invalidatetoken/100'); |
|
86
|
|
|
$I->seeResponseCodeIs(HttpCode::NOT_FOUND); |
|
87
|
|
|
$I->seeResponseContainsJson([ |
|
88
|
|
|
'success' => false, |
|
89
|
|
|
'data' => [ |
|
90
|
|
|
'token' => null, |
|
91
|
|
|
], |
|
92
|
|
|
]); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
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.