Path: blob/1.0-develop/tests/Integration/Api/Client/Server/SettingsControllerTest.php
7461 views
<?php12namespace Pterodactyl\Tests\Integration\Api\Client\Server;34use Illuminate\Http\Response;5use Pterodactyl\Models\Server;6use Pterodactyl\Models\Permission;7use Pterodactyl\Repositories\Wings\DaemonServerRepository;8use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;910class SettingsControllerTest extends ClientApiIntegrationTestCase11{12/**13* Test that the server's name can be changed.14*/15#[\PHPUnit\Framework\Attributes\DataProvider('renamePermissionsDataProvider')]16public function testServerNameCanBeChanged(array $permissions)17{18/** @var Server $server */19[$user, $server] = $this->generateTestAccount($permissions);20$originalName = $server->name;21$originalDescription = $server->description;2223$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/settings/rename", [24'name' => '',25'description' => '',26]);2728$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);29$response->assertJsonPath('errors.0.meta.rule', 'required');3031$server = $server->refresh();32$this->assertSame($originalName, $server->name);33$this->assertSame($originalDescription, $server->description);3435$this->actingAs($user)36->postJson("/api/client/servers/$server->uuid/settings/rename", [37'name' => 'Test Server Name',38'description' => 'This is a test server.',39])40->assertStatus(Response::HTTP_NO_CONTENT);4142$server = $server->refresh();43$this->assertSame('Test Server Name', $server->name);44$this->assertSame('This is a test server.', $server->description);45}4647/**48* Test that a subuser receives a permissions error if they do not have the required permission49* and attempt to change the name.50*/51public function testSubuserCannotChangeServerNameWithoutPermission()52{53[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);54$originalName = $server->name;5556$this->actingAs($user)57->postJson("/api/client/servers/$server->uuid/settings/rename", [58'name' => 'Test Server Name',59])60->assertStatus(Response::HTTP_FORBIDDEN);6162$server = $server->refresh();63$this->assertSame($originalName, $server->name);64}6566/**67* Test that a server can be reinstalled. Honestly this test doesn't do much of anything other68* than make sure the endpoint works since.69*/70#[\PHPUnit\Framework\Attributes\DataProvider('reinstallPermissionsDataProvider')]71public function testServerCanBeReinstalled(array $permissions)72{73/** @var Server $server */74[$user, $server] = $this->generateTestAccount($permissions);75$this->assertTrue($server->isInstalled());7677$service = \Mockery::mock(DaemonServerRepository::class);78$this->app->instance(DaemonServerRepository::class, $service);7980$service->expects('setServer')81->with(\Mockery::on(function ($value) use ($server) {82return $value->uuid === $server->uuid;83}))84->andReturnSelf()85->getMock()86->expects('reinstall')87->andReturnUndefined();8889$this->actingAs($user)->postJson("/api/client/servers/$server->uuid/settings/reinstall")90->assertStatus(Response::HTTP_ACCEPTED);9192$server = $server->refresh();93$this->assertSame(Server::STATUS_INSTALLING, $server->status);94}9596/**97* Test that a subuser receives a permissions error if they do not have the required permission98* and attempt to reinstall a server.99*/100public function testSubuserCannotReinstallServerWithoutPermission()101{102[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);103104$this->actingAs($user)105->postJson("/api/client/servers/$server->uuid/settings/reinstall")106->assertStatus(Response::HTTP_FORBIDDEN);107108$server = $server->refresh();109$this->assertTrue($server->isInstalled());110}111112public static function renamePermissionsDataProvider(): array113{114return [[[]], [[Permission::ACTION_SETTINGS_RENAME]]];115}116117public static function reinstallPermissionsDataProvider(): array118{119return [[[]], [[Permission::ACTION_SETTINGS_REINSTALL]]];120}121}122123124