2FAuth/tests/Unit/TwoFAccountModelTest.php

148 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\Test;
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]
2021-11-30 17:39:33 +01:00
public function test_model_configuration()
{
$this->runConfigurationAssertions(
2024-09-26 23:50:01 +02:00
new TwoFAccount,
2022-07-06 17:21:48 +02:00
[],
2021-11-30 17:39:33 +01:00
[],
['*'],
[],
[
2024-03-29 09:42:54 +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)
{
$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]));
$this->forgetMock(SettingService::class);
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);
$this->forgetMock(SettingService::class);
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_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);
$this->forgetMock(SettingService::class);
2021-11-30 17:39:33 +01:00
}
#[Test]
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(Helpers::class, function (MockInterface $helpers) {
$helpers->shouldReceive('PadToBase32Format')
->andReturn('YYYY====');
});
$twofaccount = TwoFAccount::factory()->make([
'secret' => 'yyyy',
]);
$this->assertEquals('YYYY====', $twofaccount->secret);
$this->forgetMock(SettingService::class);
}
#[Test]
public function test_user_relation()
{
2024-09-26 23:50:01 +02: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
}