Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Application/Nests/NestControllerTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Application\Nests;
4
5
use Illuminate\Http\Response;
6
use Pterodactyl\Contracts\Repository\NestRepositoryInterface;
7
use Pterodactyl\Transformers\Api\Application\NestTransformer;
8
use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;
9
10
class NestControllerTest extends ApplicationApiIntegrationTestCase
11
{
12
private NestRepositoryInterface $repository;
13
14
/**
15
* Setup tests.
16
*/
17
public function setUp(): void
18
{
19
parent::setUp();
20
21
$this->repository = $this->app->make(NestRepositoryInterface::class);
22
}
23
24
/**
25
* Test that the expected nests are returned by the request.
26
*/
27
public function testNestResponse()
28
{
29
/** @var \Pterodactyl\Models\Nest[] $nests */
30
$nests = $this->repository->all();
31
32
$response = $this->getJson('/api/application/nests');
33
$response->assertStatus(Response::HTTP_OK);
34
$response->assertJsonCount(count($nests), 'data');
35
$response->assertJsonStructure([
36
'object',
37
'data' => [['object', 'attributes' => ['id', 'uuid', 'author', 'name', 'description', 'created_at', 'updated_at']]],
38
'meta' => ['pagination' => ['total', 'count', 'per_page', 'current_page', 'total_pages']],
39
]);
40
41
$response->assertJson([
42
'object' => 'list',
43
'data' => [],
44
'meta' => [
45
'pagination' => [
46
'total' => 4,
47
'count' => 4,
48
'per_page' => 50,
49
'current_page' => 1,
50
'total_pages' => 1,
51
],
52
],
53
]);
54
55
foreach ($nests as $nest) {
56
$response->assertJsonFragment([
57
'object' => 'nest',
58
'attributes' => $this->getTransformer(NestTransformer::class)->transform($nest),
59
]);
60
}
61
}
62
63
/**
64
* Test that getting a single nest returns the expected result.
65
*/
66
public function testSingleNestResponse()
67
{
68
$nest = $this->repository->find(1);
69
70
$response = $this->getJson('/api/application/nests/' . $nest->id);
71
$response->assertStatus(Response::HTTP_OK);
72
$response->assertJsonStructure([
73
'object',
74
'attributes' => ['id', 'uuid', 'author', 'name', 'description', 'created_at', 'updated_at'],
75
]);
76
77
$response->assertJson([
78
'object' => 'nest',
79
'attributes' => $this->getTransformer(NestTransformer::class)->transform($nest),
80
]);
81
}
82
83
/**
84
* Test that including eggs in the response works as expected.
85
*/
86
public function testSingleNestWithEggsIncluded()
87
{
88
$nest = $this->repository->find(1);
89
$nest->loadMissing('eggs');
90
91
$response = $this->getJson('/api/application/nests/' . $nest->id . '?include=servers,eggs');
92
$response->assertStatus(Response::HTTP_OK);
93
$response->assertJsonStructure([
94
'object',
95
'attributes' => [
96
'relationships' => [
97
'eggs' => ['object', 'data' => []],
98
'servers' => ['object', 'data' => []],
99
],
100
],
101
]);
102
103
$response->assertJsonCount(count($nest->getRelation('eggs')), 'attributes.relationships.eggs.data');
104
}
105
106
/**
107
* Test that a missing nest returns a 404 error.
108
*/
109
public function testGetMissingNest()
110
{
111
$response = $this->getJson('/api/application/nests/nil');
112
$this->assertNotFoundJson($response);
113
}
114
115
/**
116
* Test that an authentication error occurs if a key does not have permission
117
* to access a resource.
118
*/
119
public function testErrorReturnedIfNoPermission()
120
{
121
$nest = $this->repository->find(1);
122
$this->createNewDefaultApiKey($this->getApiUser(), ['r_nests' => 0]);
123
124
$response = $this->getJson('/api/application/nests/' . $nest->id);
125
$this->assertAccessDeniedJson($response);
126
}
127
}
128
129