Skip to content

Commit f30e9ff

Browse files
committed
Added Password Expired Test
1 parent bd3b4db commit f30e9ff

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

tests/PasswordExpiredTest.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
use CleaniqueCoders\LaravelExpiry\Events\ExpiredPassword;
4+
use CleaniqueCoders\LaravelExpiry\Http\Middleware\PasswordExpiry;
5+
use CleaniqueCoders\LaravelExpiry\Listeners\LogoutOnExpired;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Illuminate\Http\Request;
8+
use Illuminate\Support\Facades\Auth;
9+
use Illuminate\Support\Facades\Event;
10+
use Workbench\App\Models\User;
11+
12+
use function Orchestra\Testbench\artisan;
13+
14+
uses(RefreshDatabase::class);
15+
16+
beforeEach(function () {
17+
artisan($this, 'migrate');
18+
19+
Event::fake(); // Prevent actual event dispatching
20+
});
21+
22+
it('fires ExpiredPassword event if password is expired', function () {
23+
Event::fake();
24+
25+
// Simulate an authenticated user whose account is expired
26+
$user = User::factory()->create(['password_expired_at' => now()->subDay()]);
27+
Auth::login($user);
28+
29+
$middleware = new PasswordExpiry;
30+
31+
// Handle the request through middleware
32+
$request = new Request;
33+
$middleware->handle($request, fn () => null);
34+
35+
// Assert that the ExpiredPassword event was fired
36+
Event::assertDispatched(ExpiredPassword::class, function ($event) use ($user) {
37+
return $event->user->is($user);
38+
});
39+
});
40+
41+
it('logs out the user when handling ExpiredPassword event', function () {
42+
// Simulate an authenticated user
43+
$user = User::factory()->create();
44+
Auth::login($user);
45+
46+
// Assert the user is logged in
47+
expect(Auth::check())->toBeTrue();
48+
49+
// Handle the event with the listener
50+
$listener = new LogoutOnExpired;
51+
$listener->handle(new ExpiredPassword($user));
52+
53+
// Assert the user is logged out
54+
expect(Auth::check())->toBeFalse();
55+
});

0 commit comments

Comments
 (0)