2017-09-04 23:12:13 +00:00
|
|
|
<?php
|
2017-09-26 02:43:01 +00:00
|
|
|
/**
|
2017-09-04 23:12:13 +00:00
|
|
|
* 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-09-04 23:12:13 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Tests\Unit\Http\Controllers\Server;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Pterodactyl\Models\Server;
|
|
|
|
use Pterodactyl\Models\Subuser;
|
2017-09-05 00:07:00 +00:00
|
|
|
use Pterodactyl\Models\Permission;
|
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
2017-11-03 21:43:28 +00:00
|
|
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
2017-09-05 00:07:00 +00:00
|
|
|
use Pterodactyl\Services\Subusers\SubuserUpdateService;
|
2017-09-04 23:12:13 +00:00
|
|
|
use Pterodactyl\Services\Subusers\SubuserCreationService;
|
|
|
|
use Pterodactyl\Services\Subusers\SubuserDeletionService;
|
2017-09-05 00:07:00 +00:00
|
|
|
use Pterodactyl\Http\Controllers\Server\SubuserController;
|
|
|
|
use Pterodactyl\Contracts\Repository\SubuserRepositoryInterface;
|
2017-11-03 21:43:28 +00:00
|
|
|
use Pterodactyl\Http\Requests\Server\Subuser\SubuserStoreFormRequest;
|
|
|
|
use Pterodactyl\Http\Requests\Server\Subuser\SubuserUpdateFormRequest;
|
2017-09-04 23:12:13 +00:00
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
class SubuserControllerTest extends ControllerTestCase
|
2017-09-04 23:12:13 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-11-03 21:43:28 +00:00
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag|\Mockery\Mock
|
2017-09-04 23:12:13 +00:00
|
|
|
*/
|
|
|
|
protected $alert;
|
|
|
|
|
|
|
|
/**
|
2017-11-03 21:43:28 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\SubuserRepositoryInterface|\Mockery\Mock
|
2017-09-04 23:12:13 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
2017-11-03 21:43:28 +00:00
|
|
|
* @var \Pterodactyl\Services\Subusers\SubuserCreationService|\Mockery\Mock
|
2017-09-04 23:12:13 +00:00
|
|
|
*/
|
|
|
|
protected $subuserCreationService;
|
|
|
|
|
|
|
|
/**
|
2017-11-03 21:43:28 +00:00
|
|
|
* @var \Pterodactyl\Services\Subusers\SubuserDeletionService|\Mockery\Mock
|
2017-09-04 23:12:13 +00:00
|
|
|
*/
|
|
|
|
protected $subuserDeletionService;
|
|
|
|
|
|
|
|
/**
|
2017-11-03 21:43:28 +00:00
|
|
|
* @var \Pterodactyl\Services\Subusers\SubuserUpdateService|\Mockery\Mock
|
2017-09-04 23:12:13 +00:00
|
|
|
*/
|
|
|
|
protected $subuserUpdateService;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->alert = m::mock(AlertsMessageBag::class);
|
|
|
|
$this->repository = m::mock(SubuserRepositoryInterface::class);
|
|
|
|
$this->subuserCreationService = m::mock(SubuserCreationService::class);
|
|
|
|
$this->subuserDeletionService = m::mock(SubuserDeletionService::class);
|
|
|
|
$this->subuserUpdateService = m::mock(SubuserUpdateService::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Test index controller.
|
|
|
|
*/
|
|
|
|
public function testIndexController()
|
|
|
|
{
|
2017-11-03 21:43:28 +00:00
|
|
|
$controller = $this->getController();
|
2017-09-04 23:12:13 +00:00
|
|
|
$server = factory(Server::class)->make();
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestAttribute('server', $server);
|
|
|
|
$this->mockInjectJavascript();
|
|
|
|
|
|
|
|
$controller->shouldReceive('authorize')->with('list-subusers', $server)->once()->andReturnNull();
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('findWhere')->with([['server_id', '=', $server->id]])->once()->andReturn(collect());
|
2017-09-04 23:12:13 +00:00
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$response = $controller->index($this->request);
|
2017-09-04 23:12:13 +00:00
|
|
|
$this->assertIsViewResponse($response);
|
|
|
|
$this->assertViewNameEquals('server.users.index', $response);
|
|
|
|
$this->assertViewHasKey('subusers', $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test view controller.
|
|
|
|
*/
|
|
|
|
public function testViewController()
|
|
|
|
{
|
2017-11-03 21:43:28 +00:00
|
|
|
$controller = $this->getController();
|
|
|
|
$subuser = factory(Subuser::class)->make();
|
|
|
|
$subuser->setRelation('permissions', collect([
|
|
|
|
(object) ['permission' => 'some.permission'],
|
|
|
|
(object) ['permission' => 'another.permission'],
|
|
|
|
]));
|
2017-09-04 23:12:13 +00:00
|
|
|
$server = factory(Server::class)->make();
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestAttribute('server', $server);
|
|
|
|
$this->setRequestAttribute('subuser', $subuser);
|
|
|
|
$this->mockInjectJavascript();
|
2017-09-04 23:12:13 +00:00
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$controller->shouldReceive('authorize')->with('view-subuser', $server)->once()->andReturnNull();
|
|
|
|
$this->repository->shouldReceive('getWithPermissions')->with($subuser)->once()->andReturn($subuser);
|
|
|
|
|
|
|
|
$response = $controller->view($this->request);
|
2017-09-04 23:12:13 +00:00
|
|
|
$this->assertIsViewResponse($response);
|
|
|
|
$this->assertViewNameEquals('server.users.view', $response);
|
|
|
|
$this->assertViewHasKey('subuser', $response);
|
|
|
|
$this->assertViewHasKey('permlist', $response);
|
|
|
|
$this->assertViewHasKey('permissions', $response);
|
|
|
|
$this->assertViewKeyEquals('subuser', $subuser, $response);
|
|
|
|
$this->assertViewKeyEquals('permlist', Permission::getPermissions(), $response);
|
|
|
|
$this->assertViewKeyEquals('permissions', collect([
|
|
|
|
'some.permission' => true,
|
|
|
|
'another.permission' => true,
|
|
|
|
]), $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the update controller.
|
|
|
|
*/
|
|
|
|
public function testUpdateController()
|
|
|
|
{
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestMockClass(SubuserUpdateFormRequest::class);
|
|
|
|
|
|
|
|
$controller = $this->getController();
|
|
|
|
$subuser = factory(Subuser::class)->make();
|
|
|
|
|
|
|
|
$this->setRequestAttribute('subuser', $subuser);
|
2017-09-04 23:12:13 +00:00
|
|
|
|
|
|
|
$this->request->shouldReceive('input')->with('permissions', [])->once()->andReturn(['some.permission']);
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->subuserUpdateService->shouldReceive('handle')->with($subuser, ['some.permission'])->once()->andReturnNull();
|
|
|
|
$this->alert->shouldReceive('success')->with(trans('server.users.user_updated'))->once()->andReturnSelf();
|
|
|
|
$this->alert->shouldReceive('flash')->withNoArgs()->once()->andReturnNull();
|
2017-09-04 23:12:13 +00:00
|
|
|
|
2017-11-05 19:33:42 +00:00
|
|
|
$response = $controller->update($this->request, 'abcd1234', $subuser->hashid);
|
2017-09-04 23:12:13 +00:00
|
|
|
$this->assertIsRedirectResponse($response);
|
2017-11-05 19:33:42 +00:00
|
|
|
$this->assertRedirectRouteEquals('server.subusers.view', $response, ['uuid' => 'abcd1234', 'id' => $subuser->hashid]);
|
2017-09-04 23:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the create controller.
|
|
|
|
*/
|
|
|
|
public function testCreateController()
|
|
|
|
{
|
2017-11-03 21:43:28 +00:00
|
|
|
$controller = $this->getController();
|
2017-09-04 23:12:13 +00:00
|
|
|
$server = factory(Server::class)->make();
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestAttribute('server', $server);
|
|
|
|
$this->mockInjectJavascript();
|
2017-09-04 23:12:13 +00:00
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$controller->shouldReceive('authorize')->with('create-subuser', $server)->once()->andReturnNull();
|
|
|
|
|
|
|
|
$response = $controller->create($this->request);
|
2017-09-04 23:12:13 +00:00
|
|
|
$this->assertIsViewResponse($response);
|
|
|
|
$this->assertViewNameEquals('server.users.new', $response);
|
|
|
|
$this->assertViewHasKey('permissions', $response);
|
|
|
|
$this->assertViewKeyEquals('permissions', Permission::getPermissions(), $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the store controller.
|
|
|
|
*/
|
|
|
|
public function testStoreController()
|
|
|
|
{
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestMockClass(SubuserStoreFormRequest::class);
|
|
|
|
$controller = $this->getController();
|
|
|
|
|
2017-09-04 23:12:13 +00:00
|
|
|
$server = factory(Server::class)->make();
|
|
|
|
$subuser = factory(Subuser::class)->make();
|
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestAttribute('server', $server);
|
|
|
|
|
2017-09-04 23:12:13 +00:00
|
|
|
$this->request->shouldReceive('input')->with('email')->once()->andReturn('user@test.com');
|
|
|
|
$this->request->shouldReceive('input')->with('permissions', [])->once()->andReturn(['some.permission']);
|
|
|
|
$this->subuserCreationService->shouldReceive('handle')->with($server, 'user@test.com', ['some.permission'])->once()->andReturn($subuser);
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->alert->shouldReceive('success')->with(trans('server.users.user_assigned'))->once()->andReturnSelf();
|
|
|
|
$this->alert->shouldReceive('flash')->withNoArgs()->once()->andReturnNull();
|
2017-09-04 23:12:13 +00:00
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$response = $controller->store($this->request);
|
2017-09-04 23:12:13 +00:00
|
|
|
$this->assertIsRedirectResponse($response);
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->assertRedirectRouteEquals('server.subusers.view', $response, [
|
2018-07-01 01:25:46 +00:00
|
|
|
'uuid' => $server->uuidShort,
|
2017-11-05 19:33:42 +00:00
|
|
|
'id' => $subuser->hashid,
|
2017-11-03 21:43:28 +00:00
|
|
|
]);
|
2017-09-04 23:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the delete controller.
|
|
|
|
*/
|
|
|
|
public function testDeleteController()
|
|
|
|
{
|
2017-11-03 21:43:28 +00:00
|
|
|
$controller = $this->getController();
|
|
|
|
|
2017-09-04 23:12:13 +00:00
|
|
|
$server = factory(Server::class)->make();
|
2017-11-03 21:43:28 +00:00
|
|
|
$subuser = factory(Subuser::class)->make();
|
2017-09-04 23:12:13 +00:00
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$this->setRequestAttribute('server', $server);
|
|
|
|
$this->setRequestAttribute('subuser', $subuser);
|
2017-09-04 23:12:13 +00:00
|
|
|
|
2017-11-03 21:43:28 +00:00
|
|
|
$controller->shouldReceive('authorize')->with('delete-subuser', $server)->once()->andReturnNull();
|
|
|
|
$this->subuserDeletionService->shouldReceive('handle')->with($subuser)->once()->andReturnNull();
|
|
|
|
|
|
|
|
$response = $controller->delete($this->request);
|
2017-09-04 23:12:13 +00:00
|
|
|
$this->assertIsResponse($response);
|
|
|
|
$this->assertResponseCodeEquals(204, $response);
|
|
|
|
}
|
2017-11-03 21:43:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a mocked instance of the controller to allow access to authorization functionality.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Http\Controllers\Server\SubuserController|\Mockery\Mock
|
|
|
|
*/
|
|
|
|
private function getController()
|
|
|
|
{
|
|
|
|
return $this->buildMockedController(SubuserController::class, [
|
|
|
|
$this->alert,
|
|
|
|
$this->subuserCreationService,
|
|
|
|
$this->subuserDeletionService,
|
|
|
|
$this->repository,
|
|
|
|
$this->subuserUpdateService,
|
|
|
|
]);
|
|
|
|
}
|
2017-09-04 23:12:13 +00:00
|
|
|
}
|