Insane number of changes required to get the build to post

This commit is contained in:
Dane Everitt 2019-11-24 20:19:31 -08:00
parent 547e8840e2
commit 2af653733c
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
12 changed files with 111 additions and 649 deletions

View file

@ -1,163 +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 Tests\TestCase;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\DaemonKey;
use Psr\Log\LoggerInterface as Writer;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class DaemonKeyDeletionServiceTest extends TestCase
{
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
protected $connection;
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
*/
protected $daemonRepository;
/**
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
*/
protected $exception;
/**
* @var \Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface|\Mockery\Mock
*/
protected $repository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
*/
protected $serverRepository;
/**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService
*/
protected $service;
/**
* @var \Psr\Log\LoggerInterface|\Mockery\Mock
*/
protected $writer;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class);
$this->exception = m::mock(RequestException::class);
$this->repository = m::mock(DaemonKeyRepositoryInterface::class);
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
$this->writer = m::mock(Writer::class);
$this->service = new DaemonKeyDeletionService(
$this->connection,
$this->repository,
$this->daemonRepository,
$this->serverRepository,
$this->writer
);
}
/**
* Test that a daemon key is deleted correctly.
*/
public function testKeyIsDeleted()
{
$server = factory(Server::class)->make();
$key = factory(DaemonKey::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('findFirstWhere')->with([
['user_id', '=', 100],
['server_id', '=', $server->id],
])->once()->andReturn($key);
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturn(1);
$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);
$this->assertTrue(true);
}
/**
* Test that a daemon key can be deleted when only a server ID is passed.
*/
public function testKeyIsDeletedIfIdIsPassedInPlaceOfModel()
{
$server = factory(Server::class)->make();
$key = factory(DaemonKey::class)->make();
$this->serverRepository->shouldReceive('find')->with($server->id)->once()->andReturn($server);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('findFirstWhere')->with([
['user_id', '=', 100],
['server_id', '=', $server->id],
])->once()->andReturn($key);
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturn(1);
$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);
$this->assertTrue(true);
}
/**
* Test that an exception is properly handled if thrown by guzzle.
*/
public function testExceptionReturnedByGuzzleIsHandled()
{
$server = factory(Server::class)->make();
$key = factory(DaemonKey::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('findFirstWhere')->with([
['user_id', '=', 100],
['server_id', '=', $server->id],
])->once()->andReturn($key);
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturn(1);
$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();
try {
$this->service->handle($server, 100);
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DisplayException::class, $exception);
$this->assertEquals(trans('admin/server.exceptions.daemon_exception', [
'code' => 'E_CONN_REFUSED',
]), $exception->getMessage());
}
}
}

View file

@ -1,132 +0,0 @@
<?php
namespace Tests\Unit\Services\Servers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Server;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Services\Servers\DetailsModificationService;
use Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService;
use Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService;
class DetailsModificationServiceTest extends TestCase
{
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
private $connection;
/**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService|\Mockery\Mock
*/
private $keyCreationService;
/**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService|\Mockery\Mock
*/
private $keyDeletionService;
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository|\Mockery\Mock
*/
private $repository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->keyCreationService = m::mock(DaemonKeyCreationService::class);
$this->keyDeletionService = m::mock(DaemonKeyDeletionService::class);
$this->repository = m::mock(ServerRepository::class);
}
/**
* Test basic updating of core variables when a model is provided.
*/
public function testDetailsAreEdited()
{
$server = factory(Server::class)->make(['owner_id' => 1]);
$data = ['owner_id' => 1, 'name' => 'New Name', 'description' => 'New Description'];
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
$this->repository->shouldReceive('setFreshModel')->once()->with(false)->andReturnSelf();
$this->repository->shouldReceive('update')->once()->with($server->id, [
'external_id' => null,
'owner_id' => $data['owner_id'],
'name' => $data['name'],
'description' => $data['description'],
], true, true)->andReturn(true);
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
$response = $this->getService()->handle($server, $data);
$this->assertTrue($response);
}
/**
* Test that a model is returned if requested.
*/
public function testModelIsReturned()
{
$server = factory(Server::class)->make(['owner_id' => 1]);
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
$this->repository->shouldReceive('setFreshModel')->once()->with(true)->andReturnSelf();
$this->repository->shouldReceive('update')->once()->andReturn($server);
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
$response = $this->getService()->returnUpdatedModel()->handle($server, ['owner_id' => 1]);
$this->assertInstanceOf(Server::class, $response);
}
/**
* Test that the daemon secret is reset if the owner id changes.
*/
public function testEditShouldResetDaemonSecretIfOwnerIdIsChanged()
{
$server = factory(Server::class)->make([
'owner_id' => 1,
]);
$data = ['owner_id' => 2, 'name' => 'New Name', 'description' => 'New Description', 'external_id' => 'abcd1234'];
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
$this->repository->shouldReceive('setFreshModel')->once()->with(false)->andReturnSelf();
$this->repository->shouldReceive('update')->once()->with($server->id, [
'external_id' => 'abcd1234',
'owner_id' => $data['owner_id'],
'name' => $data['name'],
'description' => $data['description'],
], true, true)->andReturn(true);
$this->keyDeletionService->shouldReceive('handle')->once()->with($server, $server->owner_id)->andReturnNull();
$this->keyCreationService->shouldReceive('handle')->once()->with($server->id, $data['owner_id'])->andReturnNull();
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
$response = $this->getService()->handle($server, $data);
$this->assertTrue($response);
}
/**
* Return an instance of the service with mocked dependencies for testing.
*
* @return \Pterodactyl\Services\Servers\DetailsModificationService
*/
private function getService(): DetailsModificationService
{
return new DetailsModificationService(
$this->connection,
$this->keyCreationService,
$this->keyDeletionService,
$this->repository
);
}
}

View file

@ -1,74 +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\Subusers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Subuser;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Subusers\SubuserDeletionService;
use Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
class SubuserDeletionServiceTest extends TestCase
{
/**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
private $connection;
/**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService|\Mockery\Mock
*/
private $keyDeletionService;
/**
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
*/
private $repository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->connection = m::mock(ConnectionInterface::class);
$this->keyDeletionService = m::mock(DaemonKeyDeletionService::class);
$this->repository = m::mock(SubuserRepositoryInterface::class);
}
/**
* Test that a subuser is deleted correctly.
*/
public function testSubuserIsDeleted()
{
$subuser = factory(Subuser::class)->make();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->keyDeletionService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id)->once()->andReturnNull();
$this->repository->shouldReceive('delete')->with($subuser->id)->once()->andReturn(1);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->getService()->handle($subuser);
$this->assertTrue(true);
}
/**
* Return an instance of the service with mocked dependencies for testing.
*
* @return \Pterodactyl\Services\Subusers\SubuserDeletionService
*/
private function getService(): SubuserDeletionService
{
return new SubuserDeletionService($this->connection, $this->keyDeletionService, $this->repository);
}
}