2018-03-04 22:30:16 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Tests\Integration\Api\Application\Location;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Node;
|
2018-03-05 04:35:57 +00:00
|
|
|
use Illuminate\Http\Response;
|
2018-03-04 22:30:16 +00:00
|
|
|
use Pterodactyl\Models\Location;
|
|
|
|
use Pterodactyl\Transformers\Api\Application\NodeTransformer;
|
|
|
|
use Pterodactyl\Transformers\Api\Application\ServerTransformer;
|
2022-05-04 23:01:29 +00:00
|
|
|
use Pterodactyl\Transformers\Api\Application\LocationTransformer;
|
2018-03-04 22:30:16 +00:00
|
|
|
use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;
|
|
|
|
|
|
|
|
class LocationControllerTest extends ApplicationApiIntegrationTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Test getting all locations through the API.
|
|
|
|
*/
|
|
|
|
public function testGetLocations()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$locations = Location::factory()->times(2)->create();
|
2018-03-04 22:30:16 +00:00
|
|
|
|
2021-04-17 19:02:08 +00:00
|
|
|
$response = $this->getJson('/api/application/locations?per_page=60');
|
2018-03-05 04:35:57 +00:00
|
|
|
$response->assertStatus(Response::HTTP_OK);
|
2018-03-04 22:30:16 +00:00
|
|
|
$response->assertJsonCount(2, 'data');
|
|
|
|
$response->assertJsonStructure([
|
|
|
|
'object',
|
|
|
|
'data' => [
|
|
|
|
['object', 'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at']],
|
|
|
|
['object', 'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at']],
|
|
|
|
],
|
|
|
|
'meta' => ['pagination' => ['total', 'count', 'per_page', 'current_page', 'total_pages']],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response
|
|
|
|
->assertJson([
|
|
|
|
'object' => 'list',
|
|
|
|
'data' => [[], []],
|
|
|
|
'meta' => [
|
|
|
|
'pagination' => [
|
|
|
|
'total' => 2,
|
|
|
|
'count' => 2,
|
2021-04-17 19:02:08 +00:00
|
|
|
'per_page' => 60,
|
2018-03-04 22:30:16 +00:00
|
|
|
'current_page' => 1,
|
|
|
|
'total_pages' => 1,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
])
|
|
|
|
->assertJsonFragment([
|
|
|
|
'object' => 'location',
|
|
|
|
'attributes' => [
|
|
|
|
'id' => $locations[0]->id,
|
|
|
|
'short' => $locations[0]->short,
|
|
|
|
'long' => $locations[0]->long,
|
|
|
|
'created_at' => $this->formatTimestamp($locations[0]->created_at),
|
|
|
|
'updated_at' => $this->formatTimestamp($locations[0]->updated_at),
|
|
|
|
],
|
|
|
|
])->assertJsonFragment([
|
|
|
|
'object' => 'location',
|
|
|
|
'attributes' => [
|
|
|
|
'id' => $locations[1]->id,
|
|
|
|
'short' => $locations[1]->short,
|
|
|
|
'long' => $locations[1]->long,
|
|
|
|
'created_at' => $this->formatTimestamp($locations[1]->created_at),
|
|
|
|
'updated_at' => $this->formatTimestamp($locations[1]->updated_at),
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting a single location on the API.
|
|
|
|
*/
|
|
|
|
public function testGetSingleLocation()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$location = Location::factory()->create();
|
2018-03-04 22:30:16 +00:00
|
|
|
|
2018-03-05 04:35:57 +00:00
|
|
|
$response = $this->getJson('/api/application/locations/' . $location->id);
|
|
|
|
$response->assertStatus(Response::HTTP_OK);
|
2018-03-04 22:30:16 +00:00
|
|
|
$response->assertJsonCount(2);
|
|
|
|
$response->assertJsonStructure(['object', 'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at']]);
|
|
|
|
$response->assertJson([
|
|
|
|
'object' => 'location',
|
|
|
|
'attributes' => [
|
|
|
|
'id' => $location->id,
|
|
|
|
'short' => $location->short,
|
|
|
|
'long' => $location->long,
|
|
|
|
'created_at' => $this->formatTimestamp($location->created_at),
|
|
|
|
'updated_at' => $this->formatTimestamp($location->updated_at),
|
|
|
|
],
|
|
|
|
], true);
|
|
|
|
}
|
|
|
|
|
2021-12-04 18:50:36 +00:00
|
|
|
/**
|
|
|
|
* Test that a location can be created.
|
|
|
|
*/
|
|
|
|
public function testCreateLocation()
|
|
|
|
{
|
|
|
|
$response = $this->postJson('/api/application/locations', [
|
|
|
|
'short' => 'inhouse',
|
|
|
|
'long' => 'This is my inhouse location',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$response->assertStatus(Response::HTTP_CREATED);
|
|
|
|
$response->assertJsonCount(3);
|
|
|
|
$response->assertJsonStructure([
|
|
|
|
'object',
|
|
|
|
'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at'],
|
|
|
|
'meta' => ['resource'],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('locations', ['short' => 'inhouse', 'long' => 'This is my inhouse location']);
|
|
|
|
|
|
|
|
$location = Location::where('short', 'inhouse')->first();
|
|
|
|
$response->assertJson([
|
|
|
|
'object' => 'location',
|
|
|
|
'attributes' => $this->getTransformer(LocationTransformer::class)->transform($location),
|
|
|
|
'meta' => [
|
|
|
|
'resource' => route('api.application.locations.view', $location->id),
|
|
|
|
],
|
|
|
|
], true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a location can be updated.
|
|
|
|
*/
|
|
|
|
public function testUpdateLocation()
|
|
|
|
{
|
|
|
|
$location = Location::factory()->create();
|
|
|
|
|
|
|
|
$response = $this->patchJson('/api/application/locations/' . $location->id, [
|
|
|
|
'short' => 'new inhouse',
|
2022-05-04 23:01:29 +00:00
|
|
|
'long' => 'This is my new inhouse location',
|
2021-12-04 18:50:36 +00:00
|
|
|
]);
|
|
|
|
$response->assertStatus(Response::HTTP_OK);
|
|
|
|
$response->assertJsonCount(2);
|
|
|
|
$response->assertJsonStructure([
|
|
|
|
'object',
|
2022-05-04 23:01:29 +00:00
|
|
|
'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at'],
|
2021-12-04 18:50:36 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertDatabaseHas('locations', ['short' => 'new inhouse', 'long' => 'This is my new inhouse location']);
|
|
|
|
$location = $location->fresh();
|
|
|
|
|
|
|
|
$response->assertJson([
|
|
|
|
'object' => 'location',
|
|
|
|
'attributes' => $this->getTransformer(LocationTransformer::class)->transform($location),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a location can be deleted from the database.
|
|
|
|
*/
|
|
|
|
public function testDeleteLocation()
|
|
|
|
{
|
|
|
|
$location = Location::factory()->create();
|
|
|
|
$this->assertDatabaseHas('locations', ['id' => $location->id]);
|
|
|
|
|
|
|
|
$response = $this->delete('/api/application/locations/' . $location->id);
|
|
|
|
$response->assertStatus(Response::HTTP_NO_CONTENT);
|
|
|
|
|
|
|
|
$this->assertDatabaseMissing('locations', ['id' => $location->id]);
|
|
|
|
}
|
|
|
|
|
2018-03-04 22:30:16 +00:00
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* Test that all the defined relationships for a location can be loaded successfully.
|
2018-03-04 22:30:16 +00:00
|
|
|
*/
|
|
|
|
public function testRelationshipsCanBeLoaded()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$location = Location::factory()->create();
|
2018-03-04 22:30:16 +00:00
|
|
|
$server = $this->createServerModel(['user_id' => $this->getApiUser()->id, 'location_id' => $location->id]);
|
|
|
|
|
2018-03-05 04:35:57 +00:00
|
|
|
$response = $this->getJson('/api/application/locations/' . $location->id . '?include=servers,nodes');
|
|
|
|
$response->assertStatus(Response::HTTP_OK);
|
2018-03-04 22:30:16 +00:00
|
|
|
$response->assertJsonCount(2)->assertJsonCount(2, 'attributes.relationships');
|
|
|
|
$response->assertJsonStructure([
|
|
|
|
'attributes' => [
|
|
|
|
'relationships' => [
|
|
|
|
'nodes' => ['object', 'data' => [['attributes' => ['id']]]],
|
|
|
|
'servers' => ['object', 'data' => [['attributes' => ['id']]]],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Just assert that we see the expected relationship IDs in the response.
|
|
|
|
$response->assertJson([
|
|
|
|
'attributes' => [
|
|
|
|
'relationships' => [
|
|
|
|
'nodes' => [
|
|
|
|
'object' => 'list',
|
|
|
|
'data' => [
|
|
|
|
[
|
|
|
|
'object' => 'node',
|
|
|
|
'attributes' => $this->getTransformer(NodeTransformer::class)->transform($server->getRelation('node')),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'servers' => [
|
|
|
|
'object' => 'list',
|
|
|
|
'data' => [
|
|
|
|
[
|
|
|
|
'object' => 'server',
|
|
|
|
'attributes' => $this->getTransformer(ServerTransformer::class)->transform($server),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a relationship that an API key does not have permission to access
|
|
|
|
* cannot be loaded onto the model.
|
|
|
|
*/
|
|
|
|
public function testKeyWithoutPermissionCannotLoadRelationship()
|
|
|
|
{
|
|
|
|
$this->createNewDefaultApiKey($this->getApiUser(), ['r_nodes' => 0]);
|
|
|
|
|
2021-01-23 20:09:16 +00:00
|
|
|
$location = Location::factory()->create();
|
|
|
|
Node::factory()->create(['location_id' => $location->id]);
|
2018-03-04 22:30:16 +00:00
|
|
|
|
2018-03-05 04:35:57 +00:00
|
|
|
$response = $this->getJson('/api/application/locations/' . $location->id . '?include=nodes');
|
|
|
|
$response->assertStatus(Response::HTTP_OK);
|
2018-03-04 22:30:16 +00:00
|
|
|
$response->assertJsonCount(2)->assertJsonCount(1, 'attributes.relationships');
|
|
|
|
$response->assertJsonStructure([
|
|
|
|
'attributes' => [
|
|
|
|
'relationships' => [
|
|
|
|
'nodes' => ['object', 'attributes'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Just assert that we see the expected relationship IDs in the response.
|
|
|
|
$response->assertJson([
|
|
|
|
'attributes' => [
|
|
|
|
'relationships' => [
|
|
|
|
'nodes' => [
|
|
|
|
'object' => 'null_resource',
|
|
|
|
'attributes' => null,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a missing location returns a 404 error.
|
|
|
|
*
|
|
|
|
* GET /api/application/locations/:id
|
|
|
|
*/
|
|
|
|
public function testGetMissingLocation()
|
|
|
|
{
|
2018-03-05 04:35:57 +00:00
|
|
|
$response = $this->getJson('/api/application/locations/nil');
|
2018-03-04 22:30:16 +00:00
|
|
|
$this->assertNotFoundJson($response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an authentication error occurs if a key does not have permission
|
|
|
|
* to access a resource.
|
|
|
|
*/
|
|
|
|
public function testErrorReturnedIfNoPermission()
|
|
|
|
{
|
2021-01-23 20:09:16 +00:00
|
|
|
$location = Location::factory()->create();
|
2018-03-04 22:30:16 +00:00
|
|
|
$this->createNewDefaultApiKey($this->getApiUser(), ['r_locations' => 0]);
|
|
|
|
|
2018-03-05 04:35:57 +00:00
|
|
|
$response = $this->getJson('/api/application/locations/' . $location->id);
|
2018-03-04 22:30:16 +00:00
|
|
|
$this->assertAccessDeniedJson($response);
|
|
|
|
}
|
|
|
|
}
|