misc_pterodactyl-panel/tests/Unit/Http/Controllers/Server/Files/RemoteRequestControllerTest.php

160 lines
6.4 KiB
PHP
Raw Normal View History

2017-09-04 19:34:38 +00:00
<?php
2017-09-26 02:43:01 +00:00
/**
2017-09-04 19:34:38 +00:00
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
2017-09-04 19:34:38 +00:00
*/
namespace Tests\Unit\Http\Controllers\Server\Files;
use Mockery as m;
use Pterodactyl\Models\Server;
use Tests\Traits\MocksRequestException;
2017-09-04 19:34:38 +00:00
use GuzzleHttp\Exception\RequestException;
use Illuminate\Contracts\Config\Repository;
use Pterodactyl\Exceptions\PterodactylException;
use Tests\Unit\Http\Controllers\ControllerTestCase;
2017-09-04 19:34:38 +00:00
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
2017-09-04 19:34:38 +00:00
use Pterodactyl\Http\Controllers\Server\Files\RemoteRequestController;
class RemoteRequestControllerTest extends ControllerTestCase
2017-09-04 19:34:38 +00:00
{
use MocksRequestException;
2017-09-04 19:34:38 +00:00
/**
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
2017-09-04 19:34:38 +00:00
*/
protected $config;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface|\Mockery\Mock
2017-09-04 19:34:38 +00:00
*/
protected $repository;
2017-09-04 19:34:38 +00:00
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->config = m::mock(Repository::class);
$this->repository = m::mock(FileRepositoryInterface::class);
2017-09-04 19:34:38 +00:00
}
/**
* Test the directory listing controller.
*/
public function testDirectoryController()
{
$controller = $this->getController();
2017-09-04 19:34:38 +00:00
$server = factory(Server::class)->make();
$this->setRequestAttribute('server', $server);
$this->setRequestAttribute('server_token', 'abc123');
2017-09-04 19:34:38 +00:00
$controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
2017-09-04 19:34:38 +00:00
$this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/');
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
2017-09-04 19:34:38 +00:00
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf()
2017-09-04 19:34:38 +00:00
->shouldReceive('getDirectory')->with('/')->once()->andReturn(['folders' => 1, 'files' => 2]);
$this->config->shouldReceive('get')->with('pterodactyl.files.editable')->once()->andReturn([]);
$response = $controller->directory($this->request);
2017-09-04 19:34:38 +00:00
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('server.files.list', $response);
$this->assertViewHasKey('files', $response);
$this->assertViewHasKey('folders', $response);
$this->assertViewHasKey('editableMime', $response);
$this->assertViewHasKey('directory', $response);
$this->assertViewKeyEquals('files', 2, $response);
$this->assertViewKeyEquals('folders', 1, $response);
$this->assertViewKeyEquals('editableMime', [], $response);
$this->assertViewKeyEquals('directory.first', false, $response);
$this->assertViewKeyEquals('directory.header', '', $response);
}
/**
* Test that the controller properly handles an exception thrown by the daemon conneciton.
*/
public function testExceptionThrownByDaemonConnectionIsHandledByDisplayController()
{
$this->configureExceptionMock();
$controller = $this->getController();
2017-09-04 19:34:38 +00:00
$server = factory(Server::class)->make();
$this->setRequestAttribute('server', $server);
2017-09-04 19:34:38 +00:00
$controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
2017-09-04 19:34:38 +00:00
$this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/');
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
try {
$controller->directory($this->request);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
}
2017-09-04 19:34:38 +00:00
}
/**
* Test the store controller.
*/
public function testStoreController()
{
$controller = $this->getController();
2017-09-04 19:34:38 +00:00
$server = factory(Server::class)->make();
$this->setRequestAttribute('server', $server);
$this->setRequestAttribute('server_token', 'abc123');
2017-09-04 19:34:38 +00:00
$controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
2017-09-04 19:34:38 +00:00
$this->request->shouldReceive('input')->with('file')->once()->andReturn('file.txt');
$this->request->shouldReceive('input')->with('contents')->once()->andReturn('file contents');
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
2017-09-04 19:34:38 +00:00
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf()
2017-09-04 19:34:38 +00:00
->shouldReceive('putContent')->with('file.txt', 'file contents')->once()->andReturnNull();
$response = $controller->store($this->request);
2017-09-04 19:34:38 +00:00
$this->assertIsResponse($response);
$this->assertResponseCodeEquals(204, $response);
}
/**
* Test that the controller properly handles an exception thrown by the daemon conneciton.
*/
public function testExceptionThrownByDaemonConnectionIsHandledByStoreController()
{
$this->configureExceptionMock();
$controller = $this->getController();
2017-09-04 19:34:38 +00:00
$server = factory(Server::class)->make();
$this->setRequestAttribute('server', $server);
2017-09-04 19:34:38 +00:00
$controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
2017-09-04 19:34:38 +00:00
try {
$controller->store($this->request);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
}
}
2017-09-04 19:34:38 +00:00
/**
* Return a mocked instance of the controller to allow access to authorization functionality.
*
* @return \Pterodactyl\Http\Controllers\Server\Files\RemoteRequestController|\Mockery\Mock
*/
private function getController()
{
return $this->buildMockedController(RemoteRequestController::class, [$this->config, $this->repository]);
2017-09-04 19:34:38 +00:00
}
}