Path: blob/1.0-develop/tests/Integration/Api/Client/Server/Startup/UpdateStartupVariableTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server\Startup;34use Pterodactyl\Models\User;5use Illuminate\Http\Response;6use Pterodactyl\Models\Permission;7use Pterodactyl\Models\EggVariable;8use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;910class UpdateStartupVariableTest extends ClientApiIntegrationTestCase11{12/**13* Test that a startup variable can be edited successfully for a server.14*/15#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]16public function testStartupVariableCanBeUpdated(array $permissions)17{18/** @var \Pterodactyl\Models\Server $server */19[$user, $server] = $this->generateTestAccount($permissions);20$server->fill([21'startup' => 'java {{SERVER_JARFILE}} --version {{BUNGEE_VERSION}}',22])->save();2324$response = $this->actingAs($user)->putJson($this->link($server) . '/startup/variable', [25'key' => 'BUNGEE_VERSION',26'value' => '1.2.3',27]);2829$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);30$response->assertJsonPath('errors.0.code', 'ValidationException');31$response->assertJsonPath('errors.0.detail', 'The value may only contain letters and numbers.');3233$response = $this->actingAs($user)->putJson($this->link($server) . '/startup/variable', [34'key' => 'BUNGEE_VERSION',35'value' => '123',36]);3738$response->assertOk();39$response->assertJsonPath('object', EggVariable::RESOURCE_NAME);40$this->assertJsonTransformedWith($response->json('attributes'), $server->variables[0]);41$response->assertJsonPath('meta.startup_command', 'java bungeecord.jar --version 123');42$response->assertJsonPath('meta.raw_startup_command', $server->startup);43}4445/**46* Test that variables that are either not user_viewable, or not user_editable, cannot be47* updated via this endpoint.48*/49#[\PHPUnit\Framework\Attributes\DataProvider('permissionsDataProvider')]50public function testStartupVariableCannotBeUpdatedIfNotUserViewableOrEditable(array $permissions)51{52/** @var \Pterodactyl\Models\Server $server */53[$user, $server] = $this->generateTestAccount($permissions);5455$egg = $this->cloneEggAndVariables($server->egg);56$egg->variables()->where('env_variable', 'BUNGEE_VERSION')->update(['user_viewable' => false]);57$egg->variables()->where('env_variable', 'SERVER_JARFILE')->update(['user_editable' => false]);5859$server->fill(['egg_id' => $egg->id])->save();60$server->refresh();6162$response = $this->actingAs($user)->putJson($this->link($server) . '/startup/variable', [63'key' => 'BUNGEE_VERSION',64'value' => '123',65]);6667$response->assertStatus(Response::HTTP_BAD_REQUEST);68$response->assertJsonPath('errors.0.code', 'BadRequestHttpException');69$response->assertJsonPath('errors.0.detail', 'The environment variable you are trying to edit does not exist.');7071$response = $this->actingAs($user)->putJson($this->link($server) . '/startup/variable', [72'key' => 'SERVER_JARFILE',73'value' => 'server2.jar',74]);7576$response->assertStatus(Response::HTTP_BAD_REQUEST);77$response->assertJsonPath('errors.0.code', 'BadRequestHttpException');78$response->assertJsonPath('errors.0.detail', 'The environment variable you are trying to edit is read-only.');79}8081/**82* Test that a hidden variable is not included in the startup_command output for the server if83* a different variable is updated.84*/85public function testHiddenVariablesAreNotReturnedInStartupCommandWhenUpdatingVariable()86{87/** @var \Pterodactyl\Models\Server $server */88[$user, $server] = $this->generateTestAccount();8990$egg = $this->cloneEggAndVariables($server->egg);91$egg->variables()->first()->update(['user_viewable' => false]);9293$server->fill([94'egg_id' => $egg->id,95'startup' => 'java {{SERVER_JARFILE}} --version {{BUNGEE_VERSION}}',96])->save();9798$server->refresh();99100$response = $this->actingAs($user)->putJson($this->link($server) . '/startup/variable', [101'key' => 'SERVER_JARFILE',102'value' => 'server2.jar',103]);104105$response->assertOk();106$response->assertJsonPath('meta.startup_command', 'java server2.jar --version [hidden]');107$response->assertJsonPath('meta.raw_startup_command', $server->startup);108}109110/**111* Test that an egg variable with a validation rule of 'nullable|string' works if no value112* is passed through in the request.113*114* @see https://github.com/pterodactyl/panel/issues/2433115*/116public function testEggVariableWithNullableStringIsNotRequired()117{118/** @var \Pterodactyl\Models\Server $server */119[$user, $server] = $this->generateTestAccount();120121$egg = $this->cloneEggAndVariables($server->egg);122$egg->variables()->first()->update(['rules' => 'nullable|string']);123124$server->fill(['egg_id' => $egg->id])->save();125$server->refresh();126127$response = $this->actingAs($user)->putJson($this->link($server) . '/startup/variable', [128'key' => 'BUNGEE_VERSION',129'value' => '',130]);131132$response->assertOk();133$response->assertJsonPath('attributes.server_value', null);134}135136/**137* Test that a variable cannot be updated if the user does not have permission to perform138* that action, or they aren't assigned at all to the server.139*/140public function testStartupVariableCannotBeUpdatedIfNotUserViewable()141{142[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);143$this->actingAs($user)->putJson($this->link($server) . '/startup/variable')->assertForbidden();144145$user2 = User::factory()->create();146$this->actingAs($user2)->putJson($this->link($server) . '/startup/variable')->assertNotFound();147}148149public static function permissionsDataProvider(): array150{151return [[[]], [[Permission::ACTION_STARTUP_UPDATE]]];152}153}154155156