Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Client/Server/SettingsControllerTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client\Server;
4
5
use Illuminate\Http\Response;
6
use Pterodactyl\Models\Server;
7
use Pterodactyl\Models\Permission;
8
use Pterodactyl\Repositories\Wings\DaemonServerRepository;
9
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
10
11
class SettingsControllerTest extends ClientApiIntegrationTestCase
12
{
13
/**
14
* Test that the server's name can be changed.
15
*/
16
#[\PHPUnit\Framework\Attributes\DataProvider('renamePermissionsDataProvider')]
17
public function testServerNameCanBeChanged(array $permissions)
18
{
19
/** @var Server $server */
20
[$user, $server] = $this->generateTestAccount($permissions);
21
$originalName = $server->name;
22
$originalDescription = $server->description;
23
24
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/settings/rename", [
25
'name' => '',
26
'description' => '',
27
]);
28
29
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
30
$response->assertJsonPath('errors.0.meta.rule', 'required');
31
32
$server = $server->refresh();
33
$this->assertSame($originalName, $server->name);
34
$this->assertSame($originalDescription, $server->description);
35
36
$this->actingAs($user)
37
->postJson("/api/client/servers/$server->uuid/settings/rename", [
38
'name' => 'Test Server Name',
39
'description' => 'This is a test server.',
40
])
41
->assertStatus(Response::HTTP_NO_CONTENT);
42
43
$server = $server->refresh();
44
$this->assertSame('Test Server Name', $server->name);
45
$this->assertSame('This is a test server.', $server->description);
46
}
47
48
/**
49
* Test that a subuser receives a permissions error if they do not have the required permission
50
* and attempt to change the name.
51
*/
52
public function testSubuserCannotChangeServerNameWithoutPermission()
53
{
54
[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);
55
$originalName = $server->name;
56
57
$this->actingAs($user)
58
->postJson("/api/client/servers/$server->uuid/settings/rename", [
59
'name' => 'Test Server Name',
60
])
61
->assertStatus(Response::HTTP_FORBIDDEN);
62
63
$server = $server->refresh();
64
$this->assertSame($originalName, $server->name);
65
}
66
67
/**
68
* Test that a server can be reinstalled. Honestly this test doesn't do much of anything other
69
* than make sure the endpoint works since.
70
*/
71
#[\PHPUnit\Framework\Attributes\DataProvider('reinstallPermissionsDataProvider')]
72
public function testServerCanBeReinstalled(array $permissions)
73
{
74
/** @var Server $server */
75
[$user, $server] = $this->generateTestAccount($permissions);
76
$this->assertTrue($server->isInstalled());
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->status);
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->assertTrue($server->isInstalled());
111
}
112
113
public static function renamePermissionsDataProvider(): array
114
{
115
return [[[]], [[Permission::ACTION_SETTINGS_RENAME]]];
116
}
117
118
public static function reinstallPermissionsDataProvider(): array
119
{
120
return [[[]], [[Permission::ACTION_SETTINGS_REINSTALL]]];
121
}
122
}
123
124