2FAuth/tests/Unit/TwoFAccountModelTest.php

156 lines
4.3 KiB
PHP
Raw Normal View History

2021-11-30 17:39:33 +01:00
<?php
namespace Tests\Unit;
use App\Events\TwoFAccountDeleted;
use App\Helpers\Helpers;
2022-11-22 15:15:52 +01:00
use App\Models\TwoFAccount;
use App\Services\SettingService;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2021-11-30 17:39:33 +01:00
use Illuminate\Support\Facades\Crypt;
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;
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
[],
['*'],
[],
2022-11-22 15:15:52 +01:00
['id' => 'int'],
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)
{
$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([
$attribute => 'STRING==',
2021-11-30 17:39:33 +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)
{
$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)
{
$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);
}
/**
* @test
*/
2023-08-01 11:28:27 +02:00
#[RunInSeparateProcess]
#[PreserveGlobalState(false)]
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);
}
/**
* @test
*/
public function test_user_relation()
{
2023-03-10 22:59:46 +01:00
$model = new TwoFAccount();
$relation = $model->user();
$this->assertInstanceOf(BelongsTo::class, $relation);
$this->assertEquals('user_id', $relation->getForeignKeyName());
}
2022-11-22 15:15:52 +01:00
}