From d2afc29a80950ba190612db23e75946de757925b Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Fri, 5 Jan 2018 18:27:47 -0600 Subject: [PATCH] Refactor how repositories for the daemon work. --- .../Commands/Server/RebuildServerCommand.php | 2 +- .../Daemon/BaseRepositoryInterface.php | 43 +++--- .../Daemon/CommandRepositoryInterface.php | 11 +- .../ConfigurationRepositoryInterface.php | 11 +- .../Daemon/FileRepositoryInterface.php | 22 ++- .../Daemon/PowerRepositoryInterface.php | 11 +- .../Daemon/ServerRepositoryInterface.php | 23 +-- .../Controllers/Admin/ServersController.php | 4 +- app/Http/Controllers/Base/IndexController.php | 5 +- .../Server/Files/FileActionsController.php | 5 +- .../Server/Files/RemoteRequestController.php | 10 +- .../Server/UpdateFileContentsFormRequest.php | 5 +- app/Jobs/Schedule/RunTaskJob.php | 10 +- app/Models/DaemonKey.php | 14 +- app/Repositories/Daemon/BaseRepository.php | 129 ++++++++-------- app/Repositories/Daemon/CommandRepository.php | 19 +-- .../Daemon/ConfigurationRepository.php | 15 +- app/Repositories/Daemon/FileRepository.php | 65 ++++---- app/Repositories/Daemon/PowerRepository.php | 23 ++- app/Repositories/Daemon/ServerRepository.php | 64 +++++--- .../Eloquent/DaemonKeyRepository.php | 2 +- app/Repositories/Eloquent/UserRepository.php | 2 +- app/Repositories/Wings/BaseRepository.php | 146 ------------------ app/Repositories/Wings/CommandRepository.php | 30 ---- .../Wings/ConfigurationRepository.php | 24 --- app/Repositories/Wings/FileRepository.php | 114 -------------- app/Repositories/Wings/PowerRepository.php | 40 ----- app/Repositories/Wings/ServerRepository.php | 89 ----------- .../SetDefaultAllocationService.php | 2 +- .../DaemonKeys/DaemonKeyDeletionService.php | 2 +- .../RevokeMultipleDaemonKeysService.php | 8 +- app/Services/Nodes/NodeUpdateService.php | 2 +- .../Servers/BuildModificationService.php | 2 +- .../Servers/ContainerRebuildService.php | 57 ++----- .../Servers/DetailsModificationService.php | 2 +- .../Servers/ReinstallServerService.php | 23 +-- .../Servers/ServerCreationService.php | 3 +- .../Servers/ServerDeletionService.php | 2 +- .../Servers/StartupModificationService.php | 2 +- app/Services/Servers/SuspensionService.php | 2 +- .../Subusers/SubuserUpdateService.php | 2 +- app/Services/Users/UserUpdateService.php | 1 - .../Controllers/Base/IndexControllerTest.php | 13 +- .../Files/FileActionsControllerTest.php | 11 +- .../Files/RemoteRequestControllerTest.php | 17 +- tests/Unit/Jobs/Schedule/RunTaskJobTest.php | 22 ++- .../SetDefaultAllocationServiceTest.php | 12 +- .../DaemonKeyDeletionServiceTest.php | 11 +- .../RevokeMultipleDaemonKeysServiceTest.php | 23 +-- .../Services/Nodes/NodeUpdateServiceTest.php | 17 +- .../Servers/ContainerRebuildServiceTest.php | 95 ++---------- .../DetailsModificationServiceTest.php | 13 +- .../Servers/ReinstallServerServiceTest.php | 45 ++---- .../Servers/ServerCreationServiceTest.php | 11 +- .../Servers/ServerDeletionServiceTest.php | 16 +- .../StartupModificationServiceTest.php | 11 +- .../Servers/SuspensionServiceTest.php | 13 +- .../Subusers/SubuserUpdateServiceTest.php | 7 +- 58 files changed, 388 insertions(+), 997 deletions(-) delete mode 100644 app/Repositories/Wings/BaseRepository.php delete mode 100644 app/Repositories/Wings/CommandRepository.php delete mode 100644 app/Repositories/Wings/ConfigurationRepository.php delete mode 100644 app/Repositories/Wings/FileRepository.php delete mode 100644 app/Repositories/Wings/PowerRepository.php delete mode 100644 app/Repositories/Wings/ServerRepository.php diff --git a/app/Console/Commands/Server/RebuildServerCommand.php b/app/Console/Commands/Server/RebuildServerCommand.php index c6b562b06..ac239b1ee 100644 --- a/app/Console/Commands/Server/RebuildServerCommand.php +++ b/app/Console/Commands/Server/RebuildServerCommand.php @@ -77,7 +77,7 @@ class RebuildServerCommand extends Command $json = array_merge($this->configurationStructureService->handle($server), ['rebuild' => true]); try { - $this->daemonRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update($json); + $this->daemonRepository->setServer($server)->update($json); } catch (RequestException $exception) { $this->output->error(trans('command/messages.server.rebuild_failed', [ 'name' => $server->name, diff --git a/app/Contracts/Repository/Daemon/BaseRepositoryInterface.php b/app/Contracts/Repository/Daemon/BaseRepositoryInterface.php index 803edca64..398469265 100644 --- a/app/Contracts/Repository/Daemon/BaseRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/BaseRepositoryInterface.php @@ -1,68 +1,65 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Contracts\Repository\Daemon; +use GuzzleHttp\Client; +use Pterodactyl\Models\Node; +use Pterodactyl\Models\Server; + interface BaseRepositoryInterface { /** * Set the node model to be used for this daemon connection. * - * @param int $id + * @param \Pterodactyl\Models\Node $node * @return $this - * - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ - public function setNode($id); + public function setNode(Node $node); /** * Return the node model being used. * * @return \Pterodactyl\Models\Node */ - public function getNode(); + public function getNode(): Node; /** - * Set the UUID for the server to be used in the X-Access-Server header for daemon requests. + * Set the Server model to use when requesting information from the Daemon. * - * @param null|string $server + * @param \Pterodactyl\Models\Server $server * @return $this */ - public function setAccessServer($server = null); + public function setServer(Server $server); /** - * Return the UUID of the server being used in requests. + * Return the Server model. * - * @return string + * @return \Pterodactyl\Models\Server|null */ - public function getAccessServer(); + public function getServer(); /** * Set the token to be used in the X-Access-Token header for requests to the daemon. * - * @param null|string $token + * @param string $token * @return $this */ - public function setAccessToken($token = null); + public function setToken(string $token); /** * Return the access token being used for requests. * - * @return string + * @return string|null */ - public function getAccessToken(); + public function getToken(); /** * Return an instance of the Guzzle HTTP Client to be used for requests. * * @param array $headers * @return \GuzzleHttp\Client + * + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ - public function getHttpClient(array $headers = []); + public function getHttpClient(array $headers = []): Client; } diff --git a/app/Contracts/Repository/Daemon/CommandRepositoryInterface.php b/app/Contracts/Repository/Daemon/CommandRepositoryInterface.php index 398c1dc16..ff6c33cce 100644 --- a/app/Contracts/Repository/Daemon/CommandRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/CommandRepositoryInterface.php @@ -1,14 +1,9 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Contracts\Repository\Daemon; +use Psr\Http\Message\ResponseInterface; + interface CommandRepositoryInterface extends BaseRepositoryInterface { /** @@ -17,5 +12,5 @@ interface CommandRepositoryInterface extends BaseRepositoryInterface * @param string $command * @return \Psr\Http\Message\ResponseInterface */ - public function send($command); + public function send(string $command): ResponseInterface; } diff --git a/app/Contracts/Repository/Daemon/ConfigurationRepositoryInterface.php b/app/Contracts/Repository/Daemon/ConfigurationRepositoryInterface.php index d0a3772da..2b8bceda9 100644 --- a/app/Contracts/Repository/Daemon/ConfigurationRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/ConfigurationRepositoryInterface.php @@ -1,14 +1,9 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Contracts\Repository\Daemon; +use Psr\Http\Message\ResponseInterface; + interface ConfigurationRepositoryInterface extends BaseRepositoryInterface { /** @@ -17,5 +12,5 @@ interface ConfigurationRepositoryInterface extends BaseRepositoryInterface * @param array $overrides * @return \Psr\Http\Message\ResponseInterface */ - public function update(array $overrides = []); + public function update(array $overrides = []): ResponseInterface; } diff --git a/app/Contracts/Repository/Daemon/FileRepositoryInterface.php b/app/Contracts/Repository/Daemon/FileRepositoryInterface.php index 6b9facc09..aba7af0ea 100644 --- a/app/Contracts/Repository/Daemon/FileRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/FileRepositoryInterface.php @@ -1,35 +1,31 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Contracts\Repository\Daemon; +use stdClass; +use Psr\Http\Message\ResponseInterface; + interface FileRepositoryInterface extends BaseRepositoryInterface { /** * Return stat information for a given file. * * @param string $path - * @return object + * @return \stdClass * * @throws \GuzzleHttp\Exception\RequestException */ - public function getFileStat($path); + public function getFileStat(string $path): stdClass; /** * Return the contents of a given file if it can be edited in the Panel. * * @param string $path - * @return object + * @return \stdClass * * @throws \GuzzleHttp\Exception\RequestException */ - public function getContent($path); + public function getContent(string $path): stdClass; /** * Save new contents to a given file. @@ -40,7 +36,7 @@ interface FileRepositoryInterface extends BaseRepositoryInterface * * @throws \GuzzleHttp\Exception\RequestException */ - public function putContent($path, $content); + public function putContent(string $path, string $content): ResponseInterface; /** * Return a directory listing for a given path. @@ -50,5 +46,5 @@ interface FileRepositoryInterface extends BaseRepositoryInterface * * @throws \GuzzleHttp\Exception\RequestException */ - public function getDirectory($path); + public function getDirectory(string $path): array; } diff --git a/app/Contracts/Repository/Daemon/PowerRepositoryInterface.php b/app/Contracts/Repository/Daemon/PowerRepositoryInterface.php index 255109cfd..6b6210431 100644 --- a/app/Contracts/Repository/Daemon/PowerRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/PowerRepositoryInterface.php @@ -1,14 +1,9 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Contracts\Repository\Daemon; +use Psr\Http\Message\ResponseInterface; + interface PowerRepositoryInterface extends BaseRepositoryInterface { const SIGNAL_START = 'start'; @@ -24,5 +19,5 @@ interface PowerRepositoryInterface extends BaseRepositoryInterface * * @throws \Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException */ - public function sendSignal($signal); + public function sendSignal(string $signal): ResponseInterface; } diff --git a/app/Contracts/Repository/Daemon/ServerRepositoryInterface.php b/app/Contracts/Repository/Daemon/ServerRepositoryInterface.php index 19806ac49..a00686a05 100644 --- a/app/Contracts/Repository/Daemon/ServerRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/ServerRepositoryInterface.php @@ -1,11 +1,4 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Contracts\Repository\Daemon; @@ -30,7 +23,7 @@ interface ServerRepositoryInterface extends BaseRepositoryInterface * @param array $data * @return \Psr\Http\Message\ResponseInterface */ - public function update(array $data); + public function update(array $data): ResponseInterface; /** * Mark a server to be reinstalled on the system. @@ -38,42 +31,42 @@ interface ServerRepositoryInterface extends BaseRepositoryInterface * @param array|null $data * @return \Psr\Http\Message\ResponseInterface */ - public function reinstall($data = null); + public function reinstall(array $data = null): ResponseInterface; /** * Mark a server as needing a container rebuild the next time the server is booted. * * @return \Psr\Http\Message\ResponseInterface */ - public function rebuild(); + public function rebuild(): ResponseInterface; /** * Suspend a server on the daemon. * * @return \Psr\Http\Message\ResponseInterface */ - public function suspend(); + public function suspend(): ResponseInterface; /** * Un-suspend a server on the daemon. * * @return \Psr\Http\Message\ResponseInterface */ - public function unsuspend(); + public function unsuspend(): ResponseInterface; /** * Delete a server on the daemon. * * @return \Psr\Http\Message\ResponseInterface */ - public function delete(); + public function delete(): ResponseInterface; /** * Return detials on a specific server. * * @return \Psr\Http\Message\ResponseInterface */ - public function details(); + public function details(): ResponseInterface; /** * Revoke an access key on the daemon before the time is expired. @@ -83,5 +76,5 @@ interface ServerRepositoryInterface extends BaseRepositoryInterface * * @throws \GuzzleHttp\Exception\RequestException */ - public function revokeAccessKey($key); + public function revokeAccessKey($key): ResponseInterface; } diff --git a/app/Http/Controllers/Admin/ServersController.php b/app/Http/Controllers/Admin/ServersController.php index fb393e65c..e2bfa8fd4 100644 --- a/app/Http/Controllers/Admin/ServersController.php +++ b/app/Http/Controllers/Admin/ServersController.php @@ -457,12 +457,10 @@ class ServersController extends Controller * * @param \Pterodactyl\Models\Server $server * @return \Illuminate\Http\RedirectResponse - * - * @throws \Pterodactyl\Exceptions\DisplayException */ public function rebuildContainer(Server $server) { - $this->containerRebuildService->rebuild($server); + $this->containerRebuildService->handle($server); $this->alert->success(trans('admin/server.alerts.rebuild_on_boot'))->flash(); return redirect()->route('admin.servers.view.manage', $server->id); diff --git a/app/Http/Controllers/Base/IndexController.php b/app/Http/Controllers/Base/IndexController.php index 7c2cc2f1c..70b5250f0 100644 --- a/app/Http/Controllers/Base/IndexController.php +++ b/app/Http/Controllers/Base/IndexController.php @@ -80,10 +80,7 @@ class IndexController extends Controller } try { - $response = $this->daemonRepository->setNode($server->node_id) - ->setAccessServer($server->uuid) - ->setAccessToken($token) - ->details(); + $response = $this->daemonRepository->setServer($server)->setToken($token)->details(); } catch (RequestException $exception) { throw new HttpException(500, $exception->getMessage()); } diff --git a/app/Http/Controllers/Server/Files/FileActionsController.php b/app/Http/Controllers/Server/Files/FileActionsController.php index 08e6fa881..ca3c093aa 100644 --- a/app/Http/Controllers/Server/Files/FileActionsController.php +++ b/app/Http/Controllers/Server/Files/FileActionsController.php @@ -96,7 +96,6 @@ class FileActionsController extends Controller * @return \Illuminate\View\View * * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function view(UpdateFileContentsFormRequest $request, string $uuid, string $file): View { @@ -104,9 +103,7 @@ class FileActionsController extends Controller $dirname = pathinfo($file, PATHINFO_DIRNAME); try { - $content = $this->repository->setNode($server->node_id)->setAccessServer($server->uuid) - ->setAccessToken($request->attributes->get('server_token')) - ->getContent($file); + $content = $this->repository->setServer($server)->setToken($request->attributes->get('server_token'))->getContent($file); } catch (RequestException $exception) { throw new DaemonConnectionException($exception); } diff --git a/app/Http/Controllers/Server/Files/RemoteRequestController.php b/app/Http/Controllers/Server/Files/RemoteRequestController.php index 81dba4cab..f792e3ccd 100644 --- a/app/Http/Controllers/Server/Files/RemoteRequestController.php +++ b/app/Http/Controllers/Server/Files/RemoteRequestController.php @@ -66,10 +66,7 @@ class RemoteRequestController extends Controller } try { - $listing = $this->repository->setNode($server->node_id) - ->setAccessServer($server->uuid) - ->setAccessToken($request->attributes->get('server_token')) - ->getDirectory($requestDirectory); + $listing = $this->repository->setServer($server)->setToken($request->attributes->get('server_token'))->getDirectory($requestDirectory); } catch (RequestException $exception) { throw new DaemonConnectionException($exception); } @@ -90,7 +87,6 @@ class RemoteRequestController extends Controller * * @throws \Illuminate\Auth\Access\AuthorizationException * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function store(Request $request): Response { @@ -98,9 +94,7 @@ class RemoteRequestController extends Controller $this->authorize('save-files', $server); try { - $this->repository->setNode($server->node_id) - ->setAccessServer($server->uuid) - ->setAccessToken($request->attributes->get('server_token')) + $this->repository->setServer($server)->setToken($request->attributes->get('server_token')) ->putContent($request->input('file'), $request->input('contents') ?? ''); return response('', 204); diff --git a/app/Http/Requests/Server/UpdateFileContentsFormRequest.php b/app/Http/Requests/Server/UpdateFileContentsFormRequest.php index d2337ba6a..da316cc22 100644 --- a/app/Http/Requests/Server/UpdateFileContentsFormRequest.php +++ b/app/Http/Requests/Server/UpdateFileContentsFormRequest.php @@ -69,7 +69,6 @@ class UpdateFileContentsFormRequest extends ServerFormRequest * @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException * @throws \Pterodactyl\Exceptions\Http\Server\FileTypeNotEditableException - * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ private function checkFileCanBeEdited($server, $token) { @@ -77,9 +76,7 @@ class UpdateFileContentsFormRequest extends ServerFormRequest $repository = app()->make(FileRepositoryInterface::class); try { - $stats = $repository->setNode($server->node_id)->setAccessServer($server->uuid) - ->setAccessToken($token) - ->getFileStat($this->route()->parameter('file')); + $stats = $repository->setServer($server)->setToken($token)->getFileStat($this->route()->parameter('file')); } catch (RequestException $exception) { switch ($exception->getCode()) { case 404: diff --git a/app/Jobs/Schedule/RunTaskJob.php b/app/Jobs/Schedule/RunTaskJob.php index 15ee850e9..021fa496b 100644 --- a/app/Jobs/Schedule/RunTaskJob.php +++ b/app/Jobs/Schedule/RunTaskJob.php @@ -98,15 +98,13 @@ class RunTaskJob extends Job implements ShouldQueue // Perform the provided task aganist the daemon. switch ($task->action) { case 'power': - $this->powerRepository->setNode($server->node_id) - ->setAccessServer($server->uuid) - ->setAccessToken($keyProviderService->handle($server, $user)) + $this->powerRepository->setServer($server) + ->setToken($keyProviderService->handle($server, $user)) ->sendSignal($task->payload); break; case 'command': - $this->commandRepository->setNode($server->node_id) - ->setAccessServer($server->uuid) - ->setAccessToken($keyProviderService->handle($server, $user)) + $this->commandRepository->setServer($server) + ->setToken($keyProviderService->handle($server, $user)) ->send($task->payload); break; default: diff --git a/app/Models/DaemonKey.php b/app/Models/DaemonKey.php index 625df0c9c..59cab2354 100644 --- a/app/Models/DaemonKey.php +++ b/app/Models/DaemonKey.php @@ -27,12 +27,13 @@ namespace Pterodactyl\Models; use Sofa\Eloquence\Eloquence; use Sofa\Eloquence\Validable; use Illuminate\Database\Eloquent\Model; +use Znck\Eloquent\Traits\BelongsToThrough; use Sofa\Eloquence\Contracts\CleansAttributes; use Sofa\Eloquence\Contracts\Validable as ValidableContract; class DaemonKey extends Model implements CleansAttributes, ValidableContract { - use Eloquence, Validable; + use BelongsToThrough, Eloquence, Validable; /** * @var string @@ -91,6 +92,17 @@ class DaemonKey extends Model implements CleansAttributes, ValidableContract return $this->belongsTo(Server::class); } + /** + * Return the node relation. + * + * @return \Znck\Eloquent\Relations\BelongsToThrough + * @throws \Exception + */ + public function node() + { + return $this->belongsToThrough(Node::class, Server::class); + } + /** * Return the user relation. * diff --git a/app/Repositories/Daemon/BaseRepository.php b/app/Repositories/Daemon/BaseRepository.php index 42e343aeb..8b401526a 100644 --- a/app/Repositories/Daemon/BaseRepository.php +++ b/app/Repositories/Daemon/BaseRepository.php @@ -1,149 +1,152 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Repositories\Daemon; +use RuntimeException; use GuzzleHttp\Client; -use Webmozart\Assert\Assert; +use Pterodactyl\Models\Node; +use Pterodactyl\Models\Server; use Illuminate\Foundation\Application; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; -use Illuminate\Contracts\Config\Repository as ConfigRepository; use Pterodactyl\Contracts\Repository\Daemon\BaseRepositoryInterface; -class BaseRepository implements BaseRepositoryInterface +abstract class BaseRepository implements BaseRepositoryInterface { /** * @var \Illuminate\Foundation\Application */ - protected $app; + private $app; /** - * @var + * @var \Pterodactyl\Models\Server */ - protected $accessServer; + private $server; /** - * @var + * @var string|null */ - protected $accessToken; + private $token; /** - * @var + * @var \Pterodactyl\Models\Node */ - protected $node; - - /** - * @var \Illuminate\Contracts\Config\Repository - */ - protected $config; + private $node; /** * @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface */ - protected $nodeRepository; + private $nodeRepository; /** * BaseRepository constructor. * * @param \Illuminate\Foundation\Application $app - * @param \Illuminate\Contracts\Config\Repository $config * @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository */ - public function __construct( - Application $app, - ConfigRepository $config, - NodeRepositoryInterface $nodeRepository - ) { + public function __construct(Application $app, NodeRepositoryInterface $nodeRepository) + { $this->app = $app; - $this->config = $config; $this->nodeRepository = $nodeRepository; } /** - * {@inheritdoc} + * Set the node model to be used for this daemon connection. + * + * @param \Pterodactyl\Models\Node $node + * @return $this */ - public function setNode($id) + public function setNode(Node $node) { - Assert::numeric($id, 'The first argument passed to setNode must be numeric, received %s.'); - - $this->node = $this->nodeRepository->find($id); + $this->node = $node; return $this; } /** - * {@inheritdoc} + * Return the node model being used. + * + * @return \Pterodactyl\Models\Node */ - public function getNode() + public function getNode(): Node { return $this->node; } /** - * {@inheritdoc} + * Set the Server model to use when requesting information from the Daemon. + * + * @param \Pterodactyl\Models\Server $server + * @return $this */ - public function setAccessServer($server = null) + public function setServer(Server $server) { - Assert::nullOrString($server, 'The first argument passed to setAccessServer must be null or a string, received %s.'); - - $this->accessServer = $server; + $this->server = $server; return $this; } /** - * {@inheritdoc} + * Return the Server model. + * + * @return \Pterodactyl\Models\Server|null */ - public function getAccessServer() + public function getServer() { - return $this->accessServer; + return $this->server; } /** - * {@inheritdoc} + * Set the token to be used in the X-Access-Token header for requests to the daemon. + * + * @param string $token + * @return $this */ - public function setAccessToken($token = null) + public function setToken(string $token) { - Assert::nullOrString($token, 'The first argument passed to setAccessToken must be null or a string, received %s.'); - - $this->accessToken = $token; + $this->token = $token; return $this; } /** - * {@inheritdoc} + * Return the access token being used for requests. + * + * @return string|null */ - public function getAccessToken() + public function getToken() { - return $this->accessToken; + return $this->token; } /** - * {@inheritdoc} + * Return an instance of the Guzzle HTTP Client to be used for requests. + * + * @param array $headers + * @return \GuzzleHttp\Client */ - public function getHttpClient(array $headers = []) + public function getHttpClient(array $headers = []): Client { - if (! is_null($this->accessServer)) { - $headers['X-Access-Server'] = $this->getAccessServer(); + // If no node is set, load the relationship onto the Server model + // and pass that to the setNode function. + if (! $this->getNode() instanceof Node) { + if (! $this->getServer() instanceof Server) { + throw new RuntimeException('An instance of ' . Node::class . ' or ' . Server::class . ' must be set on this repository in order to return a client.'); + } + + $this->getServer()->loadMissing('node'); + $this->setNode($this->getServer()->getRelation('node')); } - if (! is_null($this->accessToken)) { - $headers['X-Access-Token'] = $this->getAccessToken(); - } elseif (! is_null($this->node)) { - $headers['X-Access-Token'] = $this->getNode()->daemonSecret; + if ($this->getServer() instanceof Server) { + $headers['X-Access-Server'] = $this->getServer()->uuid; } + $headers['X-Access-Token'] = $this->getToken() ?? $this->getNode()->daemonSecret; + return new Client([ 'base_uri' => sprintf('%s://%s:%s/v1/', $this->getNode()->scheme, $this->getNode()->fqdn, $this->getNode()->daemonListen), - 'timeout' => $this->config->get('pterodactyl.guzzle.timeout'), - 'connect_timeout' => $this->config->get('pterodactyl.guzzle.connect_timeout'), + 'timeout' => config('pterodactyl.guzzle.timeout'), + 'connect_timeout' => config('pterodactyl.guzzle.connect_timeout'), 'headers' => $headers, ]); } diff --git a/app/Repositories/Daemon/CommandRepository.php b/app/Repositories/Daemon/CommandRepository.php index 29720c69d..31cb6b9b7 100644 --- a/app/Repositories/Daemon/CommandRepository.php +++ b/app/Repositories/Daemon/CommandRepository.php @@ -1,26 +1,21 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Repositories\Daemon; -use Webmozart\Assert\Assert; +use Psr\Http\Message\ResponseInterface; use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface; class CommandRepository extends BaseRepository implements CommandRepositoryInterface { /** - * {@inheritdoc} + * Send a command to a server. + * + * @param string $command + * @return \Psr\Http\Message\ResponseInterface + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ - public function send($command) + public function send(string $command): ResponseInterface { - Assert::stringNotEmpty($command, 'First argument passed to send must be a non-empty string, received %s.'); - return $this->getHttpClient()->request('POST', 'server/command', [ 'json' => [ 'command' => $command, diff --git a/app/Repositories/Daemon/ConfigurationRepository.php b/app/Repositories/Daemon/ConfigurationRepository.php index 42cf476e2..f1b063694 100644 --- a/app/Repositories/Daemon/ConfigurationRepository.php +++ b/app/Repositories/Daemon/ConfigurationRepository.php @@ -1,22 +1,19 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Repositories\Daemon; +use Psr\Http\Message\ResponseInterface; use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface; class ConfigurationRepository extends BaseRepository implements ConfigurationRepositoryInterface { /** - * {@inheritdoc} + * Update the configuration details for the specified node using data from the database. + * + * @param array $overrides + * @return \Psr\Http\Message\ResponseInterface */ - public function update(array $overrides = []) + public function update(array $overrides = []): ResponseInterface { $node = $this->getNode(); $structure = [ diff --git a/app/Repositories/Daemon/FileRepository.php b/app/Repositories/Daemon/FileRepository.php index 3a5bb980a..2f702d13d 100644 --- a/app/Repositories/Daemon/FileRepository.php +++ b/app/Repositories/Daemon/FileRepository.php @@ -1,23 +1,23 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Repositories\Daemon; -use Webmozart\Assert\Assert; +use stdClass; +use Psr\Http\Message\ResponseInterface; use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface; class FileRepository extends BaseRepository implements FileRepositoryInterface { - public function getFileStat($path) + /** + * Return stat information for a given file. + * + * @param string $path + * @return \stdClass + * + * @throws \GuzzleHttp\Exception\RequestException + */ + public function getFileStat(string $path): stdClass { - Assert::stringNotEmpty($path, 'First argument passed to getStat must be a non-empty string, received %s.'); - $file = pathinfo($path); $file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/'; @@ -30,12 +30,15 @@ class FileRepository extends BaseRepository implements FileRepositoryInterface } /** - * {@inheritdoc} + * Return the contents of a given file if it can be edited in the Panel. + * + * @param string $path + * @return \stdClass + * + * @throws \GuzzleHttp\Exception\RequestException */ - public function getContent($path) + public function getContent(string $path): stdClass { - Assert::stringNotEmpty($path, 'First argument passed to getContent must be a non-empty string, received %s.'); - $file = pathinfo($path); $file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/'; @@ -48,13 +51,16 @@ class FileRepository extends BaseRepository implements FileRepositoryInterface } /** - * {@inheritdoc} + * Save new contents to a given file. + * + * @param string $path + * @param string $content + * @return \Psr\Http\Message\ResponseInterface + * + * @throws \GuzzleHttp\Exception\RequestException */ - public function putContent($path, $content) + public function putContent(string $path, string $content): ResponseInterface { - Assert::stringNotEmpty($path, 'First argument passed to putContent must be a non-empty string, received %s.'); - Assert::string($content, 'Second argument passed to putContent must be a string, received %s.'); - $file = pathinfo($path); $file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/'; @@ -67,20 +73,19 @@ class FileRepository extends BaseRepository implements FileRepositoryInterface } /** - * {@inheritdoc} + * Return a directory listing for a given path. + * + * @param string $path + * @return array + * + * @throws \GuzzleHttp\Exception\RequestException */ - public function getDirectory($path) + public function getDirectory(string $path): array { - Assert::string($path, 'First argument passed to getDirectory must be a string, received %s.'); - - $response = $this->getHttpClient()->request('GET', sprintf( - 'server/directory/%s', - rawurlencode($path) - )); + $response = $this->getHttpClient()->request('GET', sprintf('server/directory/%s', rawurlencode($path))); $contents = json_decode($response->getBody()); - $files = []; - $folders = []; + $files = $folders = []; foreach ($contents as $value) { if ($value->directory) { diff --git a/app/Repositories/Daemon/PowerRepository.php b/app/Repositories/Daemon/PowerRepository.php index 2feaa8010..20fc79338 100644 --- a/app/Repositories/Daemon/PowerRepository.php +++ b/app/Repositories/Daemon/PowerRepository.php @@ -1,27 +1,23 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Repositories\Daemon; -use Webmozart\Assert\Assert; +use Psr\Http\Message\ResponseInterface; use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface; use Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException; class PowerRepository extends BaseRepository implements PowerRepositoryInterface { /** - * {@inheritdoc} + * Send a power signal to a server. + * + * @param string $signal + * @return \Psr\Http\Message\ResponseInterface + * + * @throws \Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException */ - public function sendSignal($signal) + public function sendSignal(string $signal): ResponseInterface { - Assert::stringNotEmpty($signal, 'The first argument passed to sendSignal must be a non-empty string, received %s.'); - switch ($signal) { case self::SIGNAL_START: case self::SIGNAL_STOP: @@ -32,9 +28,8 @@ class PowerRepository extends BaseRepository implements PowerRepositoryInterface 'action' => $signal, ], ]); - break; default: - throw new InvalidPowerSignalException('The signal ' . $signal . ' is not defined and could not be processed.'); + throw new InvalidPowerSignalException('The signal "' . $signal . '" is not defined and could not be processed.'); } } } diff --git a/app/Repositories/Daemon/ServerRepository.php b/app/Repositories/Daemon/ServerRepository.php index d46749e5d..3af381d1c 100644 --- a/app/Repositories/Daemon/ServerRepository.php +++ b/app/Repositories/Daemon/ServerRepository.php @@ -19,9 +19,8 @@ class ServerRepository extends BaseRepository implements ServerRepositoryInterfa */ public function create(array $structure, array $overrides = []): ResponseInterface { - // Loop through overrides. foreach ($overrides as $key => $value) { - array_set($structure, $key, $value); + $structure[$key] = value($value); } return $this->getHttpClient()->request('POST', 'servers', [ @@ -30,9 +29,12 @@ class ServerRepository extends BaseRepository implements ServerRepositoryInterfa } /** - * {@inheritdoc} + * Update server details on the daemon. + * + * @param array $data + * @return \Psr\Http\Message\ResponseInterface */ - public function update(array $data) + public function update(array $data): ResponseInterface { return $this->getHttpClient()->request('PATCH', 'server', [ 'json' => $data, @@ -40,65 +42,77 @@ class ServerRepository extends BaseRepository implements ServerRepositoryInterfa } /** - * {@inheritdoc} + * Mark a server to be reinstalled on the system. + * + * @param array|null $data + * @return \Psr\Http\Message\ResponseInterface */ - public function reinstall($data = null) + public function reinstall(array $data = null): ResponseInterface { - Assert::nullOrIsArray($data, 'First argument passed to reinstall must be null or an array, received %s.'); - - if (is_null($data)) { - return $this->getHttpClient()->request('POST', 'server/reinstall'); - } - return $this->getHttpClient()->request('POST', 'server/reinstall', [ - 'json' => $data, + 'json' => $data ?? [], ]); } /** - * {@inheritdoc} + * Mark a server as needing a container rebuild the next time the server is booted. + * + * @return \Psr\Http\Message\ResponseInterface */ - public function rebuild() + public function rebuild(): ResponseInterface { return $this->getHttpClient()->request('POST', 'server/rebuild'); } /** - * {@inheritdoc} + * Suspend a server on the daemon. + * + * @return \Psr\Http\Message\ResponseInterface */ - public function suspend() + public function suspend(): ResponseInterface { return $this->getHttpClient()->request('POST', 'server/suspend'); } /** - * {@inheritdoc} + * Un-suspend a server on the daemon. + * + * @return \Psr\Http\Message\ResponseInterface */ - public function unsuspend() + public function unsuspend(): ResponseInterface { return $this->getHttpClient()->request('POST', 'server/unsuspend'); } /** - * {@inheritdoc} + * Delete a server on the daemon. + * + * @return \Psr\Http\Message\ResponseInterface */ - public function delete() + public function delete(): ResponseInterface { return $this->getHttpClient()->request('DELETE', 'servers'); } /** - * {@inheritdoc} + * Return detials on a specific server. + * + * @return \Psr\Http\Message\ResponseInterface */ - public function details() + public function details(): ResponseInterface { return $this->getHttpClient()->request('GET', 'server'); } /** - * {@inheritdoc} + * Revoke an access key on the daemon before the time is expired. + * + * @param string|array $key + * @return \Psr\Http\Message\ResponseInterface + * + * @throws \GuzzleHttp\Exception\RequestException */ - public function revokeAccessKey($key) + public function revokeAccessKey($key): ResponseInterface { if (is_array($key)) { return $this->getHttpClient()->request('POST', 'keys', [ diff --git a/app/Repositories/Eloquent/DaemonKeyRepository.php b/app/Repositories/Eloquent/DaemonKeyRepository.php index ba246f458..c53f8a471 100644 --- a/app/Repositories/Eloquent/DaemonKeyRepository.php +++ b/app/Repositories/Eloquent/DaemonKeyRepository.php @@ -70,7 +70,7 @@ class DaemonKeyRepository extends EloquentRepository implements DaemonKeyReposit */ public function getKeysForRevocation(User $user): Collection { - return $this->getBuilder()->with('server:id,uuid,node_id')->where('user_id', $user->id)->get($this->getColumns()); + return $this->getBuilder()->with('node')->where('user_id', $user->id)->get($this->getColumns()); } /** diff --git a/app/Repositories/Eloquent/UserRepository.php b/app/Repositories/Eloquent/UserRepository.php index 4b033dc14..3a0a07942 100644 --- a/app/Repositories/Eloquent/UserRepository.php +++ b/app/Repositories/Eloquent/UserRepository.php @@ -30,7 +30,7 @@ class UserRepository extends EloquentRepository implements UserRepositoryInterfa public function getAllUsersWithCounts(): LengthAwarePaginator { return $this->getBuilder()->withCount('servers', 'subuserOf') - ->setSearchTerm($this->getSearchTerm()) + ->search($this->getSearchTerm()) ->paginate(50, $this->getColumns()); } diff --git a/app/Repositories/Wings/BaseRepository.php b/app/Repositories/Wings/BaseRepository.php deleted file mode 100644 index eee71104a..000000000 --- a/app/Repositories/Wings/BaseRepository.php +++ /dev/null @@ -1,146 +0,0 @@ -. - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ - -namespace Pterodactyl\Repositories\Wings; - -use GuzzleHttp\Client; -use Webmozart\Assert\Assert; -use Illuminate\Foundation\Application; -use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; -use Illuminate\Contracts\Config\Repository as ConfigRepository; -use Pterodactyl\Contracts\Repository\Daemon\BaseRepositoryInterface; - -class BaseRepository implements BaseRepositoryInterface -{ - /** - * @var \Illuminate\Foundation\Application - */ - protected $app; - - /** - * @var - */ - protected $accessServer; - - /** - * @var - */ - protected $accessToken; - - /** - * @var - */ - protected $node; - - /** - * @var \Illuminate\Contracts\Config\Repository - */ - protected $config; - - /** - * @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface - */ - protected $nodeRepository; - - /** - * BaseRepository constructor. - * - * @param \Illuminate\Foundation\Application $app - * @param \Illuminate\Contracts\Config\Repository $config - * @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository - */ - public function __construct( - Application $app, - ConfigRepository $config, - NodeRepositoryInterface $nodeRepository - ) { - $this->app = $app; - $this->config = $config; - $this->nodeRepository = $nodeRepository; - } - - /** - * {@inheritdoc} - */ - public function setNode($id) - { - Assert::numeric($id, 'The first argument passed to setNode must be numeric, received %s.'); - - $this->node = $this->nodeRepository->find($id); - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getNode() - { - return $this->node; - } - - /** - * {@inheritdoc} - */ - public function setAccessServer($server = null) - { - Assert::nullOrString($server, 'The first argument passed to setAccessServer must be null or a string, received %s.'); - - $this->accessServer = $server; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getAccessServer() - { - return $this->accessServer; - } - - /** - * {@inheritdoc} - */ - public function setAccessToken($token = null) - { - Assert::nullOrString($token, 'The first argument passed to setAccessToken must be null or a string, received %s.'); - - $this->accessToken = $token; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getAccessToken() - { - return $this->accessToken; - } - - /** - * {@inheritdoc} - */ - public function getHttpClient(array $headers = []) - { - if (! is_null($this->accessToken)) { - $headers['Authorization'] = 'Bearer ' . $this->getAccessToken(); - } elseif (! is_null($this->node)) { - $headers['Authorization'] = 'Bearer ' . $this->getNode()->daemonSecret; - } - - return new Client([ - 'base_uri' => sprintf('%s://%s:%s/v1/', $this->getNode()->scheme, $this->getNode()->fqdn, $this->getNode()->daemonListen), - 'timeout' => $this->config->get('pterodactyl.guzzle.timeout'), - 'connect_timeout' => $this->config->get('pterodactyl.guzzle.connect_timeout'), - 'headers' => $headers, - ]); - } -} diff --git a/app/Repositories/Wings/CommandRepository.php b/app/Repositories/Wings/CommandRepository.php deleted file mode 100644 index 7c1e3eb19..000000000 --- a/app/Repositories/Wings/CommandRepository.php +++ /dev/null @@ -1,30 +0,0 @@ -. - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ - -namespace Pterodactyl\Repositories\Wings; - -use Webmozart\Assert\Assert; -use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface; - -class CommandRepository extends BaseRepository implements CommandRepositoryInterface -{ - /** - * {@inheritdoc} - */ - public function send($command) - { - Assert::stringNotEmpty($command, 'First argument passed to send must be a non-empty string, received %s.'); - - return $this->getHttpClient()->request('POST', 'server/' . $this->getAccessServer() . '/command', [ - 'json' => [ - 'command' => $command, - ], - ]); - } -} diff --git a/app/Repositories/Wings/ConfigurationRepository.php b/app/Repositories/Wings/ConfigurationRepository.php deleted file mode 100644 index db487d6ae..000000000 --- a/app/Repositories/Wings/ConfigurationRepository.php +++ /dev/null @@ -1,24 +0,0 @@ -. - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ - -namespace Pterodactyl\Repositories\Wings; - -use Pterodactyl\Exceptions\PterodactylException; -use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface; - -class ConfigurationRepository extends BaseRepository implements ConfigurationRepositoryInterface -{ - /** - * {@inheritdoc} - */ - public function update(array $overrides = []) - { - throw new PterodactylException('This has not yet been configured.'); - } -} diff --git a/app/Repositories/Wings/FileRepository.php b/app/Repositories/Wings/FileRepository.php deleted file mode 100644 index ab515cab8..000000000 --- a/app/Repositories/Wings/FileRepository.php +++ /dev/null @@ -1,114 +0,0 @@ -. - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ - -namespace Pterodactyl\Repositories\Wings; - -use Webmozart\Assert\Assert; -use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface; - -class FileRepository extends BaseRepository implements FileRepositoryInterface -{ - /** - * {@inheritdoc} - */ - public function getFileStat($path) - { - Assert::stringNotEmpty($path, 'First argument passed to getStat must be a non-empty string, received %s.'); - - $file = pathinfo($path); - $file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/'; - - $response = $this->getHttpClient()->request('GET', sprintf( - 'server/' . $this->getAccessServer() . '/file/stat/%s', - rawurlencode($file['dirname'] . $file['basename']) - )); - - return json_decode($response->getBody()); - } - - /** - * {@inheritdoc} - */ - public function getContent($path) - { - Assert::stringNotEmpty($path, 'First argument passed to getContent must be a non-empty string, received %s.'); - - $file = pathinfo($path); - $file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/'; - - $response = $this->getHttpClient()->request('GET', sprintf( - 'server/' . $this->getAccessServer() . '/file/f/%s', - rawurlencode($file['dirname'] . $file['basename']) - )); - - return object_get(json_decode($response->getBody()), 'content'); - } - - /** - * {@inheritdoc} - */ - public function putContent($path, $content) - { - Assert::stringNotEmpty($path, 'First argument passed to putContent must be a non-empty string, received %s.'); - Assert::string($content, 'Second argument passed to putContent must be a string, received %s.'); - - $file = pathinfo($path); - $file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/'; - - return $this->getHttpClient()->request('POST', 'server/' . $this->getAccessServer() . '/file/save', [ - 'json' => [ - 'path' => rawurlencode($file['dirname'] . $file['basename']), - 'content' => $content, - ], - ]); - } - - /** - * {@inheritdoc} - */ - public function getDirectory($path) - { - Assert::string($path, 'First argument passed to getDirectory must be a string, received %s.'); - - $response = $this->getHttpClient()->request('GET', sprintf( - 'server/' . $this->getAccessServer() . '/directory/%s', - rawurlencode($path) - )); - - $contents = json_decode($response->getBody()); - $files = []; - $folders = []; - - foreach ($contents as $value) { - if ($value->directory) { - array_push($folders, [ - 'entry' => $value->name, - 'directory' => trim($path, '/'), - 'size' => null, - 'date' => strtotime($value->modified), - 'mime' => $value->mime, - ]); - } elseif ($value->file) { - array_push($files, [ - 'entry' => $value->name, - 'directory' => trim($path, '/'), - 'extension' => pathinfo($value->name, PATHINFO_EXTENSION), - 'size' => human_readable($value->size), - 'date' => strtotime($value->modified), - 'mime' => $value->mime, - ]); - } - } - - return [ - 'files' => $files, - 'folders' => $folders, - ]; - } -} diff --git a/app/Repositories/Wings/PowerRepository.php b/app/Repositories/Wings/PowerRepository.php deleted file mode 100644 index 281cdca28..000000000 --- a/app/Repositories/Wings/PowerRepository.php +++ /dev/null @@ -1,40 +0,0 @@ -. - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ - -namespace Pterodactyl\Repositories\Wings; - -use Webmozart\Assert\Assert; -use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface; -use Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException; - -class PowerRepository extends BaseRepository implements PowerRepositoryInterface -{ - /** - * {@inheritdoc} - */ - public function sendSignal($signal) - { - Assert::stringNotEmpty($signal, 'The first argument passed to sendSignal must be a non-empty string, received %s.'); - - switch ($signal) { - case self::SIGNAL_START: - case self::SIGNAL_STOP: - case self::SIGNAL_RESTART: - case self::SIGNAL_KILL: - return $this->getHttpClient()->request('PUT', 'server/' . $this->getAccessServer() . '/power', [ - 'json' => [ - 'action' => $signal, - ], - ]); - break; - default: - throw new InvalidPowerSignalException('The signal ' . $signal . ' is not defined and could not be processed.'); - } - } -} diff --git a/app/Repositories/Wings/ServerRepository.php b/app/Repositories/Wings/ServerRepository.php deleted file mode 100644 index 3a7653d96..000000000 --- a/app/Repositories/Wings/ServerRepository.php +++ /dev/null @@ -1,89 +0,0 @@ -. - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ - -namespace Pterodactyl\Repositories\Wings; - -use Psr\Http\Message\ResponseInterface; -use Pterodactyl\Exceptions\PterodactylException; -use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface; - -class ServerRepository extends BaseRepository implements ServerRepositoryInterface -{ - /** - * {@inheritdoc} - */ - public function create(array $structure, array $overrides = []): ResponseInterface - { - throw new PterodactylException('This feature is not yet implemented.'); - } - - /** - * {@inheritdoc} - */ - public function update(array $data) - { - throw new PterodactylException('This feature is not yet implemented.'); - } - - /** - * {@inheritdoc} - */ - public function reinstall($data = null) - { - throw new PterodactylException('This feature is not yet implemented.'); - } - - /** - * {@inheritdoc} - */ - public function rebuild() - { - return $this->getHttpClient()->request('POST', 'server/' . $this->getAccessServer() . '/rebuild'); - } - - /** - * {@inheritdoc} - */ - public function suspend() - { - return $this->getHttpClient()->request('POST', 'server/' . $this->getAccessServer() . '/suspend'); - } - - /** - * {@inheritdoc} - */ - public function unsuspend() - { - return $this->getHttpClient()->request('POST', 'server/' . $this->getAccessServer() . '/unsuspend'); - } - - /** - * {@inheritdoc} - */ - public function delete() - { - return $this->getHttpClient()->request('DELETE', 'server/' . $this->getAccessServer()); - } - - /** - * {@inheritdoc} - */ - public function details() - { - return $this->getHttpClient()->request('GET', 'server/' . $this->getAccessServer()); - } - - /** - * {@inheritdoc} - */ - public function revokeAccessKey($key) - { - throw new PterodactylException('This feature is not yet implemented.'); - } -} diff --git a/app/Services/Allocations/SetDefaultAllocationService.php b/app/Services/Allocations/SetDefaultAllocationService.php index 552914097..6e9010316 100644 --- a/app/Services/Allocations/SetDefaultAllocationService.php +++ b/app/Services/Allocations/SetDefaultAllocationService.php @@ -87,7 +87,7 @@ class SetDefaultAllocationService // Update on the daemon. try { - $this->daemonRepository->setAccessServer($server->uuid)->setNode($server->node_id)->update([ + $this->daemonRepository->setServer($server)->update([ 'build' => [ 'default' => [ 'ip' => $model->ip, diff --git a/app/Services/DaemonKeys/DaemonKeyDeletionService.php b/app/Services/DaemonKeys/DaemonKeyDeletionService.php index 6cc605eb8..553258ce7 100644 --- a/app/Services/DaemonKeys/DaemonKeyDeletionService.php +++ b/app/Services/DaemonKeys/DaemonKeyDeletionService.php @@ -108,7 +108,7 @@ class DaemonKeyDeletionService $this->repository->delete($key->id); try { - $this->daemonRepository->setNode($server->node_id)->revokeAccessKey($key->secret); + $this->daemonRepository->setServer($server)->revokeAccessKey($key->secret); } catch (RequestException $exception) { $response = $exception->getResponse(); $this->connection->rollBack(); diff --git a/app/Services/DaemonKeys/RevokeMultipleDaemonKeysService.php b/app/Services/DaemonKeys/RevokeMultipleDaemonKeysService.php index 93b8b2041..7059be88e 100644 --- a/app/Services/DaemonKeys/RevokeMultipleDaemonKeysService.php +++ b/app/Services/DaemonKeys/RevokeMultipleDaemonKeysService.php @@ -46,22 +46,20 @@ class RevokeMultipleDaemonKeysService * * @param \Pterodactyl\Models\User $user * @param bool $ignoreConnectionErrors - * - * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException */ public function handle(User $user, bool $ignoreConnectionErrors = false) { $keys = $this->repository->getKeysForRevocation($user); - $keys->groupBy('server.node_id')->each(function ($group, $node) use ($ignoreConnectionErrors) { + $keys->groupBy('node.id')->each(function ($group, $nodeId) use ($ignoreConnectionErrors) { try { - $this->daemonRepository->setNode($node)->revokeAccessKey(collect($group)->pluck('secret')->toArray()); + $this->daemonRepository->setNode(collect($group)->first()->getRelation('node'))->revokeAccessKey(collect($group)->pluck('secret')->toArray()); } catch (RequestException $exception) { if (! $ignoreConnectionErrors) { throw new DaemonConnectionException($exception); } - $this->setConnectionException($node, $exception); + $this->setConnectionException($nodeId, $exception); } $this->repository->deleteKeys(collect($group)->pluck('id')->toArray()); diff --git a/app/Services/Nodes/NodeUpdateService.php b/app/Services/Nodes/NodeUpdateService.php index 4a969255b..ce81d6f8a 100644 --- a/app/Services/Nodes/NodeUpdateService.php +++ b/app/Services/Nodes/NodeUpdateService.php @@ -75,7 +75,7 @@ class NodeUpdateService $updateResponse = $this->repository->withoutFreshModel()->update($node->id, $data); try { - $this->configRepository->setNode($node->id)->update(); + $this->configRepository->setNode($node)->update(); } catch (RequestException $exception) { $response = $exception->getResponse(); $this->writer->warning($exception); diff --git a/app/Services/Servers/BuildModificationService.php b/app/Services/Servers/BuildModificationService.php index 9aa1fa4c5..3b452e1ed 100644 --- a/app/Services/Servers/BuildModificationService.php +++ b/app/Services/Servers/BuildModificationService.php @@ -154,7 +154,7 @@ class BuildModificationService })->toArray()); try { - $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update([ + $this->daemonServerRepository->setServer($server->uuid)->update([ 'build' => $this->getBuild(), ]); diff --git a/app/Services/Servers/ContainerRebuildService.php b/app/Services/Servers/ContainerRebuildService.php index 49c51ed5a..0dc25b0e4 100644 --- a/app/Services/Servers/ContainerRebuildService.php +++ b/app/Services/Servers/ContainerRebuildService.php @@ -1,77 +1,42 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Pterodactyl\Services\Servers; -use Illuminate\Log\Writer; use Pterodactyl\Models\Server; use GuzzleHttp\Exception\RequestException; -use Pterodactyl\Exceptions\DisplayException; -use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; -use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; +use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface; class ContainerRebuildService { - /** - * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface - */ - protected $daemonServerRepository; - /** * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface */ - protected $repository; - - /** - * @var \Illuminate\Log\Writer - */ - protected $writer; + private $repository; /** * ContainerRebuildService constructor. * - * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository - * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository - * @param \Illuminate\Log\Writer $writer + * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $repository */ - public function __construct( - DaemonServerRepositoryInterface $daemonServerRepository, - ServerRepositoryInterface $repository, - Writer $writer - ) { - $this->daemonServerRepository = $daemonServerRepository; + public function __construct(ServerRepositoryInterface $repository) + { $this->repository = $repository; - $this->writer = $writer; } /** * Mark a server for rebuild on next boot cycle. * - * @param int|\Pterodactyl\Models\Server $server + * @param \Pterodactyl\Models\Server $server * - * @throws \Pterodactyl\Exceptions\DisplayException + * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException */ - public function rebuild($server) + public function handle(Server $server) { - if (! $server instanceof Server) { - $server = $this->repository->find($server); - } - try { - $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->rebuild(); + $this->repository->setServer($server)->rebuild(); } catch (RequestException $exception) { - $response = $exception->getResponse(); - $this->writer->warning($exception); - - throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ - 'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(), - ])); + throw new DaemonConnectionException($exception); } } } diff --git a/app/Services/Servers/DetailsModificationService.php b/app/Services/Servers/DetailsModificationService.php index f500ab3d2..5ba4f779b 100644 --- a/app/Services/Servers/DetailsModificationService.php +++ b/app/Services/Servers/DetailsModificationService.php @@ -128,7 +128,7 @@ class DetailsModificationService $this->repository->withoutFreshModel()->update($server->id, ['image' => $image]); try { - $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update([ + $this->daemonServerRepository->setServer($server)->update([ 'build' => [ 'image' => $image, ], diff --git a/app/Services/Servers/ReinstallServerService.php b/app/Services/Servers/ReinstallServerService.php index 7b8a09728..682813e36 100644 --- a/app/Services/Servers/ReinstallServerService.php +++ b/app/Services/Servers/ReinstallServerService.php @@ -9,12 +9,11 @@ namespace Pterodactyl\Services\Servers; -use Illuminate\Log\Writer; use Pterodactyl\Models\Server; use GuzzleHttp\Exception\RequestException; use Illuminate\Database\ConnectionInterface; -use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; +use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; class ReinstallServerService @@ -34,29 +33,21 @@ class ReinstallServerService */ protected $repository; - /** - * @var \Illuminate\Log\Writer - */ - protected $writer; - /** * ReinstallService constructor. * * @param \Illuminate\Database\ConnectionInterface $database * @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository - * @param \Illuminate\Log\Writer $writer */ public function __construct( ConnectionInterface $database, DaemonServerRepositoryInterface $daemonServerRepository, - ServerRepositoryInterface $repository, - Writer $writer + ServerRepositoryInterface $repository ) { $this->daemonServerRepository = $daemonServerRepository; $this->database = $database; $this->repository = $repository; - $this->writer = $writer; } /** @@ -64,6 +55,7 @@ class ReinstallServerService * * @throws \Pterodactyl\Exceptions\DisplayException * @throws \Pterodactyl\Exceptions\Model\DataValidationException + * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException */ public function reinstall($server) { @@ -77,15 +69,10 @@ class ReinstallServerService ]); try { - $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->reinstall(); + $this->daemonServerRepository->setServer($server)->reinstall(); $this->database->commit(); } catch (RequestException $exception) { - $response = $exception->getResponse(); - $this->writer->warning($exception); - - throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [ - 'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(), - ])); + throw new DaemonConnectionException($exception); } } } diff --git a/app/Services/Servers/ServerCreationService.php b/app/Services/Servers/ServerCreationService.php index 5aa1d7b24..82c49000c 100644 --- a/app/Services/Servers/ServerCreationService.php +++ b/app/Services/Servers/ServerCreationService.php @@ -163,8 +163,9 @@ class ServerCreationService $structure = $this->configurationStructureService->handle($server); // Create the server on the daemon & commit it to the database. + $node = $this->nodeRepository->find($server->node_id); try { - $this->daemonServerRepository->setNode($server->node_id)->create($structure, [ + $this->daemonServerRepository->setNode($node)->create($structure, [ 'start_on_completion' => (bool) array_get($data, 'start_on_completion', false), ]); $this->connection->commit(); diff --git a/app/Services/Servers/ServerDeletionService.php b/app/Services/Servers/ServerDeletionService.php index 4ae5c645d..e6c098165 100644 --- a/app/Services/Servers/ServerDeletionService.php +++ b/app/Services/Servers/ServerDeletionService.php @@ -110,7 +110,7 @@ class ServerDeletionService } try { - $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->delete(); + $this->daemonServerRepository->setServer($server)->delete(); } catch (RequestException $exception) { $response = $exception->getResponse(); diff --git a/app/Services/Servers/StartupModificationService.php b/app/Services/Servers/StartupModificationService.php index 62c614bc8..76a10ad0d 100644 --- a/app/Services/Servers/StartupModificationService.php +++ b/app/Services/Servers/StartupModificationService.php @@ -112,7 +112,7 @@ class StartupModificationService ]); try { - $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update($daemonData); + $this->daemonServerRepository->setServer($server)->update($daemonData); } catch (RequestException $exception) { $this->connection->rollBack(); throw new DaemonConnectionException($exception); diff --git a/app/Services/Servers/SuspensionService.php b/app/Services/Servers/SuspensionService.php index 045d92cb3..1feb2887d 100644 --- a/app/Services/Servers/SuspensionService.php +++ b/app/Services/Servers/SuspensionService.php @@ -96,7 +96,7 @@ class SuspensionService ]); try { - $this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->$action(); + $this->daemonServerRepository->setServer($server)->$action(); $this->database->commit(); return true; diff --git a/app/Services/Subusers/SubuserUpdateService.php b/app/Services/Subusers/SubuserUpdateService.php index 9cf02e4b4..f56e47b9d 100644 --- a/app/Services/Subusers/SubuserUpdateService.php +++ b/app/Services/Subusers/SubuserUpdateService.php @@ -96,7 +96,7 @@ class SubuserUpdateService try { $token = $this->keyProviderService->handle($subuser->getRelation('server'), $subuser->getRelation('user'), false); - $this->daemonRepository->setNode($subuser->getRelation('server')->node_id)->revokeAccessKey($token); + $this->daemonRepository->setServer($subuser->getRelation('server'))->revokeAccessKey($token); } catch (RequestException $exception) { $this->connection->rollBack(); throw new DaemonConnectionException($exception); diff --git a/app/Services/Users/UserUpdateService.php b/app/Services/Users/UserUpdateService.php index 9e755dd9f..1a767ae1a 100644 --- a/app/Services/Users/UserUpdateService.php +++ b/app/Services/Users/UserUpdateService.php @@ -55,7 +55,6 @@ class UserUpdateService * * @throws \Pterodactyl\Exceptions\Model\DataValidationException * @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException - * @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException */ public function handle(User $user, array $data): Collection { diff --git a/tests/Unit/Http/Controllers/Base/IndexControllerTest.php b/tests/Unit/Http/Controllers/Base/IndexControllerTest.php index 8dab9007a..6056747ed 100644 --- a/tests/Unit/Http/Controllers/Base/IndexControllerTest.php +++ b/tests/Unit/Http/Controllers/Base/IndexControllerTest.php @@ -11,6 +11,7 @@ namespace Tests\Unit\Http\Controllers\Base; use Mockery as m; use Pterodactyl\Models\User; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; use Tests\Assertions\ControllerAssertionsTrait; use Tests\Unit\Http\Controllers\ControllerTestCase; @@ -85,20 +86,18 @@ class IndexControllerTest extends ControllerTestCase { $user = $this->generateRequestUserModel(); $server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]); + $psrResponse = new Response; $this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server); $this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123'); - $this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf() - ->shouldReceive('setAccessToken')->with('test123')->once()->andReturnSelf() - ->shouldReceive('details')->withNoArgs()->once()->andReturnSelf(); - - $this->daemonRepository->shouldReceive('getBody')->withNoArgs()->once()->andReturn('["test"]'); + $this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf() + ->shouldReceive('setToken')->with('test123')->once()->andReturnSelf() + ->shouldReceive('details')->withNoArgs()->once()->andReturn($psrResponse); $response = $this->controller->status($this->request, $server->uuidShort); $this->assertIsJsonResponse($response); - $this->assertResponseJsonEquals(['test'], $response); + $this->assertResponseJsonEquals(json_encode($psrResponse->getBody()), $response); } /** diff --git a/tests/Unit/Http/Controllers/Server/Files/FileActionsControllerTest.php b/tests/Unit/Http/Controllers/Server/Files/FileActionsControllerTest.php index f6d8cac0c..1e5cac0a7 100644 --- a/tests/Unit/Http/Controllers/Server/Files/FileActionsControllerTest.php +++ b/tests/Unit/Http/Controllers/Server/Files/FileActionsControllerTest.php @@ -98,10 +98,9 @@ class FileActionsControllerTest extends ControllerTestCase $this->setRequestAttribute('file_stats', 'fileStatsObject'); $this->mockInjectJavascript(['stat' => 'fileStatsObject']); - $this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf() - ->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf() - ->shouldReceive('getContent')->with($file)->once()->andReturn('file contents'); + $this->repository->shouldReceive('setServer')->with($server)->once()->andReturnSelf() + ->shouldReceive('setToken')->with('abc123')->once()->andReturnSelf() + ->shouldReceive('getContent')->with($file)->once()->andReturn((object) ['test']); $response = $controller->view($this->request, '1234', $file); $this->assertIsViewResponse($response); @@ -112,7 +111,7 @@ class FileActionsControllerTest extends ControllerTestCase $this->assertViewHasKey('directory', $response); $this->assertViewKeyEquals('file', $file, $response); $this->assertViewKeyEquals('stat', 'fileStatsObject', $response); - $this->assertViewKeyEquals('contents', 'file contents', $response); + $this->assertViewKeyEquals('contents', (object) ['test'], $response); $this->assertViewKeyEquals('directory', $expected, $response); } @@ -131,7 +130,7 @@ class FileActionsControllerTest extends ControllerTestCase $this->setRequestAttribute('server_token', 'abc123'); $this->setRequestAttribute('file_stats', 'fileStatsObject'); - $this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock()); + $this->repository->shouldReceive('setServer')->with($server)->once()->andThrow($this->getExceptionMock()); try { $controller->view($this->request, '1234', 'file.txt'); diff --git a/tests/Unit/Http/Controllers/Server/Files/RemoteRequestControllerTest.php b/tests/Unit/Http/Controllers/Server/Files/RemoteRequestControllerTest.php index b44b225e2..6291a748a 100644 --- a/tests/Unit/Http/Controllers/Server/Files/RemoteRequestControllerTest.php +++ b/tests/Unit/Http/Controllers/Server/Files/RemoteRequestControllerTest.php @@ -10,6 +10,7 @@ namespace Tests\Unit\Http\Controllers\Server\Files; use Mockery as m; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; use Tests\Traits\MocksRequestException; use GuzzleHttp\Exception\RequestException; @@ -58,9 +59,8 @@ class RemoteRequestControllerTest extends ControllerTestCase $controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull(); $this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/'); - $this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf() - ->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf() + $this->repository->shouldReceive('setServer')->with($server)->once()->andReturnSelf() + ->shouldReceive('setToken')->with('abc123')->once()->andReturnSelf() ->shouldReceive('getDirectory')->with('/')->once()->andReturn(['folders' => 1, 'files' => 2]); $this->config->shouldReceive('get')->with('pterodactyl.files.editable')->once()->andReturn([]); @@ -91,7 +91,7 @@ class RemoteRequestControllerTest extends ControllerTestCase $controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull(); $this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/'); - $this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock()); + $this->repository->shouldReceive('setServer')->with($server)->once()->andThrow($this->getExceptionMock()); try { $controller->directory($this->request); @@ -115,10 +115,9 @@ class RemoteRequestControllerTest extends ControllerTestCase $controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull(); $this->request->shouldReceive('input')->with('file')->once()->andReturn('file.txt'); $this->request->shouldReceive('input')->with('contents')->once()->andReturn('file contents'); - $this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf() - ->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf() - ->shouldReceive('putContent')->with('file.txt', 'file contents')->once()->andReturnNull(); + $this->repository->shouldReceive('setServer')->with($server)->once()->andReturnSelf() + ->shouldReceive('setToken')->with('abc123')->once()->andReturnSelf() + ->shouldReceive('putContent')->with('file.txt', 'file contents')->once()->andReturn(new Response); $response = $controller->store($this->request); $this->assertIsResponse($response); @@ -137,7 +136,7 @@ class RemoteRequestControllerTest extends ControllerTestCase $this->setRequestAttribute('server', $server); $controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull(); - $this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock()); + $this->repository->shouldReceive('setServer')->with($server)->once()->andThrow($this->getExceptionMock()); try { $controller->store($this->request); diff --git a/tests/Unit/Jobs/Schedule/RunTaskJobTest.php b/tests/Unit/Jobs/Schedule/RunTaskJobTest.php index c26bfbaf3..a32d7dfe1 100644 --- a/tests/Unit/Jobs/Schedule/RunTaskJobTest.php +++ b/tests/Unit/Jobs/Schedule/RunTaskJobTest.php @@ -14,6 +14,7 @@ use Carbon\Carbon; use Tests\TestCase; use Pterodactyl\Models\Task; use Pterodactyl\Models\User; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; use Pterodactyl\Models\Schedule; use Illuminate\Support\Facades\Bus; @@ -90,10 +91,9 @@ class RunTaskJobTest extends TestCase $this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task); $this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456'); - $this->powerRepository->shouldReceive('setNode')->with($task->server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($task->server->uuid)->once()->andReturnSelf() - ->shouldReceive('setAccessToken')->with('123456')->once()->andReturnSelf() - ->shouldReceive('sendSignal')->with($task->payload)->once()->andReturnNull(); + $this->powerRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf() + ->shouldReceive('setToken')->with('123456')->once()->andReturnSelf() + ->shouldReceive('sendSignal')->with($task->payload)->once()->andReturn(new Response); $this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull(); $this->taskRepository->shouldReceive('getNextTask')->with($schedule->id, $task->sequence_id)->once()->andReturnNull(); @@ -120,10 +120,9 @@ class RunTaskJobTest extends TestCase $this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task); $this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456'); - $this->commandRepository->shouldReceive('setNode')->with($task->server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($task->server->uuid)->once()->andReturnSelf() - ->shouldReceive('setAccessToken')->with('123456')->once()->andReturnSelf() - ->shouldReceive('send')->with($task->payload)->once()->andReturnNull(); + $this->commandRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf() + ->shouldReceive('setToken')->with('123456')->once()->andReturnSelf() + ->shouldReceive('send')->with($task->payload)->once()->andReturn(new Response); $this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull(); $this->taskRepository->shouldReceive('getNextTask')->with($schedule->id, $task->sequence_id)->once()->andReturnNull(); @@ -150,10 +149,9 @@ class RunTaskJobTest extends TestCase $this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task); $this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456'); - $this->commandRepository->shouldReceive('setNode')->with($task->server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($task->server->uuid)->once()->andReturnSelf() - ->shouldReceive('setAccessToken')->with('123456')->once()->andReturnSelf() - ->shouldReceive('send')->with($task->payload)->once()->andReturnNull(); + $this->commandRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf() + ->shouldReceive('setToken')->with('123456')->once()->andReturnSelf() + ->shouldReceive('send')->with($task->payload)->once()->andReturn(new Response); $this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull(); diff --git a/tests/Unit/Services/Allocations/SetDefaultAllocationServiceTest.php b/tests/Unit/Services/Allocations/SetDefaultAllocationServiceTest.php index 99916bc6b..e382a8636 100644 --- a/tests/Unit/Services/Allocations/SetDefaultAllocationServiceTest.php +++ b/tests/Unit/Services/Allocations/SetDefaultAllocationServiceTest.php @@ -4,6 +4,7 @@ namespace Tests\Unit\Services\Allocations; use Mockery as m; use Tests\TestCase; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; use Pterodactyl\Models\Allocation; use Tests\Traits\MocksRequestException; @@ -71,10 +72,9 @@ class SetDefaultAllocationServiceTest extends TestCase $this->serverRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf(); $this->serverRepository->shouldReceive('update')->with($model->id, [ 'allocation_id' => $allocations->first()->id, - ])->once()->andReturnNull(); + ])->once()->andReturn(new Response); - $this->daemonRepository->shouldReceive('setAccessServer')->with($model->uuid)->once()->andReturnSelf(); - $this->daemonRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf(); + $this->daemonRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf(); $this->daemonRepository->shouldReceive('update')->with([ 'build' => [ 'default' => [ @@ -85,7 +85,7 @@ class SetDefaultAllocationServiceTest extends TestCase return $item->pluck('port'); })->toArray(), ], - ])->once()->andReturnNull(); + ])->once()->andReturn(new Response); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $response = $this->getService()->handle($useModel ? $model : 1234, $allocations->first()->id); @@ -121,9 +121,9 @@ class SetDefaultAllocationServiceTest extends TestCase $this->serverRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf(); $this->serverRepository->shouldReceive('update')->with($model->id, [ 'allocation_id' => $allocation->id, - ])->once()->andReturnNull(); + ])->once()->andReturn(new Response); - $this->daemonRepository->shouldReceive('setAccessServer->setNode->update')->once()->andThrow($this->getExceptionMock()); + $this->daemonRepository->shouldReceive('setServer->update')->once()->andThrow($this->getExceptionMock()); $this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); try { diff --git a/tests/Unit/Services/DaemonKeys/DaemonKeyDeletionServiceTest.php b/tests/Unit/Services/DaemonKeys/DaemonKeyDeletionServiceTest.php index 558c1eb45..74007dda6 100644 --- a/tests/Unit/Services/DaemonKeys/DaemonKeyDeletionServiceTest.php +++ b/tests/Unit/Services/DaemonKeys/DaemonKeyDeletionServiceTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Services\DaemonKeys; use Mockery as m; use Tests\TestCase; use Illuminate\Log\Writer; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; use Pterodactyl\Models\DaemonKey; use GuzzleHttp\Exception\RequestException; @@ -98,8 +99,8 @@ class DaemonKeyDeletionServiceTest extends TestCase ])->once()->andReturn($key); $this->repository->shouldReceive('delete')->with($key->id)->once()->andReturn(1); - $this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf() - ->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturnNull(); + $this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf() + ->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturn(new Response); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->service->handle($server, 100); @@ -122,8 +123,8 @@ class DaemonKeyDeletionServiceTest extends TestCase ])->once()->andReturn($key); $this->repository->shouldReceive('delete')->with($key->id)->once()->andReturn(1); - $this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf() - ->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturnNull(); + $this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf() + ->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturn(new Response); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->service->handle($server->id, 100); @@ -145,7 +146,7 @@ class DaemonKeyDeletionServiceTest extends TestCase ])->once()->andReturn($key); $this->repository->shouldReceive('delete')->with($key->id)->once()->andReturn(1); - $this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->exception); + $this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andThrow($this->exception); $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); diff --git a/tests/Unit/Services/DaemonKeys/RevokeMultipleDaemonKeysServiceTest.php b/tests/Unit/Services/DaemonKeys/RevokeMultipleDaemonKeysServiceTest.php index 950824cb3..dbc20d577 100644 --- a/tests/Unit/Services/DaemonKeys/RevokeMultipleDaemonKeysServiceTest.php +++ b/tests/Unit/Services/DaemonKeys/RevokeMultipleDaemonKeysServiceTest.php @@ -4,8 +4,9 @@ namespace Tests\Unit\Services\DaemonKeys; use Mockery as m; use Tests\TestCase; +use Pterodactyl\Models\Node; use Pterodactyl\Models\User; -use Pterodactyl\Models\Server; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\DaemonKey; use Tests\Traits\MocksRequestException; use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface; @@ -43,13 +44,13 @@ class RevokeMultipleDaemonKeysServiceTest extends TestCase public function testSuccessfulKeyRevocation() { $user = factory(User::class)->make(); - $server = factory(Server::class)->make(); + $node = factory(Node::class)->make(); $key = factory(DaemonKey::class)->make(['user_id' => $user->id]); - $key->setRelation('server', $server); + $key->setRelation('node', $node); $this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key])); - $this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf(); - $this->daemonRepository->shouldReceive('revokeAccessKey')->with([$key->secret])->once()->andReturnNull(); + $this->daemonRepository->shouldReceive('setNode')->with($node)->once()->andReturnSelf(); + $this->daemonRepository->shouldReceive('revokeAccessKey')->with([$key->secret])->once()->andReturn(new Response); $this->repository->shouldReceive('deleteKeys')->with([$key->id])->once()->andReturnNull(); @@ -67,9 +68,9 @@ class RevokeMultipleDaemonKeysServiceTest extends TestCase $this->configureExceptionMock(); $user = factory(User::class)->make(); - $server = factory(Server::class)->make(); + $node = factory(Node::class)->make(); $key = factory(DaemonKey::class)->make(['user_id' => $user->id]); - $key->setRelation('server', $server); + $key->setRelation('node', $node); $this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key])); $this->daemonRepository->shouldReceive('setNode->revokeAccessKey')->with([$key->secret])->once()->andThrow($this->getExceptionMock()); @@ -86,9 +87,9 @@ class RevokeMultipleDaemonKeysServiceTest extends TestCase $this->configureExceptionMock(); $user = factory(User::class)->make(); - $server = factory(Server::class)->make(); + $node = factory(Node::class)->make(); $key = factory(DaemonKey::class)->make(['user_id' => $user->id]); - $key->setRelation('server', $server); + $key->setRelation('node', $node); $this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key])); $this->daemonRepository->shouldReceive('setNode->revokeAccessKey')->with([$key->secret])->once()->andThrow($this->getExceptionMock()); @@ -98,8 +99,8 @@ class RevokeMultipleDaemonKeysServiceTest extends TestCase $service = $this->getService(); $service->handle($user, true); $this->assertNotEmpty($service->getExceptions()); - $this->assertArrayHasKey($server->node_id, $service->getExceptions()); - $this->assertSame(array_get($service->getExceptions(), $server->node_id), $this->getExceptionMock()); + $this->assertArrayHasKey($node->id, $service->getExceptions()); + $this->assertSame(array_get($service->getExceptions(), $node->id), $this->getExceptionMock()); $this->assertTrue(true); } diff --git a/tests/Unit/Services/Nodes/NodeUpdateServiceTest.php b/tests/Unit/Services/Nodes/NodeUpdateServiceTest.php index 0a68c80b0..ded8b7326 100644 --- a/tests/Unit/Services/Nodes/NodeUpdateServiceTest.php +++ b/tests/Unit/Services/Nodes/NodeUpdateServiceTest.php @@ -15,6 +15,7 @@ use Tests\TestCase; use Illuminate\Log\Writer; use phpmock\phpunit\PHPMock; use Pterodactyl\Models\Node; +use GuzzleHttp\Psr7\Response; use GuzzleHttp\Exception\RequestException; use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Services\Nodes\NodeUpdateService; @@ -90,8 +91,8 @@ class NodeUpdateServiceTest extends TestCase 'daemonSecret' => 'random_string', ])->andReturn(true); - $this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf() - ->shouldReceive('update')->withNoArgs()->once()->andReturnNull(); + $this->configRepository->shouldReceive('setNode')->with($this->node)->once()->andReturnSelf() + ->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response); $this->assertTrue($this->service->handle($this->node, ['name' => 'NewName', 'reset_secret' => true])); } @@ -106,8 +107,8 @@ class NodeUpdateServiceTest extends TestCase 'name' => 'NewName', ])->andReturn(true); - $this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf() - ->shouldReceive('update')->withNoArgs()->once()->andReturnNull(); + $this->configRepository->shouldReceive('setNode')->with($this->node)->once()->andReturnSelf() + ->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response); $this->assertTrue($this->service->handle($this->node, ['name' => 'NewName'])); } @@ -120,9 +121,9 @@ class NodeUpdateServiceTest extends TestCase $this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('update')->with($this->node->id, [ 'name' => 'NewName', - ])->andReturn(true); + ])->andReturn(new Response); - $this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andThrow($this->exception); + $this->configRepository->shouldReceive('setNode')->with($this->node)->once()->andThrow($this->exception); $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400); @@ -149,8 +150,8 @@ class NodeUpdateServiceTest extends TestCase 'name' => 'NewName', ])->andReturn(true); - $this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf() - ->shouldReceive('update')->withNoArgs()->once()->andReturnNull(); + $this->configRepository->shouldReceive('setNode')->with($this->node)->once()->andReturnSelf() + ->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response); $this->assertTrue($this->service->handle($this->node->id, ['name' => 'NewName'])); } diff --git a/tests/Unit/Services/Servers/ContainerRebuildServiceTest.php b/tests/Unit/Services/Servers/ContainerRebuildServiceTest.php index d4e1dd8ac..a16c41225 100644 --- a/tests/Unit/Services/Servers/ContainerRebuildServiceTest.php +++ b/tests/Unit/Services/Servers/ContainerRebuildServiceTest.php @@ -1,42 +1,28 @@ . - * - * This software is licensed under the terms of the MIT license. - * https://opensource.org/licenses/MIT - */ namespace Tests\Unit\Services\Servers; use Exception; use Mockery as m; use Tests\TestCase; -use Illuminate\Log\Writer; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; use GuzzleHttp\Exception\RequestException; -use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Services\Servers\ContainerRebuildService; -use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; -use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; +use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface; class ContainerRebuildServiceTest extends TestCase { /** - * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface + * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock */ - protected $daemonServerRepository; + protected $repository; /** * @var \GuzzleHttp\Exception\RequestException */ protected $exception; - /** - * @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface - */ - protected $repository; - /** * @var \Pterodactyl\Models\Server */ @@ -47,11 +33,6 @@ class ContainerRebuildServiceTest extends TestCase */ protected $service; - /** - * @var \Illuminate\Log\Writer - */ - protected $writer; - /** * Setup tests. */ @@ -59,81 +40,33 @@ class ContainerRebuildServiceTest extends TestCase { parent::setUp(); - $this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class); $this->exception = m::mock(RequestException::class)->makePartial(); $this->repository = m::mock(ServerRepositoryInterface::class); - $this->writer = m::mock(Writer::class); $this->server = factory(Server::class)->make(['node_id' => 1]); - - $this->service = new ContainerRebuildService( - $this->daemonServerRepository, - $this->repository, - $this->writer - ); + $this->service = new ContainerRebuildService($this->repository); } /** - * Test that a server is marked for rebuild when it's model is passed to the function. + * Test that a server is marked for rebuild. */ - public function testServerShouldBeMarkedForARebuildWhenModelIsPassed() + public function testServerIsMarkedForRebuild() { - $this->repository->shouldNotReceive('find'); - $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf() - ->shouldReceive('rebuild')->withNoArgs()->once()->andReturnNull(); + $this->repository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf() + ->shouldReceive('rebuild')->withNoArgs()->once()->andReturn(new Response); - $this->service->rebuild($this->server); - } - - /** - * Test that a server is marked for rebuild when the ID of the server is passed to the function. - */ - public function testServerShouldBeMarkedForARebuildWhenServerIdIsPassed() - { - $this->repository->shouldReceive('find')->with($this->server->id)->once()->andReturn($this->server); - - $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf() - ->shouldReceive('rebuild')->withNoArgs()->once()->andReturnNull(); - - $this->service->rebuild($this->server->id); + $this->service->handle($this->server); } /** * Test that an exception thrown by guzzle is rendered as a displayable exception. - */ - public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable() - { - $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id) - ->once()->andThrow($this->exception); - - $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400); - - $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); - - try { - $this->service->rebuild($this->server); - } catch (Exception $exception) { - $this->assertInstanceOf(DisplayException::class, $exception); - $this->assertEquals( - trans('admin/server.exceptions.daemon_exception', ['code' => 400]), - $exception->getMessage() - ); - } - } - - /** - * Test that an exception thrown by something other than guzzle is not transformed to a displayable. * - * @expectedException \Exception + * @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException */ - public function testExceptionNotThrownByGuzzleShouldNotBeTransformedToDisplayable() + public function testExceptionThrownByGuzzle() { - $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id) - ->once()->andThrow(new Exception()); + $this->repository->shouldReceive('setServer')->with($this->server)->once()->andThrow($this->exception); - $this->service->rebuild($this->server); + $this->service->handle($this->server); } } diff --git a/tests/Unit/Services/Servers/DetailsModificationServiceTest.php b/tests/Unit/Services/Servers/DetailsModificationServiceTest.php index 3d04b0106..410ec5b95 100644 --- a/tests/Unit/Services/Servers/DetailsModificationServiceTest.php +++ b/tests/Unit/Services/Servers/DetailsModificationServiceTest.php @@ -13,6 +13,7 @@ use Exception; use Mockery as m; use Tests\TestCase; use Illuminate\Log\Writer; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; use GuzzleHttp\Exception\RequestException; use Illuminate\Database\ConnectionInterface; @@ -183,13 +184,12 @@ class DetailsModificationServiceTest extends TestCase 'image' => 'new/image', ])->once()->andReturnNull(); - $this->daemonServerRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf() + $this->daemonServerRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf() ->shouldReceive('update')->with([ 'build' => [ 'image' => 'new/image', ], - ])->once()->andReturnNull(); + ])->once()->andReturn(new Response); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); @@ -211,13 +211,12 @@ class DetailsModificationServiceTest extends TestCase 'image' => 'new/image', ])->once()->andReturnNull(); - $this->daemonServerRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf() + $this->daemonServerRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf() ->shouldReceive('update')->with([ 'build' => [ 'image' => 'new/image', ], - ])->once()->andReturnNull(); + ])->once()->andReturn(new Response); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); @@ -238,7 +237,7 @@ class DetailsModificationServiceTest extends TestCase 'image' => 'new/image', ])->once()->andReturnNull(); - $this->daemonServerRepository->shouldReceive('setNode')->andThrow($this->exception); + $this->daemonServerRepository->shouldReceive('setServer')->andThrow($this->exception); $this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400); diff --git a/tests/Unit/Services/Servers/ReinstallServerServiceTest.php b/tests/Unit/Services/Servers/ReinstallServerServiceTest.php index 82f018670..349aa571a 100644 --- a/tests/Unit/Services/Servers/ReinstallServerServiceTest.php +++ b/tests/Unit/Services/Servers/ReinstallServerServiceTest.php @@ -12,11 +12,10 @@ namespace Tests\Unit\Services\Servers; use Exception; use Mockery as m; use Tests\TestCase; -use Illuminate\Log\Writer; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; use GuzzleHttp\Exception\RequestException; use Illuminate\Database\ConnectionInterface; -use Pterodactyl\Exceptions\DisplayException; use Pterodactyl\Services\Servers\ReinstallServerService; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; @@ -53,11 +52,6 @@ class ReinstallServerServiceTest extends TestCase */ protected $service; - /** - * @var \Illuminate\Log\Writer - */ - protected $writer; - /** * Setup tests. */ @@ -69,15 +63,13 @@ class ReinstallServerServiceTest extends TestCase $this->database = m::mock(ConnectionInterface::class); $this->exception = m::mock(RequestException::class)->makePartial(); $this->repository = m::mock(ServerRepositoryInterface::class); - $this->writer = m::mock(Writer::class); $this->server = factory(Server::class)->make(['node_id' => 1]); $this->service = new ReinstallServerService( $this->database, $this->daemonServerRepository, - $this->repository, - $this->writer + $this->repository ); } @@ -94,9 +86,8 @@ class ReinstallServerServiceTest extends TestCase 'installed' => 0, ])->once()->andReturnNull(); - $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf() - ->shouldReceive('reinstall')->withNoArgs()->once()->andReturnNull(); + $this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf() + ->shouldReceive('reinstall')->withNoArgs()->once()->andReturn(new Response); $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->service->reinstall($this->server); @@ -115,9 +106,8 @@ class ReinstallServerServiceTest extends TestCase 'installed' => 0, ])->once()->andReturnNull(); - $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf() - ->shouldReceive('reinstall')->withNoArgs()->once()->andReturnNull(); + $this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf() + ->shouldReceive('reinstall')->withNoArgs()->once()->andReturn(new Response); $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->service->reinstall($this->server->id); @@ -125,6 +115,8 @@ class ReinstallServerServiceTest extends TestCase /** * Test that an exception thrown by guzzle is rendered as a displayable exception. + * + * @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException */ public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable() { @@ -134,23 +126,9 @@ class ReinstallServerServiceTest extends TestCase 'installed' => 0, ])->once()->andReturnNull(); - $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id) - ->once()->andThrow($this->exception); + $this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andThrow($this->exception); - $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() - ->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400); - - $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); - - try { - $this->service->reinstall($this->server); - } catch (Exception $exception) { - $this->assertInstanceOf(DisplayException::class, $exception); - $this->assertEquals( - trans('admin/server.exceptions.daemon_exception', ['code' => 400]), - $exception->getMessage() - ); - } + $this->service->reinstall($this->server); } /** @@ -166,8 +144,7 @@ class ReinstallServerServiceTest extends TestCase 'installed' => 0, ])->once()->andReturnNull(); - $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id) - ->once()->andThrow(new Exception()); + $this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andThrow(new Exception()); $this->service->reinstall($this->server); } diff --git a/tests/Unit/Services/Servers/ServerCreationServiceTest.php b/tests/Unit/Services/Servers/ServerCreationServiceTest.php index b50ad1d84..51ed61912 100644 --- a/tests/Unit/Services/Servers/ServerCreationServiceTest.php +++ b/tests/Unit/Services/Servers/ServerCreationServiceTest.php @@ -4,6 +4,7 @@ namespace Tests\Unit\Services\Servers; use Mockery as m; use Tests\TestCase; +use Pterodactyl\Models\Node; use Pterodactyl\Models\User; use Tests\Traits\MocksUuids; use Pterodactyl\Models\Server; @@ -132,7 +133,10 @@ class ServerCreationServiceTest extends TestCase ])->once()->andReturn(true); $this->configurationStructureService->shouldReceive('handle')->with($model)->once()->andReturn(['test' => 'struct']); - $this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf(); + $node = factory(Node::class)->make(); + $this->nodeRepository->shouldReceive('find')->with($model->node_id)->once()->andReturn($node); + + $this->daemonServerRepository->shouldReceive('setNode')->with($node)->once()->andReturnSelf(); $this->daemonServerRepository->shouldReceive('create')->with(['test' => 'struct'], ['start_on_completion' => false])->once(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); @@ -158,7 +162,10 @@ class ServerCreationServiceTest extends TestCase $this->validatorService->shouldReceive('setUserLevel')->once()->andReturnNull(); $this->validatorService->shouldReceive('handle')->once()->andReturn(collect([])); $this->configurationStructureService->shouldReceive('handle')->once()->andReturn([]); - $this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andThrow($this->exception); + + $node = factory(Node::class)->make(); + $this->nodeRepository->shouldReceive('find')->with($model->node_id)->once()->andReturn($node); + $this->daemonServerRepository->shouldReceive('setNode')->with($node)->once()->andThrow($this->exception); $this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); try { diff --git a/tests/Unit/Services/Servers/ServerDeletionServiceTest.php b/tests/Unit/Services/Servers/ServerDeletionServiceTest.php index 26d7fa103..eb5ffc244 100644 --- a/tests/Unit/Services/Servers/ServerDeletionServiceTest.php +++ b/tests/Unit/Services/Servers/ServerDeletionServiceTest.php @@ -13,6 +13,7 @@ use Exception; use Mockery as m; use Tests\TestCase; use Illuminate\Log\Writer; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; use GuzzleHttp\Exception\RequestException; use Illuminate\Database\ConnectionInterface; @@ -111,9 +112,8 @@ class ServerDeletionServiceTest extends TestCase */ public function testServerCanBeDeletedWithoutForce() { - $this->daemonServerRepository->shouldReceive('setNode')->with($this->model->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($this->model->uuid)->once()->andReturnSelf() - ->shouldReceive('delete')->withNoArgs()->once()->andReturnNull(); + $this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf() + ->shouldReceive('delete')->withNoArgs()->once()->andReturn(new Response); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf() @@ -133,8 +133,7 @@ class ServerDeletionServiceTest extends TestCase */ public function testServerShouldBeDeletedEvenWhenFailureOccursIfForceIsSet() { - $this->daemonServerRepository->shouldReceive('setNode')->with($this->model->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($this->model->uuid)->once()->andReturnSelf() + $this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf() ->shouldReceive('delete')->withNoArgs()->once()->andThrow($this->exception); $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull(); @@ -157,7 +156,7 @@ class ServerDeletionServiceTest extends TestCase */ public function testExceptionShouldBeThrownIfDaemonReturnsAnErrorAndForceIsNotSet() { - $this->daemonServerRepository->shouldReceive('setNode->setAccessServer->delete')->once()->andThrow($this->exception); + $this->daemonServerRepository->shouldReceive('setServer->delete')->once()->andThrow($this->exception); $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull(); $this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull(); @@ -179,9 +178,8 @@ class ServerDeletionServiceTest extends TestCase $this->repository->shouldReceive('setColumns')->with(['id', 'node_id', 'uuid'])->once()->andReturnSelf() ->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model); - $this->daemonServerRepository->shouldReceive('setNode')->with($this->model->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($this->model->uuid)->once()->andReturnSelf() - ->shouldReceive('delete')->withNoArgs()->once()->andReturn(1); + $this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf() + ->shouldReceive('delete')->withNoArgs()->once()->andReturn(new Response); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf() diff --git a/tests/Unit/Services/Servers/StartupModificationServiceTest.php b/tests/Unit/Services/Servers/StartupModificationServiceTest.php index 0c06b1799..fe8392cfe 100644 --- a/tests/Unit/Services/Servers/StartupModificationServiceTest.php +++ b/tests/Unit/Services/Servers/StartupModificationServiceTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Services\Servers; use Mockery as m; use Tests\TestCase; use Pterodactyl\Models\User; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; use Illuminate\Database\ConnectionInterface; use Pterodactyl\Services\Servers\EnvironmentService; @@ -88,11 +89,10 @@ class StartupModificationServiceTest extends TestCase ], ['variable_value' => 'stored-value'])->once()->andReturnNull(); $this->environmentService->shouldReceive('handle')->with($model)->once()->andReturn(['env']); - $this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf(); - $this->daemonServerRepository->shouldReceive('setAccessServer')->with($model->uuid)->once()->andReturnSelf(); + $this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf(); $this->daemonServerRepository->shouldReceive('update')->with([ 'build' => ['env|overwrite' => ['env']], - ])->once()->andReturnSelf(); + ])->once()->andReturn(new Response); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); @@ -135,8 +135,7 @@ class StartupModificationServiceTest extends TestCase $this->environmentService->shouldReceive('handle')->with($model)->once()->andReturn(['env']); - $this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf(); - $this->daemonServerRepository->shouldReceive('setAccessServer')->with($model->uuid)->once()->andReturnSelf(); + $this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf(); $this->daemonServerRepository->shouldReceive('update')->with([ 'build' => [ 'env|overwrite' => ['env'], @@ -147,7 +146,7 @@ class StartupModificationServiceTest extends TestCase 'pack' => 'xyz987', 'skip_scripts' => false, ], - ])->once()->andReturnSelf(); + ])->once()->andReturn(new Response); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); diff --git a/tests/Unit/Services/Servers/SuspensionServiceTest.php b/tests/Unit/Services/Servers/SuspensionServiceTest.php index e899ca747..116014277 100644 --- a/tests/Unit/Services/Servers/SuspensionServiceTest.php +++ b/tests/Unit/Services/Servers/SuspensionServiceTest.php @@ -13,6 +13,7 @@ use Exception; use Mockery as m; use Tests\TestCase; use Illuminate\Log\Writer; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; use GuzzleHttp\Exception\RequestException; use Illuminate\Database\ConnectionInterface; @@ -104,9 +105,8 @@ class SuspensionServiceTest extends TestCase $this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('update')->with($this->server->id, ['suspended' => true])->once()->andReturnNull(); - $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf() - ->shouldReceive('suspend')->withNoArgs()->once()->andReturnNull(); + $this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf() + ->shouldReceive('suspend')->withNoArgs()->once()->andReturn(new Response); $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->assertTrue($this->service->toggle($this->server)); @@ -123,9 +123,8 @@ class SuspensionServiceTest extends TestCase $this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('update')->with($this->server->id, ['suspended' => false])->once()->andReturnNull(); - $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf() - ->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf() - ->shouldReceive('unsuspend')->withNoArgs()->once()->andReturnNull(); + $this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf() + ->shouldReceive('unsuspend')->withNoArgs()->once()->andReturn(new Response); $this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->assertTrue($this->service->toggle($this->server, 'unsuspend')); @@ -162,7 +161,7 @@ class SuspensionServiceTest extends TestCase $this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf() ->shouldReceive('update')->with($this->server->id, ['suspended' => true])->once()->andReturnNull(); - $this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id) + $this->daemonServerRepository->shouldReceive('setServer')->with($this->server) ->once()->andThrow($this->exception); $this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf() diff --git a/tests/Unit/Services/Subusers/SubuserUpdateServiceTest.php b/tests/Unit/Services/Subusers/SubuserUpdateServiceTest.php index 550411337..6ca75c0fe 100644 --- a/tests/Unit/Services/Subusers/SubuserUpdateServiceTest.php +++ b/tests/Unit/Services/Subusers/SubuserUpdateServiceTest.php @@ -12,6 +12,7 @@ namespace Tests\Unit\Services\Subusers; use Mockery as m; use Tests\TestCase; use Pterodactyl\Models\User; +use GuzzleHttp\Psr7\Response; use Pterodactyl\Models\Server; use Pterodactyl\Models\Subuser; use Tests\Traits\MocksRequestException; @@ -90,8 +91,8 @@ class SubuserUpdateServiceTest extends TestCase $this->permissionService->shouldReceive('handle')->with($subuser->id, ['some-permission'])->once()->andReturnNull(); $this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123'); - $this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andReturnSelf(); - $this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturnNull(); + $this->daemonRepository->shouldReceive('setServer')->with($subuser->server)->once()->andReturnSelf(); + $this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturn(new Response); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); @@ -116,7 +117,7 @@ class SubuserUpdateServiceTest extends TestCase $this->permissionService->shouldReceive('handle')->with($subuser->id, [])->once()->andReturnNull(); $this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123'); - $this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andThrow($this->getExceptionMock()); + $this->daemonRepository->shouldReceive('setServer')->with($subuser->server)->once()->andThrow($this->getExceptionMock()); $this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); try {