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

205 lines
8.3 KiB
PHP
Raw Normal View History

2017-08-24 02:34: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-24 02:34:11 +00:00
*/
namespace Tests\Unit\Services\Subusers;
use Mockery as m;
2017-08-27 19:55:25 +00:00
use Tests\TestCase;
2017-08-24 02:34:11 +00:00
use phpmock\phpunit\PHPMock;
2017-08-27 19:55:25 +00:00
use Pterodactyl\Models\User;
2017-08-24 02:34:11 +00:00
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser;
2017-08-27 19:55:25 +00:00
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Users\UserCreationService;
2017-08-27 19:55:25 +00:00
use Pterodactyl\Services\Subusers\SubuserCreationService;
use Pterodactyl\Services\Subusers\PermissionCreationService;
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
2017-09-25 03:28:16 +00:00
use Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService;
use Pterodactyl\Exceptions\Repository\RecordNotFoundException;
2017-08-27 19:55:25 +00:00
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
use Pterodactyl\Exceptions\Service\Subuser\UserIsServerOwnerException;
use Pterodactyl\Exceptions\Service\Subuser\ServerSubuserExistsException;
2017-08-24 02:34:11 +00:00
class SubuserCreationServiceTest extends TestCase
{
use PHPMock;
/**
2017-09-25 03:28:16 +00:00
* @var \Illuminate\Database\ConnectionInterface|\Mockery\Mock
2017-08-24 02:34:11 +00:00
*/
protected $connection;
/**
2017-09-25 03:28:16 +00:00
* @var \Pterodactyl\Services\DaemonKeys\DaemonKeyCreationService|\Mockery\Mock
2017-08-24 02:34:11 +00:00
*/
2017-09-25 03:28:16 +00:00
protected $keyCreationService;
2017-08-24 02:34:11 +00:00
/**
2017-09-25 03:28:16 +00:00
* @var \Pterodactyl\Services\Subusers\PermissionCreationService|\Mockery\Mock
2017-08-24 02:34:11 +00:00
*/
protected $permissionService;
2017-08-24 02:34:11 +00:00
/**
2017-09-25 03:28:16 +00:00
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
2017-08-24 02:34:11 +00:00
*/
protected $subuserRepository;
/**
2017-09-25 03:28:16 +00:00
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface|\Mockery\Mock
2017-08-24 02:34:11 +00:00
*/
protected $serverRepository;
/**
* @var \Pterodactyl\Services\Subusers\SubuserCreationService
*/
protected $service;
/**
2017-09-25 03:28:16 +00:00
* @var \Pterodactyl\Services\Users\UserCreationService|\Mockery\Mock
2017-08-24 02:34:11 +00:00
*/
protected $userCreationService;
/**
2017-09-25 03:28:16 +00:00
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
2017-08-24 02:34:11 +00:00
*/
protected $userRepository;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->getFunctionMock('\\Pterodactyl\\Services\\Subusers', 'str_random')->expects($this->any())->willReturn('random_string');
2017-08-24 02:34:11 +00:00
$this->connection = m::mock(ConnectionInterface::class);
2017-09-25 03:28:16 +00:00
$this->keyCreationService = m::mock(DaemonKeyCreationService::class);
$this->permissionService = m::mock(PermissionCreationService::class);
2017-08-24 02:34:11 +00:00
$this->subuserRepository = m::mock(SubuserRepositoryInterface::class);
$this->serverRepository = m::mock(ServerRepositoryInterface::class);
$this->userCreationService = m::mock(UserCreationService::class);
2017-08-24 02:34:11 +00:00
$this->userRepository = m::mock(UserRepositoryInterface::class);
$this->service = new SubuserCreationService(
$this->connection,
2017-09-25 03:28:16 +00:00
$this->keyCreationService,
$this->permissionService,
2017-08-24 02:34:11 +00:00
$this->serverRepository,
$this->subuserRepository,
2017-09-25 03:28:16 +00:00
$this->userCreationService,
$this->userRepository
2017-08-24 02:34:11 +00:00
);
}
/**
* Test that a user without an existing account can be added as a subuser.
*/
public function testAccountIsCreatedForNewUser()
{
$permissions = ['test-1' => 'test:1', 'test-2' => null];
$server = factory(Server::class)->make();
$user = factory(User::class)->make();
$subuser = factory(Subuser::class)->make(['user_id' => $user->id, 'server_id' => $server->id]);
2017-09-04 23:12:13 +00:00
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->userRepository->shouldReceive('findFirstWhere')->with([['email', '=', $user->email]])->once()->andThrow(new RecordNotFoundException);
2017-08-24 02:34:11 +00:00
$this->userCreationService->shouldReceive('handle')->with([
'email' => $user->email,
'username' => substr(strtok($user->email, '@'), 0, 8) . '_' . 'random_string',
2017-08-24 02:34:11 +00:00
'name_first' => 'Server',
'name_last' => 'Subuser',
'root_admin' => false,
])->once()->andReturn($user);
2017-09-25 03:28:16 +00:00
$this->subuserRepository->shouldReceive('create')->with(['user_id' => $user->id, 'server_id' => $server->id])
->once()->andReturn($subuser);
$this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturnNull();
$this->permissionService->shouldReceive('handle')->with($subuser->id, array_keys($permissions))->once()->andReturnNull();
2017-08-24 02:34:11 +00:00
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($server, $user->email, array_keys($permissions));
$this->assertInstanceOf(Subuser::class, $response);
$this->assertSame($subuser, $response);
}
/**
* Test that an existing user can be added as a subuser.
*/
public function testExistingUserCanBeAddedAsASubuser()
{
$permissions = ['view-sftp', 'reset-sftp'];
2017-08-24 02:34:11 +00:00
$server = factory(Server::class)->make();
$user = factory(User::class)->make();
$subuser = factory(Subuser::class)->make(['user_id' => $user->id, 'server_id' => $server->id]);
2017-09-27 03:16:26 +00:00
$this->serverRepository->shouldReceive('find')->with($server->id)->once()->andReturn($server);
2017-09-04 23:12:13 +00:00
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->userRepository->shouldReceive('findFirstWhere')->with([['email', '=', $user->email]])->once()->andReturn($user);
2017-08-24 02:34:11 +00:00
$this->subuserRepository->shouldReceive('findCountWhere')->with([
['user_id', '=', $user->id],
['server_id', '=', $server->id],
])->once()->andReturn(0);
2017-09-25 03:28:16 +00:00
$this->subuserRepository->shouldReceive('create')->with(['user_id' => $user->id, 'server_id' => $server->id])
->once()->andReturn($subuser);
$this->keyCreationService->shouldReceive('handle')->with($server->id, $user->id)->once()->andReturnNull();
$this->permissionService->shouldReceive('handle')->with($subuser->id, $permissions)->once()->andReturnNull();
2017-08-24 02:34:11 +00:00
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
2017-09-27 03:16:26 +00:00
$response = $this->service->handle($server->id, $user->email, $permissions);
2017-08-24 02:34:11 +00:00
$this->assertInstanceOf(Subuser::class, $response);
$this->assertSame($subuser, $response);
}
/**
2017-08-27 19:55:25 +00:00
* Test that an exception gets thrown if the subuser is actually the server owner.
2017-08-24 02:34:11 +00:00
*/
public function testExceptionIsThrownIfUserIsServerOwner()
{
$user = factory(User::class)->make();
$server = factory(Server::class)->make(['owner_id' => $user->id]);
2017-09-04 23:12:13 +00:00
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->userRepository->shouldReceive('findFirstWhere')->with([['email', '=', $user->email]])->once()->andReturn($user);
2017-08-24 02:34:11 +00:00
try {
$this->service->handle($server, $user->email, []);
} catch (DisplayException $exception) {
$this->assertInstanceOf(UserIsServerOwnerException::class, $exception);
$this->assertEquals(trans('exceptions.subusers.user_is_owner'), $exception->getMessage());
2017-08-24 02:34:11 +00:00
}
}
/**
* Test that an exception is thrown if the user is already added as a subuser.
*/
public function testExceptionIsThrownIfUserIsAlreadyASubuser()
{
$user = factory(User::class)->make();
$server = factory(Server::class)->make();
2017-09-04 23:12:13 +00:00
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->userRepository->shouldReceive('findFirstWhere')->with([['email', '=', $user->email]])->once()->andReturn($user);
2017-08-24 02:34:11 +00:00
$this->subuserRepository->shouldReceive('findCountWhere')->with([
['user_id', '=', $user->id],
['server_id', '=', $server->id],
])->once()->andReturn(1);
try {
$this->service->handle($server, $user->email, []);
} catch (DisplayException $exception) {
$this->assertInstanceOf(ServerSubuserExistsException::class, $exception);
$this->assertEquals(trans('exceptions.subusers.subuser_exists'), $exception->getMessage());
2017-08-24 02:34:11 +00:00
}
}
}