2017-08-13 19:55:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Servers;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-08-16 04:21:01 +00:00
|
|
|
use Tests\TestCase;
|
2018-01-06 00:27:47 +00:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
2017-08-16 04:21:01 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
2018-05-13 21:41:01 +00:00
|
|
|
use Psr\Log\LoggerInterface as Writer;
|
2018-01-28 23:14:14 +00:00
|
|
|
use Tests\Traits\MocksRequestException;
|
2017-08-13 19:55:09 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
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
|
|
|
{
|
2018-01-28 23:14:14 +00:00
|
|
|
use MocksRequestException;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-28 23:14:14 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2018-01-28 23:14:14 +00:00
|
|
|
private $connection;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-28 23:14:14 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2018-01-28 23:14:14 +00:00
|
|
|
private $daemonServerRepository;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-28 23:14:14 +00:00
|
|
|
* @var \Pterodactyl\Services\Databases\DatabaseManagementService|\Mockery\Mock
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2018-01-28 23:14:14 +00:00
|
|
|
private $databaseManagementService;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-28 23:14:14 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface|\Mockery\Mock
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2018-01-28 23:14:14 +00:00
|
|
|
private $databaseRepository;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-28 23:14:14 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2018-01-28 23:14:14 +00:00
|
|
|
private $repository;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
/**
|
2018-05-13 20:40:31 +00:00
|
|
|
* @var \Psr\Log\LoggerInterface|\Mockery\Mock
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2018-01-28 23:14:14 +00:00
|
|
|
private $writer;
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-08-13 19:55:09 +00:00
|
|
|
{
|
|
|
|
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->repository = m::mock(ServerRepositoryInterface::class);
|
|
|
|
$this->writer = m::mock(Writer::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a server can be force deleted by setting it in a function call.
|
|
|
|
*/
|
|
|
|
public function testForceParameterCanBeSet()
|
|
|
|
{
|
2018-01-28 23:14:14 +00:00
|
|
|
$response = $this->getService()->withForce(true);
|
2017-08-13 19:55:09 +00:00
|
|
|
|
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()
|
|
|
|
{
|
2018-01-28 23:14:14 +00:00
|
|
|
$model = factory(Server::class)->make();
|
2017-08-13 19:55:09 +00:00
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setServer')->once()->with($model)->andReturnSelf();
|
|
|
|
$this->daemonServerRepository->shouldReceive('delete')->once()->withNoArgs()->andReturn(new Response);
|
2017-08-13 19:55:09 +00:00
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->once()->withNoArgs()->andReturnNull();
|
|
|
|
$this->databaseRepository->shouldReceive('setColumns')->once()->with('id')->andReturnSelf();
|
|
|
|
$this->databaseRepository->shouldReceive('findWhere')->once()->with([
|
|
|
|
['server_id', '=', $model->id],
|
|
|
|
])->andReturn(collect([(object) ['id' => 50]]));
|
2017-08-13 19:55:09 +00:00
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->databaseManagementService->shouldReceive('delete')->once()->with(50)->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('delete')->once()->with($model->id)->andReturn(1);
|
|
|
|
$this->connection->shouldReceive('commit')->once()->withNoArgs()->andReturnNull();
|
|
|
|
|
|
|
|
$this->getService()->handle($model);
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a server is deleted when force is set.
|
|
|
|
*/
|
|
|
|
public function testServerShouldBeDeletedEvenWhenFailureOccursIfForceIsSet()
|
|
|
|
{
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->configureExceptionMock();
|
|
|
|
$model = factory(Server::class)->make();
|
|
|
|
|
|
|
|
$this->daemonServerRepository->shouldReceive('setServer')->once()->with($model)->andReturnSelf();
|
|
|
|
$this->daemonServerRepository->shouldReceive('delete')->once()->withNoArgs()->andThrow($this->getExceptionMock());
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
|
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf();
|
|
|
|
$this->databaseRepository->shouldReceive('findWhere')->with([
|
|
|
|
['server_id', '=', $model->id],
|
|
|
|
])->once()->andReturn(collect([(object) ['id' => 50]]));
|
2017-08-13 19:55:09 +00:00
|
|
|
|
|
|
|
$this->databaseManagementService->shouldReceive('delete')->with(50)->once()->andReturnNull();
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->repository->shouldReceive('delete')->with($model->id)->once()->andReturn(1);
|
2017-08-13 19:55:09 +00:00
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->getService()->withForce()->handle($model);
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if a server cannot be deleted from the node and force is not set.
|
2018-01-28 23:14:14 +00:00
|
|
|
*
|
|
|
|
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
|
|
|
public function testExceptionShouldBeThrownIfDaemonReturnsAnErrorAndForceIsNotSet()
|
|
|
|
{
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->configureExceptionMock();
|
|
|
|
$model = factory(Server::class)->make();
|
2017-08-13 19:55:09 +00:00
|
|
|
|
2018-01-28 23:14:14 +00:00
|
|
|
$this->daemonServerRepository->shouldReceive('setServer->delete')->once()->andThrow($this->getExceptionMock());
|
|
|
|
|
|
|
|
$this->getService()->handle($model);
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-28 23:14:14 +00:00
|
|
|
* Return an instance of the class with mocked dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Servers\ServerDeletionService
|
2017-08-13 19:55:09 +00:00
|
|
|
*/
|
2018-01-28 23:14:14 +00:00
|
|
|
private function getService(): ServerDeletionService
|
2017-08-13 19:55:09 +00:00
|
|
|
{
|
2018-01-28 23:14:14 +00:00
|
|
|
return new ServerDeletionService(
|
|
|
|
$this->connection,
|
|
|
|
$this->daemonServerRepository,
|
|
|
|
$this->databaseRepository,
|
|
|
|
$this->databaseManagementService,
|
|
|
|
$this->repository,
|
|
|
|
$this->writer
|
|
|
|
);
|
2017-08-13 19:55:09 +00:00
|
|
|
}
|
|
|
|
}
|