2017-08-26 23:08:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
|
|
|
*
|
2017-09-26 02:43:01 +00:00
|
|
|
* This software is licensed under the terms of the MIT license.
|
|
|
|
* https://opensource.org/licenses/MIT
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Subusers;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-08-27 19:55:25 +00:00
|
|
|
use Tests\TestCase;
|
2017-11-05 18:38:39 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2018-01-06 00:27:47 +00:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
2017-08-26 23:08:11 +00:00
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Models\Subuser;
|
2017-11-03 21:52:18 +00:00
|
|
|
use Tests\Traits\MocksRequestException;
|
2017-08-27 19:55:25 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2017-09-25 03:28:16 +00:00
|
|
|
use Pterodactyl\Exceptions\PterodactylException;
|
2017-08-26 23:08:11 +00:00
|
|
|
use Pterodactyl\Services\Subusers\SubuserUpdateService;
|
2017-08-27 19:55:25 +00:00
|
|
|
use Pterodactyl\Services\Subusers\PermissionCreationService;
|
2017-09-25 03:28:16 +00:00
|
|
|
use Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService;
|
2017-08-27 19:55:25 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
|
|
|
|
use Pterodactyl\Contracts\Repository\PermissionRepositoryInterface;
|
2017-11-03 21:52:18 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2017-08-26 23:08:11 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
|
|
|
|
|
|
|
class SubuserUpdateServiceTest extends TestCase
|
|
|
|
{
|
2017-11-03 21:52:18 +00:00
|
|
|
use MocksRequestException;
|
|
|
|
|
2017-08-26 23:08:11 +00:00
|
|
|
/**
|
2017-09-25 03:28:16 +00:00
|
|
|
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2017-11-03 21:52:18 +00:00
|
|
|
private $connection;
|
2017-08-26 23:08:11 +00:00
|
|
|
|
|
|
|
/**
|
2017-09-25 03:28:16 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2017-11-03 21:52:18 +00:00
|
|
|
private $daemonRepository;
|
2017-08-26 23:08:11 +00:00
|
|
|
|
|
|
|
/**
|
2017-09-25 03:28:16 +00:00
|
|
|
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyProviderService|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private $keyProviderService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\PermissionRepositoryInterface|\Mockery\Mock
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2017-11-03 21:52:18 +00:00
|
|
|
private $permissionRepository;
|
2017-08-26 23:08:11 +00:00
|
|
|
|
|
|
|
/**
|
2017-09-25 03:28:16 +00:00
|
|
|
* @var \Pterodactyl\Services\Subusers\PermissionCreationService|\Mockery\Mock
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2017-11-03 21:52:18 +00:00
|
|
|
private $permissionService;
|
2017-08-26 23:08:11 +00:00
|
|
|
|
|
|
|
/**
|
2017-09-25 03:28:16 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2017-11-03 21:52:18 +00:00
|
|
|
private $repository;
|
2017-08-26 23:08:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
2020-05-09 16:00:52 +00:00
|
|
|
public function setUp(): void
|
2017-08-26 23:08:11 +00:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->connection = m::mock(ConnectionInterface::class);
|
|
|
|
$this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class);
|
2017-09-25 03:28:16 +00:00
|
|
|
$this->keyProviderService = m::mock(DaemonKeyProviderService::class);
|
2017-08-26 23:08:11 +00:00
|
|
|
$this->permissionRepository = m::mock(PermissionRepositoryInterface::class);
|
|
|
|
$this->permissionService = m::mock(PermissionCreationService::class);
|
|
|
|
$this->repository = m::mock(SubuserRepositoryInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that permissions are updated in the database.
|
|
|
|
*/
|
|
|
|
public function testPermissionsAreUpdated()
|
|
|
|
{
|
|
|
|
$subuser = factory(Subuser::class)->make();
|
2017-11-03 21:52:18 +00:00
|
|
|
$subuser->setRelation('server', factory(Server::class)->make());
|
2017-11-05 18:38:39 +00:00
|
|
|
$subuser->setRelation('user', factory(User::class)->make());
|
2017-08-26 23:08:11 +00:00
|
|
|
|
2017-11-05 18:38:39 +00:00
|
|
|
$this->repository->shouldReceive('loadServerAndUserRelations')->with($subuser)->once()->andReturn($subuser);
|
2017-08-26 23:08:11 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturn(1);
|
2017-09-25 03:28:16 +00:00
|
|
|
$this->permissionService->shouldReceive('handle')->with($subuser->id, ['some-permission'])->once()->andReturnNull();
|
2017-08-26 23:08:11 +00:00
|
|
|
|
2017-11-05 18:38:39 +00:00
|
|
|
$this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123');
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonRepository->shouldReceive('setServer')->with($subuser->server)->once()->andReturnSelf();
|
|
|
|
$this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturn(new Response);
|
2017-08-26 23:08:11 +00:00
|
|
|
|
|
|
|
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
2017-11-03 21:52:18 +00:00
|
|
|
$this->getService()->handle($subuser, ['some-permission']);
|
2017-09-25 03:28:16 +00:00
|
|
|
$this->assertTrue(true);
|
2017-08-26 23:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that an exception is thrown if the daemon connection fails.
|
|
|
|
*/
|
|
|
|
public function testExceptionIsThrownIfDaemonConnectionFails()
|
|
|
|
{
|
2017-11-03 21:52:18 +00:00
|
|
|
$this->configureExceptionMock();
|
|
|
|
|
2017-08-26 23:08:11 +00:00
|
|
|
$subuser = factory(Subuser::class)->make();
|
2017-11-03 21:52:18 +00:00
|
|
|
$subuser->setRelation('server', factory(Server::class)->make());
|
2017-11-05 18:38:39 +00:00
|
|
|
$subuser->setRelation('user', factory(User::class)->make());
|
2017-08-26 23:08:11 +00:00
|
|
|
|
2017-11-05 18:38:39 +00:00
|
|
|
$this->repository->shouldReceive('loadServerAndUserRelations')->with($subuser)->once()->andReturn($subuser);
|
2017-08-26 23:08:11 +00:00
|
|
|
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->permissionRepository->shouldReceive('deleteWhere')->with([['subuser_id', '=', $subuser->id]])->once()->andReturn(1);
|
2017-09-25 03:28:16 +00:00
|
|
|
$this->permissionService->shouldReceive('handle')->with($subuser->id, [])->once()->andReturnNull();
|
2017-08-26 23:08:11 +00:00
|
|
|
|
2017-11-05 18:38:39 +00:00
|
|
|
$this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123');
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->daemonRepository->shouldReceive('setServer')->with($subuser->server)->once()->andThrow($this->getExceptionMock());
|
2017-08-26 23:08:11 +00:00
|
|
|
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
|
|
|
|
|
|
|
|
try {
|
2017-11-03 21:52:18 +00:00
|
|
|
$this->getService()->handle($subuser, []);
|
2017-09-25 03:28:16 +00:00
|
|
|
} catch (PterodactylException $exception) {
|
2017-11-03 21:52:18 +00:00
|
|
|
$this->assertInstanceOf(DaemonConnectionException::class, $exception);
|
|
|
|
$this->assertInstanceOf(RequestException::class, $exception->getPrevious());
|
2017-08-26 23:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-03 21:52:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
);
|
|
|
|
}
|
2017-08-26 23:08:11 +00:00
|
|
|
}
|