diff --git a/app/Http/Controllers/Api/Client/Servers/SettingsController.php b/app/Http/Controllers/Api/Client/Servers/SettingsController.php index 64c73a8de..6090906ee 100644 --- a/app/Http/Controllers/Api/Client/Servers/SettingsController.php +++ b/app/Http/Controllers/Api/Client/Servers/SettingsController.php @@ -55,7 +55,7 @@ class SettingsController extends ClientApiController 'name' => $request->input('name'), ]); - return JsonResponse::create([], Response::HTTP_NO_CONTENT); + return new JsonResponse([], Response::HTTP_NO_CONTENT); } /** @@ -71,6 +71,6 @@ class SettingsController extends ClientApiController { $this->reinstallServerService->reinstall($server); - return JsonResponse::create([], Response::HTTP_ACCEPTED); + return new JsonResponse([], Response::HTTP_ACCEPTED); } } diff --git a/app/Services/Servers/ReinstallServerService.php b/app/Services/Servers/ReinstallServerService.php index a68e97110..aff0321ce 100644 --- a/app/Services/Servers/ReinstallServerService.php +++ b/app/Services/Servers/ReinstallServerService.php @@ -54,7 +54,7 @@ class ReinstallServerService return $this->connection->transaction(function () use ($server) { $updated = $this->repository->update($server->id, [ 'installed' => Server::STATUS_INSTALLING, - ]); + ], true, true); $this->daemonServerRepository->setServer($server)->reinstall(); diff --git a/tests/Integration/Api/Client/Server/SettingsControllerTest.php b/tests/Integration/Api/Client/Server/SettingsControllerTest.php new file mode 100644 index 000000000..99c32a56a --- /dev/null +++ b/tests/Integration/Api/Client/Server/SettingsControllerTest.php @@ -0,0 +1,128 @@ +generateTestAccount($permissions); + $originalName = $server->name; + + $response = $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/settings/rename", [ + 'name' => '', + ]); + + $response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY); + $response->assertJsonPath('errors.0.code', 'required'); + + $server = $server->refresh(); + $this->assertSame($originalName, $server->name); + + $this->actingAs($user) + ->postJson("/api/client/servers/{$server->uuid}/settings/rename", [ + 'name' => 'Test Server Name', + ]) + ->assertStatus(Response::HTTP_NO_CONTENT); + + $server = $server->refresh(); + $this->assertSame('Test Server Name', $server->name); + } + + /** + * Test that a subuser receives a permissions error if they do not have the required permission + * and attempt to change the name. + */ + public function testSubuserCannotChangeServerNameWithoutPermission() + { + [$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]); + $originalName = $server->name; + + $this->actingAs($user) + ->postJson("/api/client/servers/{$server->uuid}/settings/rename", [ + 'name' => 'Test Server Name', + ]) + ->assertStatus(Response::HTTP_FORBIDDEN); + + $server = $server->refresh(); + $this->assertSame($originalName, $server->name); + } + + /** + * Test that a server can be reinstalled. Honestly this test doesn't do much of anything other + * than make sure the endpoint works since. + * + * @param array $permissions + * @dataProvider reinstallPermissionsDataProvider + */ + public function testServerCanBeReinstalled($permissions) + { + /** @var \Pterodactyl\Models\Server $server */ + [$user, $server] = $this->generateTestAccount($permissions); + $this->assertSame(Server::STATUS_INSTALLED, $server->installed); + + $service = Mockery::mock(DaemonServerRepository::class); + $this->app->instance(DaemonServerRepository::class, $service); + + $service->expects('setServer') + ->with(Mockery::on(function ($value) use ($server) { + return $value->uuid === $server->uuid; + })) + ->andReturnSelf() + ->getMock() + ->expects('reinstall') + ->andReturnUndefined(); + + $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/settings/reinstall") + ->assertStatus(Response::HTTP_ACCEPTED); + + $server = $server->refresh(); + $this->assertSame(Server::STATUS_INSTALLING, $server->installed); + } + + /** + * Test that a subuser receives a permissions error if they do not have the required permission + * and attempt to reinstall a server. + */ + public function testSubuserCannotReinstallServerWithoutPermission() + { + [$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]); + + $this->actingAs($user) + ->postJson("/api/client/servers/{$server->uuid}/settings/reinstall") + ->assertStatus(Response::HTTP_FORBIDDEN); + + $server = $server->refresh(); + $this->assertSame(Server::STATUS_INSTALLED, $server->installed); + } + + /** + * @return array + */ + public function renamePermissionsDataProvider(): array + { + return [[[]], [[Permission::ACTION_SETTINGS_RENAME]]]; + } + + /** + * @return array + */ + public function reinstallPermissionsDataProvider(): array + { + return [[[]], [[Permission::ACTION_SETTINGS_REINSTALL]]]; + } +}