Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Application/Nests/EggControllerTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Application\Nests;
4
5
use Illuminate\Support\Arr;
6
use Pterodactyl\Models\Egg;
7
use Illuminate\Http\Response;
8
use Pterodactyl\Transformers\Api\Application\EggTransformer;
9
use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;
10
11
class EggControllerTest extends ApplicationApiIntegrationTestCase
12
{
13
/**
14
* Test that all the eggs belonging to a given nest can be returned.
15
*/
16
public function testListAllEggsInNest()
17
{
18
$eggs = Egg::query()->where('nest_id', 1)->get();
19
20
$response = $this->getJson('/api/application/nests/' . $eggs->first()->nest_id . '/eggs');
21
$response->assertStatus(Response::HTTP_OK);
22
$response->assertJsonCount(count($eggs), 'data');
23
$response->assertJsonStructure([
24
'object',
25
'data' => [
26
[
27
'object',
28
'attributes' => [
29
'id', 'uuid', 'nest', 'author', 'description', 'docker_image', 'startup', 'created_at', 'updated_at',
30
'script' => ['privileged', 'install', 'entry', 'container', 'extends'],
31
'config' => [
32
'files' => [],
33
'startup' => ['done'],
34
'stop',
35
'logs' => [],
36
'extends',
37
],
38
],
39
],
40
],
41
]);
42
43
foreach (array_get($response->json(), 'data') as $datum) {
44
$egg = $eggs->where('id', '=', $datum['attributes']['id'])->first();
45
46
$expected = json_encode(Arr::sortRecursive($datum['attributes']));
47
$actual = json_encode(Arr::sortRecursive($this->getTransformer(EggTransformer::class)->transform($egg)));
48
49
$this->assertSame(
50
$expected,
51
$actual,
52
'Unable to find JSON fragment: ' . PHP_EOL . PHP_EOL . "[$expected]" . PHP_EOL . PHP_EOL . 'within' . PHP_EOL . PHP_EOL . "[$actual]."
53
);
54
}
55
}
56
57
/**
58
* Test that a single egg can be returned.
59
*/
60
public function testReturnSingleEgg()
61
{
62
$egg = Egg::query()->findOrFail(1);
63
64
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/' . $egg->id);
65
$response->assertStatus(Response::HTTP_OK);
66
$response->assertJsonStructure([
67
'object',
68
'attributes' => [
69
'id', 'uuid', 'nest', 'author', 'description', 'docker_image', 'startup', 'script' => [], 'config' => [], 'created_at', 'updated_at',
70
],
71
]);
72
73
$response->assertJson([
74
'object' => 'egg',
75
'attributes' => $this->getTransformer(EggTransformer::class)->transform($egg),
76
], true);
77
}
78
79
/**
80
* Test that a single egg and all the defined relationships can be returned.
81
*/
82
public function testReturnSingleEggWithRelationships()
83
{
84
$egg = Egg::query()->findOrFail(1);
85
86
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/' . $egg->id . '?include=servers,variables,nest');
87
$response->assertStatus(Response::HTTP_OK);
88
$response->assertJsonStructure([
89
'object',
90
'attributes' => [
91
'relationships' => [
92
'nest' => ['object', 'attributes'],
93
'servers' => ['object', 'data' => []],
94
'variables' => ['object', 'data' => []],
95
],
96
],
97
]);
98
}
99
100
/**
101
* Test that a missing egg returns a 404 error.
102
*/
103
public function testGetMissingEgg()
104
{
105
$egg = Egg::query()->findOrFail(1);
106
107
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/nil');
108
$this->assertNotFoundJson($response);
109
}
110
111
/**
112
* Test that an authentication error occurs if a key does not have permission
113
* to access a resource.
114
*/
115
public function testErrorReturnedIfNoPermission()
116
{
117
$egg = Egg::query()->findOrFail(1);
118
$this->createNewDefaultApiKey($this->getApiUser(), ['r_eggs' => 0]);
119
120
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs');
121
$this->assertAccessDeniedJson($response);
122
}
123
}
124
125