Update egg import/update logic to all use the same pathwaus

This commit is contained in:
DaneEveritt 2022-05-15 14:40:19 -04:00
parent 6554164252
commit cca0010a00
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
12 changed files with 271 additions and 334 deletions

View file

@ -3,34 +3,19 @@
namespace Pterodactyl\Tests\Integration\Api\Application\Nests;
use Illuminate\Support\Arr;
use Pterodactyl\Models\Egg;
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
{
/**
* @var \Pterodactyl\Contracts\Repository\EggRepositoryInterface
*/
private $repository;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
$this->repository = $this->app->make(EggRepositoryInterface::class);
}
/**
* Test that all the eggs belonging to a given nest can be returned.
*/
public function testListAllEggsInNest()
{
$eggs = $this->repository->findWhere([['nest_id', '=', 1]]);
$eggs = Egg::query()->where('nest_id', 1)->get();
$response = $this->getJson('/api/application/nests/' . $eggs->first()->nest_id . '/eggs');
$response->assertStatus(Response::HTTP_OK);
@ -74,7 +59,7 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
*/
public function testReturnSingleEgg()
{
$egg = $this->repository->find(1);
$egg = Egg::query()->findOrFail(1);
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/' . $egg->id);
$response->assertStatus(Response::HTTP_OK);
@ -96,7 +81,7 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
*/
public function testReturnSingleEggWithRelationships()
{
$egg = $this->repository->find(1);
$egg = Egg::query()->findOrFail(1);
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/' . $egg->id . '?include=servers,variables,nest');
$response->assertStatus(Response::HTTP_OK);
@ -117,7 +102,7 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
*/
public function testGetMissingEgg()
{
$egg = $this->repository->find(1);
$egg = Egg::query()->findOrFail(1);
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/nil');
$this->assertNotFoundJson($response);
@ -129,7 +114,7 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
*/
public function testErrorReturnedIfNoPermission()
{
$egg = $this->repository->find(1);
$egg = Egg::query()->findOrFail(1);
$this->createNewDefaultApiKey($this->getApiUser(), ['r_eggs' => 0]);
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs');
@ -142,7 +127,7 @@ class EggControllerTest extends ApplicationApiIntegrationTestCase
*/
public function testResourceIsNotExposedWithoutPermissions()
{
$egg = $this->repository->find(1);
$egg = Egg::query()->findOrFail(1);
$this->createNewDefaultApiKey($this->getApiUser(), ['r_eggs' => 0]);
$response = $this->getJson('/api/application/nests/' . $egg->nest_id . '/eggs/nil');

View file

@ -25,7 +25,9 @@ class ServerCreationServiceTest extends IntegrationTestCase
use WithFaker;
/** @var \Mockery\MockInterface */
private $daemonServerRepository;
protected $daemonServerRepository;
protected Egg $bungeecord;
/**
* Stub the calls to Wings so that we don't actually hit those API endpoints.
@ -34,6 +36,12 @@ class ServerCreationServiceTest extends IntegrationTestCase
{
parent::setUp();
/* @noinspection PhpFieldAssignmentTypeMismatchInspection */
$this->bungeecord = Egg::query()
->where('author', 'support@pterodactyl.io')
->where('name', 'Bungeecord')
->firstOrFail();
$this->daemonServerRepository = Mockery::mock(DaemonServerRepository::class);
$this->swap(DaemonServerRepository::class, $this->daemonServerRepository);
}
@ -67,7 +75,7 @@ class ServerCreationServiceTest extends IntegrationTestCase
$allocations[0]->port,
]);
$egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1));
$egg = $this->cloneEggAndVariables($this->bungeecord);
// We want to make sure that the validator service runs as an admin, and not as a regular
// user when saving variables.
$egg->variables()->first()->update([
@ -178,7 +186,7 @@ class ServerCreationServiceTest extends IntegrationTestCase
'cpu' => 0,
'startup' => 'java server2.jar',
'image' => 'java:8',
'egg_id' => 1,
'egg_id' => $this->bungeecord->id,
'environment' => [
'BUNGEE_VERSION' => '123',
'SERVER_JARFILE' => 'server2.jar',

View file

@ -3,7 +3,6 @@
namespace Pterodactyl\Tests\Integration\Services\Servers;
use Exception;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Nest;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
@ -23,8 +22,7 @@ class StartupModificationServiceTest extends IntegrationTestCase
*/
public function testNonAdminCanModifyServerVariables()
{
// Theoretically lines up with the Bungeecord Minecraft egg.
$server = $this->createServerModel(['egg_id' => 1]);
$server = $this->createServerModel();
try {
$this->app->make(StartupModificationService::class)->handle($server, [

View file

@ -11,13 +11,25 @@ use Pterodactyl\Services\Servers\VariableValidatorService;
class VariableValidatorServiceTest extends IntegrationTestCase
{
protected Egg $egg;
public function setUp(): void
{
parent::setUp();
/* @noinspection PhpFieldAssignmentTypeMismatchInspection */
$this->egg = Egg::query()
->where('author', 'support@pterodactyl.io')
->where('name', 'Bungeecord')
->firstOrFail();
}
/**
* Test that enviornment variables for a server are validated as expected.
*/
public function testEnvironmentVariablesCanBeValidated()
{
/** @noinspection PhpParamsInspection */
$egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1));
$egg = $this->cloneEggAndVariables($this->egg);
try {
$this->getService()->handle($egg->id, [
@ -54,8 +66,7 @@ class VariableValidatorServiceTest extends IntegrationTestCase
*/
public function testNormalUserCannotValidateNonUserEditableVariables()
{
/** @noinspection PhpParamsInspection */
$egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1));
$egg = $this->cloneEggAndVariables($this->egg);
$egg->variables()->first()->update([
'user_editable' => false,
]);
@ -74,8 +85,7 @@ class VariableValidatorServiceTest extends IntegrationTestCase
public function testEnvironmentVariablesCanBeUpdatedAsAdmin()
{
/** @noinspection PhpParamsInspection */
$egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1));
$egg = $this->cloneEggAndVariables($this->egg);
$egg->variables()->first()->update([
'user_editable' => false,
]);
@ -107,8 +117,7 @@ class VariableValidatorServiceTest extends IntegrationTestCase
public function testNullableEnvironmentVariablesCanBeUsedCorrectly()
{
/** @noinspection PhpParamsInspection */
$egg = $this->cloneEggAndVariables(Egg::query()->findOrFail(1));
$egg = $this->cloneEggAndVariables($this->egg);
$egg->variables()->where('env_variable', '!=', 'BUNGEE_VERSION')->delete();
$egg->variables()->update(['rules' => 'nullable|string']);

View file

@ -4,7 +4,6 @@ namespace Pterodactyl\Tests\Traits\Integration;
use Ramsey\Uuid\Uuid;
use Pterodactyl\Models\Egg;
use Pterodactyl\Models\Nest;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
@ -52,20 +51,17 @@ trait CreatesTestModels
$attributes['allocation_id'] = $allocation->id;
}
if (!isset($attributes['nest_id'])) {
/** @var \Pterodactyl\Models\Nest $nest */
$nest = Nest::with('eggs')->first();
$attributes['nest_id'] = $nest->id;
if (empty($attributes['egg_id'])) {
$egg = !empty($attributes['nest_id'])
? Egg::query()->where('nest_id', $attributes['nest_id'])->firstOrFail()
: $this->getBungeecordEgg();
if (!isset($attributes['egg_id'])) {
$attributes['egg_id'] = $nest->getRelation('eggs')->first()->id;
}
$attributes['egg_id'] = $egg->id;
$attributes['nest_id'] = $egg->nest_id;
}
if (!isset($attributes['egg_id'])) {
/** @var \Pterodactyl\Models\Egg $egg */
$egg = Egg::where('nest_id', $attributes['nest_id'])->first();
$attributes['egg_id'] = $egg->id;
if (empty($attributes['nest_id'])) {
$attributes['nest_id'] = Egg::query()->findOrFail($attributes['egg_id'])->nest_id;
}
unset($attributes['user_id'], $attributes['location_id']);
@ -99,4 +95,13 @@ trait CreatesTestModels
return $model->fresh();
}
/**
* Most every test just assumes it is using Bungeecord this is the critical
* egg model for all tests unless specified otherwise.
*/
private function getBungeecordEgg()
{
return Egg::query()->where('author', 'support@pterodactyl.io')->where('name', 'Bungeecord')->firstOrFail();
}
}