Path: blob/1.0-develop/tests/Integration/Api/Application/Nests/EggControllerTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Application\Nests;34use Illuminate\Support\Arr;5use Pterodactyl\Models\Egg;6use Illuminate\Http\Response;7use Pterodactyl\Transformers\Api\Application\EggTransformer;8use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;910class EggControllerTest extends ApplicationApiIntegrationTestCase11{12/**13* Test that all the eggs belonging to a given nest can be returned.14*/15public function testListAllEggsInNest()16{17$eggs = Egg::query()->where('nest_id', 1)->get();1819$response = $this->getJson('/api/application/nests/' . $eggs->first()->nest_id . '/eggs');20$response->assertStatus(Response::HTTP_OK);21$response->assertJsonCount(count($eggs), 'data');22$response->assertJsonStructure([23'object',24'data' => [25[26'object',27'attributes' => [28'id', 'uuid', 'nest', 'author', 'description', 'docker_image', 'startup', 'created_at', 'updated_at',29'script' => ['privileged', 'install', 'entry', 'container', 'extends'],30'config' => [31'files' => [],32'startup' => ['done'],33'stop',34'logs' => [],35'extends',36],37],38],39],40]);4142foreach (array_get($response->json(), 'data') as $datum) {43$egg = $eggs->where('id', '=', $datum['attributes']['id'])->first();4445$expected = json_encode(Arr::sortRecursive($datum['attributes']));46$actual = json_encode(Arr::sortRecursive($this->getTransformer(EggTransformer::class)->transform($egg)));4748$this->assertSame(49$expected,50$actual,51'Unable to find JSON fragment: ' . PHP_EOL . PHP_EOL . "[$expected]" . PHP_EOL . PHP_EOL . 'within' . PHP_EOL . PHP_EOL . "[$actual]."52);53}54}5556/**57* Test that a single egg can be returned.58*/59public function testReturnSingleEgg()60{61$egg = Egg::query()->findOrFail(1);6263$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/' . $egg->id);64$response->assertStatus(Response::HTTP_OK);65$response->assertJsonStructure([66'object',67'attributes' => [68'id', 'uuid', 'nest', 'author', 'description', 'docker_image', 'startup', 'script' => [], 'config' => [], 'created_at', 'updated_at',69],70]);7172$response->assertJson([73'object' => 'egg',74'attributes' => $this->getTransformer(EggTransformer::class)->transform($egg),75], true);76}7778/**79* Test that a single egg and all the defined relationships can be returned.80*/81public function testReturnSingleEggWithRelationships()82{83$egg = Egg::query()->findOrFail(1);8485$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/' . $egg->id . '?include=servers,variables,nest');86$response->assertStatus(Response::HTTP_OK);87$response->assertJsonStructure([88'object',89'attributes' => [90'relationships' => [91'nest' => ['object', 'attributes'],92'servers' => ['object', 'data' => []],93'variables' => ['object', 'data' => []],94],95],96]);97}9899/**100* Test that a missing egg returns a 404 error.101*/102public function testGetMissingEgg()103{104$egg = Egg::query()->findOrFail(1);105106$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/nil');107$this->assertNotFoundJson($response);108}109110/**111* Test that an authentication error occurs if a key does not have permission112* to access a resource.113*/114public function testErrorReturnedIfNoPermission()115{116$egg = Egg::query()->findOrFail(1);117$this->createNewDefaultApiKey($this->getApiUser(), ['r_eggs' => 0]);118119$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs');120$this->assertAccessDeniedJson($response);121}122}123124125