Remove unused code
This commit is contained in:
parent
34916e7caf
commit
756a21ff04
15 changed files with 21 additions and 1126 deletions
File diff suppressed because one or more lines are too long
|
@ -28,7 +28,6 @@ use Pterodactyl\Http\Middleware\Api\ApiSubstituteBindings;
|
|||
use Illuminate\Foundation\Http\Middleware\ValidatePostSize;
|
||||
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
|
||||
use Pterodactyl\Http\Middleware\Server\AccessingValidServer;
|
||||
use Pterodactyl\Http\Middleware\Server\AuthenticateAsSubuser;
|
||||
use Pterodactyl\Http\Middleware\Api\Daemon\DaemonAuthenticate;
|
||||
use Pterodactyl\Http\Middleware\RequireTwoFactorAuthentication;
|
||||
use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode;
|
||||
|
@ -101,7 +100,6 @@ class Kernel extends HttpKernel
|
|||
'auth.basic' => AuthenticateWithBasicAuth::class,
|
||||
'guest' => RedirectIfAuthenticated::class,
|
||||
'server' => AccessingValidServer::class,
|
||||
'subuser.auth' => AuthenticateAsSubuser::class,
|
||||
'admin' => AdminAuthenticate::class,
|
||||
'csrf' => VerifyCsrfToken::class,
|
||||
'throttle' => ThrottleRequests::class,
|
||||
|
|
|
@ -1,59 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Pterodactyl\Http\Middleware\Server;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
|
||||
class AuthenticateAsSubuser
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
|
||||
*/
|
||||
private $keyProviderService;
|
||||
|
||||
/**
|
||||
* SubuserAccessAuthenticate constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService $keyProviderService
|
||||
*/
|
||||
public function __construct(DaemonKeyProviderService $keyProviderService)
|
||||
{
|
||||
$this->keyProviderService = $keyProviderService;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a subuser has permissions to access a server, if so set their access token.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
|
||||
*/
|
||||
public function handle(Request $request, Closure $next)
|
||||
{
|
||||
$server = $request->attributes->get('server');
|
||||
|
||||
try {
|
||||
$token = $this->keyProviderService->handle($server, $request->user());
|
||||
} catch (RecordNotFoundException $exception) {
|
||||
throw new AccessDeniedHttpException('This account does not have permission to access this server.');
|
||||
}
|
||||
|
||||
$request->attributes->set('server_token', $token);
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
|
@ -33,7 +33,7 @@ class RouteServiceProvider extends ServiceProvider
|
|||
->namespace($this->namespace . '\Auth')
|
||||
->group(base_path('routes/auth.php'));
|
||||
|
||||
Route::middleware(['web', 'csrf', 'auth', 'server', 'subuser.auth', 'node.maintenance'])
|
||||
Route::middleware(['web', 'csrf', 'auth', 'server', 'node.maintenance'])
|
||||
->prefix('/api/server/{server}')
|
||||
->namespace($this->namespace . '\Server')
|
||||
->group(base_path('routes/server.php'));
|
||||
|
|
|
@ -1,87 +0,0 @@
|
|||
<?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\DaemonKeys;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
||||
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
||||
|
||||
class DaemonKeyCreationService
|
||||
{
|
||||
/**
|
||||
* @var \Carbon\Carbon
|
||||
*/
|
||||
protected $carbon;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Config\Repository
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* DaemonKeyCreationService constructor.
|
||||
*
|
||||
* @param \Carbon\Carbon $carbon
|
||||
* @param \Illuminate\Contracts\Config\Repository $config
|
||||
* @param \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface $repository
|
||||
*/
|
||||
public function __construct(
|
||||
Carbon $carbon,
|
||||
ConfigRepository $config,
|
||||
DaemonKeyRepositoryInterface $repository
|
||||
) {
|
||||
$this->carbon = $carbon;
|
||||
$this->config = $config;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new daemon key to be used when connecting to a daemon.
|
||||
*
|
||||
* @param int $server
|
||||
* @param int $user
|
||||
* @return string
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
*/
|
||||
public function handle(int $server, int $user)
|
||||
{
|
||||
$secret = DaemonKeyRepositoryInterface::INTERNAL_KEY_IDENTIFIER . str_random(40);
|
||||
|
||||
$this->repository->withoutFreshModel()->create([
|
||||
'user_id' => $user,
|
||||
'server_id' => $server,
|
||||
'secret' => $secret,
|
||||
'expires_at' => $this->carbon->now()->addMinutes($this->config->get('pterodactyl.api.key_expire_time'))->toDateTimeString(),
|
||||
]);
|
||||
|
||||
return $secret;
|
||||
}
|
||||
}
|
|
@ -1,121 +0,0 @@
|
|||
<?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\DaemonKeys;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
||||
|
||||
class DaemonKeyProviderService
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService
|
||||
*/
|
||||
private $keyCreationService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService
|
||||
*/
|
||||
private $keyUpdateService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface
|
||||
*/
|
||||
private $subuserRepository;
|
||||
|
||||
/**
|
||||
* GetDaemonKeyService constructor.
|
||||
*
|
||||
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService $keyCreationService
|
||||
* @param \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface $repository
|
||||
* @param \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService $keyUpdateService
|
||||
* @param \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface $subuserRepository
|
||||
*/
|
||||
public function __construct(
|
||||
DaemonKeyCreationService $keyCreationService,
|
||||
DaemonKeyRepositoryInterface $repository,
|
||||
DaemonKeyUpdateService $keyUpdateService,
|
||||
SubuserRepositoryInterface $subuserRepository
|
||||
) {
|
||||
$this->keyCreationService = $keyCreationService;
|
||||
$this->keyUpdateService = $keyUpdateService;
|
||||
$this->repository = $repository;
|
||||
$this->subuserRepository = $subuserRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the access key for a user on a specific server.
|
||||
*
|
||||
* @param \Pterodactyl\Models\Server $server
|
||||
* @param \Pterodactyl\Models\User $user
|
||||
* @param bool $updateIfExpired
|
||||
* @return string
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function handle(Server $server, User $user, $updateIfExpired = true): string
|
||||
{
|
||||
try {
|
||||
$key = $this->repository->findFirstWhere([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
]);
|
||||
} catch (RecordNotFoundException $exception) {
|
||||
// If key doesn't exist but we are an admin or the server owner,
|
||||
// create it.
|
||||
if ($user->root_admin || $user->id === $server->owner_id) {
|
||||
return $this->keyCreationService->handle($server->id, $user->id);
|
||||
}
|
||||
|
||||
// Check if user is a subuser for this server. Ideally they should always have
|
||||
// a record associated with them in the database, but we should still handle
|
||||
// that potentiality here.
|
||||
//
|
||||
// If no subuser is found, a RecordNotFoundException will be thrown, thus handling
|
||||
// the parent error as well.
|
||||
$subuser = $this->subuserRepository->findFirstWhere([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
]);
|
||||
|
||||
return $this->keyCreationService->handle($subuser->server_id, $subuser->user_id);
|
||||
}
|
||||
|
||||
if (! $updateIfExpired || Carbon::now()->diffInSeconds($key->expires_at, false) > 0) {
|
||||
return $key->secret;
|
||||
}
|
||||
|
||||
return $this->keyUpdateService->handle($key->id);
|
||||
}
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
<?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\DaemonKeys;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Webmozart\Assert\Assert;
|
||||
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
||||
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
||||
|
||||
class DaemonKeyUpdateService
|
||||
{
|
||||
/**
|
||||
* @var \Carbon\Carbon
|
||||
*/
|
||||
protected $carbon;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Config\Repository
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* DaemonKeyUpdateService constructor.
|
||||
*
|
||||
* @param \Carbon\Carbon $carbon
|
||||
* @param \Illuminate\Contracts\Config\Repository $config
|
||||
* @param \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface $repository
|
||||
*/
|
||||
public function __construct(
|
||||
Carbon $carbon,
|
||||
ConfigRepository $config,
|
||||
DaemonKeyRepositoryInterface $repository
|
||||
) {
|
||||
$this->carbon = $carbon;
|
||||
$this->config = $config;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a daemon key to expire the previous one.
|
||||
*
|
||||
* @param int $key
|
||||
* @return string
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function handle($key)
|
||||
{
|
||||
Assert::integerish($key, 'First argument passed to handle must be an integer, received %s.');
|
||||
|
||||
$secret = DaemonKeyRepositoryInterface::INTERNAL_KEY_IDENTIFIER . str_random(40);
|
||||
$this->repository->withoutFreshModel()->update($key, [
|
||||
'secret' => $secret,
|
||||
'expires_at' => $this->carbon->now()->addMinutes($this->config->get('pterodactyl.api.key_expire_time'))->toDateTimeString(),
|
||||
]);
|
||||
|
||||
return $secret;
|
||||
}
|
||||
}
|
|
@ -1,72 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Http\Middleware\Server;
|
||||
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Tests\Unit\Http\Middleware\MiddlewareTestCase;
|
||||
use Pterodactyl\Http\Middleware\Server\AuthenticateAsSubuser;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
|
||||
class AuthenticateAsSubuserTest extends MiddlewareTestCase
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock
|
||||
*/
|
||||
private $keyProviderService;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->keyProviderService = m::mock(DaemonKeyProviderService::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a successful instance of the middleware.
|
||||
*/
|
||||
public function testSuccessfulMiddleware()
|
||||
{
|
||||
$model = factory(Server::class)->make();
|
||||
$user = $this->setRequestUser();
|
||||
$this->setRequestAttribute('server', $model);
|
||||
|
||||
$this->keyProviderService->shouldReceive('handle')->with($model, $user)->once()->andReturn('abc123');
|
||||
|
||||
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
||||
$this->assertRequestHasAttribute('server_token');
|
||||
$this->assertRequestAttributeEquals('abc123', 'server_token');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test middleware handles missing token exception.
|
||||
*/
|
||||
public function testExceptionIsThrownIfNoTokenIsFound()
|
||||
{
|
||||
$this->expectException(AccessDeniedHttpException::class);
|
||||
$this->expectExceptionMessage('This account does not have permission to access this server.');
|
||||
|
||||
$model = factory(Server::class)->make();
|
||||
$user = $this->setRequestUser();
|
||||
$this->setRequestAttribute('server', $model);
|
||||
|
||||
$this->keyProviderService->shouldReceive('handle')->with($model, $user)->once()->andThrow(new RecordNotFoundException);
|
||||
|
||||
$this->getMiddleware()->handle($this->request, $this->getClosureAssertions());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the middleware using mocked dependencies.
|
||||
*
|
||||
* @return \Pterodactyl\Http\Middleware\Server\AuthenticateAsSubuser
|
||||
*/
|
||||
public function getMiddleware(): AuthenticateAsSubuser
|
||||
{
|
||||
return new AuthenticateAsSubuser($this->keyProviderService);
|
||||
}
|
||||
}
|
|
@ -7,6 +7,7 @@ use Tests\TestCase;
|
|||
use Pterodactyl\Models\Allocation;
|
||||
use Pterodactyl\Services\Allocations\AllocationDeletionService;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException;
|
||||
|
||||
class AllocationDeletionServiceTest extends TestCase
|
||||
{
|
||||
|
@ -37,11 +38,11 @@ class AllocationDeletionServiceTest extends TestCase
|
|||
|
||||
/**
|
||||
* Test that an exception gets thrown if an allocation is currently assigned to a server.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\ServerUsingAllocationException
|
||||
*/
|
||||
public function testExceptionThrownIfAssignedToServer()
|
||||
{
|
||||
$this->expectException(ServerUsingAllocationException::class);
|
||||
|
||||
$model = factory(Allocation::class)->make(['server_id' => 123]);
|
||||
|
||||
$this->getService()->handle($model);
|
||||
|
|
|
@ -8,6 +8,10 @@ use Pterodactyl\Models\Node;
|
|||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Services\Allocations\AssignmentService;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException;
|
||||
use Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException;
|
||||
|
||||
class AssignmentServiceTest extends TestCase
|
||||
{
|
||||
|
@ -190,12 +194,12 @@ class AssignmentServiceTest extends TestCase
|
|||
|
||||
/**
|
||||
* Test that a CIDR IP address with a range works properly.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\CidrOutOfRangeException
|
||||
* @expectedExceptionMessage CIDR notation only allows masks between /25 and /32.
|
||||
*/
|
||||
public function testCIDRNotatedIPAddressOutsideRangeLimit()
|
||||
{
|
||||
$this->expectException(CidrOutOfRangeException::class);
|
||||
$this->expectExceptionMessage('CIDR notation only allows masks between /25 and /32.');
|
||||
|
||||
$data = [
|
||||
'allocation_ip' => '192.168.1.100/20',
|
||||
'allocation_ports' => ['2222'],
|
||||
|
@ -206,12 +210,12 @@ class AssignmentServiceTest extends TestCase
|
|||
|
||||
/**
|
||||
* Test that an exception is thrown if there are too many ports.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\TooManyPortsInRangeException
|
||||
* @expectedExceptionMessage Adding more than 1000 ports in a single range at once is not supported.
|
||||
*/
|
||||
public function testAllocationWithPortsExceedingLimit()
|
||||
{
|
||||
$this->expectException(TooManyPortsInRangeException::class);
|
||||
$this->expectExceptionMessage('Adding more than 1000 ports in a single range at once is not supported.');
|
||||
|
||||
$data = [
|
||||
'allocation_ip' => '192.168.1.1',
|
||||
'allocation_ports' => ['5000-7000'],
|
||||
|
@ -224,12 +228,12 @@ class AssignmentServiceTest extends TestCase
|
|||
|
||||
/**
|
||||
* Test that an exception is thrown if an invalid port is provided.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\InvalidPortMappingException
|
||||
* @expectedExceptionMessage The mapping provided for test123 was invalid and could not be processed.
|
||||
*/
|
||||
public function testInvalidPortProvided()
|
||||
{
|
||||
$this->expectException(InvalidPortMappingException::class);
|
||||
$this->expectExceptionMessage('The mapping provided for test123 was invalid and could not be processed.');
|
||||
|
||||
$data = [
|
||||
'allocation_ip' => '192.168.1.1',
|
||||
'allocation_ports' => ['test123'],
|
||||
|
@ -245,11 +249,12 @@ class AssignmentServiceTest extends TestCase
|
|||
* @param array $ports
|
||||
*
|
||||
* @dataProvider invalidPortsDataProvider
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\PortOutOfRangeException
|
||||
* @expectedExceptionMessage Ports in an allocation must be greater than 1024 and less than or equal to 65535.
|
||||
*/
|
||||
public function testPortRangeOutsideOfRangeLimits(array $ports)
|
||||
{
|
||||
$this->expectException(PortOutOfRangeException::class);
|
||||
$this->expectExceptionMessage('Ports in an allocation must be greater than 1024 and less than or equal to 65535.');
|
||||
|
||||
$data = ['allocation_ip' => '192.168.1.1', 'allocation_ports' => $ports];
|
||||
|
||||
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
||||
|
|
|
@ -1,156 +0,0 @@
|
|||
<?php
|
||||
|
||||
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;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Exceptions\PterodactylException;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Services\Allocations\SetDefaultAllocationService;
|
||||
use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonRepositoryInterface;
|
||||
|
||||
class SetDefaultAllocationServiceTest extends TestCase
|
||||
{
|
||||
use MocksRequestException;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
||||
*/
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $daemonRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\AllocationRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $serverRepository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->daemonRepository = m::mock(DaemonRepositoryInterface::class);
|
||||
$this->repository = m::mock(AllocationRepositoryInterface::class);
|
||||
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an allocation can be updated.
|
||||
*
|
||||
* @dataProvider useModelDataProvider
|
||||
*/
|
||||
public function testAllocationIsUpdated(bool $useModel)
|
||||
{
|
||||
$allocations = factory(Allocation::class)->times(2)->make();
|
||||
$model = factory(Server::class)->make();
|
||||
if (! $useModel) {
|
||||
$this->serverRepository->shouldReceive('find')->with(1234)->once()->andReturn($model);
|
||||
}
|
||||
|
||||
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn($allocations);
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->serverRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
|
||||
$this->serverRepository->shouldReceive('update')->with($model->id, [
|
||||
'allocation_id' => $allocations->first()->id,
|
||||
])->once()->andReturn(new Response);
|
||||
|
||||
$this->daemonRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
|
||||
$this->daemonRepository->shouldReceive('update')->with([
|
||||
'build' => [
|
||||
'default' => [
|
||||
'ip' => $allocations->first()->ip,
|
||||
'port' => $allocations->first()->port,
|
||||
],
|
||||
'ports|overwrite' => $allocations->groupBy('ip')->map(function ($item) {
|
||||
return $item->pluck('port');
|
||||
})->toArray(),
|
||||
],
|
||||
])->once()->andReturn(new Response);
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->getService()->handle($useModel ? $model : 1234, $allocations->first()->id);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertSame($allocations->first(), $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an allocation that doesn't belong to a server throws an exception.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Service\Allocation\AllocationDoesNotBelongToServerException
|
||||
*/
|
||||
public function testAllocationNotBelongingToServerThrowsException()
|
||||
{
|
||||
$model = factory(Server::class)->make();
|
||||
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn(collect());
|
||||
|
||||
$this->getService()->handle($model, 1234);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception thrown by guzzle is handled properly.
|
||||
*/
|
||||
public function testExceptionThrownByGuzzleIsHandled()
|
||||
{
|
||||
$this->configureExceptionMock();
|
||||
|
||||
$allocation = factory(Allocation::class)->make();
|
||||
$model = factory(Server::class)->make();
|
||||
|
||||
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $model->id]])->once()->andReturn(collect([$allocation]));
|
||||
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
||||
$this->serverRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
|
||||
$this->serverRepository->shouldReceive('update')->with($model->id, [
|
||||
'allocation_id' => $allocation->id,
|
||||
])->once()->andReturn(new Response);
|
||||
|
||||
$this->daemonRepository->shouldReceive('setServer->update')->once()->andThrow($this->getExceptionMock());
|
||||
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
try {
|
||||
$this->getService()->handle($model, $allocation->id);
|
||||
} catch (PterodactylException $exception) {
|
||||
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
|
||||
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider to determine if a model should be passed or an int.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function useModelDataProvider(): array
|
||||
{
|
||||
return [[false], [true]];
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the service with mocked dependencies.
|
||||
*
|
||||
* @return \Pterodactyl\Services\Allocations\SetDefaultAllocationService
|
||||
*/
|
||||
private function getService(): SetDefaultAllocationService
|
||||
{
|
||||
return new SetDefaultAllocationService($this->repository, $this->connection, $this->daemonRepository, $this->serverRepository);
|
||||
}
|
||||
}
|
|
@ -1,98 +0,0 @@
|
|||
<?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 Tests\Unit\Services\DaemonKeys;
|
||||
|
||||
use Mockery as m;
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use phpmock\phpunit\PHPMock;
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService;
|
||||
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
||||
|
||||
class DaemonKeyCreationServiceTest extends TestCase
|
||||
{
|
||||
use PHPMock;
|
||||
|
||||
/**
|
||||
* @var \Carbon\Carbon|\Mockery\Mock
|
||||
*/
|
||||
protected $carbon;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->carbon = m::mock(Carbon::class);
|
||||
$this->config = m::Mock(Repository::class);
|
||||
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
|
||||
|
||||
$this->service = new DaemonKeyCreationService($this->carbon, $this->config, $this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a daemon key is created.
|
||||
*/
|
||||
public function testDaemonKeyIsCreated()
|
||||
{
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\DaemonKeys', 'str_random')
|
||||
->expects($this->once())->willReturn('random_string');
|
||||
|
||||
$this->config->shouldReceive('get')->with('pterodactyl.api.key_expire_time')->once()->andReturn(100);
|
||||
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('addMinutes')->with(100)->once()->andReturnSelf()
|
||||
->shouldReceive('toDateTimeString')->withNoArgs()->once()->andReturn('00:00:00');
|
||||
|
||||
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('create')->with([
|
||||
'user_id' => 1,
|
||||
'server_id' => 2,
|
||||
'secret' => DaemonKeyRepositoryInterface::INTERNAL_KEY_IDENTIFIER . 'random_string',
|
||||
'expires_at' => '00:00:00',
|
||||
])->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->handle(2, 1);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertEquals('i_random_string', $response);
|
||||
}
|
||||
}
|
|
@ -1,229 +0,0 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\DaemonKeys;
|
||||
|
||||
use Mockery as m;
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Models\Subuser;
|
||||
use Pterodactyl\Models\DaemonKey;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
||||
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
|
||||
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
||||
|
||||
class DaemonKeyProviderServiceTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService|\Mockery\Mock
|
||||
*/
|
||||
private $keyCreationService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService|\Mockery\Mock
|
||||
*/
|
||||
private $keyUpdateService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $subuserRepository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
Carbon::setTestNow(Carbon::now());
|
||||
|
||||
$this->keyCreationService = m::mock(DaemonKeyCreationService::class);
|
||||
$this->keyUpdateService = m::mock(DaemonKeyUpdateService::class);
|
||||
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
|
||||
$this->subuserRepository = m::mock(SubuserRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a key is returned correctly as a non-admin.
|
||||
*/
|
||||
public function testKeyIsReturned()
|
||||
{
|
||||
$server = factory(Server::class)->make();
|
||||
$user = factory(User::class)->make();
|
||||
$key = factory(DaemonKey::class)->make();
|
||||
|
||||
$this->repository->shouldReceive('findFirstWhere')->with([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
])->once()->andReturn($key);
|
||||
|
||||
$response = $this->getService()->handle($server, $user);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertEquals($key->secret, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an expired key is updated and then returned.
|
||||
*/
|
||||
public function testExpiredKeyIsUpdated()
|
||||
{
|
||||
$server = factory(Server::class)->make();
|
||||
$user = factory(User::class)->make(['root_admin' => 0]);
|
||||
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
|
||||
|
||||
$this->repository->shouldReceive('findFirstWhere')->with([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
])->once()->andReturn($key);
|
||||
|
||||
$this->keyUpdateService->shouldReceive('handle')->with($key->id)->once()->andReturn('abc123');
|
||||
|
||||
$response = $this->getService()->handle($server, $user);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertEquals('abc123', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an expired key is not updated and the expired key is returned.
|
||||
*/
|
||||
public function testExpiredKeyIsNotUpdated()
|
||||
{
|
||||
$server = factory(Server::class)->make();
|
||||
$user = factory(User::class)->make(['root_admin' => 0]);
|
||||
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
|
||||
|
||||
$this->repository->shouldReceive('findFirstWhere')->with([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
])->once()->andReturn($key);
|
||||
|
||||
$response = $this->getService()->handle($server, $user, false);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertEquals($key->secret, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a key is created if it is missing and the user is a
|
||||
* root administrator.
|
||||
*/
|
||||
public function testMissingKeyIsCreatedIfRootAdmin()
|
||||
{
|
||||
$server = factory(Server::class)->make();
|
||||
$user = factory(User::class)->make(['root_admin' => 1]);
|
||||
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
|
||||
|
||||
$this->repository->shouldReceive('findFirstWhere')->with([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
])->once()->andThrow(new RecordNotFoundException);
|
||||
|
||||
$this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn($key->secret);
|
||||
|
||||
$response = $this->getService()->handle($server, $user, false);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertEquals($key->secret, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a key is created if it is missing and the user is the
|
||||
* server owner.
|
||||
*/
|
||||
public function testMissingKeyIsCreatedIfUserIsServerOwner()
|
||||
{
|
||||
$user = factory(User::class)->make(['root_admin' => 0]);
|
||||
$server = factory(Server::class)->make(['owner_id' => $user->id]);
|
||||
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
|
||||
|
||||
$this->repository->shouldReceive('findFirstWhere')->with([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
])->once()->andThrow(new RecordNotFoundException);
|
||||
|
||||
$this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn($key->secret);
|
||||
|
||||
$response = $this->getService()->handle($server, $user, false);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertEquals($key->secret, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a missing key is created for a subuser.
|
||||
*/
|
||||
public function testMissingKeyIsCreatedForSubuser()
|
||||
{
|
||||
$user = factory(User::class)->make(['root_admin' => 0]);
|
||||
$server = factory(Server::class)->make();
|
||||
$key = factory(DaemonKey::class)->make(['expires_at' => Carbon::now()->subHour()]);
|
||||
$subuser = factory(Subuser::class)->make(['user_id' => $user->id, 'server_id' => $server->id]);
|
||||
|
||||
$this->repository->shouldReceive('findFirstWhere')->with([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
])->once()->andThrow(new RecordNotFoundException);
|
||||
|
||||
$this->subuserRepository->shouldReceive('findFirstWhere')->once()->with([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
])->andReturn($subuser);
|
||||
|
||||
$this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturn($key->secret);
|
||||
|
||||
$response = $this->getService()->handle($server, $user, false);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertEquals($key->secret, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception is thrown if the user should not get a key.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function testExceptionIsThrownIfUserDoesNotDeserveKey()
|
||||
{
|
||||
$server = factory(Server::class)->make();
|
||||
$user = factory(User::class)->make(['root_admin' => 0]);
|
||||
|
||||
$this->repository->shouldReceive('findFirstWhere')->with([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
])->once()->andThrow(new RecordNotFoundException);
|
||||
|
||||
$this->subuserRepository->shouldReceive('findFirstWhere')->once()->with([
|
||||
['user_id', '=', $user->id],
|
||||
['server_id', '=', $server->id],
|
||||
])->andThrow(new RecordNotFoundException);
|
||||
|
||||
$this->getService()->handle($server, $user, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the service with mocked dependencies.
|
||||
*
|
||||
* @return \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService
|
||||
*/
|
||||
private function getService(): DaemonKeyProviderService
|
||||
{
|
||||
return new DaemonKeyProviderService(
|
||||
$this->keyCreationService,
|
||||
$this->repository,
|
||||
$this->keyUpdateService,
|
||||
$this->subuserRepository
|
||||
);
|
||||
}
|
||||
}
|
|
@ -1,83 +0,0 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* https://opensource.org/licenses/MIT
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Services\DaemonKeys;
|
||||
|
||||
use Mockery as m;
|
||||
use Carbon\Carbon;
|
||||
use Tests\TestCase;
|
||||
use phpmock\phpunit\PHPMock;
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService;
|
||||
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
||||
|
||||
class DaemonKeyUpdateServiceTest extends TestCase
|
||||
{
|
||||
use PHPMock;
|
||||
|
||||
/**
|
||||
* @var \Carbon\Carbon|\Mockery\Mock
|
||||
*/
|
||||
protected $carbon;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyUpdateService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->carbon = m::Mock(Carbon::class);
|
||||
$this->config = m::mock(Repository::class);
|
||||
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
|
||||
|
||||
$this->service = new DaemonKeyUpdateService($this->carbon, $this->config, $this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a key is updated.
|
||||
*/
|
||||
public function testKeyIsUpdated()
|
||||
{
|
||||
$secret = DaemonKeyRepositoryInterface::INTERNAL_KEY_IDENTIFIER . 'random_string';
|
||||
|
||||
$this->getFunctionMock('\\Pterodactyl\\Services\\DaemonKeys', 'str_random')
|
||||
->expects($this->once())->with(40)->willReturn('random_string');
|
||||
|
||||
$this->config->shouldReceive('get')->with('pterodactyl.api.key_expire_time')->once()->andReturn(100);
|
||||
$this->carbon->shouldReceive('now')->withNoArgs()->once()->andReturnSelf()
|
||||
->shouldReceive('addMinutes')->with(100)->once()->andReturnSelf()
|
||||
->shouldReceive('toDateTimeString')->withNoArgs()->once()->andReturn('00:00:00');
|
||||
|
||||
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
|
||||
$this->repository->shouldReceive('update')->with(123, [
|
||||
'secret' => $secret,
|
||||
'expires_at' => '00:00:00',
|
||||
])->once()->andReturnNull();
|
||||
|
||||
$response = $this->service->handle(123);
|
||||
$this->assertNotEmpty($response);
|
||||
$this->assertEquals($secret, $response);
|
||||
}
|
||||
}
|
|
@ -1,116 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Unit\Services\DaemonKeys;
|
||||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Pterodactyl\Models\Node;
|
||||
use Pterodactyl\Models\User;
|
||||
use GuzzleHttp\Psr7\Response;
|
||||
use Pterodactyl\Models\DaemonKey;
|
||||
use Tests\Traits\MocksRequestException;
|
||||
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
|
||||
use Pterodactyl\Services\DaemonKeys\RevokeMultipleDaemonKeysService;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
|
||||
|
||||
class RevokeMultipleDaemonKeysServiceTest extends TestCase
|
||||
{
|
||||
use MocksRequestException;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $daemonRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->daemonRepository = m::mock(ServerRepositoryInterface::class);
|
||||
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that keys can be successfully revoked.
|
||||
*/
|
||||
public function testSuccessfulKeyRevocation()
|
||||
{
|
||||
$user = factory(User::class)->make();
|
||||
$node = factory(Node::class)->make();
|
||||
$key = factory(DaemonKey::class)->make(['user_id' => $user->id]);
|
||||
$key->setRelation('node', $node);
|
||||
|
||||
$this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key]));
|
||||
$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();
|
||||
|
||||
$this->getService()->handle($user);
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an exception thrown by a call to the daemon is handled.
|
||||
*
|
||||
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
||||
*/
|
||||
public function testExceptionThrownFromDaemonCallIsHandled()
|
||||
{
|
||||
$this->configureExceptionMock();
|
||||
|
||||
$user = factory(User::class)->make();
|
||||
$node = factory(Node::class)->make();
|
||||
$key = factory(DaemonKey::class)->make(['user_id' => $user->id]);
|
||||
$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());
|
||||
|
||||
$this->getService()->handle($user);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the behavior for handling exceptions that should not be thrown
|
||||
* immediately is working correctly and adds them to the array.
|
||||
*/
|
||||
public function testIgnoredExceptionsAreHandledProperly()
|
||||
{
|
||||
$this->configureExceptionMock();
|
||||
|
||||
$user = factory(User::class)->make();
|
||||
$node = factory(Node::class)->make();
|
||||
$key = factory(DaemonKey::class)->make(['user_id' => $user->id]);
|
||||
$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());
|
||||
|
||||
$this->repository->shouldReceive('deleteKeys')->with([$key->id])->once()->andReturnNull();
|
||||
|
||||
$service = $this->getService();
|
||||
$service->handle($user, true);
|
||||
$this->assertNotEmpty($service->getExceptions());
|
||||
$this->assertArrayHasKey($node->id, $service->getExceptions());
|
||||
$this->assertSame(array_get($service->getExceptions(), $node->id), $this->getExceptionMock());
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return an instance of the service for testing.
|
||||
*
|
||||
* @return \Pterodactyl\Services\DaemonKeys\RevokeMultipleDaemonKeysService
|
||||
*/
|
||||
private function getService(): RevokeMultipleDaemonKeysService
|
||||
{
|
||||
return new RevokeMultipleDaemonKeysService($this->repository, $this->daemonRepository);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue