Path: blob/1.0-develop/tests/Integration/Services/Servers/StartupModificationServiceTest.php
7460 views
<?php12namespace Pterodactyl\Tests\Integration\Services\Servers;34use Exception;5use Pterodactyl\Models\Nest;6use Pterodactyl\Models\User;7use Pterodactyl\Models\Server;8use Pterodactyl\Models\ServerVariable;9use Illuminate\Validation\ValidationException;10use Pterodactyl\Tests\Integration\IntegrationTestCase;11use Illuminate\Database\Eloquent\ModelNotFoundException;12use Pterodactyl\Services\Servers\StartupModificationService;1314class StartupModificationServiceTest extends IntegrationTestCase15{16/**17* Test that a non-admin request to modify the server startup parameters does18* not perform any egg or nest updates. This also attempts to pass through an19* egg_id variable which should have no impact if the request is coming from20* a non-admin entity.21*/22public function testNonAdminCanModifyServerVariables()23{24$server = $this->createServerModel();2526try {27$this->app->make(StartupModificationService::class)->handle($server, [28'egg_id' => $server->egg_id + 1,29'environment' => [30'BUNGEE_VERSION' => '$$',31'SERVER_JARFILE' => 'server.jar',32],33]);3435$this->fail('This assertion should not be called.');36} catch (\Exception $exception) {37$this->assertInstanceOf(ValidationException::class, $exception);3839/** @var ValidationException $exception */40$errors = $exception->validator->errors()->toArray();4142$this->assertCount(1, $errors);43$this->assertArrayHasKey('environment.BUNGEE_VERSION', $errors);44$this->assertCount(1, $errors['environment.BUNGEE_VERSION']);45$this->assertSame('The Bungeecord Version variable may only contain letters and numbers.', $errors['environment.BUNGEE_VERSION'][0]);46}4748ServerVariable::query()->where('variable_id', $server->variables[1]->id)->delete();4950$result = $this->getService()51->handle($server, [52'egg_id' => $server->egg_id + 1,53'startup' => 'random gibberish',54'environment' => [55'BUNGEE_VERSION' => '1234',56'SERVER_JARFILE' => 'test.jar',57],58]);5960$this->assertInstanceOf(Server::class, $result);61$this->assertCount(2, $result->variables);62$this->assertSame($server->startup, $result->startup);63$this->assertSame('1234', $result->variables[0]->server_value);64$this->assertSame('test.jar', $result->variables[1]->server_value);65}6667/**68* Test that modifying an egg as an admin properly updates the data for the server.69*/70public function testServerIsProperlyModifiedAsAdminUser()71{72/** @var \Pterodactyl\Models\Egg $nextEgg */73$nextEgg = Nest::query()->findOrFail(2)->eggs()->firstOrFail();7475$server = $this->createServerModel(['egg_id' => 1]);7677$this->assertNotSame($nextEgg->id, $server->egg_id);78$this->assertNotSame($nextEgg->nest_id, $server->nest_id);7980$response = $this->getService()81->setUserLevel(User::USER_LEVEL_ADMIN)82->handle($server, [83'egg_id' => $nextEgg->id,84'startup' => 'sample startup',85'skip_scripts' => true,86'docker_image' => 'docker/hodor',87]);8889$this->assertInstanceOf(Server::class, $response);90$this->assertSame($nextEgg->id, $response->egg_id);91$this->assertSame($nextEgg->nest_id, $response->nest_id);92$this->assertSame('sample startup', $response->startup);93$this->assertSame('docker/hodor', $response->image);94$this->assertTrue($response->skip_scripts);95// Make sure we don't revert back to a lurking bug that causes servers to get marked96// as not installed when you modify the startup...97$this->assertTrue($response->isInstalled());98}99100/**101* Test that hidden variables can be updated by an admin but are not affected by a102* regular user who attempts to pass them through.103*/104public function testEnvironmentVariablesCanBeUpdatedByAdmin()105{106$server = $this->createServerModel();107$server->loadMissing(['egg', 'variables']);108109$clone = $this->cloneEggAndVariables($server->egg);110// This makes the BUNGEE_VERSION variable not user editable.111$clone->variables()->first()->update([112'user_editable' => false,113]);114115$server->fill(['egg_id' => $clone->id])->saveOrFail();116$server->refresh();117118ServerVariable::query()->updateOrCreate([119'server_id' => $server->id,120'variable_id' => $server->variables[0]->id,121], ['variable_value' => 'EXIST']);122123$response = $this->getService()->handle($server, [124'environment' => [125'BUNGEE_VERSION' => '1234',126'SERVER_JARFILE' => 'test.jar',127],128]);129130$this->assertCount(2, $response->variables);131$this->assertSame('EXIST', $response->variables[0]->server_value);132$this->assertSame('test.jar', $response->variables[1]->server_value);133134$response = $this->getService()135->setUserLevel(User::USER_LEVEL_ADMIN)136->handle($server, [137'environment' => [138'BUNGEE_VERSION' => '1234',139'SERVER_JARFILE' => 'test.jar',140],141]);142143$this->assertCount(2, $response->variables);144$this->assertSame('1234', $response->variables[0]->server_value);145$this->assertSame('test.jar', $response->variables[1]->server_value);146}147148/**149* Test that passing an invalid egg ID into the function throws an exception150* rather than silently failing or skipping.151*/152public function testInvalidEggIdTriggersException()153{154$server = $this->createServerModel();155156$this->expectException(ModelNotFoundException::class);157158$this->getService()159->setUserLevel(User::USER_LEVEL_ADMIN)160->handle($server, ['egg_id' => 123456789]);161}162163private function getService(): StartupModificationService164{165return $this->app->make(StartupModificationService::class);166}167}168169170