Skip to content

Commit

Permalink
Merge pull request #24 from nextapps-be/feature/only-purge-expired-codes
Browse files Browse the repository at this point in the history
Only purge old expired codes
  • Loading branch information
gdebrauwer authored May 18, 2022
2 parents 51562dd + a1892d8 commit cc4d611
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
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' => '[email protected]',
'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' => '[email protected]',
'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' => '[email protected]',
'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));
}
}

0 comments on commit cc4d611

Please sign in to comment.