2FAuth/tests/Unit/TwoFAccountModelTest.php

121 lines
2.9 KiB
PHP
Raw Normal View History

2021-11-30 17:39:33 +01:00
<?php
namespace Tests\Unit;
2021-12-02 13:15:53 +01:00
use App\Models\TwoFAccount;
2021-11-30 17:39:33 +01:00
use App\Events\TwoFAccountDeleted;
use Tests\ModelTestCase;
use Illuminate\Support\Facades\Event;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Crypt;
/**
2021-12-02 13:15:53 +01:00
* @covers \App\Models\TwoFAccount
2021-11-30 17:39:33 +01:00
*/
class TwoFAccountModelTest extends ModelTestCase
{
/**
* @test
*/
public function test_model_configuration()
{
$this->runConfigurationAssertions(
new TwoFAccount(),
[
'service',
'account',
'otp_type',
'digits',
'secret',
'algorithm',
'counter',
'period',
'icon'
],
2021-11-30 17:39:33 +01:00
[],
['*'],
[],
['id' => 'int'],
['deleted' => TwoFAccountDeleted::class],
['created_at', 'updated_at'],
\Illuminate\Database\Eloquent\Collection::class,
'twofaccounts',
'id',
true
);
}
/**
* @test
*
* @dataProvider provideSensitiveAttributes
*/
public function test_sensitive_attributes_are_stored_encrypted(string $attribute)
{
2021-12-01 13:47:20 +01:00
\Facades\App\Services\SettingService::shouldReceive('get')
2021-11-30 17:39:33 +01:00
->with('useEncryption')
->andReturn(true);
2021-12-02 13:15:53 +01:00
$twofaccount = TwoFAccount::factory()->make([
2021-11-30 17:39:33 +01:00
$attribute => 'string',
]);
$this->assertEquals('string', Crypt::decryptString($twofaccount->getAttributes()[$attribute]));
}
/**
* Provide attributes to test for encryption
*/
public function provideSensitiveAttributes() : array
{
return [
[
'legacy_uri'
],
[
'secret'
],
[
'account'
],
];
}
/**
* @test
*
* @dataProvider provideSensitiveAttributes
*/
public function test_sensitive_attributes_are_returned_clear(string $attribute)
{
2021-12-01 13:47:20 +01:00
\Facades\App\Services\SettingService::shouldReceive('get')
2021-11-30 17:39:33 +01:00
->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
*
* @dataProvider provideSensitiveAttributes
*/
public function test_indecipherable_attributes_returns_masked_value(string $attribute)
{
2021-12-01 13:47:20 +01:00
\Facades\App\Services\SettingService::shouldReceive('get')
2021-11-30 17:39:33 +01:00
->with('useEncryption')
->andReturn(true);
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);
}
}