Fix subuser unit tests
This commit is contained in:
parent
54228e8124
commit
133fd17da6
3 changed files with 57 additions and 89 deletions
|
@ -19,17 +19,17 @@ class SubuserDeletionService
|
|||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface
|
||||
*/
|
||||
protected $connection;
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService
|
||||
*/
|
||||
protected $keyDeletionService;
|
||||
private $keyDeletionService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* SubuserDeletionService constructor.
|
||||
|
|
|
@ -22,22 +22,17 @@ class SubuserDeletionServiceTest extends TestCase
|
|||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $connection;
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyDeletionService|\Mockery\Mock
|
||||
*/
|
||||
protected $keyDeletionService;
|
||||
private $keyDeletionService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Subusers\SubuserDeletionService
|
||||
*/
|
||||
protected $service;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
|
@ -49,18 +44,12 @@ class SubuserDeletionServiceTest extends TestCase
|
|||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->keyDeletionService = m::mock(DaemonKeyDeletionService::class);
|
||||
$this->repository = m::mock(SubuserRepositoryInterface::class);
|
||||
|
||||
$this->service = new SubuserDeletionService(
|
||||
$this->connection,
|
||||
$this->keyDeletionService,
|
||||
$this->repository
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a subuser is deleted correctly.
|
||||
*/
|
||||
public function testSubuserIsDeletedIfModelIsPassed()
|
||||
public function testSubuserIsDeleted()
|
||||
{
|
||||
$subuser = factory(Subuser::class)->make();
|
||||
|
||||
|
@ -69,24 +58,17 @@ class SubuserDeletionServiceTest extends TestCase
|
|||
$this->repository->shouldReceive('delete')->with($subuser->id)->once()->andReturnNull();
|
||||
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$this->service->handle($subuser);
|
||||
$this->getService()->handle($subuser);
|
||||
$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();
|
||||
|
||||
$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);
|
||||
return new SubuserDeletionService($this->connection, $this->keyDeletionService, $this->repository);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,36 +11,33 @@ namespace Tests\Unit\Services\Subusers;
|
|||
|
||||
use Mockery as m;
|
||||
use Tests\TestCase;
|
||||
use Illuminate\Log\Writer;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Models\Subuser;
|
||||
use Tests\Traits\MocksRequestException;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
use Illuminate\Database\ConnectionInterface;
|
||||
use Pterodactyl\Exceptions\DisplayException;
|
||||
use Pterodactyl\Exceptions\PterodactylException;
|
||||
use Pterodactyl\Services\Subusers\SubuserUpdateService;
|
||||
use Pterodactyl\Services\Subusers\PermissionCreationService;
|
||||
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
||||
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\PermissionRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
||||
|
||||
class SubuserUpdateServiceTest extends TestCase
|
||||
{
|
||||
use MocksRequestException;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $connection;
|
||||
private $connection;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $daemonRepository;
|
||||
|
||||
/**
|
||||
* @var \GuzzleHttp\Exception\RequestException|\Mockery\Mock
|
||||
*/
|
||||
protected $exception;
|
||||
private $daemonRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock
|
||||
|
@ -50,27 +47,17 @@ class SubuserUpdateServiceTest extends TestCase
|
|||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\PermissionRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $permissionRepository;
|
||||
private $permissionRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Subusers\PermissionCreationService|\Mockery\Mock
|
||||
*/
|
||||
protected $permissionService;
|
||||
private $permissionService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Subusers\SubuserUpdateService
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Log\Writer|\Mockery\Mock
|
||||
*/
|
||||
protected $writer;
|
||||
private $repository;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
|
@ -81,22 +68,10 @@ class SubuserUpdateServiceTest extends TestCase
|
|||
|
||||
$this->connection = m::mock(ConnectionInterface::class);
|
||||
$this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class);
|
||||
$this->exception = m::mock(RequestException::class);
|
||||
$this->keyProviderService = m::mock(DaemonKeyProviderService::class);
|
||||
$this->permissionRepository = m::mock(PermissionRepositoryInterface::class);
|
||||
$this->permissionService = m::mock(PermissionCreationService::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()
|
||||
{
|
||||
$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->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])
|
||||
->once()->andReturnNull();
|
||||
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->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)
|
||||
->once()->andReturn('test123');
|
||||
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andReturnSelf()
|
||||
->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturnNull();
|
||||
$this->keyProviderService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id, false)->once()->andReturn('test123');
|
||||
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andReturnSelf();
|
||||
$this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->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);
|
||||
}
|
||||
|
||||
|
@ -129,29 +102,42 @@ class SubuserUpdateServiceTest extends TestCase
|
|||
*/
|
||||
public function testExceptionIsThrownIfDaemonConnectionFails()
|
||||
{
|
||||
$subuser = factory(Subuser::class)->make();
|
||||
$subuser->server = factory(Server::class)->make();
|
||||
$this->configureExceptionMock();
|
||||
|
||||
$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->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])
|
||||
->once()->andReturnNull();
|
||||
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $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)
|
||||
->once()->andReturn('test123');
|
||||
$this->daemonRepository->shouldReceive('setNode')->once()->andThrow($this->exception);
|
||||
$this->keyProviderService->shouldReceive('handle')->with($subuser->server_id, $subuser->user_id, false)->once()->andReturn('test123');
|
||||
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andThrow($this->getExceptionMock());
|
||||
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
|
||||
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
|
||||
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
try {
|
||||
$this->service->handle($subuser->id, []);
|
||||
$this->getService()->handle($subuser, []);
|
||||
} catch (PterodactylException $exception) {
|
||||
$this->assertInstanceOf(DisplayException::class, $exception);
|
||||
$this->assertEquals(trans('exceptions.daemon_connection_failed', [
|
||||
'code' => 'E_CONN_REFUSED',
|
||||
]), $exception->getMessage());
|
||||
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
|
||||
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue