2017-08-31 02:11:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit\Services\Users;
|
|
|
|
|
|
|
|
use Mockery as m;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Tests\TestCase;
|
2017-08-31 02:11:14 +00:00
|
|
|
use Pterodactyl\Models\User;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Illuminate\Contracts\Config\Repository;
|
2017-11-18 18:35:33 +00:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2017-08-31 02:11:14 +00:00
|
|
|
use Pterodactyl\Services\Users\TwoFactorSetupService;
|
2017-08-31 02:14:20 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\UserRepositoryInterface;
|
2017-08-31 02:11:14 +00:00
|
|
|
|
|
|
|
class TwoFactorSetupServiceTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* @var \Illuminate\Contracts\Config\Repository|\Mockery\Mock
|
2017-08-31 02:11:14 +00:00
|
|
|
*/
|
2017-11-18 18:35:33 +00:00
|
|
|
private $config;
|
2017-08-31 02:11:14 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter|\Mockery\Mock
|
2017-08-31 02:11:14 +00:00
|
|
|
*/
|
2017-11-18 18:35:33 +00:00
|
|
|
private $encrypter;
|
2017-08-31 02:11:14 +00:00
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* @var \Pterodactyl\Contracts\Repository\UserRepositoryInterface|\Mockery\Mock
|
2017-08-31 02:11:14 +00:00
|
|
|
*/
|
2017-11-18 18:35:33 +00:00
|
|
|
private $repository;
|
2017-08-31 02:11:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup tests.
|
|
|
|
*/
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->config = m::mock(Repository::class);
|
2017-11-18 18:35:33 +00:00
|
|
|
$this->encrypter = m::mock(Encrypter::class);
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->repository = m::mock(UserRepositoryInterface::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the correct data is returned.
|
|
|
|
*/
|
|
|
|
public function testSecretAndImageAreReturned()
|
|
|
|
{
|
|
|
|
$model = factory(User::class)->make();
|
|
|
|
|
2019-06-22 04:55:09 +00:00
|
|
|
$this->config->shouldReceive('get')->with('pterodactyl.auth.2fa.bytes', 16)->andReturn(32);
|
|
|
|
$this->config->shouldReceive('get')->with('app.name')->andReturn('Company Name');
|
|
|
|
$this->encrypter->shouldReceive('encrypt')
|
|
|
|
->with(m::on(function ($value) {
|
|
|
|
return preg_match('/([A-Z234567]{32})/', $value) !== false;
|
|
|
|
}))
|
|
|
|
->once()
|
|
|
|
->andReturn('encryptedSecret');
|
|
|
|
|
2018-01-05 22:33:50 +00:00
|
|
|
$this->repository->shouldReceive('withoutFreshModel->update')->with($model->id, ['totp_secret' => 'encryptedSecret'])->once()->andReturnNull();
|
2017-08-31 02:11:14 +00:00
|
|
|
|
2017-11-18 18:35:33 +00:00
|
|
|
$response = $this->getService()->handle($model);
|
2017-08-31 02:11:14 +00:00
|
|
|
$this->assertNotEmpty($response);
|
2019-06-22 04:55:09 +00:00
|
|
|
|
|
|
|
$companyName = preg_quote(rawurlencode('Company Name'));
|
|
|
|
$email = preg_quote(rawurlencode($model->email));
|
|
|
|
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/otpauth:\/\/totp\/' . $companyName . ':' . $email . '\?secret=([A-Z234567]{32})&issuer=' . $companyName . '/',
|
|
|
|
$response
|
|
|
|
);
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-11-18 18:35:33 +00:00
|
|
|
* Return an instance of the service to test with mocked dependencies.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Services\Users\TwoFactorSetupService
|
2017-08-31 02:11:14 +00:00
|
|
|
*/
|
2017-11-18 18:35:33 +00:00
|
|
|
private function getService(): TwoFactorSetupService
|
2017-08-31 02:11:14 +00:00
|
|
|
{
|
2019-06-22 04:55:09 +00:00
|
|
|
return new TwoFactorSetupService($this->config, $this->encrypter, $this->repository);
|
2017-08-31 02:11:14 +00:00
|
|
|
}
|
|
|
|
}
|