Fix multiple controller unit test failures

This commit is contained in:
Dane Everitt 2017-11-03 16:43:28 -05:00
parent 7b3393aff9
commit 54228e8124
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
9 changed files with 404 additions and 317 deletions

View file

@ -10,50 +10,29 @@
namespace Tests\Unit\Http\Controllers\Server\Files;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use Illuminate\Http\Request;
use Pterodactyl\Models\Server;
use Illuminate\Contracts\Session\Session;
use Tests\Traits\MocksRequestException;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Contracts\Config\Repository;
use Tests\Assertions\ControllerAssertionsTrait;
use Pterodactyl\Exceptions\PterodactylException;
use Tests\Unit\Http\Controllers\ControllerTestCase;
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Http\Controllers\Server\Files\RemoteRequestController;
class RemoteRequestControllerTest extends TestCase
class RemoteRequestControllerTest extends ControllerTestCase
{
use ControllerAssertionsTrait;
use MocksRequestException;
/**
* @var \Illuminate\Contracts\Config\Repository
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
*/
protected $config;
/**
* @var \Pterodactyl\Http\Controllers\Server\Files\RemoteRequestController
* @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface|\Mockery\Mock
*/
protected $controller;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface
*/
protected $fileRepository;
/**
* @var \Illuminate\Http\Request
*/
protected $request;
/**
* @var \Illuminate\Contracts\Session\Session
*/
protected $session;
/**
* @var \Illuminate\Log\Writer
*/
protected $writer;
protected $repository;
/**
* Setup tests.
@ -63,17 +42,7 @@ class RemoteRequestControllerTest extends TestCase
parent::setUp();
$this->config = m::mock(Repository::class);
$this->fileRepository = m::mock(FileRepositoryInterface::class);
$this->request = m::mock(Request::class);
$this->session = m::mock(Session::class);
$this->writer = m::mock(Writer::class);
$this->controller = m::mock(RemoteRequestController::class, [
$this->config,
$this->fileRepository,
$this->session,
$this->writer,
])->makePartial();
$this->repository = m::mock(FileRepositoryInterface::class);
}
/**
@ -81,19 +50,21 @@ class RemoteRequestControllerTest extends TestCase
*/
public function testDirectoryController()
{
$server = factory(Server::class)->make();
$controller = $this->getController();
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
$this->controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
$server = factory(Server::class)->make();
$this->setRequestAttribute('server', $server);
$this->setRequestAttribute('server_token', 'abc123');
$controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
$this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/');
$this->session->shouldReceive('get')->with('server_data.token')->once()->andReturn($server->daemonSecret);
$this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with($server->daemonSecret)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf()
->shouldReceive('getDirectory')->with('/')->once()->andReturn(['folders' => 1, 'files' => 2]);
$this->config->shouldReceive('get')->with('pterodactyl.files.editable')->once()->andReturn([]);
$response = $this->controller->directory($this->request);
$response = $controller->directory($this->request);
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('server.files.list', $response);
$this->assertViewHasKey('files', $response);
@ -112,21 +83,22 @@ class RemoteRequestControllerTest extends TestCase
*/
public function testExceptionThrownByDaemonConnectionIsHandledByDisplayController()
{
$this->configureExceptionMock();
$controller = $this->getController();
$server = factory(Server::class)->make();
$exception = m::mock(RequestException::class);
$this->setRequestAttribute('server', $server);
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
$this->controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
$controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
$this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/');
$this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($exception);
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
$this->writer->shouldReceive('warning')->with($exception)->once()->andReturnNull();
$exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
$response = $this->controller->directory($this->request);
$this->assertIsJsonResponse($response);
$this->assertResponseJsonEquals(['error' => trans('exceptions.daemon_connection_failed', ['code' => 'E_CONN_REFUSED'])], $response);
$this->assertResponseCodeEquals(500, $response);
try {
$controller->directory($this->request);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
}
}
/**
@ -134,19 +106,21 @@ class RemoteRequestControllerTest extends TestCase
*/
public function testStoreController()
{
$server = factory(Server::class)->make();
$controller = $this->getController();
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
$this->controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
$this->session->shouldReceive('get')->with('server_data.token')->once()->andReturn($server->daemonSecret);
$server = factory(Server::class)->make();
$this->setRequestAttribute('server', $server);
$this->setRequestAttribute('server_token', 'abc123');
$controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
$this->request->shouldReceive('input')->with('file')->once()->andReturn('file.txt');
$this->request->shouldReceive('input')->with('contents')->once()->andReturn('file contents');
$this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with($server->daemonSecret)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf()
->shouldReceive('putContent')->with('file.txt', 'file contents')->once()->andReturnNull();
$response = $this->controller->store($this->request, '1234');
$response = $controller->store($this->request);
$this->assertIsResponse($response);
$this->assertResponseCodeEquals(204, $response);
}
@ -156,19 +130,30 @@ class RemoteRequestControllerTest extends TestCase
*/
public function testExceptionThrownByDaemonConnectionIsHandledByStoreController()
{
$this->configureExceptionMock();
$controller = $this->getController();
$server = factory(Server::class)->make();
$exception = m::mock(RequestException::class);
$this->setRequestAttribute('server', $server);
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
$this->controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
$this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($exception);
$controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
$this->writer->shouldReceive('warning')->with($exception)->once()->andReturnNull();
$exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
try {
$controller->store($this->request);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
}
}
$response = $this->controller->store($this->request, '1234');
$this->assertIsJsonResponse($response);
$this->assertResponseJsonEquals(['error' => trans('exceptions.daemon_connection_failed', ['code' => 'E_CONN_REFUSED'])], $response);
$this->assertResponseCodeEquals(500, $response);
/**
* 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]);
}
}