Fix multiple controller unit test failures
This commit is contained in:
parent
7b3393aff9
commit
54228e8124
9 changed files with 404 additions and 317 deletions
|
@ -128,7 +128,7 @@ class SubuserController extends Controller
|
||||||
*/
|
*/
|
||||||
public function update(SubuserUpdateFormRequest $request, string $uuid, string $hash): RedirectResponse
|
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();
|
$this->alert->success(trans('server.users.user_updated'))->flash();
|
||||||
|
|
||||||
return redirect()->route('server.subusers.view', ['uuid' => $uuid, 'subuser' => $hash]);
|
return redirect()->route('server.subusers.view', ['uuid' => $uuid, 'subuser' => $hash]);
|
||||||
|
@ -154,7 +154,6 @@ class SubuserController extends Controller
|
||||||
* Handles creating a new subuser.
|
* Handles creating a new subuser.
|
||||||
*
|
*
|
||||||
* @param \Pterodactyl\Http\Requests\Server\Subuser\SubuserStoreFormRequest $request
|
* @param \Pterodactyl\Http\Requests\Server\Subuser\SubuserStoreFormRequest $request
|
||||||
* @param string $uuid
|
|
||||||
* @return \Illuminate\Http\RedirectResponse
|
* @return \Illuminate\Http\RedirectResponse
|
||||||
*
|
*
|
||||||
* @throws \Exception
|
* @throws \Exception
|
||||||
|
@ -164,15 +163,15 @@ class SubuserController extends Controller
|
||||||
* @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException
|
* @throws \Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException
|
||||||
* @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException
|
* @throws \Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException
|
||||||
*/
|
*/
|
||||||
public function store(SubuserStoreFormRequest $request, $uuid): RedirectResponse
|
public function store(SubuserStoreFormRequest $request): RedirectResponse
|
||||||
{
|
{
|
||||||
$server = $request->attributes->get('server');
|
$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();
|
$this->alert->success(trans('server.users.user_assigned'))->flash();
|
||||||
|
|
||||||
return redirect()->route('server.subusers.view', [
|
return redirect()->route('server.subusers.view', [
|
||||||
'uuid' => $uuid,
|
'uuid' => $server->uuid,
|
||||||
'id' => $subuser->id,
|
'id' => $subuser->id,
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
73
tests/Traits/Http/RequestMockHelpers.php
Normal file
73
tests/Traits/Http/RequestMockHelpers.php
Normal file
|
@ -0,0 +1,73 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Traits\Http;
|
||||||
|
|
||||||
|
use Mockery as m;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Pterodactyl\Models\User;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
||||||
|
|
||||||
|
trait RequestMockHelpers
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $requestMockClass = Request::class;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Illuminate\Http\Request|\Mockery\Mock
|
||||||
|
*/
|
||||||
|
protected $request;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the class to mock for requests.
|
||||||
|
*
|
||||||
|
* @param string $class
|
||||||
|
*/
|
||||||
|
public function setRequestMockClass(string $class)
|
||||||
|
{
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
86
tests/Unit/Http/Controllers/ControllerTestCase.php
Normal file
86
tests/Unit/Http/Controllers/ControllerTestCase.php
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Unit\Http\Controllers;
|
||||||
|
|
||||||
|
use Mockery as m;
|
||||||
|
use Tests\TestCase;
|
||||||
|
use Tests\Traits\Http\RequestMockHelpers;
|
||||||
|
use Tests\Assertions\ControllerAssertionsTrait;
|
||||||
|
|
||||||
|
abstract class ControllerTestCase extends TestCase
|
||||||
|
{
|
||||||
|
use ControllerAssertionsTrait, RequestMockHelpers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Pterodactyl\Http\Controllers\Controller|\Mockery\Mock
|
||||||
|
*/
|
||||||
|
private $controller;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Setup tests.
|
||||||
|
*/
|
||||||
|
public function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,32 +10,18 @@
|
||||||
namespace Tests\Unit\Http\Controllers\Server;
|
namespace Tests\Unit\Http\Controllers\Server;
|
||||||
|
|
||||||
use Mockery as m;
|
use Mockery as m;
|
||||||
use Tests\TestCase;
|
|
||||||
use Pterodactyl\Models\Server;
|
use Pterodactyl\Models\Server;
|
||||||
use Illuminate\Contracts\Session\Session;
|
|
||||||
use Illuminate\Contracts\Config\Repository;
|
use Illuminate\Contracts\Config\Repository;
|
||||||
use Tests\Assertions\ControllerAssertionsTrait;
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
||||||
use Pterodactyl\Http\Controllers\Server\ConsoleController;
|
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;
|
protected $config;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \Pterodactyl\Http\Controllers\Server\ConsoleController
|
|
||||||
*/
|
|
||||||
protected $controller;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \Illuminate\Contracts\Session\Session
|
|
||||||
*/
|
|
||||||
protected $session;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup tests.
|
* Setup tests.
|
||||||
*/
|
*/
|
||||||
|
@ -44,9 +30,6 @@ class ConsoleControllerTest extends TestCase
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->config = m::mock(Repository::class);
|
$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)
|
public function testAllControllers($function, $view)
|
||||||
{
|
{
|
||||||
|
$controller = $this->getController();
|
||||||
$server = factory(Server::class)->make();
|
$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.count')->once()->andReturn(100);
|
||||||
$this->config->shouldReceive('get')->with('pterodactyl.console.frequency')->once()->andReturn(10);
|
$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->assertIsViewResponse($response);
|
||||||
$this->assertViewNameEquals($view, $response);
|
$this->assertViewNameEquals($view, $response);
|
||||||
}
|
}
|
||||||
|
@ -82,4 +64,14 @@ class ConsoleControllerTest extends TestCase
|
||||||
['console', 'server.console'],
|
['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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,34 +10,22 @@
|
||||||
namespace Tests\Unit\Http\Controllers\Server\Files;
|
namespace Tests\Unit\Http\Controllers\Server\Files;
|
||||||
|
|
||||||
use Mockery as m;
|
use Mockery as m;
|
||||||
use Tests\TestCase;
|
|
||||||
use phpmock\phpunit\PHPMock;
|
use phpmock\phpunit\PHPMock;
|
||||||
use Pterodactyl\Models\Node;
|
use Pterodactyl\Models\Node;
|
||||||
use Pterodactyl\Models\Server;
|
use Pterodactyl\Models\Server;
|
||||||
use Illuminate\Cache\Repository;
|
use Illuminate\Cache\Repository;
|
||||||
use Illuminate\Contracts\Session\Session;
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
||||||
use Tests\Assertions\ControllerAssertionsTrait;
|
|
||||||
use Pterodactyl\Http\Controllers\Server\Files\DownloadController;
|
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;
|
protected $cache;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \Pterodactyl\Http\Controllers\Server\Files\DownloadController
|
|
||||||
*/
|
|
||||||
protected $controller;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \Illuminate\Contracts\Session\Session
|
|
||||||
*/
|
|
||||||
protected $session;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup tests.
|
* Setup tests.
|
||||||
*/
|
*/
|
||||||
|
@ -46,9 +34,6 @@ class DownloadControllerTest extends TestCase
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->cache = m::mock(Repository::class);
|
$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()
|
public function testIndexController()
|
||||||
{
|
{
|
||||||
|
$controller = $this->getController();
|
||||||
$server = factory(Server::class)->make();
|
$server = factory(Server::class)->make();
|
||||||
$node = factory(Node::class)->make();
|
$server->setRelation('node', factory(Node::class)->make());
|
||||||
$server->node = $node;
|
|
||||||
|
|
||||||
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
|
$this->setRequestAttribute('server', $server);
|
||||||
$this->controller->shouldReceive('authorize')->with('download-files', $server)->once()->andReturnNull();
|
|
||||||
|
$controller->shouldReceive('authorize')->with('download-files', $server)->once()->andReturnNull();
|
||||||
$this->getFunctionMock('\\Pterodactyl\\Http\\Controllers\\Server\\Files', 'str_random')
|
$this->getFunctionMock('\\Pterodactyl\\Http\\Controllers\\Server\\Files', 'str_random')
|
||||||
->expects($this->once())->willReturn('randomString');
|
->expects($this->once())->willReturn('randomString');
|
||||||
|
|
||||||
$this->cache->shouldReceive('tags')->with(['Server:Downloads'])->once()->andReturnSelf()
|
$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('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->assertIsRedirectResponse($response);
|
||||||
$this->assertRedirectUrlEquals(sprintf(
|
$this->assertRedirectUrlEquals(sprintf(
|
||||||
'%s://%s:%s/v1/server/file/download/%s', $server->node->scheme, $server->node->fqdn, $server->node->daemonListen, 'randomString'
|
'%s://%s:%s/v1/server/file/download/%s', $server->node->scheme, $server->node->fqdn, $server->node->daemonListen, 'randomString'
|
||||||
), $response);
|
), $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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,51 +10,24 @@
|
||||||
namespace Tests\Unit\Http\Controllers\Server\Files;
|
namespace Tests\Unit\Http\Controllers\Server\Files;
|
||||||
|
|
||||||
use Mockery as m;
|
use Mockery as m;
|
||||||
use Tests\TestCase;
|
|
||||||
use Illuminate\Log\Writer;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Pterodactyl\Models\Server;
|
use Pterodactyl\Models\Server;
|
||||||
use Illuminate\Contracts\Session\Session;
|
use Tests\Traits\MocksRequestException;
|
||||||
use GuzzleHttp\Exception\RequestException;
|
use GuzzleHttp\Exception\RequestException;
|
||||||
use Pterodactyl\Exceptions\DisplayException;
|
use Pterodactyl\Exceptions\PterodactylException;
|
||||||
use Tests\Assertions\ControllerAssertionsTrait;
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
||||||
use Pterodactyl\Http\Requests\Server\UpdateFileContentsFormRequest;
|
use Pterodactyl\Http\Requests\Server\UpdateFileContentsFormRequest;
|
||||||
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
|
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
|
||||||
use Pterodactyl\Http\Controllers\Server\Files\FileActionsController;
|
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;
|
protected $repository;
|
||||||
|
|
||||||
/**
|
|
||||||
* @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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup tests.
|
* Setup tests.
|
||||||
|
@ -63,15 +36,7 @@ class FileActionsControllerTest extends TestCase
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->fileContentsFormRequest = m::mock(UpdateFileContentsFormRequest::class);
|
$this->repository = m::mock(FileRepositoryInterface::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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -79,14 +44,16 @@ class FileActionsControllerTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testIndexController()
|
public function testIndexController()
|
||||||
{
|
{
|
||||||
|
$controller = $this->getController();
|
||||||
$server = factory(Server::class)->make();
|
$server = factory(Server::class)->make();
|
||||||
|
|
||||||
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
|
$this->setRequestAttribute('server', $server);
|
||||||
$this->controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
|
$this->mockInjectJavascript();
|
||||||
$this->request->shouldReceive('user->can')->andReturn(true);
|
|
||||||
$this->controller->shouldReceive('injectJavascript')->once()->andReturnNull();
|
|
||||||
|
|
||||||
$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->assertIsViewResponse($response);
|
||||||
$this->assertViewNameEquals('server.files.index', $response);
|
$this->assertViewNameEquals('server.files.index', $response);
|
||||||
}
|
}
|
||||||
|
@ -98,14 +65,16 @@ class FileActionsControllerTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testCreateController($directory, $expected)
|
public function testCreateController($directory, $expected)
|
||||||
{
|
{
|
||||||
|
$controller = $this->getController();
|
||||||
$server = factory(Server::class)->make();
|
$server = factory(Server::class)->make();
|
||||||
|
|
||||||
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
|
$this->setRequestAttribute('server', $server);
|
||||||
$this->controller->shouldReceive('authorize')->with('create-files', $server)->once()->andReturnNull();
|
$this->mockInjectJavascript();
|
||||||
$this->controller->shouldReceive('injectJavascript')->once()->andReturnNull();
|
|
||||||
|
$controller->shouldReceive('authorize')->with('create-files', $server)->once()->andReturnNull();
|
||||||
$this->request->shouldReceive('get')->with('dir')->andReturn($directory);
|
$this->request->shouldReceive('get')->with('dir')->andReturn($directory);
|
||||||
|
|
||||||
$response = $this->controller->create($this->request);
|
$response = $controller->create($this->request);
|
||||||
$this->assertIsViewResponse($response);
|
$this->assertIsViewResponse($response);
|
||||||
$this->assertViewNameEquals('server.files.add', $response);
|
$this->assertViewNameEquals('server.files.add', $response);
|
||||||
$this->assertViewHasKey('directory', $response);
|
$this->assertViewHasKey('directory', $response);
|
||||||
|
@ -119,20 +88,22 @@ class FileActionsControllerTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testUpdateController($file, $expected)
|
public function testUpdateController($file, $expected)
|
||||||
{
|
{
|
||||||
|
$this->setRequestMockClass(UpdateFileContentsFormRequest::class);
|
||||||
|
|
||||||
|
$controller = $this->getController();
|
||||||
$server = factory(Server::class)->make();
|
$server = factory(Server::class)->make();
|
||||||
|
|
||||||
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
|
$this->setRequestAttribute('server', $server);
|
||||||
$this->controller->shouldReceive('authorize')->with('edit-files', $server)->once()->andReturnNull();
|
$this->setRequestAttribute('server_token', 'abc123');
|
||||||
$this->session->shouldReceive('get')->with('server_data.token')->once()->andReturn($server->daemonSecret);
|
$this->setRequestAttribute('file_stats', 'fileStatsObject');
|
||||||
$this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
|
$this->mockInjectJavascript(['stat' => 'fileStatsObject']);
|
||||||
|
|
||||||
|
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
|
||||||
->shouldReceive('setAccessServer')->with($server->uuid)->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');
|
->shouldReceive('getContent')->with($file)->once()->andReturn('file contents');
|
||||||
|
|
||||||
$this->fileContentsFormRequest->shouldReceive('getStats')->withNoArgs()->twice()->andReturn(['stats']);
|
$response = $controller->update($this->request, '1234', $file);
|
||||||
$this->controller->shouldReceive('injectJavascript')->with(['stat' => ['stats']])->once()->andReturnNull();
|
|
||||||
|
|
||||||
$response = $this->controller->update($this->fileContentsFormRequest, '1234', $file);
|
|
||||||
$this->assertIsViewResponse($response);
|
$this->assertIsViewResponse($response);
|
||||||
$this->assertViewNameEquals('server.files.edit', $response);
|
$this->assertViewNameEquals('server.files.edit', $response);
|
||||||
$this->assertViewHasKey('file', $response);
|
$this->assertViewHasKey('file', $response);
|
||||||
|
@ -140,7 +111,7 @@ class FileActionsControllerTest extends TestCase
|
||||||
$this->assertViewHasKey('contents', $response);
|
$this->assertViewHasKey('contents', $response);
|
||||||
$this->assertViewHasKey('directory', $response);
|
$this->assertViewHasKey('directory', $response);
|
||||||
$this->assertViewKeyEquals('file', $file, $response);
|
$this->assertViewKeyEquals('file', $file, $response);
|
||||||
$this->assertViewKeyEquals('stat', ['stats'], $response);
|
$this->assertViewKeyEquals('stat', 'fileStatsObject', $response);
|
||||||
$this->assertViewKeyEquals('contents', 'file contents', $response);
|
$this->assertViewKeyEquals('contents', 'file contents', $response);
|
||||||
$this->assertViewKeyEquals('directory', $expected, $response);
|
$this->assertViewKeyEquals('directory', $expected, $response);
|
||||||
}
|
}
|
||||||
|
@ -150,20 +121,23 @@ class FileActionsControllerTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testExceptionRenderedByUpdateController()
|
public function testExceptionRenderedByUpdateController()
|
||||||
{
|
{
|
||||||
|
$this->setRequestMockClass(UpdateFileContentsFormRequest::class);
|
||||||
|
$this->configureExceptionMock();
|
||||||
|
|
||||||
|
$controller = $this->getController();
|
||||||
$server = factory(Server::class)->make();
|
$server = factory(Server::class)->make();
|
||||||
$exception = m::mock(RequestException::class);
|
|
||||||
|
|
||||||
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
|
$this->setRequestAttribute('server', $server);
|
||||||
$this->controller->shouldReceive('authorize')->with('edit-files', $server)->once()->andReturnNull();
|
$this->setRequestAttribute('server_token', 'abc123');
|
||||||
$this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($exception);
|
$this->setRequestAttribute('file_stats', 'fileStatsObject');
|
||||||
|
|
||||||
$exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
|
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
|
||||||
$this->writer->shouldReceive('warning')->with($exception)->once()->andReturnNull();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$this->controller->update($this->fileContentsFormRequest, '1234', 'file.txt');
|
$controller->update($this->request, '1234', 'file.txt');
|
||||||
} catch (DisplayException $exception) {
|
} catch (PterodactylException $exception) {
|
||||||
$this->assertEquals(trans('exceptions.daemon_connection_failed', ['code' => 'E_CONN_REFUSED']), $exception->getMessage());
|
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
|
||||||
|
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,4 +173,14 @@ class FileActionsControllerTest extends TestCase
|
||||||
['./file.txt', '/'],
|
['./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]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,50 +10,29 @@
|
||||||
namespace Tests\Unit\Http\Controllers\Server\Files;
|
namespace Tests\Unit\Http\Controllers\Server\Files;
|
||||||
|
|
||||||
use Mockery as m;
|
use Mockery as m;
|
||||||
use Tests\TestCase;
|
|
||||||
use Illuminate\Log\Writer;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Pterodactyl\Models\Server;
|
use Pterodactyl\Models\Server;
|
||||||
use Illuminate\Contracts\Session\Session;
|
use Tests\Traits\MocksRequestException;
|
||||||
use GuzzleHttp\Exception\RequestException;
|
use GuzzleHttp\Exception\RequestException;
|
||||||
use Illuminate\Contracts\Config\Repository;
|
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\Contracts\Repository\Daemon\FileRepositoryInterface;
|
||||||
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
||||||
use Pterodactyl\Http\Controllers\Server\Files\RemoteRequestController;
|
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;
|
protected $config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Pterodactyl\Http\Controllers\Server\Files\RemoteRequestController
|
* @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface|\Mockery\Mock
|
||||||
*/
|
*/
|
||||||
protected $controller;
|
protected $repository;
|
||||||
|
|
||||||
/**
|
|
||||||
* @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;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup tests.
|
* Setup tests.
|
||||||
|
@ -63,17 +42,7 @@ class RemoteRequestControllerTest extends TestCase
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->config = m::mock(Repository::class);
|
$this->config = m::mock(Repository::class);
|
||||||
$this->fileRepository = m::mock(FileRepositoryInterface::class);
|
$this->repository = 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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -81,19 +50,21 @@ class RemoteRequestControllerTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testDirectoryController()
|
public function testDirectoryController()
|
||||||
{
|
{
|
||||||
$server = factory(Server::class)->make();
|
$controller = $this->getController();
|
||||||
|
|
||||||
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
|
$server = factory(Server::class)->make();
|
||||||
$this->controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
|
$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->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/');
|
||||||
$this->session->shouldReceive('get')->with('server_data.token')->once()->andReturn($server->daemonSecret);
|
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
|
||||||
$this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
|
|
||||||
->shouldReceive('setAccessServer')->with($server->uuid)->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]);
|
->shouldReceive('getDirectory')->with('/')->once()->andReturn(['folders' => 1, 'files' => 2]);
|
||||||
$this->config->shouldReceive('get')->with('pterodactyl.files.editable')->once()->andReturn([]);
|
$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->assertIsViewResponse($response);
|
||||||
$this->assertViewNameEquals('server.files.list', $response);
|
$this->assertViewNameEquals('server.files.list', $response);
|
||||||
$this->assertViewHasKey('files', $response);
|
$this->assertViewHasKey('files', $response);
|
||||||
|
@ -112,21 +83,22 @@ class RemoteRequestControllerTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testExceptionThrownByDaemonConnectionIsHandledByDisplayController()
|
public function testExceptionThrownByDaemonConnectionIsHandledByDisplayController()
|
||||||
{
|
{
|
||||||
|
$this->configureExceptionMock();
|
||||||
|
$controller = $this->getController();
|
||||||
|
|
||||||
$server = factory(Server::class)->make();
|
$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);
|
$controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
|
||||||
$this->controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
|
|
||||||
$this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/');
|
$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();
|
try {
|
||||||
$exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
|
$controller->directory($this->request);
|
||||||
|
} catch (PterodactylException $exception) {
|
||||||
$response = $this->controller->directory($this->request);
|
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
|
||||||
$this->assertIsJsonResponse($response);
|
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
|
||||||
$this->assertResponseJsonEquals(['error' => trans('exceptions.daemon_connection_failed', ['code' => 'E_CONN_REFUSED'])], $response);
|
}
|
||||||
$this->assertResponseCodeEquals(500, $response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -134,19 +106,21 @@ class RemoteRequestControllerTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testStoreController()
|
public function testStoreController()
|
||||||
{
|
{
|
||||||
$server = factory(Server::class)->make();
|
$controller = $this->getController();
|
||||||
|
|
||||||
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
|
$server = factory(Server::class)->make();
|
||||||
$this->controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
|
$this->setRequestAttribute('server', $server);
|
||||||
$this->session->shouldReceive('get')->with('server_data.token')->once()->andReturn($server->daemonSecret);
|
$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('file')->once()->andReturn('file.txt');
|
||||||
$this->request->shouldReceive('input')->with('contents')->once()->andReturn('file contents');
|
$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('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();
|
->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->assertIsResponse($response);
|
||||||
$this->assertResponseCodeEquals(204, $response);
|
$this->assertResponseCodeEquals(204, $response);
|
||||||
}
|
}
|
||||||
|
@ -156,19 +130,30 @@ class RemoteRequestControllerTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testExceptionThrownByDaemonConnectionIsHandledByStoreController()
|
public function testExceptionThrownByDaemonConnectionIsHandledByStoreController()
|
||||||
{
|
{
|
||||||
|
$this->configureExceptionMock();
|
||||||
|
$controller = $this->getController();
|
||||||
|
|
||||||
$server = factory(Server::class)->make();
|
$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);
|
$controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
|
||||||
$this->controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
|
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
|
||||||
$this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($exception);
|
|
||||||
|
|
||||||
$this->writer->shouldReceive('warning')->with($exception)->once()->andReturnNull();
|
try {
|
||||||
$exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
|
$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);
|
* Return a mocked instance of the controller to allow access to authorization functionality.
|
||||||
$this->assertResponseJsonEquals(['error' => trans('exceptions.daemon_connection_failed', ['code' => 'E_CONN_REFUSED'])], $response);
|
*
|
||||||
$this->assertResponseCodeEquals(500, $response);
|
* @return \Pterodactyl\Http\Controllers\Server\Files\RemoteRequestController|\Mockery\Mock
|
||||||
|
*/
|
||||||
|
private function getController()
|
||||||
|
{
|
||||||
|
return $this->buildMockedController(RemoteRequestController::class, [$this->config, $this->repository]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,61 +10,43 @@
|
||||||
namespace Tests\Unit\Http\Controllers\Server;
|
namespace Tests\Unit\Http\Controllers\Server;
|
||||||
|
|
||||||
use Mockery as m;
|
use Mockery as m;
|
||||||
use Tests\TestCase;
|
|
||||||
use Illuminate\Http\Request;
|
|
||||||
use Pterodactyl\Models\Server;
|
use Pterodactyl\Models\Server;
|
||||||
use Pterodactyl\Models\Subuser;
|
use Pterodactyl\Models\Subuser;
|
||||||
use Pterodactyl\Models\Permission;
|
use Pterodactyl\Models\Permission;
|
||||||
use Prologue\Alerts\AlertsMessageBag;
|
use Prologue\Alerts\AlertsMessageBag;
|
||||||
use Illuminate\Contracts\Session\Session;
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
||||||
use Tests\Assertions\ControllerAssertionsTrait;
|
|
||||||
use Pterodactyl\Services\Subusers\SubuserUpdateService;
|
use Pterodactyl\Services\Subusers\SubuserUpdateService;
|
||||||
use Pterodactyl\Services\Subusers\SubuserCreationService;
|
use Pterodactyl\Services\Subusers\SubuserCreationService;
|
||||||
use Pterodactyl\Services\Subusers\SubuserDeletionService;
|
use Pterodactyl\Services\Subusers\SubuserDeletionService;
|
||||||
use Pterodactyl\Http\Controllers\Server\SubuserController;
|
use Pterodactyl\Http\Controllers\Server\SubuserController;
|
||||||
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
|
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;
|
protected $alert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Pterodactyl\Http\Controllers\Server\SubuserController
|
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
|
||||||
*/
|
|
||||||
protected $controller;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface
|
|
||||||
*/
|
*/
|
||||||
protected $repository;
|
protected $repository;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Illuminate\Http\Request
|
* @var \Pterodactyl\Services\Subusers\SubuserCreationService|\Mockery\Mock
|
||||||
*/
|
|
||||||
protected $request;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \Illuminate\Contracts\Session\Session
|
|
||||||
*/
|
|
||||||
protected $session;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \Pterodactyl\Services\Subusers\SubuserCreationService
|
|
||||||
*/
|
*/
|
||||||
protected $subuserCreationService;
|
protected $subuserCreationService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Pterodactyl\Services\Subusers\SubuserDeletionService
|
* @var \Pterodactyl\Services\Subusers\SubuserDeletionService|\Mockery\Mock
|
||||||
*/
|
*/
|
||||||
protected $subuserDeletionService;
|
protected $subuserDeletionService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \Pterodactyl\Services\Subusers\SubuserUpdateService
|
* @var \Pterodactyl\Services\Subusers\SubuserUpdateService|\Mockery\Mock
|
||||||
*/
|
*/
|
||||||
protected $subuserUpdateService;
|
protected $subuserUpdateService;
|
||||||
|
|
||||||
|
@ -77,20 +59,9 @@ class SubuserControllerTest extends TestCase
|
||||||
|
|
||||||
$this->alert = m::mock(AlertsMessageBag::class);
|
$this->alert = m::mock(AlertsMessageBag::class);
|
||||||
$this->repository = m::mock(SubuserRepositoryInterface::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->subuserCreationService = m::mock(SubuserCreationService::class);
|
||||||
$this->subuserDeletionService = m::mock(SubuserDeletionService::class);
|
$this->subuserDeletionService = m::mock(SubuserDeletionService::class);
|
||||||
$this->subuserUpdateService = m::mock(SubuserUpdateService::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()
|
public function testIndexController()
|
||||||
{
|
{
|
||||||
|
$controller = $this->getController();
|
||||||
$server = factory(Server::class)->make();
|
$server = factory(Server::class)->make();
|
||||||
|
|
||||||
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
|
$this->setRequestAttribute('server', $server);
|
||||||
$this->controller->shouldReceive('authorize')->with('list-subusers', $server)->once()->andReturnNull();
|
$this->mockInjectJavascript();
|
||||||
$this->controller->shouldReceive('injectJavascript')->withNoArgs()->once()->andReturnNull();
|
|
||||||
|
$controller->shouldReceive('authorize')->with('list-subusers', $server)->once()->andReturnNull();
|
||||||
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $server->id]])->once()->andReturn([]);
|
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $server->id]])->once()->andReturn([]);
|
||||||
|
|
||||||
$response = $this->controller->index();
|
$response = $controller->index($this->request);
|
||||||
$this->assertIsViewResponse($response);
|
$this->assertIsViewResponse($response);
|
||||||
$this->assertViewNameEquals('server.users.index', $response);
|
$this->assertViewNameEquals('server.users.index', $response);
|
||||||
$this->assertViewHasKey('subusers', $response);
|
$this->assertViewHasKey('subusers', $response);
|
||||||
|
@ -116,20 +89,22 @@ class SubuserControllerTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testViewController()
|
public function testViewController()
|
||||||
{
|
{
|
||||||
$subuser = factory(Subuser::class)->make([
|
$controller = $this->getController();
|
||||||
'permissions' => collect([
|
$subuser = factory(Subuser::class)->make();
|
||||||
(object) ['permission' => 'some.permission'],
|
$subuser->setRelation('permissions', collect([
|
||||||
(object) ['permission' => 'another.permission'],
|
(object) ['permission' => 'some.permission'],
|
||||||
]),
|
(object) ['permission' => 'another.permission'],
|
||||||
]);
|
]));
|
||||||
$server = factory(Server::class)->make();
|
$server = factory(Server::class)->make();
|
||||||
|
|
||||||
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
|
$this->setRequestAttribute('server', $server);
|
||||||
$this->controller->shouldReceive('authorize')->with('view-subuser', $server)->once()->andReturnNull();
|
$this->setRequestAttribute('subuser', $subuser);
|
||||||
$this->repository->shouldReceive('getWithPermissions')->with(1234)->once()->andReturn($subuser);
|
$this->mockInjectJavascript();
|
||||||
$this->controller->shouldReceive('injectJavascript')->withNoArgs()->once()->andReturnNull();
|
|
||||||
|
|
||||||
$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->assertIsViewResponse($response);
|
||||||
$this->assertViewNameEquals('server.users.view', $response);
|
$this->assertViewNameEquals('server.users.view', $response);
|
||||||
$this->assertViewHasKey('subuser', $response);
|
$this->assertViewHasKey('subuser', $response);
|
||||||
|
@ -148,18 +123,21 @@ class SubuserControllerTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testUpdateController()
|
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->request->shouldReceive('input')->with('permissions', [])->once()->andReturn(['some.permission']);
|
||||||
$this->subuserUpdateService->shouldReceive('handle')->with(1234, ['some.permission'])->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('success')->with(trans('server.users.user_updated'))->once()->andReturnSelf();
|
||||||
->shouldReceive('flash')->withNoArgs()->once()->andReturnNull();
|
$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->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()
|
public function testCreateController()
|
||||||
{
|
{
|
||||||
|
$controller = $this->getController();
|
||||||
$server = factory(Server::class)->make();
|
$server = factory(Server::class)->make();
|
||||||
|
|
||||||
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
|
$this->setRequestAttribute('server', $server);
|
||||||
$this->controller->shouldReceive('authorize')->with('create-subuser', $server)->once()->andReturnNull();
|
$this->mockInjectJavascript();
|
||||||
$this->controller->shouldReceive('injectJavascript')->withNoArgs()->once()->andReturnNull();
|
|
||||||
|
|
||||||
$response = $this->controller->create();
|
$controller->shouldReceive('authorize')->with('create-subuser', $server)->once()->andReturnNull();
|
||||||
|
|
||||||
|
$response = $controller->create($this->request);
|
||||||
$this->assertIsViewResponse($response);
|
$this->assertIsViewResponse($response);
|
||||||
$this->assertViewNameEquals('server.users.new', $response);
|
$this->assertViewNameEquals('server.users.new', $response);
|
||||||
$this->assertViewHasKey('permissions', $response);
|
$this->assertViewHasKey('permissions', $response);
|
||||||
|
@ -185,20 +165,26 @@ class SubuserControllerTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function testStoreController()
|
public function testStoreController()
|
||||||
{
|
{
|
||||||
|
$this->setRequestMockClass(SubuserStoreFormRequest::class);
|
||||||
|
$controller = $this->getController();
|
||||||
|
|
||||||
$server = factory(Server::class)->make();
|
$server = factory(Server::class)->make();
|
||||||
$subuser = factory(Subuser::class)->make();
|
$subuser = factory(Subuser::class)->make();
|
||||||
|
|
||||||
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
|
$this->setRequestAttribute('server', $server);
|
||||||
$this->controller->shouldReceive('authorize')->with('create-subuser', $server)->once()->andReturnNull();
|
|
||||||
$this->request->shouldReceive('input')->with('email')->once()->andReturn('user@test.com');
|
$this->request->shouldReceive('input')->with('email')->once()->andReturn('user@test.com');
|
||||||
$this->request->shouldReceive('input')->with('permissions', [])->once()->andReturn(['some.permission']);
|
$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->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()
|
$this->alert->shouldReceive('success')->with(trans('server.users.user_assigned'))->once()->andReturnSelf();
|
||||||
->shouldReceive('flash')->withNoArgs()->once()->andReturnNull();
|
$this->alert->shouldReceive('flash')->withNoArgs()->once()->andReturnNull();
|
||||||
|
|
||||||
$response = $this->controller->store($this->request, $server->uuid);
|
$response = $controller->store($this->request);
|
||||||
$this->assertIsRedirectResponse($response);
|
$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()
|
public function testDeleteController()
|
||||||
{
|
{
|
||||||
|
$controller = $this->getController();
|
||||||
|
|
||||||
$server = factory(Server::class)->make();
|
$server = factory(Server::class)->make();
|
||||||
|
$subuser = factory(Subuser::class)->make();
|
||||||
|
|
||||||
$this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server);
|
$this->setRequestAttribute('server', $server);
|
||||||
$this->controller->shouldReceive('authorize')->with('delete-subuser', $server)->once()->andReturnNull();
|
$this->setRequestAttribute('subuser', $subuser);
|
||||||
$this->subuserDeletionService->shouldReceive('handle')->with(1234)->once()->andReturnNull();
|
|
||||||
|
|
||||||
$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->assertIsResponse($response);
|
||||||
$this->assertResponseCodeEquals(204, $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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,22 +2,14 @@
|
||||||
|
|
||||||
namespace Tests\Unit\Http\Middleware;
|
namespace Tests\Unit\Http\Middleware;
|
||||||
|
|
||||||
use Mockery as m;
|
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
use Illuminate\Http\Request;
|
use Tests\Traits\Http\RequestMockHelpers;
|
||||||
use Pterodactyl\Models\User;
|
|
||||||
use Tests\Traits\Http\MocksMiddlewareClosure;
|
use Tests\Traits\Http\MocksMiddlewareClosure;
|
||||||
use Symfony\Component\HttpFoundation\ParameterBag;
|
|
||||||
use Tests\Assertions\MiddlewareAttributeAssertionsTrait;
|
use Tests\Assertions\MiddlewareAttributeAssertionsTrait;
|
||||||
|
|
||||||
abstract class MiddlewareTestCase extends TestCase
|
abstract class MiddlewareTestCase extends TestCase
|
||||||
{
|
{
|
||||||
use MiddlewareAttributeAssertionsTrait, MocksMiddlewareClosure;
|
use MiddlewareAttributeAssertionsTrait, MocksMiddlewareClosure, RequestMockHelpers;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var \Illuminate\Http\Request|\Mockery\Mock
|
|
||||||
*/
|
|
||||||
protected $request;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Setup tests with a mocked request object and normal attributes.
|
* Setup tests with a mocked request object and normal attributes.
|
||||||
|
@ -26,33 +18,6 @@ abstract class MiddlewareTestCase extends TestCase
|
||||||
{
|
{
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->request = m::mock(Request::class);
|
$this->buildRequestMock();
|
||||||
$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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue