forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandControllerTest.php
More file actions
93 lines (76 loc) · 3.25 KB
/
CommandControllerTest.php
File metadata and controls
93 lines (76 loc) · 3.25 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
<?php
namespace Pterodactyl\Tests\Integration\Api\Client\Server;
use Mockery;
use GuzzleHttp\Psr7\Request;
use Illuminate\Http\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Permission;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use Pterodactyl\Repositories\Wings\DaemonCommandRepository;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Tests\Integration\Api\Client\ClientApiIntegrationTestCase;
class CommandControllerTest extends ClientApiIntegrationTestCase
{
/**
* Test that a validation error is returned if there is no command present in the
* request.
*/
public function testValidationErrorIsReturnedIfNoCommandIsPresent()
{
[$user, $server] = $this->generateTestAccount();
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
'command' => '',
]);
$response->assertStatus(Response::HTTP_UNPROCESSABLE_ENTITY);
$response->assertJsonPath('errors.0.meta.rule', 'required');
}
/**
* Test that a subuser without the required permission receives an error when trying to
* execute the command.
*/
public function testSubuserWithoutPermissionReceivesError()
{
[$user, $server] = $this->generateTestAccount([Permission::ACTION_WEBSOCKET_CONNECT]);
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
'command' => 'say Test',
]);
$response->assertStatus(Response::HTTP_FORBIDDEN);
}
/**
* Test that a command can be sent to the server.
*/
public function testCommandCanSendToServer()
{
[$user, $server] = $this->generateTestAccount([Permission::ACTION_CONTROL_CONSOLE]);
$mock = $this->mock(DaemonCommandRepository::class);
$mock->expects('setServer')
->with(Mockery::on(fn (Server $value) => $value->is($server)))
->andReturnSelf();
$mock->expects('send')->with('say Test')->andReturn(new GuzzleResponse());
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
'command' => 'say Test',
]);
$response->assertStatus(Response::HTTP_NO_CONTENT);
}
/**
* Test that an error is returned when the server is offline that is more specific than the
* regular daemon connection error.
*/
public function testErrorIsReturnedWhenServerIsOffline()
{
[$user, $server] = $this->generateTestAccount();
$mock = $this->mock(DaemonCommandRepository::class);
$mock->expects('setServer->send')->andThrows(
new DaemonConnectionException(
new BadResponseException('', new Request('GET', 'test'), new GuzzleResponse(Response::HTTP_BAD_GATEWAY))
)
);
$response = $this->actingAs($user)->postJson("/api/client/servers/$server->uuid/command", [
'command' => 'say Test',
]);
$response->assertStatus(Response::HTTP_BAD_GATEWAY);
$response->assertJsonPath('errors.0.code', 'HttpException');
$response->assertJsonPath('errors.0.detail', 'Server must be online in order to send commands.');
}
}