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 test_subuser_without_permission_receives_error(): void { [$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 test_command_can_send_to_server(): void { [$user, $server] = $this->generateTestAccount([Permission::ACTION_CONTROL_CONSOLE]); $server = \Mockery::mock($server)->makePartial(); $this->instance(Server::class, $server); $server->expects('send')->with('say Test')->andReturn(new GuzzleResponse()); $request = new SendCommandRequest(['command' => 'say Test']); $cc = resolve(CommandController::class); $response = $cc->index($request, $server); $this->assertEquals(Response::HTTP_NO_CONTENT, $response->getStatusCode()); } /** * Test that an error is returned when the server is offline that is more specific than the * regular daemon connection error. */ public function test_error_is_returned_when_server_is_offline(): void { [$user, $server] = $this->generateTestAccount(); $server = \Mockery::mock($server)->makePartial(); $server->expects('send')->andThrows(new ConnectionException(previous: new BadResponseException('', new Request('GET', 'test'), new GuzzleResponse(Response::HTTP_BAD_GATEWAY)))); $this->instance(Server::class, $server); $request = new SendCommandRequest(['command' => 'say Test']); $cc = resolve(CommandController::class); $this->expectException(HttpException::class); $this->expectExceptionMessageMatches('/Server must be online in order to send commands\./'); $response = $cc->index($request, $server); $this->assertEquals(Response::HTTP_BAD_GATEWAY, $response->getStatusCode()); } }