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,34 +10,22 @@
namespace Tests\Unit\Http\Controllers\Server\Files;
use Mockery as m;
use Tests\TestCase;
use phpmock\phpunit\PHPMock;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Server;
use Illuminate\Cache\Repository;
use Illuminate\Contracts\Session\Session;
use Tests\Assertions\ControllerAssertionsTrait;
use Tests\Unit\Http\Controllers\ControllerTestCase;
use Pterodactyl\Http\Controllers\Server\Files\DownloadController;
class DownloadControllerTest extends TestCase
class DownloadControllerTest extends ControllerTestCase
{
use ControllerAssertionsTrait, PHPMock;
use PHPMock;
/**
* @var \Illuminate\Cache\Repository
* @var \Illuminate\Cache\Repository|\Mockery\Mock
*/
protected $cache;
/**
* @var \Pterodactyl\Http\Controllers\Server\Files\DownloadController
*/
protected $controller;
/**
* @var \Illuminate\Contracts\Session\Session
*/
protected $session;
/**
* Setup tests.
*/
@ -46,9 +34,6 @@ class DownloadControllerTest extends TestCase
parent::setUp();
$this->cache = m::mock(Repository::class);
$this->session = m::mock(Session::class);
$this->controller = m::mock(DownloadController::class, [$this->cache, $this->session])->makePartial();
}
/**
@ -56,22 +41,33 @@ class DownloadControllerTest extends TestCase
*/
public function testIndexController()
{
$controller = $this->getController();
$server = factory(Server::class)->make();
$node = factory(Node::class)->make();
$server->node = $node;
$server->setRelation('node', factory(Node::class)->make());
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
$this->controller->shouldReceive('authorize')->with('download-files', $server)->once()->andReturnNull();
$this->setRequestAttribute('server', $server);
$controller->shouldReceive('authorize')->with('download-files', $server)->once()->andReturnNull();
$this->getFunctionMock('\\Pterodactyl\\Http\\Controllers\\Server\\Files', 'str_random')
->expects($this->once())->willReturn('randomString');
$this->cache->shouldReceive('tags')->with(['Server:Downloads'])->once()->andReturnSelf()
->shouldReceive('put')->with('randomString', ['server' => $server->uuid, 'path' => '/my/file.txt'], 5)->once()->andReturnNull();
$this->cache->shouldReceive('tags')->with(['Server:Downloads'])->once()->andReturnSelf();
$this->cache->shouldReceive('put')->with('randomString', ['server' => $server->uuid, 'path' => '/my/file.txt'], 5)->once()->andReturnNull();
$response = $this->controller->index('1234', '/my/file.txt');
$response = $controller->index($this->request, $server->uuidShort, '/my/file.txt');
$this->assertIsRedirectResponse($response);
$this->assertRedirectUrlEquals(sprintf(
'%s://%s:%s/v1/server/file/download/%s', $server->node->scheme, $server->node->fqdn, $server->node->daemonListen, 'randomString'
), $response);
}
/**
* Return a mocked instance of the controller to allow access to authorization functionality.
*
* @return \Pterodactyl\Http\Controllers\Server\Files\DownloadController|\Mockery\Mock
*/
private function getController()
{
return $this->buildMockedController(DownloadController::class, [$this->cache]);
}
}

View file

@ -10,51 +10,24 @@
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 Pterodactyl\Exceptions\DisplayException;
use Tests\Assertions\ControllerAssertionsTrait;
use Pterodactyl\Exceptions\PterodactylException;
use Tests\Unit\Http\Controllers\ControllerTestCase;
use Pterodactyl\Http\Requests\Server\UpdateFileContentsFormRequest;
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
use Pterodactyl\Http\Controllers\Server\Files\FileActionsController;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class FileActionsControllerTest extends TestCase
class FileActionsControllerTest extends ControllerTestCase
{
use ControllerAssertionsTrait;
use MocksRequestException;
/**
* @var \Pterodactyl\Http\Controllers\Server\Files\FileActionsController
* @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface|\Mockery\Mock
*/
protected $controller;
/**
* @var \Pterodactyl\Http\Requests\Server\UpdateFileContentsFormRequest
*/
protected $fileContentsFormRequest;
/**
* @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,15 +36,7 @@ class FileActionsControllerTest extends TestCase
{
parent::setUp();
$this->fileContentsFormRequest = m::mock(UpdateFileContentsFormRequest::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(FileActionsController::class, [
$this->fileRepository, $this->session, $this->writer,
])->makePartial();
$this->repository = m::mock(FileRepositoryInterface::class);
}
/**
@ -79,14 +44,16 @@ class FileActionsControllerTest extends TestCase
*/
public function testIndexController()
{
$controller = $this->getController();
$server = factory(Server::class)->make();
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
$this->controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
$this->request->shouldReceive('user->can')->andReturn(true);
$this->controller->shouldReceive('injectJavascript')->once()->andReturnNull();
$this->setRequestAttribute('server', $server);
$this->mockInjectJavascript();
$response = $this->controller->index($this->request);
$controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
$this->request->shouldReceive('user->can')->andReturn(true);
$response = $controller->index($this->request);
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('server.files.index', $response);
}
@ -98,14 +65,16 @@ class FileActionsControllerTest extends TestCase
*/
public function testCreateController($directory, $expected)
{
$controller = $this->getController();
$server = factory(Server::class)->make();
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
$this->controller->shouldReceive('authorize')->with('create-files', $server)->once()->andReturnNull();
$this->controller->shouldReceive('injectJavascript')->once()->andReturnNull();
$this->setRequestAttribute('server', $server);
$this->mockInjectJavascript();
$controller->shouldReceive('authorize')->with('create-files', $server)->once()->andReturnNull();
$this->request->shouldReceive('get')->with('dir')->andReturn($directory);
$response = $this->controller->create($this->request);
$response = $controller->create($this->request);
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('server.files.add', $response);
$this->assertViewHasKey('directory', $response);
@ -119,20 +88,22 @@ class FileActionsControllerTest extends TestCase
*/
public function testUpdateController($file, $expected)
{
$this->setRequestMockClass(UpdateFileContentsFormRequest::class);
$controller = $this->getController();
$server = factory(Server::class)->make();
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
$this->controller->shouldReceive('authorize')->with('edit-files', $server)->once()->andReturnNull();
$this->session->shouldReceive('get')->with('server_data.token')->once()->andReturn($server->daemonSecret);
$this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
$this->setRequestAttribute('server', $server);
$this->setRequestAttribute('server_token', 'abc123');
$this->setRequestAttribute('file_stats', 'fileStatsObject');
$this->mockInjectJavascript(['stat' => 'fileStatsObject']);
$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('getContent')->with($file)->once()->andReturn('file contents');
$this->fileContentsFormRequest->shouldReceive('getStats')->withNoArgs()->twice()->andReturn(['stats']);
$this->controller->shouldReceive('injectJavascript')->with(['stat' => ['stats']])->once()->andReturnNull();
$response = $this->controller->update($this->fileContentsFormRequest, '1234', $file);
$response = $controller->update($this->request, '1234', $file);
$this->assertIsViewResponse($response);
$this->assertViewNameEquals('server.files.edit', $response);
$this->assertViewHasKey('file', $response);
@ -140,7 +111,7 @@ class FileActionsControllerTest extends TestCase
$this->assertViewHasKey('contents', $response);
$this->assertViewHasKey('directory', $response);
$this->assertViewKeyEquals('file', $file, $response);
$this->assertViewKeyEquals('stat', ['stats'], $response);
$this->assertViewKeyEquals('stat', 'fileStatsObject', $response);
$this->assertViewKeyEquals('contents', 'file contents', $response);
$this->assertViewKeyEquals('directory', $expected, $response);
}
@ -150,20 +121,23 @@ class FileActionsControllerTest extends TestCase
*/
public function testExceptionRenderedByUpdateController()
{
$this->setRequestMockClass(UpdateFileContentsFormRequest::class);
$this->configureExceptionMock();
$controller = $this->getController();
$server = factory(Server::class)->make();
$exception = m::mock(RequestException::class);
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
$this->controller->shouldReceive('authorize')->with('edit-files', $server)->once()->andReturnNull();
$this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($exception);
$this->setRequestAttribute('server', $server);
$this->setRequestAttribute('server_token', 'abc123');
$this->setRequestAttribute('file_stats', 'fileStatsObject');
$exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
$this->writer->shouldReceive('warning')->with($exception)->once()->andReturnNull();
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
try {
$this->controller->update($this->fileContentsFormRequest, '1234', 'file.txt');
} catch (DisplayException $exception) {
$this->assertEquals(trans('exceptions.daemon_connection_failed', ['code' => 'E_CONN_REFUSED']), $exception->getMessage());
$controller->update($this->request, '1234', 'file.txt');
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
}
}
@ -199,4 +173,14 @@ class FileActionsControllerTest extends TestCase
['./file.txt', '/'],
];
}
/**
* Return a mocked instance of the controller to allow access to authorization functionality.
*
* @return \Pterodactyl\Http\Controllers\Server\Files\FileActionsController|\Mockery\Mock
*/
private function getController()
{
return $this->buildMockedController(FileActionsController::class, [$this->repository]);
}
}

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]);
}
}