Skip to content

Commit 82d1f1f

Browse files
committed
Fix
1 parent 3957070 commit 82d1f1f

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

tests/Feature/SuspiciousActivityNotificationTest.php

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
$this->loadLaravelMigrations();
1414
$this->artisan('migrate', ['--database' => 'testing'])->run();
1515
Cache::flush();
16+
17+
// Reset Carbon time to ensure test isolation (prevents time mocking from leaking between tests)
18+
\Illuminate\Support\Carbon::setTestNow();
1619
});
1720

1821
it('sends suspicious activity notification when enabled and multiple failed logins detected', function () {
@@ -149,8 +152,12 @@
149152
'authentication-log.suspicious.usual_hours' => [9, 10, 11, 12, 13, 14, 15, 16, 17],
150153
]);
151154

155+
// Set current time to 3 AM (outside usual hours) FIRST, before creating user/logs
156+
$testTime = \Illuminate\Support\Carbon::create(2024, 1, 1, 3, 0, 0);
157+
\Illuminate\Support\Carbon::setTestNow($testTime);
158+
152159
$user = TestUser::factory()->create([
153-
'created_at' => now()->subMinutes(10),
160+
'created_at' => $testTime->copy()->subMinutes(10),
154161
]);
155162

156163
// Set request values and generate device ID
@@ -160,21 +167,17 @@
160167
request()->headers->set('User-Agent', $sameUserAgent);
161168
$deviceId = \Rappasoft\LaravelAuthenticationLog\Helpers\DeviceFingerprint::generate(request());
162169

163-
// Create a previous successful login so device is recognized as known
170+
// Create a previous successful login so device is recognized as known (30 minutes before test time)
164171
AuthenticationLog::factory()->create([
165172
'authenticatable_type' => get_class($user),
166173
'authenticatable_id' => $user->id,
167174
'login_successful' => true,
168-
'login_at' => now()->subMinutes(30),
175+
'login_at' => $testTime->copy()->subMinutes(30),
169176
'ip_address' => $sameIp,
170177
'user_agent' => $sameUserAgent,
171178
'device_id' => $deviceId,
172179
]);
173180

174-
// Set current time to 3 AM (outside usual hours)
175-
$originalNow = now();
176-
\Illuminate\Support\Carbon::setTestNow(now()->setHour(3)->setMinute(0));
177-
178181
Event::dispatch(new Login('web', $user, false));
179182

180183
Notification::assertSentTo($user, SuspiciousActivity::class, function ($notification) {
@@ -183,7 +186,7 @@
183186
});
184187

185188
// Reset time
186-
\Illuminate\Support\Carbon::setTestNow($originalNow);
189+
\Illuminate\Support\Carbon::setTestNow();
187190
});
188191

189192
it('does not send notification for unusual login times when check is disabled', function () {
@@ -200,34 +203,34 @@
200203
'created_at' => now()->subMinutes(10),
201204
]);
202205

206+
// Set current time to 3 AM (outside usual hours) FIRST, before creating logs
207+
$testTime = \Illuminate\Support\Carbon::create(2024, 1, 1, 3, 0, 0);
208+
\Illuminate\Support\Carbon::setTestNow($testTime);
209+
203210
// Set request values and generate device ID
204211
$sameIp = '192.168.1.1';
205212
$sameUserAgent = 'Test Browser';
206213
request()->server->set('REMOTE_ADDR', $sameIp);
207214
request()->headers->set('User-Agent', $sameUserAgent);
208215
$deviceId = \Rappasoft\LaravelAuthenticationLog\Helpers\DeviceFingerprint::generate(request());
209216

210-
// Create a previous successful login so device is recognized as known
217+
// Create a previous successful login so device is recognized as known (30 minutes before test time)
211218
AuthenticationLog::factory()->create([
212219
'authenticatable_type' => get_class($user),
213220
'authenticatable_id' => $user->id,
214221
'login_successful' => true,
215-
'login_at' => now()->subMinutes(30),
222+
'login_at' => $testTime->copy()->subMinutes(30),
216223
'ip_address' => $sameIp,
217224
'user_agent' => $sameUserAgent,
218225
'device_id' => $deviceId,
219226
]);
220227

221-
// Set current time to 3 AM (outside usual hours)
222-
$originalNow = now();
223-
\Illuminate\Support\Carbon::setTestNow(now()->setHour(3)->setMinute(0));
224-
225228
Event::dispatch(new Login('web', $user, false));
226229

227230
Notification::assertNothingSent();
228231

229232
// Reset time
230-
\Illuminate\Support\Carbon::setTestNow($originalNow);
233+
\Illuminate\Support\Carbon::setTestNow();
231234
});
232235

233236
it('respects rate limiting for suspicious activity notifications', function () {
@@ -460,7 +463,6 @@
460463
]);
461464

462465
// Set time to unusual hour
463-
$originalNow = now();
464466
\Illuminate\Support\Carbon::setTestNow(now()->setHour(3)->setMinute(0));
465467

466468
// Trigger login which should detect multiple suspicious activities
@@ -476,5 +478,5 @@
476478
});
477479

478480
// Reset time
479-
\Illuminate\Support\Carbon::setTestNow($originalNow);
481+
\Illuminate\Support\Carbon::setTestNow();
480482
});

0 commit comments

Comments
 (0)