Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Application/Location/LocationControllerTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Application\Location;
4
5
use Pterodactyl\Models\Node;
6
use Illuminate\Http\Response;
7
use Pterodactyl\Models\Location;
8
use Pterodactyl\Transformers\Api\Application\NodeTransformer;
9
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
10
use Pterodactyl\Transformers\Api\Application\LocationTransformer;
11
use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;
12
13
class LocationControllerTest extends ApplicationApiIntegrationTestCase
14
{
15
/**
16
* Test getting all locations through the API.
17
*/
18
public function testGetLocations()
19
{
20
$locations = Location::factory()->times(2)->create();
21
22
$response = $this->getJson('/api/application/locations?per_page=60');
23
$response->assertStatus(Response::HTTP_OK);
24
$response->assertJsonCount(2, 'data');
25
$response->assertJsonStructure([
26
'object',
27
'data' => [
28
['object', 'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at']],
29
['object', 'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at']],
30
],
31
'meta' => ['pagination' => ['total', 'count', 'per_page', 'current_page', 'total_pages']],
32
]);
33
34
$response
35
->assertJson([
36
'object' => 'list',
37
'data' => [[], []],
38
'meta' => [
39
'pagination' => [
40
'total' => 2,
41
'count' => 2,
42
'per_page' => 60,
43
'current_page' => 1,
44
'total_pages' => 1,
45
],
46
],
47
])
48
->assertJsonFragment([
49
'object' => 'location',
50
'attributes' => [
51
'id' => $locations[0]->id,
52
'short' => $locations[0]->short,
53
'long' => $locations[0]->long,
54
'created_at' => $this->formatTimestamp($locations[0]->created_at),
55
'updated_at' => $this->formatTimestamp($locations[0]->updated_at),
56
],
57
])->assertJsonFragment([
58
'object' => 'location',
59
'attributes' => [
60
'id' => $locations[1]->id,
61
'short' => $locations[1]->short,
62
'long' => $locations[1]->long,
63
'created_at' => $this->formatTimestamp($locations[1]->created_at),
64
'updated_at' => $this->formatTimestamp($locations[1]->updated_at),
65
],
66
]);
67
}
68
69
/**
70
* Test getting a single location on the API.
71
*/
72
public function testGetSingleLocation()
73
{
74
$location = Location::factory()->create();
75
76
$response = $this->getJson('/api/application/locations/' . $location->id);
77
$response->assertStatus(Response::HTTP_OK);
78
$response->assertJsonCount(2);
79
$response->assertJsonStructure(['object', 'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at']]);
80
$response->assertJson([
81
'object' => 'location',
82
'attributes' => [
83
'id' => $location->id,
84
'short' => $location->short,
85
'long' => $location->long,
86
'created_at' => $this->formatTimestamp($location->created_at),
87
'updated_at' => $this->formatTimestamp($location->updated_at),
88
],
89
], true);
90
}
91
92
/**
93
* Test that a location can be created.
94
*/
95
public function testCreateLocation()
96
{
97
$response = $this->postJson('/api/application/locations', [
98
'short' => 'inhouse',
99
'long' => 'This is my inhouse location',
100
]);
101
102
$response->assertStatus(Response::HTTP_CREATED);
103
$response->assertJsonCount(3);
104
$response->assertJsonStructure([
105
'object',
106
'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at'],
107
'meta' => ['resource'],
108
]);
109
110
$this->assertDatabaseHas('locations', ['short' => 'inhouse', 'long' => 'This is my inhouse location']);
111
112
$location = Location::where('short', 'inhouse')->first();
113
$response->assertJson([
114
'object' => 'location',
115
'attributes' => $this->getTransformer(LocationTransformer::class)->transform($location),
116
'meta' => [
117
'resource' => route('api.application.locations.view', $location->id),
118
],
119
], true);
120
}
121
122
/**
123
* Test that a location can be updated.
124
*/
125
public function testUpdateLocation()
126
{
127
$location = Location::factory()->create();
128
129
$response = $this->patchJson('/api/application/locations/' . $location->id, [
130
'short' => 'new inhouse',
131
'long' => 'This is my new inhouse location',
132
]);
133
$response->assertStatus(Response::HTTP_OK);
134
$response->assertJsonCount(2);
135
$response->assertJsonStructure([
136
'object',
137
'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at'],
138
]);
139
140
$this->assertDatabaseHas('locations', ['short' => 'new inhouse', 'long' => 'This is my new inhouse location']);
141
$location = $location->fresh();
142
143
$response->assertJson([
144
'object' => 'location',
145
'attributes' => $this->getTransformer(LocationTransformer::class)->transform($location),
146
]);
147
}
148
149
/**
150
* Test that a location can be deleted from the database.
151
*/
152
public function testDeleteLocation()
153
{
154
$location = Location::factory()->create();
155
$this->assertDatabaseHas('locations', ['id' => $location->id]);
156
157
$response = $this->delete('/api/application/locations/' . $location->id);
158
$response->assertStatus(Response::HTTP_NO_CONTENT);
159
160
$this->assertDatabaseMissing('locations', ['id' => $location->id]);
161
}
162
163
/**
164
* Test that all the defined relationships for a location can be loaded successfully.
165
*/
166
public function testRelationshipsCanBeLoaded()
167
{
168
$location = Location::factory()->create();
169
$server = $this->createServerModel(['user_id' => $this->getApiUser()->id, 'location_id' => $location->id]);
170
171
$response = $this->getJson('/api/application/locations/' . $location->id . '?include=servers,nodes');
172
$response->assertStatus(Response::HTTP_OK);
173
$response->assertJsonCount(2)->assertJsonCount(2, 'attributes.relationships');
174
$response->assertJsonStructure([
175
'attributes' => [
176
'relationships' => [
177
'nodes' => ['object', 'data' => [['attributes' => ['id']]]],
178
'servers' => ['object', 'data' => [['attributes' => ['id']]]],
179
],
180
],
181
]);
182
183
// Just assert that we see the expected relationship IDs in the response.
184
$response->assertJson([
185
'attributes' => [
186
'relationships' => [
187
'nodes' => [
188
'object' => 'list',
189
'data' => [
190
[
191
'object' => 'node',
192
'attributes' => $this->getTransformer(NodeTransformer::class)->transform($server->getRelation('node')),
193
],
194
],
195
],
196
'servers' => [
197
'object' => 'list',
198
'data' => [
199
[
200
'object' => 'server',
201
'attributes' => $this->getTransformer(ServerTransformer::class)->transform($server),
202
],
203
],
204
],
205
],
206
],
207
]);
208
}
209
210
/**
211
* Test that a relationship that an API key does not have permission to access
212
* cannot be loaded onto the model.
213
*/
214
public function testKeyWithoutPermissionCannotLoadRelationship()
215
{
216
$this->createNewDefaultApiKey($this->getApiUser(), ['r_nodes' => 0]);
217
218
$location = Location::factory()->create();
219
Node::factory()->create(['location_id' => $location->id]);
220
221
$response = $this->getJson('/api/application/locations/' . $location->id . '?include=nodes');
222
$response->assertStatus(Response::HTTP_OK);
223
$response->assertJsonCount(2)->assertJsonCount(1, 'attributes.relationships');
224
$response->assertJsonStructure([
225
'attributes' => [
226
'relationships' => [
227
'nodes' => ['object', 'attributes'],
228
],
229
],
230
]);
231
232
// Just assert that we see the expected relationship IDs in the response.
233
$response->assertJson([
234
'attributes' => [
235
'relationships' => [
236
'nodes' => [
237
'object' => 'null_resource',
238
'attributes' => null,
239
],
240
],
241
],
242
]);
243
}
244
245
/**
246
* Test that a missing location returns a 404 error.
247
*
248
* GET /api/application/locations/:id
249
*/
250
public function testGetMissingLocation()
251
{
252
$response = $this->getJson('/api/application/locations/nil');
253
$this->assertNotFoundJson($response);
254
}
255
256
/**
257
* Test that an authentication error occurs if a key does not have permission
258
* to access a resource.
259
*/
260
public function testErrorReturnedIfNoPermission()
261
{
262
$location = Location::factory()->create();
263
$this->createNewDefaultApiKey($this->getApiUser(), ['r_locations' => 0]);
264
265
$response = $this->getJson('/api/application/locations/' . $location->id);
266
$this->assertAccessDeniedJson($response);
267
}
268
}
269
270