1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace codecept\Helper; |
4
|
|
|
|
5
|
|
|
// here you can define custom actions |
6
|
|
|
// all public methods declared in helper class will be available in $I |
7
|
|
|
|
8
|
|
|
use Codeception\Module\REST; |
9
|
|
|
use Codeception\TestInterface; |
10
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Entities\Grant; |
11
|
|
|
use SlayerBirden\DataFlowServer\Authentication\Entities\Token; |
12
|
|
|
use SlayerBirden\DataFlowServer\Authorization\Entities\Permission; |
13
|
|
|
use SlayerBirden\DataFlowServer\Authorization\Service\ResourceManager; |
14
|
|
|
use SlayerBirden\DataFlowServer\Domain\Entities\User; |
15
|
|
|
|
16
|
|
|
class Api extends \Codeception\Module |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
private $userId; |
22
|
|
|
/** |
23
|
|
|
* @var int |
24
|
|
|
*/ |
25
|
|
|
private $tokenId; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param TestInterface $test |
29
|
|
|
* @throws \Codeception\Exception\ModuleException |
30
|
|
|
*/ |
31
|
|
|
public function _before(TestInterface $test) |
32
|
|
|
{ |
33
|
|
|
/** @var ZendExpressive3 $ze3I */ |
34
|
|
|
$ze3I = $this->getModule('\\' . ZendExpressive3::class); |
35
|
|
|
|
36
|
|
|
/** @var ResourceManager $resourceManager */ |
37
|
|
|
$resourceManager = $ze3I->container->get(ResourceManager::class); |
38
|
|
|
|
39
|
|
|
$resources = $resourceManager->getAllResources(); |
40
|
|
|
|
41
|
|
|
/** @var CleanDoctrine2 $doctrineI */ |
42
|
|
|
$doctrineI = $this->getModule('\\' . CleanDoctrine2::class); |
43
|
|
|
|
44
|
|
|
$this->userId = $doctrineI->haveInRepository(User::class, [ |
45
|
|
|
'first' => 'Tester', |
46
|
|
|
'last' => 'Tester', |
47
|
|
|
'email' => '[email protected]', |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
$user = $doctrineI->grabEntityFromRepository(User::class, ['id' => $this->userId]); |
51
|
|
|
|
52
|
|
|
$this->tokenId = $doctrineI->haveInRepository(Token::class, [ |
53
|
|
|
'owner' => $user, |
54
|
|
|
'token' => 'X-X-X', |
55
|
|
|
'active' => 1, |
56
|
|
|
'createdAt' => new \DateTime(), |
57
|
|
|
'due' => new \DateTime('2130-01-01 00:00:00'), |
58
|
|
|
]); |
59
|
|
|
|
60
|
|
|
$this->addPermissions($resources); |
61
|
|
|
$this->addGrants($resources); |
62
|
|
|
|
63
|
|
|
/** @var REST $I */ |
64
|
|
|
$I = $this->getModule('REST'); |
65
|
|
|
$I->amBearerAuthenticated('X-X-X'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getCurrentUserId(): int |
69
|
|
|
{ |
70
|
|
|
return $this->userId; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param array $resources |
75
|
|
|
* @throws \Codeception\Exception\ModuleException |
76
|
|
|
*/ |
77
|
|
|
private function addPermissions(array $resources) |
78
|
|
|
{ |
79
|
|
|
/** @var CleanDoctrine2 $I */ |
80
|
|
|
$I = $this->getModule('\\' . CleanDoctrine2::class); |
81
|
|
|
$user = $I->grabEntityFromRepository(User::class, ['id' => $this->userId]); |
82
|
|
|
|
83
|
|
|
foreach ($resources as $resource) { |
84
|
|
|
$I->haveInRepository(Permission::class, [ |
85
|
|
|
'user' => $user, |
86
|
|
|
'resource' => $resource, |
87
|
|
|
]); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $resources |
93
|
|
|
* @throws \Codeception\Exception\ModuleException |
94
|
|
|
*/ |
95
|
|
|
private function addGrants(array $resources) |
96
|
|
|
{ |
97
|
|
|
/** @var CleanDoctrine2 $I */ |
98
|
|
|
$I = $this->getModule('\\' . CleanDoctrine2::class); |
99
|
|
|
$token = $I->grabEntityFromRepository(Token::class, ['id' => $this->tokenId]); |
100
|
|
|
|
101
|
|
|
foreach ($resources as $resource) { |
102
|
|
|
$I->haveInRepository(Grant::class, [ |
103
|
|
|
'token' => $token, |
104
|
|
|
'resource' => $resource, |
105
|
|
|
]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|