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.'); } }