misc_pterodactyl-panel/tests/Unit/Services/Users/ToggleTwoFactorServiceTest.php

129 lines
4.4 KiB
PHP
Raw Normal View History

<?php
namespace Tests\Unit\Services\Users;
use Mockery as m;
2017-11-18 18:35:33 +00:00
use Carbon\Carbon;
2017-08-31 02:14:20 +00:00
use Tests\TestCase;
use Pterodactyl\Models\User;
2017-11-18 18:35:33 +00:00
use PragmaRX\Google2FA\Google2FA;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Contracts\Encryption\Encrypter;
use Pterodactyl\Services\Users\ToggleTwoFactorService;
2017-08-31 02:14:20 +00:00
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
class ToggleTwoFactorServiceTest extends TestCase
{
2017-11-18 18:35:33 +00:00
const TEST_WINDOW_INT = 4;
const USER_TOTP_SECRET = 'encryptedValue';
const DECRYPTED_USER_SECRET = 'decryptedValue';
/**
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
*/
private $config;
/**
2017-11-18 18:35:33 +00:00
* @var \Illuminate\Contracts\Encryption\Encrypter|\Mockery\Mock
*/
2017-11-18 18:35:33 +00:00
private $encrypter;
/**
2017-11-18 18:35:33 +00:00
* @var \PragmaRX\Google2FA\Google2FA|\Mockery\Mock
*/
2017-11-18 18:35:33 +00:00
private $google2FA;
/**
2017-11-18 18:35:33 +00:00
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
*/
2017-11-18 18:35:33 +00:00
private $repository;
/**
* Setup tests.
*/
public function setUp(): void
{
parent::setUp();
2017-11-18 18:35:33 +00:00
Carbon::setTestNow(Carbon::now());
2017-11-18 18:35:33 +00:00
$this->config = m::mock(Repository::class);
$this->encrypter = m::mock(Encrypter::class);
$this->google2FA = m::mock(Google2FA::class);
$this->repository = m::mock(UserRepositoryInterface::class);
2017-11-18 18:35:33 +00:00
$this->config->shouldReceive('get')->with('pterodactyl.auth.2fa.window')->once()->andReturn(self::TEST_WINDOW_INT);
$this->encrypter->shouldReceive('decrypt')->with(self::USER_TOTP_SECRET)->once()->andReturn(self::DECRYPTED_USER_SECRET);
}
/**
* Test that 2FA can be enabled for a user.
*/
public function testTwoFactorIsEnabledForUser()
{
2017-11-18 18:35:33 +00:00
$model = factory(User::class)->make(['totp_secret' => self::USER_TOTP_SECRET, 'use_totp' => false]);
2017-11-18 18:35:33 +00:00
$this->google2FA->shouldReceive('verifyKey')->with(self::DECRYPTED_USER_SECRET, 'test-token', self::TEST_WINDOW_INT)->once()->andReturn(true);
2018-01-05 22:33:50 +00:00
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
2017-11-18 18:35:33 +00:00
'totp_authenticated_at' => Carbon::now(),
'use_totp' => true,
])->once()->andReturnNull();
2017-11-18 18:35:33 +00:00
$this->assertTrue($this->getService()->handle($model, 'test-token'));
}
/**
* Test that 2FA can be disabled for a user.
*/
public function testTwoFactorIsDisabled()
{
2017-11-18 18:35:33 +00:00
$model = factory(User::class)->make(['totp_secret' => self::USER_TOTP_SECRET, 'use_totp' => true]);
2017-11-18 18:35:33 +00:00
$this->google2FA->shouldReceive('verifyKey')->with(self::DECRYPTED_USER_SECRET, 'test-token', self::TEST_WINDOW_INT)->once()->andReturn(true);
2018-01-05 22:33:50 +00:00
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
2017-11-18 18:35:33 +00:00
'totp_authenticated_at' => Carbon::now(),
'use_totp' => false,
])->once()->andReturnNull();
2017-11-18 18:35:33 +00:00
$this->assertTrue($this->getService()->handle($model, 'test-token'));
}
/**
* Test that 2FA will remain disabled for a user.
*/
public function testTwoFactorRemainsDisabledForUser()
{
2017-11-18 18:35:33 +00:00
$model = factory(User::class)->make(['totp_secret' => self::USER_TOTP_SECRET, 'use_totp' => false]);
2017-11-18 18:35:33 +00:00
$this->google2FA->shouldReceive('verifyKey')->with(self::DECRYPTED_USER_SECRET, 'test-token', self::TEST_WINDOW_INT)->once()->andReturn(true);
2018-01-05 22:33:50 +00:00
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, [
2017-11-18 18:35:33 +00:00
'totp_authenticated_at' => Carbon::now(),
'use_totp' => false,
])->once()->andReturnNull();
2017-11-18 18:35:33 +00:00
$this->assertTrue($this->getService()->handle($model, 'test-token', false));
}
/**
* Test that an exception is thrown if the token provided is invalid.
*
* @expectedException \Pterodactyl\Exceptions\Service\User\TwoFactorAuthenticationTokenInvalid
*/
public function testExceptionIsThrownIfTokenIsInvalid()
{
2017-11-18 18:35:33 +00:00
$model = factory(User::class)->make(['totp_secret' => self::USER_TOTP_SECRET]);
$this->google2FA->shouldReceive('verifyKey')->once()->andReturn(false);
2017-11-18 18:35:33 +00:00
$this->getService()->handle($model, 'test-token');
}
/**
2017-11-18 18:35:33 +00:00
* Return an instance of the service with mocked dependencies.
*
* @return \Pterodactyl\Services\Users\ToggleTwoFactorService
*/
2017-11-18 18:35:33 +00:00
private function getService(): ToggleTwoFactorService
{
2017-11-18 18:35:33 +00:00
return new ToggleTwoFactorService($this->encrypter, $this->google2FA, $this->config, $this->repository);
}
}