Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/tests/Integration/Api/Client/Server/CommandControllerTest.php
7461 views
1
<?php
2
3
namespace Pterodactyl\Tests\Integration\Api\Client\Server;
4
5
use GuzzleHttp\Psr7\Request;
6
use Illuminate\Http\Response;
7
use Pterodactyl\Models\Server;
8
use Pterodactyl\Models\Permission;
9
use GuzzleHttp\Exception\BadResponseException;
10
use GuzzleHttp\Psr7\Response as GuzzleResponse;
11
use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
12
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
13
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
14
15
class CommandControllerTest extends ClientApiIntegrationTestCase
16
{
17
/**
18
* Test that a validation error is returned if there is no command present in the
19
* request.
20
*/
21
public function testValidationErrorIsReturnedIfNoCommandIsPresent()
22
{
23
[$user, $server] = $this->generateTestAccount();
24
25
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
26
'command' => '',
27
]);
28
29
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
30
$response->assertJsonPath('errors.0.meta.rule', 'required');
31
}
32
33
/**
34
* Test that a subuser without the required permission receives an error when trying to
35
* execute the command.
36
*/
37
public function testSubuserWithoutPermissionReceivesError()
38
{
39
[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);
40
41
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
42
'command' => 'say Test',
43
]);
44
45
$response->assertStatus(Response::HTTP_FORBIDDEN);
46
}
47
48
/**
49
* Test that a command can be sent to the server.
50
*/
51
public function testCommandCanSendToServer()
52
{
53
[$user, $server] = $this->generateTestAccount([Permission::ACTION_CONTROL_CONSOLE]);
54
55
$mock = $this->mock(DaemonCommandRepository::class);
56
$mock->expects('setServer')
57
->with(\Mockery::on(fn (Server $value) => $value->is($server)))
58
->andReturnSelf();
59
60
$mock->expects('send')->with('say Test')->andReturn(new GuzzleResponse());
61
62
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
63
'command' => 'say Test',
64
]);
65
66
$response->assertStatus(Response::HTTP_NO_CONTENT);
67
}
68
69
/**
70
* Test that an error is returned when the server is offline that is more specific than the
71
* regular daemon connection error.
72
*/
73
public function testErrorIsReturnedWhenServerIsOffline()
74
{
75
[$user, $server] = $this->generateTestAccount();
76
77
$mock = $this->mock(DaemonCommandRepository::class);
78
$mock->expects('setServer->send')->andThrows(
79
new DaemonConnectionException(
80
new BadResponseException('', new Request('GET', 'test'), new GuzzleResponse(Response::HTTP_BAD_GATEWAY))
81
)
82
);
83
84
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
85
'command' => 'say Test',
86
]);
87
88
$response->assertStatus(Response::HTTP_BAD_GATEWAY);
89
$response->assertJsonPath('errors.0.code', 'HttpException');
90
$response->assertJsonPath('errors.0.detail', 'Server must be online in order to send commands.');
91
}
92
}
93
94