2017-09-02 23:56:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Http\Controllers\Base;
|
|
|
|
|
|
|
|
use Mockery as m;
|
|
|
|
use Prologue\Alerts\AlertsMessageBag;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Illuminate\Contracts\Config\Repository;
|
2017-11-18 18:35:33 +00:00
|
|
|
use Tests\Unit\Http\Controllers\ControllerTestCase;
|
2017-09-03 21:41:03 +00:00
|
|
|
use Pterodactyl\Services\Users\TwoFactorSetupService;
|
|
|
|
use Pterodactyl\Services\Users\ToggleTwoFactorService;
|
|
|
|
use Pterodactyl\Http\Controllers\Base\SecurityController;
|
2017-09-02 23:56:15 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\SessionRepositoryInterface;
|
|
|
|
use Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid;
|
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
class SecurityControllerTest extends ControllerTestCase
|
2017-09-02 23:56:15 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* @var \Prologue\Alerts\AlertsMessageBag|\Mockery\Mock
|
2017-09-02 23:56:15 +00:00
|
|
|
*/
|
|
|
|
protected $alert;
|
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
|
2017-09-02 23:56:15 +00:00
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\SessionRepositoryInterface|\Mockery\Mock
|
2017-09-02 23:56:15 +00:00
|
|
|
*/
|
|
|
|
protected $repository;
|
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* @var \Pterodactyl\Services\Users\ToggleTwoFactorService|\Mockery\Mock
|
2017-09-02 23:56:15 +00:00
|
|
|
*/
|
|
|
|
protected $toggleTwoFactorService;
|
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* @var \Pterodactyl\Services\Users\TwoFactorSetupService|\Mockery\Mock
|
2017-09-02 23:56:15 +00:00
|
|
|
*/
|
|
|
|
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->toggleTwoFactorService = m::mock(ToggleTwoFactorService::class);
|
|
|
|
$this->twoFactorSetupService = m::mock(TwoFactorSetupService::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the index controller when using a database driver.
|
|
|
|
*/
|
|
|
|
public function testIndexControllerWithDatabaseDriver()
|
|
|
|
{
|
2017-12-17 19:07:38 +00:00
|
|
|
$model = $this->generateRequestUserModel();
|
2017-09-02 23:56:15 +00:00
|
|
|
|
|
|
|
$this->config->shouldReceive('get')->with('session.driver')->once()->andReturn('database');
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->repository->shouldReceive('getUserSessions')->with($model->id)->once()->andReturn(collect(['sessions']));
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$response = $this->getController()->index($this->request);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsViewResponse($response);
|
|
|
|
$this->assertViewNameEquals('base.security', $response);
|
|
|
|
$this->assertViewHasKey('sessions', $response);
|
2018-01-05 04:49:50 +00:00
|
|
|
$this->assertViewKeyEquals('sessions', collect(['sessions']), $response);
|
2017-09-02 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the index controller when not using the database driver.
|
|
|
|
*/
|
|
|
|
public function testIndexControllerWithoutDatabaseDriver()
|
|
|
|
{
|
|
|
|
$this->config->shouldReceive('get')->with('session.driver')->once()->andReturn('redis');
|
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$response = $this->getController()->index($this->request);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsViewResponse($response);
|
|
|
|
$this->assertViewNameEquals('base.security', $response);
|
|
|
|
$this->assertViewHasKey('sessions', $response);
|
|
|
|
$this->assertViewKeyEquals('sessions', null, $response);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test TOTP generation controller.
|
|
|
|
*/
|
|
|
|
public function testGenerateTotpController()
|
|
|
|
{
|
2017-12-17 19:07:38 +00:00
|
|
|
$model = $this->generateRequestUserModel();
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$this->twoFactorSetupService->shouldReceive('handle')->with($model)->once()->andReturn('qrCodeImage');
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$response = $this->getController()->generateTotp($this->request);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsJsonResponse($response);
|
2019-06-22 05:01:42 +00:00
|
|
|
$this->assertResponseJsonEquals(['qrImage' => 'https://api.qrserver.com/v1/create-qr-code/?size=200x200&data=qrCodeImage'], $response);
|
2017-09-02 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 19:57:07 +00:00
|
|
|
/**
|
|
|
|
* Test TOTP setting controller when no exception is thrown by the service.
|
|
|
|
*/
|
|
|
|
public function testSetTotpControllerSuccess()
|
|
|
|
{
|
|
|
|
$model = $this->generateRequestUserModel();
|
|
|
|
|
|
|
|
$this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken');
|
|
|
|
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken')->once();
|
|
|
|
|
|
|
|
$response = $this->getController()->setTotp($this->request);
|
|
|
|
$this->assertIsResponse($response);
|
|
|
|
$this->assertSame('true', $response->getContent());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test TOTP setting controller when an exception is thrown by the service.
|
|
|
|
*/
|
|
|
|
public function testSetTotpControllerWhenExceptionIsThrown()
|
|
|
|
{
|
|
|
|
$model = $this->generateRequestUserModel();
|
|
|
|
|
|
|
|
$this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken');
|
|
|
|
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken')->once()->andThrow(new TwoFactorAuthenticationTokenInvalid());
|
|
|
|
|
|
|
|
$response = $this->getController()->setTotp($this->request);
|
|
|
|
$this->assertIsResponse($response);
|
|
|
|
$this->assertSame('false', $response->getContent());
|
|
|
|
}
|
|
|
|
|
2017-09-02 23:56:15 +00:00
|
|
|
/**
|
|
|
|
* Test the disable totp controller when no exception is thrown by the service.
|
|
|
|
*/
|
|
|
|
public function testDisableTotpControllerSuccess()
|
|
|
|
{
|
2017-12-17 19:07:38 +00:00
|
|
|
$model = $this->generateRequestUserModel();
|
2017-09-02 23:56:15 +00:00
|
|
|
|
|
|
|
$this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken');
|
2017-12-17 19:07:38 +00:00
|
|
|
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'testToken', false)->once()->andReturn(true);
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$response = $this->getController()->disableTotp($this->request);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsRedirectResponse($response);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertRedirectRouteEquals('account.security', $response);
|
2017-09-02 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the disable totp controller when an exception is thrown by the service.
|
|
|
|
*/
|
|
|
|
public function testDisableTotpControllerWhenExceptionIsThrown()
|
|
|
|
{
|
2017-12-17 19:07:38 +00:00
|
|
|
$model = $this->generateRequestUserModel();
|
2017-09-02 23:56:15 +00:00
|
|
|
|
|
|
|
$this->request->shouldReceive('input')->with('token')->once()->andReturn('testToken');
|
2017-11-18 18:35:33 +00:00
|
|
|
$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();
|
|
|
|
$this->alert->shouldReceive('flash')->withNoArgs()->once()->andReturnNull();
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$response = $this->getController()->disableTotp($this->request);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsRedirectResponse($response);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertRedirectRouteEquals('account.security', $response);
|
2017-09-02 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the revoke controller.
|
|
|
|
*/
|
|
|
|
public function testRevokeController()
|
|
|
|
{
|
2017-12-17 19:07:38 +00:00
|
|
|
$model = $this->generateRequestUserModel();
|
2017-09-02 23:56:15 +00:00
|
|
|
|
|
|
|
$this->repository->shouldReceive('deleteUserSession')->with($model->id, 123)->once()->andReturnNull();
|
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$response = $this->getController()->revoke($this->request, 123);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsRedirectResponse($response);
|
2017-09-03 21:32:52 +00:00
|
|
|
$this->assertRedirectRouteEquals('account.security', $response);
|
2017-09-02 23:56:15 +00:00
|
|
|
}
|
2017-11-18 18:35:33 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an instance of the controller for testing with mocked dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Http\Controllers\Base\SecurityController
|
|
|
|
*/
|
|
|
|
private function getController(): SecurityController
|
|
|
|
{
|
|
|
|
return new SecurityController(
|
|
|
|
$this->alert,
|
|
|
|
$this->config,
|
|
|
|
$this->repository,
|
|
|
|
$this->toggleTwoFactorService,
|
|
|
|
$this->twoFactorSetupService
|
|
|
|
);
|
|
|
|
}
|
2017-09-02 23:56:15 +00:00
|
|
|
}
|