Skip to content

Commit b12f6f1

Browse files
committed
Tests for RemoteRequestController
1 parent 5455446 commit b12f6f1

File tree

3 files changed

+197
-16
lines changed

3 files changed

+197
-16
lines changed

app/Http/Controllers/Server/Files/RemoteRequestController.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,11 @@ public function directory(Request $request)
110110
->getDirectory($requestDirectory);
111111
} catch (RequestException $exception) {
112112
$this->writer->warning($exception);
113+
$response = $exception->getResponse();
113114

114-
if (! is_null($exception->getResponse())) {
115-
return response()->json(
116-
['error' => $exception->getResponse()->getBody()], $exception->getResponse()->getStatusCode()
117-
);
118-
} else {
119-
return response()->json(['error' => trans('server.files.exceptions.list_directory')], 500);
120-
}
115+
return response()->json(['error' => trans('exceptions.daemon_connection_failed', [
116+
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
117+
])], 500);
121118
}
122119

123120
return view('server.files.list', [
@@ -151,16 +148,12 @@ public function store(Request $request, $uuid)
151148

152149
return response('', 204);
153150
} catch (RequestException $exception) {
154-
$response = $exception->getResponse();
155151
$this->writer->warning($exception);
152+
$response = $exception->getResponse();
156153

157-
if (! is_null($response)) {
158-
return response()->json(['error' => $response->getBody()], $response->getStatusCode());
159-
} else {
160-
return response()->json(['error' => trans('exceptions.daemon_connection_failed', [
161-
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
162-
])], 500);
163-
}
154+
return response()->json(['error' => trans('exceptions.daemon_connection_failed', [
155+
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
156+
])], 500);
164157
}
165158
}
166159
}

resources/lang/en/server.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@
206206
'exceptions' => [
207207
'invalid_mime' => 'This type of file cannot be edited via the Panel\'s built-in editor.',
208208
'max_size' => 'This file is too large to edit via the Panel\'s built-in editor.',
209-
'list_directory' => 'An error was encountered while attempting to get the contents of this directory. Please try again.',
210209
],
211210
'header' => 'File Manager',
212211
'header_sub' => 'Manage all of your files directly from the web.',
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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

Comments
 (0)