misc_pterodactyl-panel/tests/Unit/Services/Subusers/SubuserUpdateServiceTest.php

148 lines
5.8 KiB
PHP
Raw Normal View History

<?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
*/
namespace Tests\Unit\Services\Subusers;
use Mockery as m;
2017-08-27 19:55:25 +00:00
use Tests\TestCase;
use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response;
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;
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;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class SubuserUpdateServiceTest extends TestCase
{
2017-11-03 21:52:18 +00:00
use MocksRequestException;
/**
2017-09-25 03:28:16 +00:00
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
*/
2017-11-03 21:52:18 +00:00
private $connection;
/**
2017-09-25 03:28:16 +00:00
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
*/
2017-11-03 21:52:18 +00:00
private $daemonRepository;
/**
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-11-03 21:52:18 +00:00
private $permissionRepository;
/**
2017-09-25 03:28:16 +00:00
* @var \Pterodactyl\Services\Subusers\PermissionCreationService|\Mockery\Mock
*/
2017-11-03 21:52:18 +00:00
private $permissionService;
/**
2017-09-25 03:28:16 +00:00
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
*/
2017-11-03 21:52:18 +00:00
private $repository;
/**
* Setup tests.
*/
public function setUp()
{
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);
$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());
$subuser->setRelation('user', factory(User::class)->make());
$this->repository->shouldReceive('loadServerAndUserRelations')->with($subuser)->once()->andReturn($subuser);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$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();
$this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setServer')->with($subuser->server)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturn(new Response);
$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);
}
/**
* Test that an exception is thrown if the daemon connection fails.
*/
public function testExceptionIsThrownIfDaemonConnectionFails()
{
2017-11-03 21:52:18 +00:00
$this->configureExceptionMock();
$subuser = factory(Subuser::class)->make();
2017-11-03 21:52:18 +00:00
$subuser->setRelation('server', factory(Server::class)->make());
$subuser->setRelation('user', factory(User::class)->make());
$this->repository->shouldReceive('loadServerAndUserRelations')->with($subuser)->once()->andReturn($subuser);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$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();
$this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setServer')->with($subuser->server)->once()->andThrow($this->getExceptionMock());
$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-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
);
}
}