Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Application/ApplicationApiIntegrationTestCase.php
7460 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Application;
4
5
use Illuminate\Http\Request;
6
use Pterodactyl\Models\User;
7
use PHPUnit\Framework\Assert;
8
use Pterodactyl\Models\ApiKey;
9
use Pterodactyl\Services\Acl\Api\AdminAcl;
10
use Pterodactyl\Tests\Integration\IntegrationTestCase;
11
use Illuminate\Foundation\Testing\DatabaseTransactions;
12
use Pterodactyl\Tests\Traits\Integration\CreatesTestModels;
13
use Pterodactyl\Transformers\Api\Application\BaseTransformer;
14
use Pterodactyl\Transformers\Api\Client\BaseClientTransformer;
15
use Pterodactyl\Tests\Traits\Http\IntegrationJsonRequestAssertions;
16
17
abstract class ApplicationApiIntegrationTestCase extends IntegrationTestCase
18
{
19
use CreatesTestModels;
20
use DatabaseTransactions;
21
use IntegrationJsonRequestAssertions;
22
23
private ApiKey $key;
24
25
private User $user;
26
27
/**
28
* Bootstrap application API tests. Creates a default admin user and associated API key
29
* and also sets some default headers required for accessing the API.
30
*/
31
public function setUp(): void
32
{
33
parent::setUp();
34
35
$this->user = $this->createApiUser();
36
$this->key = $this->createApiKey($this->user);
37
38
$this
39
->withHeader('Accept', 'application/vnd.pterodactyl.v1+json')
40
->withHeader('Authorization', 'Bearer ' . $this->key->identifier . decrypt($this->key->token));
41
}
42
43
public function getApiUser(): User
44
{
45
return $this->user;
46
}
47
48
public function getApiKey(): ApiKey
49
{
50
return $this->key;
51
}
52
53
/**
54
* Creates a new default API key and refreshes the headers using it.
55
*/
56
protected function createNewDefaultApiKey(User $user, array $permissions = []): ApiKey
57
{
58
$this->key = $this->createApiKey($user, $permissions);
59
60
$this->withHeader('Authorization', 'Bearer ' . $this->key->identifier . decrypt($this->key->token));
61
62
return $this->key;
63
}
64
65
/**
66
* Create an administrative user.
67
*/
68
protected function createApiUser(): User
69
{
70
return User::factory()->create([
71
'root_admin' => true,
72
]);
73
}
74
75
/**
76
* Create a new application API key for a given user model.
77
*/
78
protected function createApiKey(User $user, array $permissions = []): ApiKey
79
{
80
return ApiKey::factory()->create(array_merge([
81
'user_id' => $user->id,
82
'key_type' => ApiKey::TYPE_APPLICATION,
83
'r_servers' => AdminAcl::READ | AdminAcl::WRITE,
84
'r_nodes' => AdminAcl::READ | AdminAcl::WRITE,
85
'r_allocations' => AdminAcl::READ | AdminAcl::WRITE,
86
'r_users' => AdminAcl::READ | AdminAcl::WRITE,
87
'r_locations' => AdminAcl::READ | AdminAcl::WRITE,
88
'r_nests' => AdminAcl::READ | AdminAcl::WRITE,
89
'r_eggs' => AdminAcl::READ | AdminAcl::WRITE,
90
'r_database_hosts' => AdminAcl::READ | AdminAcl::WRITE,
91
'r_server_databases' => AdminAcl::READ | AdminAcl::WRITE,
92
], $permissions));
93
}
94
95
/**
96
* Return a transformer that can be used for testing purposes.
97
*/
98
protected function getTransformer(string $abstract): BaseTransformer
99
{
100
$request = Request::createFromGlobals();
101
$request->setUserResolver(function () {
102
return $this->getApiKey()->user;
103
});
104
105
$transformer = $abstract::fromRequest($request);
106
107
Assert::assertInstanceOf(BaseTransformer::class, $transformer);
108
Assert::assertNotInstanceOf(BaseClientTransformer::class, $transformer);
109
110
return $transformer;
111
}
112
}
113
114