2017-08-13 19:55:09 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Servers;
|
|
|
|
|
|
|
|
use Exception;
|
|
|
|
use Mockery as m;
|
2017-08-16 04:21:01 +00:00
|
|
|
use Tests\TestCase;
|
|
|
|
use Illuminate\Log\Writer;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2017-08-13 19:55:09 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2017-08-27 20:10:51 +00:00
|
|
|
use Pterodactyl\Services\Servers\ServerDeletionService;
|
2017-10-19 03:32:19 +00:00
|
|
|
use Pterodactyl\Services\Databases\DatabaseManagementService;
|
2017-08-16 04:21:01 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class ServerDeletionServiceTest extends TestCase
|
2017-08-13 19:55:09 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $daemonServerRepository;
|
|
|
|
|
|
|
|
/**
|
2017-10-19 03:32:19 +00:00
|
|
|
* @var \Pterodactyl\Services\Databases\DatabaseManagementService
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
|
|
|
protected $databaseManagementService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $databaseRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \GuzzleHttp\Exception\RequestException
|
|
|
|
*/
|
|
|
|
protected $exception;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Models\Server
|
|
|
|
*/
|
|
|
|
protected $model;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
2017-08-27 20:10:51 +00:00
|
|
|
* @var \Pterodactyl\Services\Servers\ServerDeletionService
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Log\Writer
|
|
|
|
*/
|
|
|
|
protected $writer;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->connection = m::mock(ConnectionInterface::class);
|
|
|
|
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
|
|
|
|
$this->databaseRepository = m::mock(DatabaseRepositoryInterface::class);
|
|
|
|
$this->databaseManagementService = m::mock(DatabaseManagementService::class);
|
|
|
|
$this->exception = m::mock(RequestException::class);
|
|
|
|
$this->model = factory(Server::class)->make();
|
|
|
|
$this->repository = m::mock(ServerRepositoryInterface::class);
|
|
|
|
$this->writer = m::mock(Writer::class);
|
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
$this->service = new ServerDeletionService(
|
2017-08-13 19:55:09 +00:00
|
|
|
$this->connection,
|
|
|
|
$this->daemonServerRepository,
|
|
|
|
$this->databaseRepository,
|
|
|
|
$this->databaseManagementService,
|
|
|
|
$this->repository,
|
|
|
|
$this->writer
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a server can be force deleted by setting it in a function call.
|
|
|
|
*/
|
|
|
|
public function testForceParameterCanBeSet()
|
|
|
|
{
|
|
|
|
$response = $this->service->withForce(true);
|
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
$this->assertInstanceOf(ServerDeletionService::class, $response);
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a server can be deleted when force is not set.
|
|
|
|
*/
|
|
|
|
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->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->databaseRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
|
|
|
|
->shouldReceive('findWhere')->with([
|
|
|
|
['server_id', '=', $this->model->id],
|
|
|
|
])->once()->andReturn(collect([(object) ['id' => 50]]));
|
|
|
|
|
|
|
|
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturnNull();
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->service->handle($this->model);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a server is deleted when force is set.
|
|
|
|
*/
|
|
|
|
public function testServerShouldBeDeletedEvenWhenFailureOccursIfForceIsSet()
|
|
|
|
{
|
|
|
|
$this->daemonServerRepository->shouldReceive('setNode')->with($this->model->node_id)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('setAccessServer')->with($this->model->uuid)->once()->andReturnSelf()
|
|
|
|
->shouldReceive('delete')->withNoArgs()->once()->andThrow($this->exception);
|
|
|
|
|
|
|
|
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
|
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->databaseRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
|
|
|
|
->shouldReceive('findWhere')->with([
|
|
|
|
['server_id', '=', $this->model->id],
|
|
|
|
])->once()->andReturn(collect([(object) ['id' => 50]]));
|
|
|
|
|
|
|
|
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturnNull();
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->service->withForce()->handle($this->model);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if a server cannot be deleted from the node and force is not set.
|
|
|
|
*/
|
|
|
|
public function testExceptionShouldBeThrownIfDaemonReturnsAnErrorAndForceIsNotSet()
|
|
|
|
{
|
|
|
|
$this->daemonServerRepository->shouldReceive('setNode->setAccessServer->delete')->once()->andThrow($this->exception);
|
|
|
|
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->service->handle($this->model);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->assertInstanceOf(DisplayException::class, $exception);
|
|
|
|
$this->assertEquals(trans('admin/server.exceptions.daemon_exception', [
|
|
|
|
'code' => 'E_CONN_REFUSED',
|
|
|
|
]), $exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an integer can be passed in place of the Server model.
|
|
|
|
*/
|
|
|
|
public function testIntegerCanBePassedInPlaceOfServerModel()
|
|
|
|
{
|
|
|
|
$this->repository->shouldReceive('withColumns')->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()->andReturnNull();
|
|
|
|
|
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
|
|
|
$this->databaseRepository->shouldReceive('withColumns')->with('id')->once()->andReturnSelf()
|
|
|
|
->shouldReceive('findWhere')->with([
|
|
|
|
['server_id', '=', $this->model->id],
|
|
|
|
])->once()->andReturn(collect([(object) ['id' => 50]]));
|
|
|
|
|
|
|
|
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('delete')->with($this->model->id)->once()->andReturnNull();
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
$this->service->handle($this->model->id);
|
|
|
|
}
|
|
|
|
}
|