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