|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pterodactyl\Tests\Integration\Api\Client\Server; |
| 4 | + |
| 5 | +use Mockery; |
| 6 | +use Illuminate\Http\Response; |
| 7 | +use Pterodactyl\Models\Server; |
| 8 | +use Pterodactyl\Models\Permission; |
| 9 | +use Pterodactyl\Repositories\Wings\DaemonServerRepository; |
| 10 | +use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase; |
| 11 | + |
| 12 | +class SettingsControllerTest extends ClientApiIntegrationTestCase |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Test that the server's name can be changed. |
| 16 | + * |
| 17 | + * @param array $permissions |
| 18 | + * @dataProvider renamePermissionsDataProvider |
| 19 | + */ |
| 20 | + public function testServerNameCanBeChanged($permissions) |
| 21 | + { |
| 22 | + /** @var \Pterodactyl\Models\Server $server */ |
| 23 | + [$user, $server] = $this->generateTestAccount($permissions); |
| 24 | + $originalName = $server->name; |
| 25 | + |
| 26 | + $response = $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/settings/rename", [ |
| 27 | + 'name' => '', |
| 28 | + ]); |
| 29 | + |
| 30 | + $response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY); |
| 31 | + $response->assertJsonPath('errors.0.code', 'required'); |
| 32 | + |
| 33 | + $server = $server->refresh(); |
| 34 | + $this->assertSame($originalName, $server->name); |
| 35 | + |
| 36 | + $this->actingAs($user) |
| 37 | + ->postJson("/api/client/servers/{$server->uuid}/settings/rename", [ |
| 38 | + 'name' => 'Test Server Name', |
| 39 | + ]) |
| 40 | + ->assertStatus(Response::HTTP_NO_CONTENT); |
| 41 | + |
| 42 | + $server = $server->refresh(); |
| 43 | + $this->assertSame('Test Server Name', $server->name); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Test that a subuser receives a permissions error if they do not have the required permission |
| 48 | + * and attempt to change the name. |
| 49 | + */ |
| 50 | + public function testSubuserCannotChangeServerNameWithoutPermission() |
| 51 | + { |
| 52 | + [$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]); |
| 53 | + $originalName = $server->name; |
| 54 | + |
| 55 | + $this->actingAs($user) |
| 56 | + ->postJson("/api/client/servers/{$server->uuid}/settings/rename", [ |
| 57 | + 'name' => 'Test Server Name', |
| 58 | + ]) |
| 59 | + ->assertStatus(Response::HTTP_FORBIDDEN); |
| 60 | + |
| 61 | + $server = $server->refresh(); |
| 62 | + $this->assertSame($originalName, $server->name); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Test that a server can be reinstalled. Honestly this test doesn't do much of anything other |
| 67 | + * than make sure the endpoint works since. |
| 68 | + * |
| 69 | + * @param array $permissions |
| 70 | + * @dataProvider reinstallPermissionsDataProvider |
| 71 | + */ |
| 72 | + public function testServerCanBeReinstalled($permissions) |
| 73 | + { |
| 74 | + /** @var \Pterodactyl\Models\Server $server */ |
| 75 | + [$user, $server] = $this->generateTestAccount($permissions); |
| 76 | + $this->assertSame(Server::STATUS_INSTALLED, $server->installed); |
| 77 | + |
| 78 | + $service = Mockery::mock(DaemonServerRepository::class); |
| 79 | + $this->app->instance(DaemonServerRepository::class, $service); |
| 80 | + |
| 81 | + $service->expects('setServer') |
| 82 | + ->with(Mockery::on(function ($value) use ($server) { |
| 83 | + return $value->uuid === $server->uuid; |
| 84 | + })) |
| 85 | + ->andReturnSelf() |
| 86 | + ->getMock() |
| 87 | + ->expects('reinstall') |
| 88 | + ->andReturnUndefined(); |
| 89 | + |
| 90 | + $this->actingAs($user)->postJson("/api/client/servers/{$server->uuid}/settings/reinstall") |
| 91 | + ->assertStatus(Response::HTTP_ACCEPTED); |
| 92 | + |
| 93 | + $server = $server->refresh(); |
| 94 | + $this->assertSame(Server::STATUS_INSTALLING, $server->installed); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Test that a subuser receives a permissions error if they do not have the required permission |
| 99 | + * and attempt to reinstall a server. |
| 100 | + */ |
| 101 | + public function testSubuserCannotReinstallServerWithoutPermission() |
| 102 | + { |
| 103 | + [$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]); |
| 104 | + |
| 105 | + $this->actingAs($user) |
| 106 | + ->postJson("/api/client/servers/{$server->uuid}/settings/reinstall") |
| 107 | + ->assertStatus(Response::HTTP_FORBIDDEN); |
| 108 | + |
| 109 | + $server = $server->refresh(); |
| 110 | + $this->assertSame(Server::STATUS_INSTALLED, $server->installed); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return array |
| 115 | + */ |
| 116 | + public function renamePermissionsDataProvider(): array |
| 117 | + { |
| 118 | + return [[[]], [[Permission::ACTION_SETTINGS_RENAME]]]; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return array |
| 123 | + */ |
| 124 | + public function reinstallPermissionsDataProvider(): array |
| 125 | + { |
| 126 | + return [[[]], [[Permission::ACTION_SETTINGS_REINSTALL]]]; |
| 127 | + } |
| 128 | +} |
0 commit comments