Fix subuser unit tests

This commit is contained in:
Dane Everitt 2017-11-03 16:52:18 -05:00
parent 54228e8124
commit 133fd17da6
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
3 changed files with 57 additions and 89 deletions

View file

@ -19,17 +19,17 @@ class SubuserDeletionService
/** /**
* @var \Illuminate\Database\ConnectionInterface * @var \Illuminate\Database\ConnectionInterface
*/ */
protected $connection; private $connection;
/** /**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService * @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService
*/ */
protected $keyDeletionService; private $keyDeletionService;
/** /**
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface * @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface
*/ */
protected $repository; private $repository;
/** /**
* SubuserDeletionService constructor. * SubuserDeletionService constructor.

View file

@ -22,22 +22,17 @@ class SubuserDeletionServiceTest extends TestCase
/** /**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock * @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/ */
protected $connection; private $connection;
/** /**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService|\Mockery\Mock * @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService|\Mockery\Mock
*/ */
protected $keyDeletionService; private $keyDeletionService;
/** /**
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock * @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
*/ */
protected $repository; private $repository;
/**
* @var \Pterodactyl\Services\Subusers\SubuserDeletionService
*/
protected $service;
/** /**
* Setup tests. * Setup tests.
@ -49,18 +44,12 @@ class SubuserDeletionServiceTest extends TestCase
$this->connection = m::mock(ConnectionInterface::class); $this->connection = m::mock(ConnectionInterface::class);
$this->keyDeletionService = m::mock(DaemonKeyDeletionService::class); $this->keyDeletionService = m::mock(DaemonKeyDeletionService::class);
$this->repository = m::mock(SubuserRepositoryInterface::class); $this->repository = m::mock(SubuserRepositoryInterface::class);
$this->service = new SubuserDeletionService(
$this->connection,
$this->keyDeletionService,
$this->repository
);
} }
/** /**
* Test that a subuser is deleted correctly. * Test that a subuser is deleted correctly.
*/ */
public function testSubuserIsDeletedIfModelIsPassed() public function testSubuserIsDeleted()
{ {
$subuser = factory(Subuser::class)->make(); $subuser = factory(Subuser::class)->make();
@ -69,24 +58,17 @@ class SubuserDeletionServiceTest extends TestCase
$this->repository->shouldReceive('delete')->with($subuser->id)->once()->andReturnNull(); $this->repository->shouldReceive('delete')->with($subuser->id)->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($subuser); $this->getService()->handle($subuser);
$this->assertTrue(true); $this->assertTrue(true);
} }
/** /**
* Test that a subuser is deleted correctly if only the subuser ID is passed. * Return an instance of the service with mocked dependencies for testing.
*
* @return \Pterodactyl\Services\Subusers\SubuserDeletionService
*/ */
public function testSubuserIsDeletedIfIdPassedInPlaceOfModel() private function getService(): SubuserDeletionService
{ {
$subuser = factory(Subuser::class)->make(); return new SubuserDeletionService($this->connection, $this->keyDeletionService, $this->repository);
$this->repository->shouldReceive('find')->with($subuser->id)->once()->andReturn($subuser);
$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()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($subuser->id);
$this->assertTrue(true);
} }
} }

View file

@ -11,36 +11,33 @@ namespace Tests\Unit\Services\Subusers;
use Mockery as m; use Mockery as m;
use Tests\TestCase; use Tests\TestCase;
use Illuminate\Log\Writer;
use Pterodactyl\Models\Server; use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser; use Pterodactyl\Models\Subuser;
use Tests\Traits\MocksRequestException;
use GuzzleHttp\Exception\RequestException; use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface; use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\PterodactylException; use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Services\Subusers\SubuserUpdateService; use Pterodactyl\Services\Subusers\SubuserUpdateService;
use Pterodactyl\Services\Subusers\PermissionCreationService; use Pterodactyl\Services\Subusers\PermissionCreationService;
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService; use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface; use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
use Pterodactyl\Contracts\Repository\PermissionRepositoryInterface; use Pterodactyl\Contracts\Repository\PermissionRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class SubuserUpdateServiceTest extends TestCase class SubuserUpdateServiceTest extends TestCase
{ {
use MocksRequestException;
/** /**
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock * @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/ */
protected $connection; private $connection;
/** /**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock * @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
*/ */
protected $daemonRepository; private $daemonRepository;
/**
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
*/
protected $exception;
/** /**
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock * @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock
@ -50,27 +47,17 @@ class SubuserUpdateServiceTest extends TestCase
/** /**
* @var \Pterodactyl\Contracts\Repository\PermissionRepositoryInterface|\Mockery\Mock * @var \Pterodactyl\Contracts\Repository\PermissionRepositoryInterface|\Mockery\Mock
*/ */
protected $permissionRepository; private $permissionRepository;
/** /**
* @var \Pterodactyl\Services\Subusers\PermissionCreationService|\Mockery\Mock * @var \Pterodactyl\Services\Subusers\PermissionCreationService|\Mockery\Mock
*/ */
protected $permissionService; private $permissionService;
/** /**
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock * @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
*/ */
protected $repository; private $repository;
/**
* @var \Pterodactyl\Services\Subusers\SubuserUpdateService
*/
protected $service;
/**
* @var \Illuminate\Log\Writer|\Mockery\Mock
*/
protected $writer;
/** /**
* Setup tests. * Setup tests.
@ -81,22 +68,10 @@ class SubuserUpdateServiceTest extends TestCase
$this->connection = m::mock(ConnectionInterface::class); $this->connection = m::mock(ConnectionInterface::class);
$this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class); $this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class);
$this->exception = m::mock(RequestException::class);
$this->keyProviderService = m::mock(DaemonKeyProviderService::class); $this->keyProviderService = m::mock(DaemonKeyProviderService::class);
$this->permissionRepository = m::mock(PermissionRepositoryInterface::class); $this->permissionRepository = m::mock(PermissionRepositoryInterface::class);
$this->permissionService = m::mock(PermissionCreationService::class); $this->permissionService = m::mock(PermissionCreationService::class);
$this->repository = m::mock(SubuserRepositoryInterface::class); $this->repository = m::mock(SubuserRepositoryInterface::class);
$this->writer = m::mock(Writer::class);
$this->service = new SubuserUpdateService(
$this->connection,
$this->keyProviderService,
$this->daemonRepository,
$this->permissionService,
$this->permissionRepository,
$this->repository,
$this->writer
);
} }
/** /**
@ -105,22 +80,20 @@ class SubuserUpdateServiceTest extends TestCase
public function testPermissionsAreUpdated() public function testPermissionsAreUpdated()
{ {
$subuser = factory(Subuser::class)->make(); $subuser = factory(Subuser::class)->make();
$subuser->server = factory(Server::class)->make(); $subuser->setRelation('server', factory(Server::class)->make());
$this->repository->shouldReceive('getWithServer')->with($subuser->id)->once()->andReturn($subuser); $this->repository->shouldReceive('getWithServer')->with($subuser)->once()->andReturn($subuser);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]]) $this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturnNull();
->once()->andReturnNull();
$this->permissionService->shouldReceive('handle')->with($subuser->id, ['some-permission'])->once()->andReturnNull(); $this->permissionService->shouldReceive('handle')->with($subuser->id, ['some-permission'])->once()->andReturnNull();
$this->keyProviderService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id, false) $this->keyProviderService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id, false)->once()->andReturn('test123');
->once()->andReturn('test123'); $this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andReturnSelf() $this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturnNull();
->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturnNull();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($subuser->id, ['some-permission']); $this->getService()->handle($subuser, ['some-permission']);
$this->assertTrue(true); $this->assertTrue(true);
} }
@ -129,29 +102,42 @@ class SubuserUpdateServiceTest extends TestCase
*/ */
public function testExceptionIsThrownIfDaemonConnectionFails() public function testExceptionIsThrownIfDaemonConnectionFails()
{ {
$subuser = factory(Subuser::class)->make(); $this->configureExceptionMock();
$subuser->server = factory(Server::class)->make();
$this->repository->shouldReceive('getWithServer')->with($subuser->id)->once()->andReturn($subuser); $subuser = factory(Subuser::class)->make();
$subuser->setRelation('server', factory(Server::class)->make());
$this->repository->shouldReceive('getWithServer')->with($subuser)->once()->andReturn($subuser);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]]) $this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturnNull();
->once()->andReturnNull();
$this->permissionService->shouldReceive('handle')->with($subuser->id, [])->once()->andReturnNull(); $this->permissionService->shouldReceive('handle')->with($subuser->id, [])->once()->andReturnNull();
$this->keyProviderService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id, false) $this->keyProviderService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id, false)->once()->andReturn('test123');
->once()->andReturn('test123'); $this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andThrow($this->getExceptionMock());
$this->daemonRepository->shouldReceive('setNode')->once()->andThrow($this->exception);
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull(); $this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
try { try {
$this->service->handle($subuser->id, []); $this->getService()->handle($subuser, []);
} catch (PterodactylException $exception) { } catch (PterodactylException $exception) {
$this->assertInstanceOf(DisplayException::class, $exception); $this->assertInstanceOf(DaemonConnectionException::class, $exception);
$this->assertEquals(trans('exceptions.daemon_connection_failed', [ $this->assertInstanceOf(RequestException::class, $exception->getPrevious());
'code' => 'E_CONN_REFUSED',
]), $exception->getMessage());
} }
} }
/**
* Return an instance of the service with mocked dependencies for testing.
*
* @return \Pterodactyl\Services\Subusers\SubuserUpdateService
*/
private function getService(): SubuserUpdateService
{
return new SubuserUpdateService(
$this->connection,
$this->keyProviderService,
$this->daemonRepository,
$this->permissionService,
$this->permissionRepository,
$this->repository
);
}
} }