2017-09-03 21:32:52 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-09-03 21:32:52 +00:00
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-09-03 21:32:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Http\Controllers\Server\Files;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Pterodactyl\Models\Server;
|
2017-11-03 21:43:28 +00:00
|
|
|
use Tests\Traits\MocksRequestException;
|
2017-09-03 21:41:03 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2017-11-03 21:43:28 +00:00
|
|
|
use Pterodactyl\Exceptions\PterodactylException;
|
|
|
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Pterodactyl\Http\Requests\Server\UpdateFileContentsFormRequest;
|
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
|
|
|
|
use Pterodactyl\Http\Controllers\Server\Files\FileActionsController;
|
2017-11-03 21:43:28 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-09-03 21:32:52 +00:00
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
class FileActionsControllerTest extends ControllerTestCase
|
2017-09-03 21:32:52 +00:00
|
|
|
{
|
2017-11-03 21:43:28 +00:00
|
|
|
use MocksRequestException;
|
2017-09-03 21:32:52 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-03 21:43:28 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface|\Mockery\Mock
|
2017-09-03 21:32:52 +00:00
|
|
|
*/
|
2017-11-03 21:43:28 +00:00
|
|
|
protected $repository;
|
2017-09-03 21:32:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->repository = m::mock(FileRepositoryInterface::class);
|
2017-09-03 21:32:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the index view controller.
|
|
|
|
*/
|
|
|
|
public function testIndexController()
|
|
|
|
{
|
2017-11-03 21:43:28 +00:00
|
|
|
$controller = $this->getController();
|
2017-09-03 21:32:52 +00:00
|
|
|
$server = factory(Server::class)->make();
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestAttribute('server', $server);
|
|
|
|
$this->mockInjectJavascript();
|
|
|
|
|
|
|
|
$controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->request->shouldReceive('user->can')->andReturn(true);
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$response = $controller->index($this->request);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertIsViewResponse($response);
|
|
|
|
$this->assertViewNameEquals('server.files.index', $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the file creation view controller.
|
|
|
|
*
|
|
|
|
* @dataProvider directoryNameProvider
|
|
|
|
*/
|
|
|
|
public function testCreateController($directory, $expected)
|
|
|
|
{
|
2017-11-03 21:43:28 +00:00
|
|
|
$controller = $this->getController();
|
2017-09-03 21:32:52 +00:00
|
|
|
$server = factory(Server::class)->make();
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestAttribute('server', $server);
|
|
|
|
$this->mockInjectJavascript();
|
|
|
|
|
|
|
|
$controller->shouldReceive('authorize')->with('create-files', $server)->once()->andReturnNull();
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->request->shouldReceive('get')->with('dir')->andReturn($directory);
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$response = $controller->create($this->request);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertIsViewResponse($response);
|
|
|
|
$this->assertViewNameEquals('server.files.add', $response);
|
|
|
|
$this->assertViewHasKey('directory', $response);
|
|
|
|
$this->assertViewKeyEquals('directory', $expected, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the update controller.
|
|
|
|
*
|
|
|
|
* @dataProvider fileNameProvider
|
|
|
|
*/
|
|
|
|
public function testUpdateController($file, $expected)
|
|
|
|
{
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestMockClass(UpdateFileContentsFormRequest::class);
|
|
|
|
|
|
|
|
$controller = $this->getController();
|
2017-09-03 21:32:52 +00:00
|
|
|
$server = factory(Server::class)->make();
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestAttribute('server', $server);
|
|
|
|
$this->setRequestAttribute('server_token', 'abc123');
|
|
|
|
$this->setRequestAttribute('file_stats', 'fileStatsObject');
|
|
|
|
$this->mockInjectJavascript(['stat' => 'fileStatsObject']);
|
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->repository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('setToken')->with('abc123')->once()->andReturnSelf()
|
2018-01-21 22:08:34 +00:00
|
|
|
->shouldReceive('getContent')->with($file)->once()->andReturn('test');
|
2017-09-03 21:32:52 +00:00
|
|
|
|
2018-01-05 04:49:50 +00:00
|
|
|
$response = $controller->view($this->request, '1234', $file);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertIsViewResponse($response);
|
|
|
|
$this->assertViewNameEquals('server.files.edit', $response);
|
|
|
|
$this->assertViewHasKey('file', $response);
|
|
|
|
$this->assertViewHasKey('stat', $response);
|
|
|
|
$this->assertViewHasKey('contents', $response);
|
|
|
|
$this->assertViewHasKey('directory', $response);
|
|
|
|
$this->assertViewKeyEquals('file', $file, $response);
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->assertViewKeyEquals('stat', 'fileStatsObject', $response);
|
2018-01-21 22:08:34 +00:00
|
|
|
$this->assertViewKeyEquals('contents', 'test', $response);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertViewKeyEquals('directory', $expected, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is handled correctly in the controller.
|
|
|
|
*/
|
|
|
|
public function testExceptionRenderedByUpdateController()
|
|
|
|
{
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestMockClass(UpdateFileContentsFormRequest::class);
|
|
|
|
$this->configureExceptionMock();
|
|
|
|
|
|
|
|
$controller = $this->getController();
|
2017-09-03 21:32:52 +00:00
|
|
|
$server = factory(Server::class)->make();
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestAttribute('server', $server);
|
|
|
|
$this->setRequestAttribute('server_token', 'abc123');
|
|
|
|
$this->setRequestAttribute('file_stats', 'fileStatsObject');
|
2017-09-03 21:32:52 +00:00
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->repository->shouldReceive('setServer')->with($server)->once()->andThrow($this->getExceptionMock());
|
2017-09-03 21:32:52 +00:00
|
|
|
|
|
|
|
try {
|
2018-01-05 04:49:50 +00:00
|
|
|
$controller->view($this->request, '1234', 'file.txt');
|
2017-11-03 21:43:28 +00:00
|
|
|
} catch (PterodactylException $exception) {
|
|
|
|
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
|
|
|
|
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
|
2017-09-03 21:32:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a list of directory names and the expected output from formatting.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function directoryNameProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[null, ''],
|
|
|
|
['/', ''],
|
|
|
|
['', ''],
|
|
|
|
['my/directory', 'my/directory/'],
|
|
|
|
['/my/directory/', 'my/directory/'],
|
|
|
|
['/////my/directory////', 'my/directory/'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides a list of file names and the expected output from formatting.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function fileNameProvider()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
['/my/file.txt', 'my/'],
|
|
|
|
['my/file.txt', 'my/'],
|
|
|
|
['file.txt', '/'],
|
|
|
|
['/file.txt', '/'],
|
|
|
|
['./file.txt', '/'],
|
|
|
|
];
|
|
|
|
}
|
2017-11-03 21:43:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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]);
|
|
|
|
}
|
2017-09-03 21:32:52 +00:00
|
|
|
}
|