mirror of
https://github.com/Bubka/2FAuth.git
synced 2024-12-04 22:31:58 +01:00
142 lines
3.7 KiB
PHP
142 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Console;
|
|
|
|
use App\Models\AuthLog;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Carbon;
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use Tests\FeatureTestCase;
|
|
|
|
class PurgeLogTest extends FeatureTestCase
|
|
{
|
|
/**
|
|
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
|
|
*/
|
|
protected $user;
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function setUp() : void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->user = User::factory()->create();
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function test_purgeLog_completes()
|
|
{
|
|
$this->artisan('2fauth:purge-log')
|
|
->assertSuccessful();
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function test_purgeLog_defaults_to_one_year()
|
|
{
|
|
$oneYearOldLog = AuthLog::factory()->daysAgo(366)->for($this->user, 'authenticatable')->create();
|
|
$sixMonthsOldLog = AuthLog::factory()->daysAgo(364)->for($this->user, 'authenticatable')->create();
|
|
|
|
$this->artisan('2fauth:purge-log');
|
|
|
|
$this->assertDatabaseHas('auth_logs', [
|
|
'id' => $sixMonthsOldLog->id,
|
|
]);
|
|
$this->assertDatabaseMissing('auth_logs', [
|
|
'id' => $oneYearOldLog->id,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function test_purgeLog_deletes_records_older_than_retention_time()
|
|
{
|
|
$retention = 180;
|
|
config(['2fauth.config.authLogRetentionTime' => $retention]);
|
|
$log = AuthLog::factory()->daysAgo($retention + 1)->for($this->user, 'authenticatable')->create();
|
|
|
|
$this->artisan('2fauth:purge-log');
|
|
|
|
$this->assertDatabaseMissing('auth_logs', [
|
|
'id' => $log->id,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function test_purgeLog_deletes_logout_only_records_older_than_retention_time()
|
|
{
|
|
$retention = 180;
|
|
config(['2fauth.config.authLogRetentionTime' => $retention]);
|
|
$log = AuthLog::factory()->logoutOnly()->for($this->user, 'authenticatable')->create();
|
|
|
|
$this->travelTo(Carbon::now()->addDays($retention + 1));
|
|
$this->artisan('2fauth:purge-log');
|
|
|
|
$this->assertDatabaseMissing('auth_logs', [
|
|
'id' => $log->id,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
public function test_purgeLog_does_not_delete_records_younger_than_retention_time()
|
|
{
|
|
$retention = 180;
|
|
config(['2fauth.config.authLogRetentionTime' => $retention]);
|
|
$log = AuthLog::factory()->daysAgo($retention - 1)->for($this->user, 'authenticatable')->create();
|
|
|
|
$this->artisan('2fauth:purge-log');
|
|
|
|
$this->assertDatabaseHas('auth_logs', [
|
|
'id' => $log->id,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* @test
|
|
*/
|
|
#[DataProvider('provideInvalidConfig')]
|
|
public function test_purgeLog_with_invalid_config_defaults_to_one_year($config)
|
|
{
|
|
config(['2fauth.config.authLogRetentionTime' => $config]);
|
|
$oneYearOldLog = AuthLog::factory()->daysAgo(366)->for($this->user, 'authenticatable')->create();
|
|
$sixMonthsOldLog = AuthLog::factory()->daysAgo(364)->for($this->user, 'authenticatable')->create();
|
|
|
|
$this->artisan('2fauth:purge-log');
|
|
|
|
$this->assertDatabaseHas('auth_logs', [
|
|
'id' => $sixMonthsOldLog->id,
|
|
]);
|
|
$this->assertDatabaseMissing('auth_logs', [
|
|
'id' => $oneYearOldLog->id,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Provide invalid config for validation test
|
|
*/
|
|
public static function provideInvalidConfig() : array
|
|
{
|
|
return [
|
|
'NULL' => [
|
|
null,
|
|
],
|
|
'EMPTY' => [
|
|
'',
|
|
],
|
|
'STRING' => [
|
|
'ljhkjh',
|
|
],
|
|
];
|
|
}
|
|
}
|