2017-09-02 23:56:15 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Http\Controllers\Base;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2018-07-05 02:20:33 +00:00
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Illuminate\Support\Collection;
|
2017-09-02 23:56:15 +00:00
|
|
|
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 TOTP generation controller.
|
|
|
|
*/
|
2018-07-05 02:20:33 +00:00
|
|
|
public function testIndexWithout2FactorEnabled()
|
2017-09-02 23:56:15 +00:00
|
|
|
{
|
2018-07-05 02:20:33 +00:00
|
|
|
$model = $this->generateRequestUserModel(['use_totp' => 0]);
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2018-07-05 02:20:33 +00:00
|
|
|
$this->twoFactorSetupService->shouldReceive('handle')->with($model)->once()->andReturn(new Collection([
|
|
|
|
'image' => 'test-image',
|
|
|
|
'secret' => 'secret-code',
|
|
|
|
]));
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2018-07-05 02:20:33 +00:00
|
|
|
$response = $this->getController()->index($this->request);
|
2017-09-02 23:56:15 +00:00
|
|
|
$this->assertIsJsonResponse($response);
|
2018-07-05 02:20:33 +00:00
|
|
|
$this->assertResponseCodeEquals(Response::HTTP_OK, $response);
|
|
|
|
$this->assertResponseJsonEquals(['enabled' => false, 'qr_image' => 'test-image', 'secret' => 'secret-code'], $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.
|
|
|
|
*/
|
2018-07-05 02:20:33 +00:00
|
|
|
public function testIndexWith2FactorEnabled()
|
2018-05-13 19:57:07 +00:00
|
|
|
{
|
2018-07-05 02:20:33 +00:00
|
|
|
$this->generateRequestUserModel(['use_totp' => 1]);
|
2018-05-13 19:57:07 +00:00
|
|
|
|
2018-07-05 02:20:33 +00:00
|
|
|
$response = $this->getController()->index($this->request);
|
|
|
|
$this->assertIsJsonResponse($response);
|
|
|
|
$this->assertResponseCodeEquals(Response::HTTP_OK, $response);
|
|
|
|
$this->assertResponseJsonEquals(['enabled' => true], $response);
|
2018-05-13 19:57:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-05 02:20:33 +00:00
|
|
|
* Test that a 2FA token can be stored or deleted.
|
|
|
|
*
|
|
|
|
* @param string $func
|
|
|
|
* @dataProvider functionCallDataProvider
|
2018-05-13 19:57:07 +00:00
|
|
|
*/
|
2018-07-05 02:20:33 +00:00
|
|
|
public function testStore(string $func)
|
2018-05-13 19:57:07 +00:00
|
|
|
{
|
|
|
|
$model = $this->generateRequestUserModel();
|
|
|
|
|
2018-07-05 02:20:33 +00:00
|
|
|
$this->mockRequestInput('token', 'some-token');
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2018-07-05 02:20:33 +00:00
|
|
|
if ($func === 'delete') {
|
|
|
|
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'some-token', false);
|
|
|
|
} else {
|
|
|
|
$this->toggleTwoFactorService->shouldReceive('handle')->with($model, 'some-token');
|
|
|
|
}
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2018-07-05 02:20:33 +00:00
|
|
|
$response = $this->getController()->{$func}($this->request);
|
|
|
|
$this->assertIsJsonResponse($response);
|
|
|
|
$this->assertResponseCodeEquals(Response::HTTP_OK, $response);
|
|
|
|
$this->assertResponseJsonEquals(['success' => true], $response);
|
2017-09-02 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-05 02:20:33 +00:00
|
|
|
* Test an invalid token exception is handled.
|
|
|
|
*
|
|
|
|
* @param string $func
|
|
|
|
* @dataProvider functionCallDataProvider
|
2017-09-02 23:56:15 +00:00
|
|
|
*/
|
2018-07-05 02:20:33 +00:00
|
|
|
public function testStoreWithInvalidTokenException(string $func)
|
2017-09-02 23:56:15 +00:00
|
|
|
{
|
2018-07-05 02:20:33 +00:00
|
|
|
$this->generateRequestUserModel();
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2018-07-05 02:20:33 +00:00
|
|
|
$this->mockRequestInput('token');
|
|
|
|
$this->toggleTwoFactorService->shouldReceive('handle')->andThrow(new TwoFactorAuthenticationTokenInvalid);
|
2017-09-02 23:56:15 +00:00
|
|
|
|
2018-07-05 02:20:33 +00:00
|
|
|
$response = $this->getController()->{$func}($this->request);
|
|
|
|
$this->assertIsJsonResponse($response);
|
|
|
|
$this->assertResponseCodeEquals(Response::HTTP_OK, $response);
|
|
|
|
$this->assertResponseJsonEquals(['success' => false], $response);
|
2017-09-02 23:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-05 02:20:33 +00:00
|
|
|
* @return array
|
2017-09-02 23:56:15 +00:00
|
|
|
*/
|
2018-07-05 02:20:33 +00:00
|
|
|
public function functionCallDataProvider()
|
2017-09-02 23:56:15 +00:00
|
|
|
{
|
2018-07-05 02:20:33 +00:00
|
|
|
return [['store'], ['delete']];
|
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
|
|
|
}
|