|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * Pterodactyl - Panel |
| 4 | + * Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +namespace Tests\Unit\Http\Controllers\Server\Files; |
| 26 | + |
| 27 | +use GuzzleHttp\Exception\RequestException; |
| 28 | +use Illuminate\Contracts\Config\Repository; |
| 29 | +use Illuminate\Contracts\Session\Session; |
| 30 | +use Illuminate\Http\Request; |
| 31 | +use Illuminate\Log\Writer; |
| 32 | +use Mockery as m; |
| 33 | +use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface; |
| 34 | +use Pterodactyl\Http\Controllers\Server\Files\RemoteRequestController; |
| 35 | +use Pterodactyl\Models\Server; |
| 36 | +use Tests\Assertions\ControllerAssertionsTrait; |
| 37 | +use Tests\TestCase; |
| 38 | + |
| 39 | +class RemoteRequestControllerTest extends TestCase |
| 40 | +{ |
| 41 | + use ControllerAssertionsTrait; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var \Illuminate\Contracts\Config\Repository |
| 45 | + */ |
| 46 | + protected $config; |
| 47 | + |
| 48 | + /** |
| 49 | + * @var \Pterodactyl\Http\Controllers\Server\Files\RemoteRequestController |
| 50 | + */ |
| 51 | + protected $controller; |
| 52 | + |
| 53 | + /** |
| 54 | + * @var \Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface |
| 55 | + */ |
| 56 | + protected $fileRepository; |
| 57 | + |
| 58 | + /** |
| 59 | + * @var \Illuminate\Http\Request |
| 60 | + */ |
| 61 | + protected $request; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var \Illuminate\Contracts\Session\Session |
| 65 | + */ |
| 66 | + protected $session; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var \Illuminate\Log\Writer |
| 70 | + */ |
| 71 | + protected $writer; |
| 72 | + |
| 73 | + /** |
| 74 | + * Setup tests. |
| 75 | + */ |
| 76 | + public function setUp() |
| 77 | + { |
| 78 | + parent::setUp(); |
| 79 | + |
| 80 | + $this->config = m::mock(Repository::class); |
| 81 | + $this->fileRepository = m::mock(FileRepositoryInterface::class); |
| 82 | + $this->request = m::mock(Request::class); |
| 83 | + $this->session = m::mock(Session::class); |
| 84 | + $this->writer = m::mock(Writer::class); |
| 85 | + |
| 86 | + $this->controller = m::mock(RemoteRequestController::class, [ |
| 87 | + $this->config, |
| 88 | + $this->fileRepository, |
| 89 | + $this->session, |
| 90 | + $this->writer, |
| 91 | + ])->makePartial(); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Test the directory listing controller. |
| 96 | + */ |
| 97 | + public function testDirectoryController() |
| 98 | + { |
| 99 | + $server = factory(Server::class)->make(); |
| 100 | + |
| 101 | + $this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server); |
| 102 | + $this->controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull(); |
| 103 | + $this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/'); |
| 104 | + $this->session->shouldReceive('get')->with('server_data.token')->once()->andReturn($server->daemonSecret); |
| 105 | + $this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf() |
| 106 | + ->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf() |
| 107 | + ->shouldReceive('setAccessToken')->with($server->daemonSecret)->once()->andReturnSelf() |
| 108 | + ->shouldReceive('getDirectory')->with('/')->once()->andReturn(['folders' => 1, 'files' => 2]); |
| 109 | + $this->config->shouldReceive('get')->with('pterodactyl.files.editable')->once()->andReturn([]); |
| 110 | + |
| 111 | + $response = $this->controller->directory($this->request); |
| 112 | + $this->assertIsViewResponse($response); |
| 113 | + $this->assertViewNameEquals('server.files.list', $response); |
| 114 | + $this->assertViewHasKey('files', $response); |
| 115 | + $this->assertViewHasKey('folders', $response); |
| 116 | + $this->assertViewHasKey('editableMime', $response); |
| 117 | + $this->assertViewHasKey('directory', $response); |
| 118 | + $this->assertViewKeyEquals('files', 2, $response); |
| 119 | + $this->assertViewKeyEquals('folders', 1, $response); |
| 120 | + $this->assertViewKeyEquals('editableMime', [], $response); |
| 121 | + $this->assertViewKeyEquals('directory.first', false, $response); |
| 122 | + $this->assertViewKeyEquals('directory.header', '', $response); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Test that the controller properly handles an exception thrown by the daemon conneciton. |
| 127 | + */ |
| 128 | + public function testExceptionThrownByDaemonConnectionIsHandledByDisplayController() |
| 129 | + { |
| 130 | + $server = factory(Server::class)->make(); |
| 131 | + $exception = m::mock(RequestException::class); |
| 132 | + |
| 133 | + $this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server); |
| 134 | + $this->controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull(); |
| 135 | + $this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/'); |
| 136 | + $this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($exception); |
| 137 | + |
| 138 | + $this->writer->shouldReceive('warning')->with($exception)->once()->andReturnNull(); |
| 139 | + $exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull(); |
| 140 | + |
| 141 | + $response = $this->controller->directory($this->request); |
| 142 | + $this->assertIsJsonResponse($response); |
| 143 | + $this->assertResponseJsonEquals(['error' => trans('exceptions.daemon_connection_failed', ['code' => 'E_CONN_REFUSED'])], $response); |
| 144 | + $this->assertResponseCodeEquals(500, $response); |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Test the store controller. |
| 149 | + */ |
| 150 | + public function testStoreController() |
| 151 | + { |
| 152 | + $server = factory(Server::class)->make(); |
| 153 | + |
| 154 | + $this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server); |
| 155 | + $this->controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull(); |
| 156 | + $this->session->shouldReceive('get')->with('server_data.token')->once()->andReturn($server->daemonSecret); |
| 157 | + $this->request->shouldReceive('input')->with('file')->once()->andReturn('file.txt'); |
| 158 | + $this->request->shouldReceive('input')->with('contents')->once()->andReturn('file contents'); |
| 159 | + $this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf() |
| 160 | + ->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf() |
| 161 | + ->shouldReceive('setAccessToken')->with($server->daemonSecret)->once()->andReturnSelf() |
| 162 | + ->shouldReceive('putContent')->with('file.txt', 'file contents')->once()->andReturnNull(); |
| 163 | + |
| 164 | + $response = $this->controller->store($this->request, '1234'); |
| 165 | + $this->assertIsResponse($response); |
| 166 | + $this->assertResponseCodeEquals(204, $response); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Test that the controller properly handles an exception thrown by the daemon conneciton. |
| 171 | + */ |
| 172 | + public function testExceptionThrownByDaemonConnectionIsHandledByStoreController() |
| 173 | + { |
| 174 | + $server = factory(Server::class)->make(); |
| 175 | + $exception = m::mock(RequestException::class); |
| 176 | + |
| 177 | + $this->session->shouldReceive('get')->with('server_data.model')->once()->andReturn($server); |
| 178 | + $this->controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull(); |
| 179 | + $this->fileRepository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($exception); |
| 180 | + |
| 181 | + $this->writer->shouldReceive('warning')->with($exception)->once()->andReturnNull(); |
| 182 | + $exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull(); |
| 183 | + |
| 184 | + $response = $this->controller->store($this->request, '1234'); |
| 185 | + $this->assertIsJsonResponse($response); |
| 186 | + $this->assertResponseJsonEquals(['error' => trans('exceptions.daemon_connection_failed', ['code' => 'E_CONN_REFUSED'])], $response); |
| 187 | + $this->assertResponseCodeEquals(500, $response); |
| 188 | + } |
| 189 | +} |
0 commit comments