Finish up unit tests for base controllers
This commit is contained in:
parent
4203cdcb77
commit
37508a370d
7 changed files with 507 additions and 40 deletions
|
@ -67,6 +67,16 @@ class SecurityController extends Controller
|
|||
*/
|
||||
protected $twoFactorSetupService;
|
||||
|
||||
/**
|
||||
* SecurityController constructor.
|
||||
*
|
||||
* @param \Prologue\Alerts\AlertsMessageBag $alert
|
||||
* @param \Illuminate\Contracts\Config\Repository $config
|
||||
* @param \Illuminate\Contracts\Session\Session $session
|
||||
* @param \Pterodactyl\Contracts\Repository\SessionRepositoryInterface $repository
|
||||
* @param \Pterodactyl\Services\Users\ToggleTwoFactorService $toggleTwoFactorService
|
||||
* @param \Pterodactyl\Services\Users\TwoFactorSetupService $twoFactorSetupService
|
||||
*/
|
||||
public function __construct(
|
||||
AlertsMessageBag $alert,
|
||||
ConfigRepository $config,
|
||||
|
@ -120,6 +130,9 @@ class SecurityController extends Controller
|
|||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function setTotp(Request $request)
|
||||
{
|
||||
|
@ -137,6 +150,9 @@ class SecurityController extends Controller
|
|||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\RedirectResponse
|
||||
*
|
||||
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
||||
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
||||
*/
|
||||
public function disableTotp(Request $request)
|
||||
{
|
||||
|
|
|
@ -24,46 +24,84 @@
|
|||
|
||||
namespace Tests\Assertions;
|
||||
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Response;
|
||||
use Illuminate\View\View;
|
||||
use PHPUnit_Framework_Assert;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
|
||||
trait ControllerAssertionsTrait
|
||||
{
|
||||
/**
|
||||
* Assert that a response is an instance of Illuminate View.
|
||||
*
|
||||
* @param mixed $response
|
||||
*/
|
||||
public function assertIsViewResponse($response)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a response is an instance of Illuminate Redirect Response.
|
||||
*
|
||||
* @param mixed $response
|
||||
*/
|
||||
public function assertIsRedirectResponse($response)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertInstanceOf(RedirectResponse::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a response is an instance of Illuminate Json Response.
|
||||
*
|
||||
* @param mixed $response
|
||||
*/
|
||||
public function assertIsJsonResponse($response)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertInstanceOf(JsonResponse::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a response is an instance of Illuminate Response.
|
||||
*
|
||||
* @param mixed $response
|
||||
*/
|
||||
public function assertIsResponse($response)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertInstanceOf(Response::class, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a view name equals the passed name.
|
||||
*
|
||||
* @param string $name
|
||||
* @param \Illuminate\View\View $view
|
||||
* @param string $name
|
||||
* @param mixed $view
|
||||
*/
|
||||
public function assertViewNameEquals($name, $view)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $view);
|
||||
PHPUnit_Framework_Assert::assertEquals($name, $view->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a view name does not equal a provided name.
|
||||
*
|
||||
* @param string $name
|
||||
* @param \Illuminate\View\View $view
|
||||
* @param string $name
|
||||
* @param mixed $view
|
||||
*/
|
||||
public function assertViewNameNotEquals($name, $view)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $view);
|
||||
PHPUnit_Framework_Assert::assertNotEquals($name, $view->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a view has an attribute passed into it.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param \Illuminate\View\View $view
|
||||
* @param string $attribute
|
||||
* @param mixed $view
|
||||
*/
|
||||
public function assertViewHasKey($attribute, $view)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $view);
|
||||
|
||||
if (str_contains($attribute, '.')) {
|
||||
PHPUnit_Framework_Assert::assertNotEquals(
|
||||
'__TEST__FAIL',
|
||||
|
@ -77,13 +115,11 @@ trait ControllerAssertionsTrait
|
|||
/**
|
||||
* Assert that a view does not have a specific attribute passed in.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param \Illuminate\View\View $view
|
||||
* @param string $attribute
|
||||
* @param mixed $view
|
||||
*/
|
||||
public function assertViewNotHasKey($attribute, $view)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $view);
|
||||
|
||||
if (str_contains($attribute, '.')) {
|
||||
PHPUnit_Framework_Assert::assertEquals(
|
||||
'__TEST__PASS',
|
||||
|
@ -97,36 +133,78 @@ trait ControllerAssertionsTrait
|
|||
/**
|
||||
* Assert that a view attribute equals a given parameter.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param \Illuminate\View\View $view
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $view
|
||||
*/
|
||||
public function assertViewKeyEquals($attribute, $value, $view)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $view);
|
||||
PHPUnit_Framework_Assert::assertEquals($value, array_get($view->getData(), $attribute, '__TEST__FAIL'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a view attribute does not equal a given parameter.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param \Illuminate\View\View $view
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @param mixed $view
|
||||
*/
|
||||
public function assertViewKeyNotEquals($attribute, $value, $view)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertInstanceOf(View::class, $view);
|
||||
PHPUnit_Framework_Assert::assertNotEquals($value, array_get($view->getData(), $attribute, '__TEST__FAIL'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $route
|
||||
* @param \Illuminate\Http\RedirectResponse $response
|
||||
* Assert that a route redirect equals a given route name.
|
||||
*
|
||||
* @param string $route
|
||||
* @param mixed $response
|
||||
*/
|
||||
public function assertRouteRedirectEquals($route, $response)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertInstanceOf(RedirectResponse::class, $response);
|
||||
PHPUnit_Framework_Assert::assertEquals(route($route), $response->getTargetUrl());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a response code equals a given code.
|
||||
*
|
||||
* @param int $code
|
||||
* @param mixed $response
|
||||
*/
|
||||
public function assertResponseCodeEquals($code, $response)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertEquals($code, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a response code does not equal a given code.
|
||||
*
|
||||
* @param int $code
|
||||
* @param mixed $response
|
||||
*/
|
||||
public function assertResponseCodeNotEquals($code, $response)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertNotEquals($code, $response->getStatusCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that a response is in a JSON format.
|
||||
*
|
||||
* @param mixed $response
|
||||
*/
|
||||
public function assertResponseHasJsonHeaders($response)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertEquals('application/json', $response->headers->get('content-type'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert that response JSON matches a given JSON string.
|
||||
*
|
||||
* @param array|string $json
|
||||
* @param mixed $response
|
||||
*/
|
||||
public function assertResponseJsonEquals($json, $response)
|
||||
{
|
||||
PHPUnit_Framework_Assert::assertEquals(is_array($json) ? json_encode($json) : $json, $response->getContent());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -90,13 +90,14 @@ class DatabaseControllerTest extends TestCase
|
|||
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn('getAllWithNodes');
|
||||
$this->repository->shouldReceive('getWithViewDetails')->withNoArgs()->once()->andReturn('getWithViewDetails');
|
||||
|
||||
$view = $this->controller->index();
|
||||
$response = $this->controller->index();
|
||||
|
||||
$this->assertViewNameEquals('admin.databases.index', $view);
|
||||
$this->assertViewHasKey('locations', $view);
|
||||
$this->assertViewHasKey('hosts', $view);
|
||||
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $view);
|
||||
$this->assertViewKeyEquals('hosts', 'getWithViewDetails', $view);
|
||||
$this->assertIsViewResponse($response);
|
||||
$this->assertViewNameEquals('admin.databases.index', $response);
|
||||
$this->assertViewHasKey('locations', $response);
|
||||
$this->assertViewHasKey('hosts', $response);
|
||||
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $response);
|
||||
$this->assertViewKeyEquals('hosts', 'getWithViewDetails', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -107,12 +108,13 @@ class DatabaseControllerTest extends TestCase
|
|||
$this->locationRepository->shouldReceive('getAllWithNodes')->withNoArgs()->once()->andReturn('getAllWithNodes');
|
||||
$this->repository->shouldReceive('getWithServers')->with(1)->once()->andReturn('getWithServers');
|
||||
|
||||
$view = $this->controller->view(1);
|
||||
$response = $this->controller->view(1);
|
||||
|
||||
$this->assertViewNameEquals('admin.databases.view', $view);
|
||||
$this->assertViewHasKey('locations', $view);
|
||||
$this->assertViewHasKey('host', $view);
|
||||
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $view);
|
||||
$this->assertViewKeyEquals('host', 'getWithServers', $view);
|
||||
$this->assertIsViewResponse($response);
|
||||
$this->assertViewNameEquals('admin.databases.view', $response);
|
||||
$this->assertViewHasKey('locations', $response);
|
||||
$this->assertViewHasKey('host', $response);
|
||||
$this->assertViewKeyEquals('locations', 'getAllWithNodes', $response);
|
||||
$this->assertViewKeyEquals('host', 'getWithServers', $response);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -28,7 +28,6 @@ use Mockery as m;
|
|||
use Tests\TestCase;
|
||||
use Illuminate\Http\Request;
|
||||
use Pterodactyl\Models\User;
|
||||
use Illuminate\Http\Response;
|
||||
use Prologue\Alerts\AlertsMessageBag;
|
||||
use Tests\Assertions\ControllerAssertionsTrait;
|
||||
use Pterodactyl\Services\Api\KeyCreationService;
|
||||
|
@ -91,6 +90,7 @@ class APIControllerTest extends TestCase
|
|||
$this->repository->shouldReceive('findWhere')->with([['user_id', '=', $model->id]])->once()->andReturn(['testkeys']);
|
||||
|
||||
$response = $this->controller->index($this->request);
|
||||
$this->assertIsViewResponse($response);
|
||||
$this->assertViewNameEquals('base.api.index', $response);
|
||||
$this->assertViewHasKey('keys', $response);
|
||||
$this->assertViewKeyEquals('keys', ['testkeys'], $response);
|
||||
|
@ -107,6 +107,7 @@ class APIControllerTest extends TestCase
|
|||
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($model);
|
||||
|
||||
$response = $this->controller->create($this->request);
|
||||
$this->assertIsViewResponse($response);
|
||||
$this->assertViewNameEquals('base.api.new', $response);
|
||||
$this->assertViewHasKey('permissions.user', $response);
|
||||
$this->assertViewHasKey('permissions.admin', $response);
|
||||
|
@ -147,6 +148,7 @@ class APIControllerTest extends TestCase
|
|||
->shouldReceive('flash')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->controller->store($this->request);
|
||||
$this->assertIsRedirectResponse($response);
|
||||
$this->assertRouteRedirectEquals('account.api', $response);
|
||||
}
|
||||
|
||||
|
@ -164,9 +166,9 @@ class APIControllerTest extends TestCase
|
|||
])->once()->andReturnNull();
|
||||
|
||||
$response = $this->controller->revoke($this->request, 'testKey123');
|
||||
$this->assertInstanceOf(Response::class, $response);
|
||||
$this->assertIsResponse($response);
|
||||
$this->assertEmpty($response->getContent());
|
||||
$this->assertEquals(204, $response->getStatusCode());
|
||||
$this->assertResponseCodeEquals(204, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -77,6 +77,7 @@ class AccountControllerTest extends TestCase
|
|||
{
|
||||
$response = $this->controller->index();
|
||||
|
||||
$this->assertIsViewResponse($response);
|
||||
$this->assertViewNameEquals('base.account', $response);
|
||||
}
|
||||
|
||||
|
@ -93,6 +94,7 @@ class AccountControllerTest extends TestCase
|
|||
$this->alert->shouldReceive('success->flash')->once()->andReturnNull();
|
||||
|
||||
$response = $this->controller->update($this->request);
|
||||
$this->assertIsRedirectResponse($response);
|
||||
$this->assertRouteRedirectEquals('account', $response);
|
||||
}
|
||||
|
||||
|
@ -109,6 +111,7 @@ class AccountControllerTest extends TestCase
|
|||
$this->alert->shouldReceive('success->flash')->once()->andReturnNull();
|
||||
|
||||
$response = $this->controller->update($this->request);
|
||||
$this->assertIsRedirectResponse($response);
|
||||
$this->assertRouteRedirectEquals('account', $response);
|
||||
}
|
||||
|
||||
|
@ -127,6 +130,7 @@ class AccountControllerTest extends TestCase
|
|||
$this->alert->shouldReceive('success->flash')->once()->andReturnNull();
|
||||
|
||||
$response = $this->controller->update($this->request);
|
||||
$this->assertIsRedirectResponse($response);
|
||||
$this->assertRouteRedirectEquals('account', $response);
|
||||
}
|
||||
}
|
||||
|
|
159
tests/Unit/Http/Controllers/Base/IndexControllerTest.php
Normal file
159
tests/Unit/Http/Controllers/Base/IndexControllerTest.php
Normal file
|
@ -0,0 +1,159 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Http\Controllers\Base;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Mockery as m;
|
||||
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
|
||||
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
|
||||
use Pterodactyl\Http\Controllers\Base\IndexController;
|
||||
use Pterodactyl\Models\Server;
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Services\Servers\ServerAccessHelperService;
|
||||
use Tests\Assertions\ControllerAssertionsTrait;
|
||||
use Tests\TestCase;
|
||||
|
||||
class IndexControllerTest extends TestCase
|
||||
{
|
||||
use ControllerAssertionsTrait;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Servers\ServerAccessHelperService
|
||||
*/
|
||||
protected $access;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Http\Controllers\Base\IndexController
|
||||
*/
|
||||
protected $controller;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
|
||||
*/
|
||||
protected $daemonRepository;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Http\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->access = m::mock(ServerAccessHelperService::class);
|
||||
$this->daemonRepository = m::mock(DaemonServerRepositoryInterface::class);
|
||||
$this->repository = m::mock(ServerRepositoryInterface::class);
|
||||
$this->request = m::mock(Request::class);
|
||||
|
||||
$this->controller = new IndexController($this->daemonRepository, $this->access, $this->repository);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the index controller.
|
||||
*/
|
||||
public function testIndexController()
|
||||
{
|
||||
$model = factory(User::class)->make();
|
||||
|
||||
$this->request->shouldReceive('user')->withNoArgs()->andReturn($model);
|
||||
$this->request->shouldReceive('input')->with('query')->once()->andReturn('searchTerm');
|
||||
$this->repository->shouldReceive('search')->with('searchTerm')->once()->andReturnSelf()
|
||||
->shouldReceive('filterUserAccessServers')->with(
|
||||
$model->id, $model->root_admin, 'all', ['user']
|
||||
)->once()->andReturn(['test']);
|
||||
|
||||
$response = $this->controller->getIndex($this->request);
|
||||
$this->assertIsViewResponse($response);
|
||||
$this->assertViewNameEquals('base.index', $response);
|
||||
$this->assertViewHasKey('servers', $response);
|
||||
$this->assertViewKeyEquals('servers', ['test'], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the status controller.
|
||||
*/
|
||||
public function testStatusController()
|
||||
{
|
||||
$user = factory(User::class)->make();
|
||||
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
|
||||
|
||||
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
|
||||
$this->access->shouldReceive('handle')->with($server->uuid, $user)->once()->andReturn($server);
|
||||
|
||||
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
|
||||
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
|
||||
->shouldReceive('setAccessToken')->with($server->daemonSecret)->once()->andReturnSelf()
|
||||
->shouldReceive('details')->withNoArgs()->once()->andReturnSelf();
|
||||
|
||||
$this->daemonRepository->shouldReceive('getBody')->withNoArgs()->once()->andReturn('["test"]');
|
||||
|
||||
$response = $this->controller->status($this->request, $server->uuid);
|
||||
$this->assertIsJsonResponse($response);
|
||||
$this->assertResponseJsonEquals(['test'], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the status controller if a server is not installed.
|
||||
*/
|
||||
public function testStatusControllerWhenServerNotInstalled()
|
||||
{
|
||||
$user = factory(User::class)->make();
|
||||
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 0]);
|
||||
|
||||
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
|
||||
$this->access->shouldReceive('handle')->with($server->uuid, $user)->once()->andReturn($server);
|
||||
|
||||
$response = $this->controller->status($this->request, $server->uuid);
|
||||
$this->assertIsJsonResponse($response);
|
||||
$this->assertResponseCodeEquals(200, $response);
|
||||
$this->assertResponseJsonEquals(['status' => 20], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the status controller when a server is suspended.
|
||||
*/
|
||||
public function testStatusControllerWhenServerIsSuspended()
|
||||
{
|
||||
$user = factory(User::class)->make();
|
||||
$server = factory(Server::class)->make(['suspended' => 1, 'installed' => 1]);
|
||||
|
||||
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($user);
|
||||
$this->access->shouldReceive('handle')->with($server->uuid, $user)->once()->andReturn($server);
|
||||
|
||||
$response = $this->controller->status($this->request, $server->uuid);
|
||||
$this->assertIsJsonResponse($response);
|
||||
$this->assertResponseCodeEquals(200, $response);
|
||||
$this->assertResponseJsonEquals(['status' => 30], $response);
|
||||
}
|
||||
}
|
206
tests/Unit/Http/Controllers/Base/SecurityControllerTest.php
Normal file
206
tests/Unit/Http/Controllers/Base/SecurityControllerTest.php
Normal file
|
@ -0,0 +1,206 @@
|
|||
<?php
|
||||
/*
|
||||
* Pterodactyl - Panel
|
||||
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
namespace Tests\Unit\Http\Controllers\Base;
|
||||
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Contracts\Session\Session;
|
||||
use Illuminate\Http\Request;
|
||||
use Mockery as m;
|
||||
use Prologue\Alerts\AlertsMessageBag;
|
||||
use Pterodactyl\Contracts\Repository\SessionRepositoryInterface;
|
||||
use Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid;
|
||||
use Pterodactyl\Http\Controllers\Base\SecurityController;
|
||||
use Pterodactyl\Models\User;
|
||||
use Pterodactyl\Services\Users\ToggleTwoFactorService;
|
||||
use Pterodactyl\Services\Users\TwoFactorSetupService;
|
||||
use Tests\Assertions\ControllerAssertionsTrait;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SecurityControllerTest extends TestCase
|
||||
{
|
||||
use ControllerAssertionsTrait;
|
||||
|
||||
/**
|
||||
* @var \Prologue\Alerts\AlertsMessageBag
|
||||
*/
|
||||
protected $alert;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Config\Repository
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Http\Controllers\Base\SecurityController
|
||||
*/
|
||||
protected $controller;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Contracts\Repository\SessionRepositoryInterface
|
||||
*/
|
||||
protected $repository;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Http\Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* @var \Illuminate\Contracts\Session\Session
|
||||
*/
|
||||
protected $session;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Users\ToggleTwoFactorService
|
||||
*/
|
||||
protected $toggleTwoFactorService;
|
||||
|
||||
/**
|
||||
* @var \Pterodactyl\Services\Users\TwoFactorSetupService
|
||||
*/
|
||||
protected $twoFactorSetupService;
|
||||
|
||||
/**
|
||||
* Setup tests.
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->alert = m::mock(AlertsMessageBag::class);
|
||||
$this->config = m::mock(Repository::class);
|
||||
$this->repository = m::mock(SessionRepositoryInterface::class);
|
||||
$this->request = m::mock(Request::class);
|
||||
$this->session = m::mock(Session::class);
|
||||
$this->toggleTwoFactorService = m::mock(ToggleTwoFactorService::class);
|
||||
$this->twoFactorSetupService = m::mock(TwoFactorSetupService::class);
|
||||
|
||||
$this->controller = new SecurityController(
|
||||
$this->alert,
|
||||
$this->config,
|
||||
$this->session,
|
||||
$this->repository,
|
||||
$this->toggleTwoFactorService,
|
||||
$this->twoFactorSetupService
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the index controller when using a database driver.
|
||||
*/
|
||||
public function testIndexControllerWithDatabaseDriver()
|
||||
{
|
||||
$model = factory(User::class)->make();
|
||||
|
||||
$this->config->shouldReceive('get')->with('session.driver')->once()->andReturn('database');
|
||||
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($model);
|
||||
$this->repository->shouldReceive('getUserSessions')->with($model->id)->once()->andReturn(['sessions']);
|
||||
|
||||
$response = $this->controller->index($this->request);
|
||||
$this->assertIsViewResponse($response);
|
||||
$this->assertViewNameEquals('base.security', $response);
|
||||
$this->assertViewHasKey('sessions', $response);
|
||||
$this->assertViewKeyEquals('sessions', ['sessions'], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the index controller when not using the database driver.
|
||||
*/
|
||||
public function testIndexControllerWithoutDatabaseDriver()
|
||||
{
|
||||
$this->config->shouldReceive('get')->with('session.driver')->once()->andReturn('redis');
|
||||
|
||||
$response = $this->controller->index($this->request);
|
||||
$this->assertIsViewResponse($response);
|
||||
$this->assertViewNameEquals('base.security', $response);
|
||||
$this->assertViewHasKey('sessions', $response);
|
||||
$this->assertViewKeyEquals('sessions', null, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test TOTP generation controller.
|
||||
*/
|
||||
public function testGenerateTotpController()
|
||||
{
|
||||
$model = factory(User::class)->make();
|
||||
|
||||
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($model);
|
||||
$this->twoFactorSetupService->shouldReceive('handle')->with($model)->once()->andReturn(['string']);
|
||||
|
||||
$response = $this->controller->generateTotp($this->request);
|
||||
$this->assertIsJsonResponse($response);
|
||||
$this->assertResponseJsonEquals(['string'], $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the disable totp controller when no exception is thrown by the service.
|
||||
*/
|
||||
public function testDisableTotpControllerSuccess()
|
||||
{
|
||||
$model = factory(User::class)->make();
|
||||
|
||||
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($model);
|
||||
$this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken');
|
||||
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken', false)->once()->andReturnNull();
|
||||
|
||||
$response = $this->controller->disableTotp($this->request);
|
||||
$this->assertIsRedirectResponse($response);
|
||||
$this->assertRouteRedirectEquals('account.security', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the disable totp controller when an exception is thrown by the service.
|
||||
*/
|
||||
public function testDisableTotpControllerWhenExceptionIsThrown()
|
||||
{
|
||||
$model = factory(User::class)->make();
|
||||
|
||||
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($model);
|
||||
$this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken');
|
||||
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken', false)->once()
|
||||
->andThrow(new TwoFactorAuthenticationTokenInvalid);
|
||||
$this->alert->shouldReceive('danger')->with(trans('base.security.2fa_disable_error'))->once()->andReturnSelf()
|
||||
->shouldReceive('flash')->withNoArgs()->once()->andReturnNull();
|
||||
|
||||
$response = $this->controller->disableTotp($this->request);
|
||||
$this->assertIsRedirectResponse($response);
|
||||
$this->assertRouteRedirectEquals('account.security', $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the revoke controller.
|
||||
*/
|
||||
public function testRevokeController()
|
||||
{
|
||||
$model = factory(User::class)->make();
|
||||
|
||||
$this->request->shouldReceive('user')->withNoArgs()->once()->andReturn($model);
|
||||
$this->repository->shouldReceive('deleteUserSession')->with($model->id, 123)->once()->andReturnNull();
|
||||
|
||||
$response = $this->controller->revoke($this->request, 123);
|
||||
$this->assertIsRedirectResponse($response);
|
||||
$this->assertRouteRedirectEquals('account.security', $response);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue