Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Client/Server/PowerControllerTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client\Server;
4
5
use Illuminate\Http\Response;
6
use Pterodactyl\Models\Permission;
7
use Pterodactyl\Repositories\Wings\DaemonPowerRepository;
8
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
9
10
class PowerControllerTest extends ClientApiIntegrationTestCase
11
{
12
/**
13
* Test that a subuser without permission to send a command to the server receives
14
* an error in response. This checks against the specific permission needed to send
15
* the command to the server.
16
*
17
* @param string[] $permissions
18
*/
19
#[\PHPUnit\Framework\Attributes\DataProvider('invalidPermissionDataProvider')]
20
public function testSubuserWithoutPermissionsReceivesError(string $action, array $permissions)
21
{
22
[$user, $server] = $this->generateTestAccount($permissions);
23
24
$this->actingAs($user)
25
->postJson("/api/client/servers/$server->uuid/power", ['signal' => $action])
26
->assertStatus(Response::HTTP_FORBIDDEN);
27
}
28
29
/**
30
* Test that sending an invalid power signal returns an error.
31
*/
32
public function testInvalidPowerSignalResultsInError()
33
{
34
[$user, $server] = $this->generateTestAccount();
35
36
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/power", [
37
'signal' => 'invalid',
38
]);
39
40
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
41
$response->assertJsonPath('errors.0.meta.rule', 'in');
42
$response->assertJsonPath('errors.0.detail', 'The selected signal is invalid.');
43
}
44
45
/**
46
* Test that sending a valid power actions works.
47
*/
48
#[\PHPUnit\Framework\Attributes\DataProvider('validPowerActionDataProvider')]
49
public function testActionCanBeSentToServer(string $action, string $permission)
50
{
51
$service = \Mockery::mock(DaemonPowerRepository::class);
52
$this->app->instance(DaemonPowerRepository::class, $service);
53
54
[$user, $server] = $this->generateTestAccount([$permission]);
55
56
$service->expects('setServer')
57
->with(\Mockery::on(function ($value) use ($server) {
58
return $server->uuid === $value->uuid;
59
}))
60
->andReturnSelf()
61
->getMock()
62
->expects('send')
63
->with(trim($action));
64
65
$this->actingAs($user)
66
->postJson("/api/client/servers/$server->uuid/power", ['signal' => $action])
67
->assertStatus(Response::HTTP_NO_CONTENT);
68
}
69
70
/**
71
* Returns invalid permission combinations for a given power action.
72
*/
73
public static function invalidPermissionDataProvider(): array
74
{
75
return [
76
['start', [Permission::ACTION_CONTROL_STOP, Permission::ACTION_CONTROL_RESTART]],
77
['stop', [Permission::ACTION_CONTROL_START]],
78
['kill', [Permission::ACTION_CONTROL_START, Permission::ACTION_CONTROL_RESTART]],
79
['restart', [Permission::ACTION_CONTROL_STOP, Permission::ACTION_CONTROL_START]],
80
['random', [Permission::ACTION_CONTROL_START]],
81
];
82
}
83
84
public static function validPowerActionDataProvider(): array
85
{
86
return [
87
['start', Permission::ACTION_CONTROL_START],
88
['stop', Permission::ACTION_CONTROL_STOP],
89
['restart', Permission::ACTION_CONTROL_RESTART],
90
['kill', Permission::ACTION_CONTROL_STOP],
91
// Yes, these spaces are intentional. You should be able to send values with or without
92
// a space on the start/end since we should be trimming the values.
93
[' restart', Permission::ACTION_CONTROL_RESTART],
94
['kill ', Permission::ACTION_CONTROL_STOP],
95
];
96
}
97
}
98
99