Skip to content

Commit acbc525

Browse files
committed
Finish unit tests for all server services
1 parent 3add44d commit acbc525

File tree

21 files changed

+1609
-206
lines changed

21 files changed

+1609
-206
lines changed

app/Contracts/Repository/Daemon/ServerRepositoryInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,12 @@ interface ServerRepositoryInterface extends BaseRepositoryInterface
3535
* @return \Psr\Http\Message\ResponseInterface
3636
*/
3737
public function create($id, $overrides = [], $start = false);
38+
39+
/**
40+
* Update server details on the daemon.
41+
*
42+
* @param array $data
43+
* @return \Psr\Http\Message\ResponseInterface
44+
*/
45+
public function update(array $data);
3846
}

app/Exceptions/Handler.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Exception;
66
use Illuminate\Auth\AuthenticationException;
77
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
8+
use Prologue\Alerts\Facades\Alert;
9+
use Pterodactyl\Exceptions\Model\DataValidationException;
810

911
class Handler extends ExceptionHandler
1012
{
@@ -20,7 +22,9 @@ class Handler extends ExceptionHandler
2022
\Illuminate\Database\Eloquent\ModelNotFoundException::class,
2123
\Illuminate\Session\TokenMismatchException::class,
2224
\Illuminate\Validation\ValidationException::class,
23-
\Pterodactyl\Exceptions\Model\DataValidationException::class,
25+
DisplayException::class,
26+
DisplayValidationException::class,
27+
DataValidationException::class,
2428
];
2529

2630
/**
@@ -51,7 +55,7 @@ public function render($request, Exception $exception)
5155
if ($request->expectsJson() || $request->isJson() || $request->is(...config('pterodactyl.json_routes'))) {
5256
$exception = $this->prepareException($exception);
5357

54-
if (config('app.debug') || $this->isHttpException($exception)) {
58+
if (config('app.debug') || $this->isHttpException($exception) || $exception instanceof DisplayException) {
5559
$displayError = $exception->getMessage();
5660
} else {
5761
$displayError = 'An unhandled exception was encountered with this request.';
@@ -64,6 +68,10 @@ public function render($request, Exception $exception)
6468
], ($this->isHttpException($exception)) ? $exception->getStatusCode() : 500, [], JSON_UNESCAPED_SLASHES);
6569

6670
parent::report($exception);
71+
} elseif ($exception instanceof DisplayException) {
72+
Alert::danger($exception->getMessage())->flash();
73+
74+
return redirect()->back()->withInput();
6775
}
6876

6977
return (isset($response)) ? $response : parent::render($request, $exception);
@@ -72,8 +80,8 @@ public function render($request, Exception $exception)
7280
/**
7381
* Convert an authentication exception into an unauthenticated response.
7482
*
75-
* @param \Illuminate\Http\Request $request
76-
* @param \Illuminate\Auth\AuthenticationException $exception
83+
* @param \Illuminate\Http\Request $request
84+
* @param \Illuminate\Auth\AuthenticationException $exception
7785
* @return \Illuminate\Http\Response
7886
*/
7987
protected function unauthenticated($request, AuthenticationException $exception)

app/Http/Controllers/Admin/ServersController.php

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Log;
2929
use Alert;
3030
use Javascript;
31+
use Prologue\Alerts\AlertsMessageBag;
3132
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
3233
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
3334
use Pterodactyl\Contracts\Repository\LocationRepositoryInterface;
@@ -36,18 +37,23 @@
3637
use Pterodactyl\Contracts\Repository\ServiceRepositoryInterface;
3738
use Pterodactyl\Http\Requests\Admin\ServerFormRequest;
3839
use Pterodactyl\Models;
40+
use Pterodactyl\Models\Server;
3941
use Illuminate\Http\Request;
4042
use GuzzleHttp\Exception\TransferException;
4143
use Pterodactyl\Exceptions\DisplayException;
4244
use Pterodactyl\Http\Controllers\Controller;
4345
use Pterodactyl\Repositories\Eloquent\DatabaseHostRepository;
44-
use Pterodactyl\Repositories\ServerRepository;
45-
use Pterodactyl\Repositories\DatabaseRepository;
4646
use Pterodactyl\Exceptions\DisplayValidationException;
4747
use Pterodactyl\Services\Servers\CreationService;
48+
use Pterodactyl\Services\Servers\DetailsModificationService;
4849

4950
class ServersController extends Controller
5051
{
52+
/**
53+
* @var \Prologue\Alerts\AlertsMessageBag
54+
*/
55+
protected $alert;
56+
5157
/**
5258
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface
5359
*/
@@ -73,6 +79,11 @@ class ServersController extends Controller
7379
*/
7480
protected $databaseHostRepository;
7581

82+
/**
83+
* @var \Pterodactyl\Services\Servers\DetailsModificationService
84+
*/
85+
protected $detailsModificationService;
86+
7687
/**
7788
* @var \Pterodactyl\Contracts\Repository\LocationRepositoryInterface
7889
*/
@@ -99,22 +110,26 @@ class ServersController extends Controller
99110
protected $serviceRepository;
100111

101112
public function __construct(
113+
AlertsMessageBag $alert,
102114
AllocationRepositoryInterface $allocationRepository,
103115
ConfigRepository $config,
104116
CreationService $service,
105117
\Pterodactyl\Services\Database\CreationService $databaseCreationService,
106118
DatabaseRepositoryInterface $databaseRepository,
107119
DatabaseHostRepository $databaseHostRepository,
120+
DetailsModificationService $detailsModificationService,
108121
LocationRepositoryInterface $locationRepository,
109122
NodeRepositoryInterface $nodeRepository,
110123
ServerRepositoryInterface $repository,
111124
ServiceRepositoryInterface $serviceRepository
112125
) {
126+
$this->alert = $alert;
113127
$this->allocationRepository = $allocationRepository;
114128
$this->config = $config;
115129
$this->databaseCreationService = $databaseCreationService;
116130
$this->databaseRepository = $databaseRepository;
117131
$this->databaseHostRepository = $databaseHostRepository;
132+
$this->detailsModificationService = $detailsModificationService;
118133
$this->locationRepository = $locationRepository;
119134
$this->nodeRepository = $nodeRepository;
120135
$this->repository = $repository;
@@ -321,61 +336,40 @@ public function viewDelete(Request $request, $id)
321336
/**
322337
* Update the details for a server.
323338
*
324-
* @param \Illuminate\Http\Request $request
325-
* @param int $id
339+
* @param \Illuminate\Http\Request $request
340+
* @param \Pterodactyl\Models\Server $server
326341
* @return \Illuminate\Http\RedirectResponse
342+
*
343+
* @throws \Pterodactyl\Exceptions\DisplayException
344+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
327345
*/
328-
public function setDetails(ServerFormRequest $request, Models\Server $server)
346+
public function setDetails(Request $request, Server $server)
329347
{
330-
dd($server);
331-
$repo = new ServerRepository;
332-
try {
333-
$repo->updateDetails($id, array_merge(
334-
$request->only('description'),
335-
$request->intersect([
336-
'owner_id', 'name', 'reset_token',
337-
])
338-
));
348+
$this->detailsModificationService->edit($server, $request->only([
349+
'owner_id', 'name', 'description', 'reset_token',
350+
]));
339351

340-
Alert::success('Server details were successfully updated.')->flash();
341-
} catch (DisplayValidationException $ex) {
342-
return redirect()->route('admin.servers.view.details', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
343-
} catch (DisplayException $ex) {
344-
Alert::danger($ex->getMessage())->flash();
345-
} catch (\Exception $ex) {
346-
Log::error($ex);
347-
Alert::danger('An unhandled exception occured while attemping to update this server. This error has been logged.')->flash();
348-
}
352+
$this->alert->success(trans('admin/server.alerts.details_updated'))->flash();
349353

350-
return redirect()->route('admin.servers.view.details', $id)->withInput();
354+
return redirect()->route('admin.servers.view.details', $server->id);
351355
}
352356

353357
/**
354358
* Set the new docker container for a server.
355359
*
356-
* @param \Illuminate\Http\Request $request
357-
* @param int $id
360+
* @param \Illuminate\Http\Request $request
361+
* @param \Pterodactyl\Models\Server $server
358362
* @return \Illuminate\Http\RedirectResponse
363+
*
364+
* @throws \Pterodactyl\Exceptions\DisplayException
365+
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
359366
*/
360-
public function setContainer(Request $request, $id)
367+
public function setContainer(Request $request, Server $server)
361368
{
362-
$repo = new ServerRepository;
363-
364-
try {
365-
$repo->updateContainer($id, $request->intersect('docker_image'));
366-
367-
Alert::success('Successfully updated this server\'s docker image.')->flash();
368-
} catch (DisplayValidationException $ex) {
369-
return redirect()->route('admin.servers.view.details', $id)->withErrors(json_decode($ex->getMessage()))->withInput();
370-
} catch (TransferException $ex) {
371-
Log::warning($ex);
372-
Alert::danger('A TransferException occured while attempting to update the container image. Is the daemon online? This error has been logged.');
373-
} catch (\Exception $ex) {
374-
Log::error($ex);
375-
Alert::danger('An unhandled exception occured while attemping to update this server\'s docker image. This error has been logged.')->flash();
376-
}
369+
$this->detailsModificationService->setDockerImage($server, $request->input('docker_image'));
370+
$this->alert->success(trans('admin/server.alerts.docker_image_updated'))->flash();
377371

378-
return redirect()->route('admin.servers.view.details', $id);
372+
return redirect()->route('admin.servers.view.details', $server->id);
379373
}
380374

381375
/**

app/Http/Requests/Admin/ServerFormRequest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,6 @@ class ServerFormRequest extends AdminFormRequest
3636
*/
3737
public function rules()
3838
{
39-
if ($this->method() === 'PATCH') {
40-
return Server::getUpdateRulesForId($this->id);
41-
}
42-
4339
return Server::getCreateRules();
4440
}
4541

app/Repositories/Daemon/BaseRepository.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ public function getAccessToken()
8888
public function getHttpClient($headers = [])
8989
{
9090
if (! is_null($this->accessServer)) {
91-
$headers[] = ['X-Access-Server' => $this->getAccessServer()];
91+
$headers['X-Access-Server'] = $this->getAccessServer();
9292
}
9393

9494
if (! is_null($this->accessToken)) {
95-
$headers[] = ['X-Access-Token' => $this->getAccessToken()];
95+
$headers['X-Access-Token'] = $this->getAccessToken();
96+
} elseif (! is_null($this->node)) {
97+
$headers['X-Access-Token'] = $this->getNode()->daemonSecret;
9698
}
9799

98100
return new Client([

app/Repositories/Daemon/ServerRepository.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,14 @@ public function create($id, $overrides = [], $start = false)
8383
'json' => $data,
8484
]);
8585
}
86+
87+
/**
88+
* {@inheritdoc}
89+
*/
90+
public function update(array $data)
91+
{
92+
return $this->getHttpClient()->request('PATCH', '/server', [
93+
'json' => $data,
94+
]);
95+
}
8696
}

app/Services/Servers/CreationService.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
namespace Pterodactyl\Services\Servers;
2626

2727
use Ramsey\Uuid\Uuid;
28-
use Illuminate\Database\ConnectionInterface;
28+
use Illuminate\Database\DatabaseManager;
2929
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
3030
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
3131
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
@@ -46,7 +46,7 @@ class CreationService
4646
protected $daemonServerRepository;
4747

4848
/**
49-
* @var \Illuminate\Database\ConnectionInterface
49+
* @var \Illuminate\Database\DatabaseManager
5050
*/
5151
protected $database;
5252

@@ -84,35 +84,35 @@ class CreationService
8484
* CreationService constructor.
8585
*
8686
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
87-
* @param \Illuminate\Database\ConnectionInterface $database
88-
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
8987
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
90-
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
88+
* @param \Illuminate\Database\DatabaseManager $database
9189
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
92-
* @param \Pterodactyl\Services\Servers\UsernameGenerationService $usernameService
90+
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
91+
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
9392
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository
93+
* @param \Pterodactyl\Services\Servers\UsernameGenerationService $usernameService
9494
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
9595
*/
9696
public function __construct(
9797
AllocationRepositoryInterface $allocationRepository,
98-
ConnectionInterface $database,
99-
ServerRepositoryInterface $repository,
10098
DaemonServerRepositoryInterface $daemonServerRepository,
101-
ServerVariableRepositoryInterface $serverVariableRepository,
99+
DatabaseManager $database,
102100
NodeRepositoryInterface $nodeRepository,
103-
UsernameGenerationService $usernameService,
101+
ServerRepositoryInterface $repository,
102+
ServerVariableRepositoryInterface $serverVariableRepository,
104103
UserRepositoryInterface $userRepository,
104+
UsernameGenerationService $usernameService,
105105
VariableValidatorService $validatorService
106106
) {
107107
$this->allocationRepository = $allocationRepository;
108+
$this->daemonServerRepository = $daemonServerRepository;
108109
$this->database = $database;
109-
$this->repository = $repository;
110110
$this->nodeRepository = $nodeRepository;
111+
$this->repository = $repository;
112+
$this->serverVariableRepository = $serverVariableRepository;
111113
$this->userRepository = $userRepository;
112114
$this->usernameService = $usernameService;
113115
$this->validatorService = $validatorService;
114-
$this->serverVariableRepository = $serverVariableRepository;
115-
$this->daemonServerRepository = $daemonServerRepository;
116116
}
117117

118118
/**
@@ -126,17 +126,14 @@ public function __construct(
126126
public function create(array $data)
127127
{
128128
// @todo auto-deployment
129-
$data['user_id'] = 1;
130-
131-
$node = $this->nodeRepository->find($data['node_id']);
132129
$validator = $this->validatorService->setAdmin()->setFields($data['environment'])->validate($data['option_id']);
133130
$uniqueShort = bin2hex(random_bytes(4));
134131

135132
$this->database->beginTransaction();
136133

137134
$server = $this->repository->create([
138135
'uuid' => Uuid::uuid4()->toString(),
139-
'uuidShort' => bin2hex(random_bytes(4)),
136+
'uuidShort' => $uniqueShort,
140137
'node_id' => $data['node_id'],
141138
'name' => $data['name'],
142139
'description' => $data['description'],
@@ -152,7 +149,7 @@ public function create(array $data)
152149
'allocation_id' => $data['allocation_id'],
153150
'service_id' => $data['service_id'],
154151
'option_id' => $data['option_id'],
155-
'pack_id' => ($data['pack_id'] == 0) ? null : $data['pack_id'],
152+
'pack_id' => (! isset($data['pack_id']) || $data['pack_id'] == 0) ? null : $data['pack_id'],
156153
'startup' => $data['startup'],
157154
'daemonSecret' => bin2hex(random_bytes(18)),
158155
'image' => $data['docker_image'],
@@ -181,9 +178,7 @@ public function create(array $data)
181178
$this->serverVariableRepository->insert($records);
182179

183180
// Create the server on the daemon & commit it to the database.
184-
$this->daemonServerRepository->setNode($server->node_id)
185-
->setAccessToken($node->daemonSecret)
186-
->create($server->id);
181+
$this->daemonServerRepository->setNode($server->node_id)->create($server->id);
187182

188183
$this->database->commit();
189184

0 commit comments

Comments
 (0)