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