Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only purge old expired codes #24

Merged
merged 1 commit into from
May 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Console/PruneCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class PruneCommand extends Command

public function handle() : void
{
VerificationCode::where('created_at', '<=', now()->subHours($this->option('hours')))->delete();
VerificationCode::query()
->where('created_at', '<=', now()->subHours($this->option('hours')))
->where('expires_at', '<', now())
->delete();
}
}
37 changes: 30 additions & 7 deletions tests/Commands/PruneCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,47 @@
class PruneCommandTest extends TestCase
{
/** @test */
public function it_cleans_the_verification_code_table()
public function it_deletes_old_expired_code()
{
VerificationCode::create([
$verificationCode = VerificationCode::create([
'code' => 'ABC123',
'verifiable' => 'taylor@laravel.com',
'created_at' => now()->subHours(5),
'expires_at' => now()->subHour(),
]);
VerificationCode::where('id', $verificationCode->id)->update(['created_at' => now()->subHours(5)]);

$this->artisan('verification-code:prune', ['--hours' => 3]);

$this->assertNull(VerificationCode::find($verificationCode->id));
}

/** @test */
public function it_does_not_delete_old_but_not_expired_code()
{
$verificationCode = VerificationCode::create([
'code' => '123ABC',
'code' => 'ABC123',
'verifiable' => 'taylor@laravel.com',
'expires_at' => now()->addHour(),
]);
VerificationCode::where('id', $verificationCode->id)->update(['created_at' => now()->subHours(5)]);

$this->artisan('verification-code:prune', ['--hours' => 3]);

$dbVerificationCodes = VerificationCode::all();
$this->assertNotNull(VerificationCode::find($verificationCode->id));
}

/** @test */
public function it_does_not_delete_code_that_is_not_old_enough()
{
$verificationCode = VerificationCode::create([
'code' => 'ABC123',
'verifiable' => 'taylor@laravel.com',
'expires_at' => now()->subHour(),
]);
VerificationCode::where('id', $verificationCode->id)->update(['created_at' => now()->subHours(2)]);

$this->artisan('verification-code:prune', ['--hours' => 3]);

$this->assertCount(1, $dbVerificationCodes);
$this->assertEquals($verificationCode->id, $dbVerificationCodes->first()->id);
$this->assertNotNull(VerificationCode::find($verificationCode->id));
}
}