Fix all currently failing tests

This commit is contained in:
Dane Everitt 2017-09-24 22:28:16 -05:00
parent 3a8bea9588
commit dd456a4c9c
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
11 changed files with 175 additions and 315 deletions

View file

@ -28,36 +28,46 @@ use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use phpmock\phpunit\PHPMock;
use Pterodactyl\Models\Server;
use Illuminate\Database\DatabaseManager;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Repositories\Eloquent\ServerRepository;
use Pterodactyl\Services\Servers\DetailsModificationService;
use Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService;
use Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService;
use Pterodactyl\Repositories\Daemon\ServerRepository as DaemonServerRepository;
class DetailsModificationServiceTest extends TestCase
{
use PHPMock;
/**
* @var \Illuminate\Database\DatabaseManager
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
protected $database;
protected $connection;
/**
* @var \Pterodactyl\Repositories\Daemon\ServerRepository
* @var \Pterodactyl\Repositories\Daemon\ServerRepository|\Mockery\Mock
*/
protected $daemonServerRepository;
/**
* @var \GuzzleHttp\Exception\RequestException
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
*/
protected $exception;
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService|\Mockery\Mock
*/
protected $keyCreationService;
/**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService|\Mockery\Mock
*/
protected $keyDeletionService;
/**
* @var \Pterodactyl\Repositories\Eloquent\ServerRepository|\Mockery\Mock
*/
protected $repository;
@ -67,7 +77,7 @@ class DetailsModificationServiceTest extends TestCase
protected $service;
/**
* @var \Illuminate\Log\Writer
* @var \Illuminate\Log\Writer|\Mockery\Mock
*/
protected $writer;
@ -78,17 +88,18 @@ class DetailsModificationServiceTest extends TestCase
{
parent::setUp();
$this->database = m::mock(DatabaseManager::class);
$this->connection = m::mock(ConnectionInterface::class);
$this->exception = m::mock(RequestException::class)->makePartial();
$this->daemonServerRepository = m::mock(DaemonServerRepository::class);
$this->keyCreationService = m::mock(DaemonKeyCreationService::class);
$this->keyDeletionService = m::mock(DaemonKeyDeletionService::class);
$this->repository = m::mock(ServerRepository::class);
$this->writer = m::mock(Writer::class);
$this->getFunctionMock('\\Pterodactyl\\Services\\Servers', 'str_random')
->expects($this->any())->willReturn('random_string');
$this->service = new DetailsModificationService(
$this->database,
$this->connection,
$this->keyCreationService,
$this->keyDeletionService,
$this->daemonServerRepository,
$this->repository,
$this->writer
@ -106,21 +117,18 @@ class DetailsModificationServiceTest extends TestCase
$data = ['owner_id' => 1, 'name' => 'New Name', 'description' => 'New Description'];
$this->repository->shouldNotReceive('find');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'owner_id' => $data['owner_id'],
'name' => $data['name'],
'description' => $data['description'],
'daemonSecret' => $server->daemonSecret,
], true, true)->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturn(true);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->edit($server, $data);
$this->assertTrue($response);
$this->service->edit($server, $data);
$this->assertTrue(true);
}
/**
@ -135,20 +143,18 @@ class DetailsModificationServiceTest extends TestCase
$data = ['owner_id' => 1, 'name' => 'New Name', 'description' => 'New Description'];
$this->repository->shouldReceive('find')->with($server->id)->once()->andReturn($server);
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'owner_id' => $data['owner_id'],
'name' => $data['name'],
'description' => $data['description'],
'daemonSecret' => $server->daemonSecret,
], true, true)->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturn(true);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->edit($server->id, $data);
$this->assertTrue($response);
$this->service->edit($server->id, $data);
$this->assertTrue(true);
}
/**
@ -163,135 +169,20 @@ class DetailsModificationServiceTest extends TestCase
$data = ['owner_id' => 2, 'name' => 'New Name', 'description' => 'New Description'];
$this->repository->shouldNotReceive('find');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'owner_id' => $data['owner_id'],
'name' => $data['name'],
'description' => $data['description'],
'daemonSecret' => 'random_string',
], true, true)->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('update')->with([
'keys' => [
$server->daemonSecret => [],
'random_string' => DaemonServerRepository::DAEMON_PERMISSIONS,
],
])->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturn(true);
$response = $this->service->edit($server, $data);
$this->assertTrue($response);
}
public function testEditShouldResetDaemonSecretIfBooleanValueIsPassed()
{
$server = factory(Server::class)->make([
'owner_id' => 1,
'node_id' => 1,
]);
$data = ['owner_id' => 1, 'name' => 'New Name', 'description' => 'New Description', 'reset_token' => true];
$this->repository->shouldNotReceive('find');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'owner_id' => $data['owner_id'],
'name' => $data['name'],
'description' => $data['description'],
'daemonSecret' => 'random_string',
], true, true)->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('update')->with([
'keys' => [
$server->daemonSecret => [],
'random_string' => DaemonServerRepository::DAEMON_PERMISSIONS,
],
])->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturn(true);
$response = $this->service->edit($server, $data);
$this->assertTrue($response);
}
/**
* Test that a displayable exception is thrown if the daemon responds with an error.
*/
public function testEditShouldThrowADisplayableExceptionIfDaemonResponseErrors()
{
$server = factory(Server::class)->make([
'owner_id' => 1,
'node_id' => 1,
]);
$data = ['owner_id' => 2, 'name' => 'New Name', 'description' => 'New Description'];
$this->repository->shouldNotReceive('find');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'owner_id' => $data['owner_id'],
'name' => $data['name'],
'description' => $data['description'],
'daemonSecret' => 'random_string',
], true, true)->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
try {
$this->service->edit($server, $data);
} catch (Exception $exception) {
$this->assertInstanceOf(DisplayException::class, $exception);
$this->assertEquals(
trans('admin/server.exceptions.daemon_exception', ['code' => 400]),
$exception->getMessage()
);
}
}
/**
* Test that an exception not stemming from Guzzle is not thrown as a displayable exception.
*
* @expectedException \Exception
*/
public function testEditShouldNotThrowDisplayableExceptionIfExceptionIsNotThrownByGuzzle()
{
$server = factory(Server::class)->make([
'owner_id' => 1,
'node_id' => 1,
]);
$data = ['owner_id' => 2, 'name' => 'New Name', 'description' => 'New Description'];
$this->repository->shouldNotReceive('find');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'owner_id' => $data['owner_id'],
'name' => $data['name'],
'description' => $data['description'],
'daemonSecret' => 'random_string',
], true, true)->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->andThrow(new Exception());
$this->keyDeletionService->shouldReceive('handle')->with($server, $server->owner_id)->once()->andReturnNull();
$this->keyCreationService->shouldReceive('handle')->with($server->id, $data['owner_id']);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->edit($server, $data);
$this->assertTrue(true);
}
/**
@ -301,8 +192,7 @@ class DetailsModificationServiceTest extends TestCase
{
$server = factory(Server::class)->make(['node_id' => 1]);
$this->repository->shouldNotReceive('find');
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'image' => 'new/image',
@ -316,9 +206,10 @@ class DetailsModificationServiceTest extends TestCase
],
])->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturn(true);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->setDockerImage($server, 'new/image');
$this->assertTrue(true);
}
/**
@ -329,7 +220,7 @@ class DetailsModificationServiceTest extends TestCase
$server = factory(Server::class)->make(['node_id' => 1]);
$this->repository->shouldReceive('find')->with($server->id)->once()->andReturn($server);
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'image' => 'new/image',
@ -343,9 +234,10 @@ class DetailsModificationServiceTest extends TestCase
],
])->once()->andReturnNull();
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturn(true);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->setDockerImage($server->id, 'new/image');
$this->assertTrue(true);
}
/**
@ -355,13 +247,14 @@ class DetailsModificationServiceTest extends TestCase
{
$server = factory(Server::class)->make(['node_id' => 1]);
$this->database->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('withoutFresh')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($server->id, [
'image' => 'new/image',
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->andThrow($this->exception);
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
@ -369,7 +262,7 @@ class DetailsModificationServiceTest extends TestCase
try {
$this->service->setDockerImage($server, 'new/image');
} catch (Exception $exception) {
} catch (PterodactylException $exception) {
$this->assertInstanceOf(DisplayException::class, $exception);
$this->assertEquals(
trans('admin/server.exceptions.daemon_exception', ['code' => 400]),