Skip to content

Commit

Permalink
ensure that expired tokens can be prunned
Browse files Browse the repository at this point in the history
  • Loading branch information
njoguamos committed Jul 25, 2024
1 parent 8d040e1 commit 466353f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
7 changes: 7 additions & 0 deletions database/factories/PesapalTokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ public function definition(): array
'expires_at' => now()->addMinutes(5),
];
}

public function expired(): static
{
return $this->state(fn (array $attributes) => [
'expires_at' => now()->subMinute()
]);
}
}
16 changes: 16 additions & 0 deletions tests/Unit/Models/PesapalTokenTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
<?php

use Illuminate\Support\Facades\DB;
use NjoguAmos\Pesapal\Models\PesapalToken;

use function Pest\Laravel\artisan;

it(description: 'can encrypts access_token when saving to database', closure: function () {
$token = PesapalToken::factory()->create();

$dbToken = DB::table('pesapal_tokens')->first();

expect(value: $token->access_token)->not->toBe(expected: $dbToken->access_token);
});

it(description: 'can prune expired tokens', closure: function () {
$expiredOtp = PesapalToken::factory()->expired()->create();
$validOtp = PesapalToken::factory()->create();

expect(value: PesapalToken::count())->toBe(expected: 2);

artisan(command: 'model:prune', parameters: ['--model' => PesapalToken::class]);

expect(value: PesapalToken::count())->toBe(expected: 1)
->and(value: $expiredOtp->fresh()?->exists())->toBeNull()
->and(value: $validOtp->fresh()->exists())->toBeTrue();
});

0 comments on commit 466353f

Please sign in to comment.