misc_pterodactyl-panel/tests/Integration/Api/Application/Eggs/EggControllerTest.php

146 lines
4.8 KiB
PHP
Raw Normal View History

2018-03-25 22:41:36 +00:00
<?php
namespace Pterodactyl\Tests\Integration\Api\Application\Eggs;
2018-03-25 22:41:36 +00:00
use Illuminate\Support\Arr;
use Illuminate\Http\Response;
use Pterodactyl\Contracts\Repository\EggRepositoryInterface;
use Pterodactyl\Transformers\Api\Application\EggTransformer;
use Pterodactyl\Tests\Integration\Api\Application\ApplicationApiIntegrationTestCase;
class EggControllerTest extends ApplicationApiIntegrationTestCase
{
private EggRepositoryInterface $repository;
2018-03-25 22:41:36 +00:00
/**
* Setup tests.
*/
public function setUp(): void
2018-03-25 22:41:36 +00:00
{
parent::setUp();
$this->repository = $this->app->make(EggRepositoryInterface::class);
}
/**
* Test that all of the eggs belonging to a given nest can be returned.
*/
public function testListAllEggsInNest()
{
$eggs = $this->repository->findWhere([['nest_id', '=', 1]]);
$response = $this->getJson('/api/application/nests/' . $eggs->first()->nest_id . '/eggs');
$response->assertStatus(Response::HTTP_OK);
$response->assertJsonCount(count($eggs), 'data');
$response->assertJsonStructure([
'object',
'data' => [
[
'object',
'attributes' => [
'id', 'uuid', 'nest', 'author', 'description', 'docker_image', 'startup', 'created_at', 'updated_at',
'script' => ['privileged', 'install', 'entry', 'container', 'extends'],
'config' => [
'files' => [],
'startup' => ['done'],
2018-03-25 22:41:36 +00:00
'stop',
'logs' => ['custom', 'location'],
'extends',
],
],
],
],
]);
foreach (array_get($response->json(), 'data') as $datum) {
$egg = $eggs->where('id', '=', $datum['attributes']['id'])->first();
$expected = json_encode(Arr::sortRecursive($datum['attributes']));
2021-08-07 21:32:40 +00:00
$actual = json_encode(Arr::sortRecursive((new EggTransformer())->transform($egg)));
2018-03-25 22:41:36 +00:00
2021-01-23 20:33:34 +00:00
$this->assertSame(
$expected,
$actual,
2018-03-25 22:41:36 +00:00
'Unable to find JSON fragment: ' . PHP_EOL . PHP_EOL . "[{$expected}]" . PHP_EOL . PHP_EOL . 'within' . PHP_EOL . PHP_EOL . "[{$actual}]."
);
}
}
/**
* Test that a single egg can be returned.
*/
public function testReturnSingleEgg()
{
$egg = $this->repository->find(1);
$response = $this->getJson('/api/application/eggs/' . $egg->id);
2018-03-25 22:41:36 +00:00
$response->assertStatus(Response::HTTP_OK);
$response->assertJsonStructure([
'object',
'attributes' => [
'id', 'uuid', 'nest', 'author', 'description', 'docker_image', 'startup', 'script' => [], 'config' => [], 'created_at', 'updated_at',
],
]);
$response->assertJson([
'object' => 'egg',
2021-08-07 21:32:40 +00:00
'attributes' => (new EggTransformer())->transform($egg),
2018-03-25 22:41:36 +00:00
], true);
}
/**
* Test that a single egg and all of the defined relationships can be returned.
*/
public function testReturnSingleEggWithRelationships()
{
$egg = $this->repository->find(1);
$response = $this->getJson('/api/application/eggs/' . $egg->id . '?include=servers,variables,nest');
2018-03-25 22:41:36 +00:00
$response->assertStatus(Response::HTTP_OK);
$response->assertJsonStructure([
'object',
'attributes' => [
'relationships' => [
'nest' => ['object', 'attributes'],
'servers' => ['object', 'data' => []],
'variables' => ['object', 'data' => []],
],
],
]);
}
/**
* Test that a missing egg returns a 404 error.
*/
public function testGetMissingEgg()
{
$response = $this->getJson('/api/application/eggs/nil');
2018-03-25 22:41:36 +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()
{
$egg = $this->repository->find(1);
$this->createNewAccessToken(['r_eggs' => 0]);
2018-03-25 22:41:36 +00:00
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs');
$this->assertAccessDeniedJson($response);
}
/**
* Test that a nests's existence is not exposed unless an API key has permission
* to access the resource.
*/
public function testResourceIsNotExposedWithoutPermissions()
{
$this->createNewAccessToken(['r_eggs' => 0]);
2018-03-25 22:41:36 +00:00
$response = $this->getJson('/api/application/eggs/nil');
2018-03-25 22:41:36 +00:00
$this->assertAccessDeniedJson($response);
}
}