From 54228e81243985fb640700425ed6ee49a8a78143 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 3 Nov 2017 16:43:28 -0500 Subject: [PATCH] Fix multiple controller unit test failures --- .../Controllers/Server/SubuserController.php | 9 +- tests/Traits/Http/RequestMockHelpers.php | 73 ++++++++ .../Http/Controllers/ControllerTestCase.php | 86 ++++++++++ .../Server/ConsoleControllerTest.php | 42 ++--- .../Server/Files/DownloadControllerTest.php | 48 +++--- .../Files/FileActionsControllerTest.php | 128 +++++++------- .../Files/RemoteRequestControllerTest.php | 137 +++++++-------- .../Server/SubuserControllerTest.php | 157 +++++++++--------- .../Http/Middleware/MiddlewareTestCase.php | 41 +---- 9 files changed, 404 insertions(+), 317 deletions(-) create mode 100644 tests/Traits/Http/RequestMockHelpers.php create mode 100644 tests/Unit/Http/Controllers/ControllerTestCase.php diff --git a/app/Http/Controllers/Server/SubuserController.php b/app/Http/Controllers/Server/SubuserController.php index eb09fb63c..00d7b9291 100644 --- a/app/Http/Controllers/Server/SubuserController.php +++ b/app/Http/Controllers/Server/SubuserController.php @@ -128,7 +128,7 @@ class SubuserController extends Controller */ public function update(SubuserUpdateFormRequest $request, string $uuid, string $hash): RedirectResponse { - $this->subuserUpdateService->handle($request->attributes->get('subuser'), $request->input('permissions')); + $this->subuserUpdateService->handle($request->attributes->get('subuser'), $request->input('permissions', [])); $this->alert->success(trans('server.users.user_updated'))->flash(); return redirect()->route('server.subusers.view', ['uuid' => $uuid, 'subuser' => $hash]); @@ -154,7 +154,6 @@ class SubuserController extends Controller * Handles creating a new subuser. * * @param \Pterodactyl\Http\Requests\Server\Subuser\SubuserStoreFormRequest $request - * @param string $uuid * @return \Illuminate\Http\RedirectResponse * * @throws \Exception @@ -164,15 +163,15 @@ class SubuserController extends Controller * @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException * @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException */ - public function store(SubuserStoreFormRequest $request, $uuid): RedirectResponse + public function store(SubuserStoreFormRequest $request): RedirectResponse { $server = $request->attributes->get('server'); - $subuser = $this->subuserCreationService->handle($server, $request->input('email'), $request->input('permissions')); + $subuser = $this->subuserCreationService->handle($server, $request->input('email'), $request->input('permissions', [])); $this->alert->success(trans('server.users.user_assigned'))->flash(); return redirect()->route('server.subusers.view', [ - 'uuid' => $uuid, + 'uuid' => $server->uuid, 'id' => $subuser->id, ]); } diff --git a/tests/Traits/Http/RequestMockHelpers.php b/tests/Traits/Http/RequestMockHelpers.php new file mode 100644 index 000000000..d6cc7865a --- /dev/null +++ b/tests/Traits/Http/RequestMockHelpers.php @@ -0,0 +1,73 @@ +requestMockClass = $class; + + $this->buildRequestMock(); + } + + /** + * Set the active request object to be an instance of a mocked request. + */ + protected function buildRequestMock() + { + $this->request = m::mock($this->requestMockClass); + if (! $this->request instanceof Request) { + throw new InvalidArgumentException('First argument passed to buildRequestMock must be an instance of \Illuminate\Http\Request when mocked.'); + } + + $this->request->attributes = new ParameterBag(); + } + + /** + * Set a request attribute on the mock object. + * + * @param string $attribute + * @param mixed $value + */ + protected function setRequestAttribute(string $attribute, $value) + { + $this->request->attributes->set($attribute, $value); + } + + /** + * Sets the mocked request user. If a user model is not provided, a factory model + * will be created and returned. + * + * @param \Pterodactyl\Models\User|null $user + * @return \Pterodactyl\Models\User + */ + protected function setRequestUser(User $user = null): User + { + $user = $user instanceof User ? $user : factory(User::class)->make(); + $this->request->shouldReceive('user')->withNoArgs()->andReturn($user); + + return $user; + } +} diff --git a/tests/Unit/Http/Controllers/ControllerTestCase.php b/tests/Unit/Http/Controllers/ControllerTestCase.php new file mode 100644 index 000000000..6caf9abda --- /dev/null +++ b/tests/Unit/Http/Controllers/ControllerTestCase.php @@ -0,0 +1,86 @@ +buildRequestMock(); + } + + /** + * Set an instance of the controller. + * + * @param \Pterodactyl\Http\Controllers\Controller|\Mockery\Mock $controller + */ + public function setControllerInstance($controller) + { + $this->controller = $controller; + } + + /** + * Return an instance of the controller. + * + * @return \Mockery\Mock|\Pterodactyl\Http\Controllers\Controller + */ + public function getControllerInstance() + { + return $this->controller; + } + + /** + * Helper function to mock injectJavascript requests. + * + * @param array|null $args + * @param bool $subset + */ + protected function mockInjectJavascript(array $args = null, bool $subset = false) + { + $controller = $this->getControllerInstance(); + + $controller->shouldReceive('setRequest')->with($this->request)->once()->andReturnSelf(); + if (is_null($args)) { + $controller->shouldReceive('injectJavascript')->withAnyArgs()->once()->andReturnNull(); + } else { + $with = $subset ? m::subset($args) : $args; + + $controller->shouldReceive('injectJavascript')->with($with)->once()->andReturnNull(); + } + } + + /** + * Build and return a mocked controller instance to use for testing. + * + * @param string $class + * @param array $args + * @return \Mockery\Mock|\Pterodactyl\Http\Controllers\Controller + */ + protected function buildMockedController(string $class, array $args = []) + { + $controller = m::mock($class, $args)->makePartial(); + + if (is_null($this->getControllerInstance())) { + $this->setControllerInstance($controller); + } + + return $this->getControllerInstance(); + } +} diff --git a/tests/Unit/Http/Controllers/Server/ConsoleControllerTest.php b/tests/Unit/Http/Controllers/Server/ConsoleControllerTest.php index a73cf13e9..ef6334657 100644 --- a/tests/Unit/Http/Controllers/Server/ConsoleControllerTest.php +++ b/tests/Unit/Http/Controllers/Server/ConsoleControllerTest.php @@ -10,32 +10,18 @@ namespace Tests\Unit\Http\Controllers\Server; use Mockery as m; -use Tests\TestCase; use Pterodactyl\Models\Server; -use Illuminate\Contracts\Session\Session; use Illuminate\Contracts\Config\Repository; -use Tests\Assertions\ControllerAssertionsTrait; +use Tests\Unit\Http\Controllers\ControllerTestCase; use Pterodactyl\Http\Controllers\Server\ConsoleController; -class ConsoleControllerTest extends TestCase +class ConsoleControllerTest extends ControllerTestCase { - use ControllerAssertionsTrait; - /** - * @var \Illuminate\Contracts\Config\Repository + * @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock */ protected $config; - /** - * @var \Pterodactyl\Http\Controllers\Server\ConsoleController - */ - protected $controller; - - /** - * @var \Illuminate\Contracts\Session\Session - */ - protected $session; - /** * Setup tests. */ @@ -44,9 +30,6 @@ class ConsoleControllerTest extends TestCase parent::setUp(); $this->config = m::mock(Repository::class); - $this->session = m::mock(Session::class); - - $this->controller = m::mock(ConsoleController::class, [$this->config, $this->session])->makePartial(); } /** @@ -56,16 +39,15 @@ class ConsoleControllerTest extends TestCase */ public function testAllControllers($function, $view) { + $controller = $this->getController(); $server = factory(Server::class)->make(); + $this->setRequestAttribute('server', $server); + $this->mockInjectJavascript(); - if ($function === 'index') { - $this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server); - } $this->config->shouldReceive('get')->with('pterodactyl.console.count')->once()->andReturn(100); $this->config->shouldReceive('get')->with('pterodactyl.console.frequency')->once()->andReturn(10); - $this->controller->shouldReceive('injectJavascript')->once()->andReturnNull(); - $response = $this->controller->$function(); + $response = $controller->$function($this->request); $this->assertIsViewResponse($response); $this->assertViewNameEquals($view, $response); } @@ -82,4 +64,14 @@ class ConsoleControllerTest extends TestCase ['console', 'server.console'], ]; } + + /** + * Return a mocked instance of the controller to allow access to authorization functionality. + * + * @return \Pterodactyl\Http\Controllers\Server\ConsoleController|\Mockery\Mock + */ + private function getController() + { + return $this->buildMockedController(ConsoleController::class, [$this->config]); + } } diff --git a/tests/Unit/Http/Controllers/Server/Files/DownloadControllerTest.php b/tests/Unit/Http/Controllers/Server/Files/DownloadControllerTest.php index ac12fcff3..d44481c1b 100644 --- a/tests/Unit/Http/Controllers/Server/Files/DownloadControllerTest.php +++ b/tests/Unit/Http/Controllers/Server/Files/DownloadControllerTest.php @@ -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]); + } } diff --git a/tests/Unit/Http/Controllers/Server/Files/FileActionsControllerTest.php b/tests/Unit/Http/Controllers/Server/Files/FileActionsControllerTest.php index 5a7bfb08d..077cf02d9 100644 --- a/tests/Unit/Http/Controllers/Server/Files/FileActionsControllerTest.php +++ b/tests/Unit/Http/Controllers/Server/Files/FileActionsControllerTest.php @@ -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]); + } } diff --git a/tests/Unit/Http/Controllers/Server/Files/RemoteRequestControllerTest.php b/tests/Unit/Http/Controllers/Server/Files/RemoteRequestControllerTest.php index 1f818ce83..b44b225e2 100644 --- a/tests/Unit/Http/Controllers/Server/Files/RemoteRequestControllerTest.php +++ b/tests/Unit/Http/Controllers/Server/Files/RemoteRequestControllerTest.php @@ -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]); } } diff --git a/tests/Unit/Http/Controllers/Server/SubuserControllerTest.php b/tests/Unit/Http/Controllers/Server/SubuserControllerTest.php index 86d7bf29a..e243cf799 100644 --- a/tests/Unit/Http/Controllers/Server/SubuserControllerTest.php +++ b/tests/Unit/Http/Controllers/Server/SubuserControllerTest.php @@ -10,61 +10,43 @@ namespace Tests\Unit\Http\Controllers\Server; use Mockery as m; -use Tests\TestCase; -use Illuminate\Http\Request; use Pterodactyl\Models\Server; use Pterodactyl\Models\Subuser; use Pterodactyl\Models\Permission; use Prologue\Alerts\AlertsMessageBag; -use Illuminate\Contracts\Session\Session; -use Tests\Assertions\ControllerAssertionsTrait; +use Tests\Unit\Http\Controllers\ControllerTestCase; use Pterodactyl\Services\Subusers\SubuserUpdateService; use Pterodactyl\Services\Subusers\SubuserCreationService; use Pterodactyl\Services\Subusers\SubuserDeletionService; use Pterodactyl\Http\Controllers\Server\SubuserController; use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface; +use Pterodactyl\Http\Requests\Server\Subuser\SubuserStoreFormRequest; +use Pterodactyl\Http\Requests\Server\Subuser\SubuserUpdateFormRequest; -class SubuserControllerTest extends TestCase +class SubuserControllerTest extends ControllerTestCase { - use ControllerAssertionsTrait; - /** - * @var \Prologue\Alerts\AlertsMessageBag + * @var \Prologue\Alerts\AlertsMessageBag|\Mockery\Mock */ protected $alert; /** - * @var \Pterodactyl\Http\Controllers\Server\SubuserController - */ - protected $controller; - - /** - * @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock */ protected $repository; /** - * @var \Illuminate\Http\Request - */ - protected $request; - - /** - * @var \Illuminate\Contracts\Session\Session - */ - protected $session; - - /** - * @var \Pterodactyl\Services\Subusers\SubuserCreationService + * @var \Pterodactyl\Services\Subusers\SubuserCreationService|\Mockery\Mock */ protected $subuserCreationService; /** - * @var \Pterodactyl\Services\Subusers\SubuserDeletionService + * @var \Pterodactyl\Services\Subusers\SubuserDeletionService|\Mockery\Mock */ protected $subuserDeletionService; /** - * @var \Pterodactyl\Services\Subusers\SubuserUpdateService + * @var \Pterodactyl\Services\Subusers\SubuserUpdateService|\Mockery\Mock */ protected $subuserUpdateService; @@ -77,20 +59,9 @@ class SubuserControllerTest extends TestCase $this->alert = m::mock(AlertsMessageBag::class); $this->repository = m::mock(SubuserRepositoryInterface::class); - $this->request = m::mock(Request::class); - $this->session = m::mock(Session::class); $this->subuserCreationService = m::mock(SubuserCreationService::class); $this->subuserDeletionService = m::mock(SubuserDeletionService::class); $this->subuserUpdateService = m::mock(SubuserUpdateService::class); - - $this->controller = m::mock(SubuserController::class, [ - $this->alert, - $this->session, - $this->subuserCreationService, - $this->subuserDeletionService, - $this->repository, - $this->subuserUpdateService, - ])->makePartial(); } /* @@ -98,14 +69,16 @@ class SubuserControllerTest 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-subusers', $server)->once()->andReturnNull(); - $this->controller->shouldReceive('injectJavascript')->withNoArgs()->once()->andReturnNull(); + $this->setRequestAttribute('server', $server); + $this->mockInjectJavascript(); + + $controller->shouldReceive('authorize')->with('list-subusers', $server)->once()->andReturnNull(); $this->repository->shouldReceive('findWhere')->with([['server_id', '=', $server->id]])->once()->andReturn([]); - $response = $this->controller->index(); + $response = $controller->index($this->request); $this->assertIsViewResponse($response); $this->assertViewNameEquals('server.users.index', $response); $this->assertViewHasKey('subusers', $response); @@ -116,20 +89,22 @@ class SubuserControllerTest extends TestCase */ public function testViewController() { - $subuser = factory(Subuser::class)->make([ - 'permissions' => collect([ - (object) ['permission' => 'some.permission'], - (object) ['permission' => 'another.permission'], - ]), - ]); + $controller = $this->getController(); + $subuser = factory(Subuser::class)->make(); + $subuser->setRelation('permissions', collect([ + (object) ['permission' => 'some.permission'], + (object) ['permission' => 'another.permission'], + ])); $server = factory(Server::class)->make(); - $this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server); - $this->controller->shouldReceive('authorize')->with('view-subuser', $server)->once()->andReturnNull(); - $this->repository->shouldReceive('getWithPermissions')->with(1234)->once()->andReturn($subuser); - $this->controller->shouldReceive('injectJavascript')->withNoArgs()->once()->andReturnNull(); + $this->setRequestAttribute('server', $server); + $this->setRequestAttribute('subuser', $subuser); + $this->mockInjectJavascript(); - $response = $this->controller->view($server->uuid, 1234); + $controller->shouldReceive('authorize')->with('view-subuser', $server)->once()->andReturnNull(); + $this->repository->shouldReceive('getWithPermissions')->with($subuser)->once()->andReturn($subuser); + + $response = $controller->view($this->request); $this->assertIsViewResponse($response); $this->assertViewNameEquals('server.users.view', $response); $this->assertViewHasKey('subuser', $response); @@ -148,18 +123,21 @@ class SubuserControllerTest extends TestCase */ public function testUpdateController() { - $server = factory(Server::class)->make(); + $this->setRequestMockClass(SubuserUpdateFormRequest::class); + + $controller = $this->getController(); + $subuser = factory(Subuser::class)->make(); + + $this->setRequestAttribute('subuser', $subuser); - $this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server); - $this->controller->shouldReceive('authorize')->with('edit-subuser', $server)->once()->andReturnNull(); $this->request->shouldReceive('input')->with('permissions', [])->once()->andReturn(['some.permission']); - $this->subuserUpdateService->shouldReceive('handle')->with(1234, ['some.permission'])->once()->andReturnNull(); - $this->alert->shouldReceive('success')->with(trans('server.users.user_updated'))->once()->andReturnSelf() - ->shouldReceive('flash')->withNoArgs()->once()->andReturnNull(); + $this->subuserUpdateService->shouldReceive('handle')->with($subuser, ['some.permission'])->once()->andReturnNull(); + $this->alert->shouldReceive('success')->with(trans('server.users.user_updated'))->once()->andReturnSelf(); + $this->alert->shouldReceive('flash')->withNoArgs()->once()->andReturnNull(); - $response = $this->controller->update($this->request, $server->uuid, 1234); + $response = $controller->update($this->request, 'abcd1234', 1234); $this->assertIsRedirectResponse($response); - $this->assertRedirectRouteEquals('server.subusers.view', $response, ['uuid' => $server->uuid, 'id' => 1234]); + $this->assertRedirectRouteEquals('server.subusers.view', $response, ['uuid' => 'abcd1234', 'id' => 1234]); } /** @@ -167,13 +145,15 @@ class SubuserControllerTest extends TestCase */ public function testCreateController() { + $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-subuser', $server)->once()->andReturnNull(); - $this->controller->shouldReceive('injectJavascript')->withNoArgs()->once()->andReturnNull(); + $this->setRequestAttribute('server', $server); + $this->mockInjectJavascript(); - $response = $this->controller->create(); + $controller->shouldReceive('authorize')->with('create-subuser', $server)->once()->andReturnNull(); + + $response = $controller->create($this->request); $this->assertIsViewResponse($response); $this->assertViewNameEquals('server.users.new', $response); $this->assertViewHasKey('permissions', $response); @@ -185,20 +165,26 @@ class SubuserControllerTest extends TestCase */ public function testStoreController() { + $this->setRequestMockClass(SubuserStoreFormRequest::class); + $controller = $this->getController(); + $server = factory(Server::class)->make(); $subuser = factory(Subuser::class)->make(); - $this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server); - $this->controller->shouldReceive('authorize')->with('create-subuser', $server)->once()->andReturnNull(); + $this->setRequestAttribute('server', $server); + $this->request->shouldReceive('input')->with('email')->once()->andReturn('user@test.com'); $this->request->shouldReceive('input')->with('permissions', [])->once()->andReturn(['some.permission']); $this->subuserCreationService->shouldReceive('handle')->with($server, 'user@test.com', ['some.permission'])->once()->andReturn($subuser); - $this->alert->shouldReceive('success')->with(trans('server.users.user_assigned'))->once()->andReturnSelf() - ->shouldReceive('flash')->withNoArgs()->once()->andReturnNull(); + $this->alert->shouldReceive('success')->with(trans('server.users.user_assigned'))->once()->andReturnSelf(); + $this->alert->shouldReceive('flash')->withNoArgs()->once()->andReturnNull(); - $response = $this->controller->store($this->request, $server->uuid); + $response = $controller->store($this->request); $this->assertIsRedirectResponse($response); - $this->assertRedirectRouteEquals('server.subusers.view', $response, ['uuid' => $server->uuid, 'id' => $subuser->id]); + $this->assertRedirectRouteEquals('server.subusers.view', $response, [ + 'uuid' => $server->uuid, + 'id' => $subuser->id, + ]); } /** @@ -206,14 +192,35 @@ class SubuserControllerTest extends TestCase */ public function testDeleteController() { + $controller = $this->getController(); + $server = factory(Server::class)->make(); + $subuser = factory(Subuser::class)->make(); - $this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server); - $this->controller->shouldReceive('authorize')->with('delete-subuser', $server)->once()->andReturnNull(); - $this->subuserDeletionService->shouldReceive('handle')->with(1234)->once()->andReturnNull(); + $this->setRequestAttribute('server', $server); + $this->setRequestAttribute('subuser', $subuser); - $response = $this->controller->delete($server->uuid, 1234); + $controller->shouldReceive('authorize')->with('delete-subuser', $server)->once()->andReturnNull(); + $this->subuserDeletionService->shouldReceive('handle')->with($subuser)->once()->andReturnNull(); + + $response = $controller->delete($this->request); $this->assertIsResponse($response); $this->assertResponseCodeEquals(204, $response); } + + /** + * Return a mocked instance of the controller to allow access to authorization functionality. + * + * @return \Pterodactyl\Http\Controllers\Server\SubuserController|\Mockery\Mock + */ + private function getController() + { + return $this->buildMockedController(SubuserController::class, [ + $this->alert, + $this->subuserCreationService, + $this->subuserDeletionService, + $this->repository, + $this->subuserUpdateService, + ]); + } } diff --git a/tests/Unit/Http/Middleware/MiddlewareTestCase.php b/tests/Unit/Http/Middleware/MiddlewareTestCase.php index 463570f0e..6356cde20 100644 --- a/tests/Unit/Http/Middleware/MiddlewareTestCase.php +++ b/tests/Unit/Http/Middleware/MiddlewareTestCase.php @@ -2,22 +2,14 @@ namespace Tests\Unit\Http\Middleware; -use Mockery as m; use Tests\TestCase; -use Illuminate\Http\Request; -use Pterodactyl\Models\User; +use Tests\Traits\Http\RequestMockHelpers; use Tests\Traits\Http\MocksMiddlewareClosure; -use Symfony\Component\HttpFoundation\ParameterBag; use Tests\Assertions\MiddlewareAttributeAssertionsTrait; abstract class MiddlewareTestCase extends TestCase { - use MiddlewareAttributeAssertionsTrait, MocksMiddlewareClosure; - - /** - * @var \Illuminate\Http\Request|\Mockery\Mock - */ - protected $request; + use MiddlewareAttributeAssertionsTrait, MocksMiddlewareClosure, RequestMockHelpers; /** * Setup tests with a mocked request object and normal attributes. @@ -26,33 +18,6 @@ abstract class MiddlewareTestCase extends TestCase { parent::setUp(); - $this->request = m::mock(Request::class); - $this->request->attributes = new ParameterBag(); - } - - /** - * Set a request attribute on the mock object. - * - * @param string $attribute - * @param mixed $value - */ - protected function setRequestAttribute(string $attribute, $value) - { - $this->request->attributes->set($attribute, $value); - } - - /** - * Sets the mocked request user. If a user model is not provided, a factory model - * will be created and returned. - * - * @param \Pterodactyl\Models\User|null $user - * @return \Pterodactyl\Models\User - */ - protected function setRequestUser(User $user = null): User - { - $user = $user instanceof User ? $user : factory(User::class)->make(); - $this->request->shouldReceive('user')->withNoArgs()->andReturn($user); - - return $user; + $this->buildRequestMock(); } }