Finish unit tests for all server services

This commit is contained in:
Dane Everitt 2017-07-22 20:15:01 -05:00
parent 3add44d342
commit acbc52506c
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
21 changed files with 1609 additions and 206 deletions

View file

@ -25,7 +25,7 @@
namespace Pterodactyl\Services\Servers;
use Ramsey\Uuid\Uuid;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\DatabaseManager;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
@ -46,7 +46,7 @@ class CreationService
protected $daemonServerRepository;
/**
* @var \Illuminate\Database\ConnectionInterface
* @var \Illuminate\Database\DatabaseManager
*/
protected $database;
@ -84,35 +84,35 @@ class CreationService
* CreationService constructor.
*
* @param \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface $allocationRepository
* @param \Illuminate\Database\ConnectionInterface $database
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
* @param \Illuminate\Database\DatabaseManager $database
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
* @param \Pterodactyl\Services\Servers\UsernameGenerationService $usernameService
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
* @param \Pterodactyl\Contracts\Repository\UserRepositoryInterface $userRepository
* @param \Pterodactyl\Services\Servers\UsernameGenerationService $usernameService
* @param \Pterodactyl\Services\Servers\VariableValidatorService $validatorService
*/
public function __construct(
AllocationRepositoryInterface $allocationRepository,
ConnectionInterface $database,
ServerRepositoryInterface $repository,
DaemonServerRepositoryInterface $daemonServerRepository,
ServerVariableRepositoryInterface $serverVariableRepository,
DatabaseManager $database,
NodeRepositoryInterface $nodeRepository,
UsernameGenerationService $usernameService,
ServerRepositoryInterface $repository,
ServerVariableRepositoryInterface $serverVariableRepository,
UserRepositoryInterface $userRepository,
UsernameGenerationService $usernameService,
VariableValidatorService $validatorService
) {
$this->allocationRepository = $allocationRepository;
$this->daemonServerRepository = $daemonServerRepository;
$this->database = $database;
$this->repository = $repository;
$this->nodeRepository = $nodeRepository;
$this->repository = $repository;
$this->serverVariableRepository = $serverVariableRepository;
$this->userRepository = $userRepository;
$this->usernameService = $usernameService;
$this->validatorService = $validatorService;
$this->serverVariableRepository = $serverVariableRepository;
$this->daemonServerRepository = $daemonServerRepository;
}
/**
@ -126,9 +126,6 @@ class CreationService
public function create(array $data)
{
// @todo auto-deployment
$data['user_id'] = 1;
$node = $this->nodeRepository->find($data['node_id']);
$validator = $this->validatorService->setAdmin()->setFields($data['environment'])->validate($data['option_id']);
$uniqueShort = bin2hex(random_bytes(4));
@ -136,7 +133,7 @@ class CreationService
$server = $this->repository->create([
'uuid' => Uuid::uuid4()->toString(),
'uuidShort' => bin2hex(random_bytes(4)),
'uuidShort' => $uniqueShort,
'node_id' => $data['node_id'],
'name' => $data['name'],
'description' => $data['description'],
@ -152,7 +149,7 @@ class CreationService
'allocation_id' => $data['allocation_id'],
'service_id' => $data['service_id'],
'option_id' => $data['option_id'],
'pack_id' => ($data['pack_id'] == 0) ? null : $data['pack_id'],
'pack_id' => (! isset($data['pack_id']) || $data['pack_id'] == 0) ? null : $data['pack_id'],
'startup' => $data['startup'],
'daemonSecret' => bin2hex(random_bytes(18)),
'image' => $data['docker_image'],
@ -181,9 +178,7 @@ class CreationService
$this->serverVariableRepository->insert($records);
// Create the server on the daemon & commit it to the database.
$this->daemonServerRepository->setNode($server->node_id)
->setAccessToken($node->daemonSecret)
->create($server->id);
$this->daemonServerRepository->setNode($server->node_id)->create($server->id);
$this->database->commit();

View file

@ -0,0 +1,156 @@
<?php
/*
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Pterodactyl\Services\Servers;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\DatabaseManager;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Models\Server;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Repositories\Daemon\ServerRepository as DaemonServerRepository;
class DetailsModificationService
{
/**
* @var \Illuminate\Database\DatabaseManager
*/
protected $database;
/**
* @var \Pterodactyl\Repositories\Daemon\ServerRepository
*/
protected $daemonServerRepository;
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
*/
protected $repository;
/**
* DetailsModificationService constructor.
*
* @param \Illuminate\Database\DatabaseManager $database
* @param \Pterodactyl\Repositories\Daemon\ServerRepository $daemonServerRepository
* @param \Pterodactyl\Repositories\Eloquent\ServerRepository $repository
*/
public function __construct(
DatabaseManager $database,
DaemonServerRepository $daemonServerRepository,
ServerRepository $repository
) {
$this->database = $database;
$this->daemonServerRepository = $daemonServerRepository;
$this->repository = $repository;
}
/**
* Update the details for a single server instance.
*
* @param int|\Pterodactyl\Models\Server $server
* @param array $data
* @return bool
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function edit($server, array $data)
{
if (! $server instanceof Server) {
$server = $this->repository->find($server);
}
$this->database->beginTransaction();
$currentSecret = $server->daemonSecret;
if (
(isset($data['reset_token']) && ! is_null($data['reset_token'])) ||
(isset($data['owner_id']) && $data['owner_id'] != $server->owner_id)
) {
$data['daemonSecret'] = bin2hex(random_bytes(18));
$shouldUpdate = true;
}
$this->repository->withoutFresh()->update($server->id, [
'owner_id' => array_get($data, 'owner_id') ?? $server->owner_id,
'name' => array_get($data, 'name') ?? $server->name,
'description' => array_get($data, 'description') ?? $server->description,
'daemonSecret' => array_get($data, 'daemonSecret') ?? $server->daemonSecret,
], true, true);
// If there are no updates, lets save the changes and return.
if (! isset($shouldUpdate)) {
return $this->database->commit();
}
try {
$this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update([
'keys' => [
(string) $currentSecret => [],
(string) $data['daemonSecret'] => $this->daemonServerRepository::DAEMON_PERMISSIONS,
],
]);
return $this->database->commit();
} catch (RequestException $exception) {
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [
'code' => $exception->getResponse()->getStatusCode(),
]));
}
}
/**
* Update the docker container for a specified server.
*
* @param int|\Pterodactyl\Models\Server $server
* @param string $image
* @return bool
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
*/
public function setDockerImage($server, $image)
{
if (! $server instanceof Server) {
$server = $this->repository->find($server);
}
$this->database->beginTransaction();
$this->repository->withoutFresh()->update($server->id, ['image' => $image]);
try {
$this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update([
'build' => [
'image' => $image,
],
]);
return $this->database->commit();
} catch (RequestException $exception) {
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [
'code' => $exception->getResponse()->getStatusCode(),
]));
}
}
}

View file

@ -47,7 +47,7 @@ class UsernameGenerationService
}
// Filter the Server Name
$name = trim(preg_replace('/[^\w]+/', '', $name), '_');
$name = trim(preg_replace('/[^A-Za-z0-9]+/', '', $name), '_');
$name = (strlen($name) < 1) ? str_random(6) : $name;
return strtolower(substr($name, 0, 6) . '_' . $unique);

View file

@ -27,9 +27,8 @@ namespace Pterodactyl\Services\Servers;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface;
use Pterodactyl\Exceptions\DisplayValidationException;
use Illuminate\Validation\Factory as ValidationFactory;
use Illuminate\Contracts\Validation\Factory as ValidationFactory;
use Pterodactyl\Contracts\Repository\OptionVariableRepositoryInterface;
use Pterodactyl\Exceptions\Services\Servers\RequiredVariableMissingException;
class VariableValidatorService
{
@ -64,10 +63,18 @@ class VariableValidatorService
protected $serverVariableRepository;
/**
* @var \Illuminate\Validation\Factory
* @var \Illuminate\Contracts\Validation\Factory
*/
protected $validator;
/**
* VariableValidatorService constructor.
*
* @param \Pterodactyl\Contracts\Repository\OptionVariableRepositoryInterface $optionVariableRepository
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $serverRepository
* @param \Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface $serverVariableRepository
* @param \Illuminate\Contracts\Validation\Factory $validator
*/
public function __construct(
OptionVariableRepositoryInterface $optionVariableRepository,
ServerRepositoryInterface $serverRepository,
@ -121,14 +128,6 @@ class VariableValidatorService
}
$variables->each(function ($item) {
if (! isset($this->fields[$item->env_variable]) && $item->required) {
if ($item->required) {
throw new RequiredVariableMissingException(
sprintf('Required service option variable %s was missing from this request.', $item->env_variable)
);
}
}
// Skip doing anything if user is not an admin and variable is not user viewable
// or editable.
if (! $this->isAdmin && (! $item->user_editable || ! $item->user_viewable)) {
@ -145,7 +144,7 @@ class VariableValidatorService
throw new DisplayValidationException(json_encode(
collect([
'notice' => [
sprintf('There was a validation error with the %s variable.', $item->name),
trans('admin/server.exceptions.bad_variable', ['name' => $item->name]),
],
])->merge($validator->errors()->toArray())
));