2021-11-30 17:39:33 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Unit;
|
|
|
|
|
|
|
|
use App\Events\TwoFAccountDeleted;
|
2022-12-13 09:08:22 +01:00
|
|
|
use App\Helpers\Helpers;
|
2022-11-22 15:15:52 +01:00
|
|
|
use App\Models\TwoFAccount;
|
|
|
|
use App\Services\SettingService;
|
2023-03-08 17:43:26 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
2021-11-30 17:39:33 +01:00
|
|
|
use Illuminate\Support\Facades\Crypt;
|
2022-07-29 18:34:27 +02:00
|
|
|
use Mockery\MockInterface;
|
2023-08-01 11:28:27 +02:00
|
|
|
use PHPUnit\Framework\Attributes\CoversClass;
|
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
|
|
use PHPUnit\Framework\Attributes\PreserveGlobalState;
|
2023-12-20 17:08:18 +01:00
|
|
|
use PHPUnit\Framework\Attributes\RequiresPhp;
|
2023-08-01 11:28:27 +02:00
|
|
|
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Tests\ModelTestCase;
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
/**
|
2023-08-01 11:28:27 +02:00
|
|
|
* TwoFAccountModelTest test class
|
2021-11-30 17:39:33 +01:00
|
|
|
*/
|
2023-08-01 11:28:27 +02:00
|
|
|
#[CoversClass(TwoFAccount::class)]
|
2021-11-30 17:39:33 +01:00
|
|
|
class TwoFAccountModelTest extends ModelTestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_model_configuration()
|
|
|
|
{
|
|
|
|
$this->runConfigurationAssertions(
|
|
|
|
new TwoFAccount(),
|
2022-07-06 17:21:48 +02:00
|
|
|
[],
|
2021-11-30 17:39:33 +01:00
|
|
|
[],
|
|
|
|
['*'],
|
|
|
|
[],
|
2024-03-14 14:49:49 +01:00
|
|
|
[
|
|
|
|
'id' => 'int',
|
|
|
|
'user_id' => 'integer'
|
|
|
|
],
|
2021-11-30 17:39:33 +01:00
|
|
|
['deleted' => TwoFAccountDeleted::class],
|
|
|
|
['created_at', 'updated_at'],
|
|
|
|
\Illuminate\Database\Eloquent\Collection::class,
|
|
|
|
'twofaccounts',
|
|
|
|
'id',
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-08-01 11:28:27 +02:00
|
|
|
#[DataProvider('provideSensitiveAttributes')]
|
2021-11-30 17:39:33 +01:00
|
|
|
public function test_sensitive_attributes_are_stored_encrypted(string $attribute)
|
|
|
|
{
|
2022-07-29 18:34:27 +02:00
|
|
|
$settingService = $this->mock(SettingService::class, function (MockInterface $settingService) {
|
|
|
|
$settingService->shouldReceive('get')
|
|
|
|
->with('useEncryption')
|
|
|
|
->andReturn(true);
|
|
|
|
});
|
|
|
|
|
2021-12-02 13:15:53 +01:00
|
|
|
$twofaccount = TwoFAccount::factory()->make([
|
2022-12-13 09:08:22 +01:00
|
|
|
$attribute => 'STRING==',
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
|
2022-12-13 09:08:22 +01:00
|
|
|
$this->assertEquals('STRING==', Crypt::decryptString($twofaccount->getAttributes()[$attribute]));
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide attributes to test for encryption
|
|
|
|
*/
|
2023-08-01 11:28:27 +02:00
|
|
|
public static function provideSensitiveAttributes() : array
|
2021-11-30 17:39:33 +01:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
2022-11-22 15:15:52 +01:00
|
|
|
'legacy_uri',
|
2021-11-30 17:39:33 +01:00
|
|
|
],
|
|
|
|
[
|
2022-11-22 15:15:52 +01:00
|
|
|
'secret',
|
2021-11-30 17:39:33 +01:00
|
|
|
],
|
|
|
|
[
|
2022-11-22 15:15:52 +01:00
|
|
|
'account',
|
2021-11-30 17:39:33 +01:00
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-08-01 11:28:27 +02:00
|
|
|
#[DataProvider('provideSensitiveAttributes')]
|
2021-11-30 17:39:33 +01:00
|
|
|
public function test_sensitive_attributes_are_returned_clear(string $attribute)
|
|
|
|
{
|
2022-07-29 18:34:27 +02:00
|
|
|
$settingService = $this->mock(SettingService::class, function (MockInterface $settingService) {
|
|
|
|
$settingService->shouldReceive('get')
|
|
|
|
->with('useEncryption')
|
|
|
|
->andReturn(false);
|
|
|
|
});
|
|
|
|
|
2021-12-02 13:15:53 +01:00
|
|
|
$twofaccount = TwoFAccount::factory()->make();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
$this->assertEquals($twofaccount->getAttributes()[$attribute], $twofaccount->$attribute);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-08-01 11:28:27 +02:00
|
|
|
#[DataProvider('provideSensitiveAttributes')]
|
2021-11-30 17:39:33 +01:00
|
|
|
public function test_indecipherable_attributes_returns_masked_value(string $attribute)
|
|
|
|
{
|
2022-07-29 18:34:27 +02:00
|
|
|
$settingService = $this->mock(SettingService::class, function (MockInterface $settingService) {
|
|
|
|
$settingService->shouldReceive('get')
|
|
|
|
->with('useEncryption')
|
|
|
|
->andReturn(true);
|
|
|
|
});
|
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
Crypt::shouldReceive('encryptString')
|
|
|
|
->andReturn('indecipherableString');
|
|
|
|
|
2021-12-02 13:15:53 +01:00
|
|
|
$twofaccount = TwoFAccount::factory()->make();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
$this->assertEquals(__('errors.indecipherable'), $twofaccount->$attribute);
|
|
|
|
}
|
2022-12-13 09:08:22 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2023-08-01 11:28:27 +02:00
|
|
|
#[RunInSeparateProcess]
|
|
|
|
#[PreserveGlobalState(false)]
|
2023-12-20 17:08:18 +01:00
|
|
|
#[RequiresPhp('< 8.3.0')]
|
2022-12-13 09:08:22 +01:00
|
|
|
public function test_secret_is_uppercased_and_padded_at_setup()
|
|
|
|
{
|
|
|
|
$settingService = $this->mock(SettingService::class, function (MockInterface $settingService) {
|
|
|
|
$settingService->shouldReceive('get')
|
|
|
|
->with('useEncryption')
|
|
|
|
->andReturn(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
$helpers = $this->mock('alias:' . Helpers::class, function (MockInterface $helpers) {
|
|
|
|
$helpers->shouldReceive('PadToBase32Format')
|
|
|
|
->andReturn('YYYY====');
|
|
|
|
});
|
|
|
|
|
|
|
|
$twofaccount = TwoFAccount::factory()->make([
|
|
|
|
'secret' => 'yyyy',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->assertEquals('YYYY====', $twofaccount->secret);
|
|
|
|
}
|
2023-03-08 17:43:26 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_user_relation()
|
|
|
|
{
|
2023-03-10 22:59:46 +01:00
|
|
|
$model = new TwoFAccount();
|
2023-03-08 17:43:26 +01:00
|
|
|
$relation = $model->user();
|
|
|
|
|
|
|
|
$this->assertInstanceOf(BelongsTo::class, $relation);
|
|
|
|
$this->assertEquals('user_id', $relation->getForeignKeyName());
|
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|