Path: blob/1.0-develop/tests/Integration/Api/Application/Location/LocationControllerTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Application\Location;34use Pterodactyl\Models\Node;5use Illuminate\Http\Response;6use Pterodactyl\Models\Location;7use Pterodactyl\Transformers\Api\Application\NodeTransformer;8use Pterodactyl\Transformers\Api\Application\ServerTransformer;9use Pterodactyl\Transformers\Api\Application\LocationTransformer;10use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;1112class LocationControllerTest extends ApplicationApiIntegrationTestCase13{14/**15* Test getting all locations through the API.16*/17public function testGetLocations()18{19$locations = Location::factory()->times(2)->create();2021$response = $this->getJson('/api/application/locations?per_page=60');22$response->assertStatus(Response::HTTP_OK);23$response->assertJsonCount(2, 'data');24$response->assertJsonStructure([25'object',26'data' => [27['object', 'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at']],28['object', 'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at']],29],30'meta' => ['pagination' => ['total', 'count', 'per_page', 'current_page', 'total_pages']],31]);3233$response34->assertJson([35'object' => 'list',36'data' => [[], []],37'meta' => [38'pagination' => [39'total' => 2,40'count' => 2,41'per_page' => 60,42'current_page' => 1,43'total_pages' => 1,44],45],46])47->assertJsonFragment([48'object' => 'location',49'attributes' => [50'id' => $locations[0]->id,51'short' => $locations[0]->short,52'long' => $locations[0]->long,53'created_at' => $this->formatTimestamp($locations[0]->created_at),54'updated_at' => $this->formatTimestamp($locations[0]->updated_at),55],56])->assertJsonFragment([57'object' => 'location',58'attributes' => [59'id' => $locations[1]->id,60'short' => $locations[1]->short,61'long' => $locations[1]->long,62'created_at' => $this->formatTimestamp($locations[1]->created_at),63'updated_at' => $this->formatTimestamp($locations[1]->updated_at),64],65]);66}6768/**69* Test getting a single location on the API.70*/71public function testGetSingleLocation()72{73$location = Location::factory()->create();7475$response = $this->getJson('/api/application/locations/' . $location->id);76$response->assertStatus(Response::HTTP_OK);77$response->assertJsonCount(2);78$response->assertJsonStructure(['object', 'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at']]);79$response->assertJson([80'object' => 'location',81'attributes' => [82'id' => $location->id,83'short' => $location->short,84'long' => $location->long,85'created_at' => $this->formatTimestamp($location->created_at),86'updated_at' => $this->formatTimestamp($location->updated_at),87],88], true);89}9091/**92* Test that a location can be created.93*/94public function testCreateLocation()95{96$response = $this->postJson('/api/application/locations', [97'short' => 'inhouse',98'long' => 'This is my inhouse location',99]);100101$response->assertStatus(Response::HTTP_CREATED);102$response->assertJsonCount(3);103$response->assertJsonStructure([104'object',105'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at'],106'meta' => ['resource'],107]);108109$this->assertDatabaseHas('locations', ['short' => 'inhouse', 'long' => 'This is my inhouse location']);110111$location = Location::where('short', 'inhouse')->first();112$response->assertJson([113'object' => 'location',114'attributes' => $this->getTransformer(LocationTransformer::class)->transform($location),115'meta' => [116'resource' => route('api.application.locations.view', $location->id),117],118], true);119}120121/**122* Test that a location can be updated.123*/124public function testUpdateLocation()125{126$location = Location::factory()->create();127128$response = $this->patchJson('/api/application/locations/' . $location->id, [129'short' => 'new inhouse',130'long' => 'This is my new inhouse location',131]);132$response->assertStatus(Response::HTTP_OK);133$response->assertJsonCount(2);134$response->assertJsonStructure([135'object',136'attributes' => ['id', 'short', 'long', 'created_at', 'updated_at'],137]);138139$this->assertDatabaseHas('locations', ['short' => 'new inhouse', 'long' => 'This is my new inhouse location']);140$location = $location->fresh();141142$response->assertJson([143'object' => 'location',144'attributes' => $this->getTransformer(LocationTransformer::class)->transform($location),145]);146}147148/**149* Test that a location can be deleted from the database.150*/151public function testDeleteLocation()152{153$location = Location::factory()->create();154$this->assertDatabaseHas('locations', ['id' => $location->id]);155156$response = $this->delete('/api/application/locations/' . $location->id);157$response->assertStatus(Response::HTTP_NO_CONTENT);158159$this->assertDatabaseMissing('locations', ['id' => $location->id]);160}161162/**163* Test that all the defined relationships for a location can be loaded successfully.164*/165public function testRelationshipsCanBeLoaded()166{167$location = Location::factory()->create();168$server = $this->createServerModel(['user_id' => $this->getApiUser()->id, 'location_id' => $location->id]);169170$response = $this->getJson('/api/application/locations/' . $location->id . '?include=servers,nodes');171$response->assertStatus(Response::HTTP_OK);172$response->assertJsonCount(2)->assertJsonCount(2, 'attributes.relationships');173$response->assertJsonStructure([174'attributes' => [175'relationships' => [176'nodes' => ['object', 'data' => [['attributes' => ['id']]]],177'servers' => ['object', 'data' => [['attributes' => ['id']]]],178],179],180]);181182// Just assert that we see the expected relationship IDs in the response.183$response->assertJson([184'attributes' => [185'relationships' => [186'nodes' => [187'object' => 'list',188'data' => [189[190'object' => 'node',191'attributes' => $this->getTransformer(NodeTransformer::class)->transform($server->getRelation('node')),192],193],194],195'servers' => [196'object' => 'list',197'data' => [198[199'object' => 'server',200'attributes' => $this->getTransformer(ServerTransformer::class)->transform($server),201],202],203],204],205],206]);207}208209/**210* Test that a relationship that an API key does not have permission to access211* cannot be loaded onto the model.212*/213public function testKeyWithoutPermissionCannotLoadRelationship()214{215$this->createNewDefaultApiKey($this->getApiUser(), ['r_nodes' => 0]);216217$location = Location::factory()->create();218Node::factory()->create(['location_id' => $location->id]);219220$response = $this->getJson('/api/application/locations/' . $location->id . '?include=nodes');221$response->assertStatus(Response::HTTP_OK);222$response->assertJsonCount(2)->assertJsonCount(1, 'attributes.relationships');223$response->assertJsonStructure([224'attributes' => [225'relationships' => [226'nodes' => ['object', 'attributes'],227],228],229]);230231// Just assert that we see the expected relationship IDs in the response.232$response->assertJson([233'attributes' => [234'relationships' => [235'nodes' => [236'object' => 'null_resource',237'attributes' => null,238],239],240],241]);242}243244/**245* Test that a missing location returns a 404 error.246*247* GET /api/application/locations/:id248*/249public function testGetMissingLocation()250{251$response = $this->getJson('/api/application/locations/nil');252$this->assertNotFoundJson($response);253}254255/**256* Test that an authentication error occurs if a key does not have permission257* to access a resource.258*/259public function testErrorReturnedIfNoPermission()260{261$location = Location::factory()->create();262$this->createNewDefaultApiKey($this->getApiUser(), ['r_locations' => 0]);263264$response = $this->getJson('/api/application/locations/' . $location->id);265$this->assertAccessDeniedJson($response);266}267}268269270