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