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 phpmock\phpunit\PHPMock;
|
|
|
|
use Pterodactyl\Models\Node;
|
|
|
|
use Pterodactyl\Models\Server;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Illuminate\Cache\Repository;
|
2017-11-03 21:43:28 +00:00
|
|
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Server\Files\DownloadController;
|
2017-09-03 21:32:52 +00:00
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
class DownloadControllerTest extends ControllerTestCase
|
2017-09-03 21:32:52 +00:00
|
|
|
{
|
2017-11-03 21:43:28 +00:00
|
|
|
use PHPMock;
|
2017-09-03 21:32:52 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-03 21:43:28 +00:00
|
|
|
* @var \Illuminate\Cache\Repository|\Mockery\Mock
|
2017-09-03 21:32:52 +00:00
|
|
|
*/
|
|
|
|
protected $cache;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->cache = m::mock(Repository::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the download controller redirects correctly.
|
|
|
|
*/
|
|
|
|
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
|
|
|
$server->setRelation('node', factory(Node::class)->make());
|
2017-09-03 21:32:52 +00:00
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestAttribute('server', $server);
|
|
|
|
|
|
|
|
$controller->shouldReceive('authorize')->with('download-files', $server)->once()->andReturnNull();
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->getFunctionMock('\\Pterodactyl\\Http\\Controllers\\Server\\Files', 'str_random')
|
|
|
|
->expects($this->once())->willReturn('randomString');
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$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();
|
2017-09-03 21:32:52 +00:00
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$response = $controller->index($this->request, $server->uuidShort, '/my/file.txt');
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertIsRedirectResponse($response);
|
|
|
|
$this->assertRedirectUrlEquals(sprintf(
|
2017-10-01 02:00:24 +00:00
|
|
|
'%s://%s:%s/v1/server/file/download/%s', $server->node->scheme, $server->node->fqdn, $server->node->daemonListen, 'randomString'
|
2017-09-03 21:32:52 +00:00
|
|
|
), $response);
|
|
|
|
}
|
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\DownloadController|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private function getController()
|
|
|
|
{
|
|
|
|
return $this->buildMockedController(DownloadController::class, [$this->cache]);
|
|
|
|
}
|
2017-09-03 21:32:52 +00:00
|
|
|
}
|