Level 2
I solved by adding the following lines in test
$currentTimeBefore = Date::now();
Date::setTestNow($currentTimeBefore->addHour());
Summer Sale! All accounts are 50% off this week.
I am writing a test case for checking whether otp is entered after 30 minutes.
Controller:
public function verifyOTP(VerifyOTPRequest $request)
{
$user = User::where([
['username', '=', $request->username],
['otp', '=', $request->otp]
])->first();
$now = Carbon::now();
if (!$user) {
return response()->json([
'success' => 'error',
'message' => 'Your OTP is not correct'
], 503);
} elseif ($user && $now->isAfter($user->otp_expiry_time)) {
return response()->json([
'success' => 'error',
'message' => 'Your OTP has been expired'
], 503);
}
auth()->login($user, true);
$user->update([
'otp' => null,
'otp_expiry_time' => null,
]);
$accessToken = auth()->user()->createToken('authToken')->plainTextToken;
return response()->json([
'success' => 'success',
'message' => 'Login Successfully',
'access_token' => $accessToken
], 200);
}
Testing:
public function test_late_otp_for_login(): void
{
$user = User::factory()->create();
$this->postJson('/api/v1/login', [
'username' => $user->username,
'password' => 'password',
]);
$knownDate = Carbon::create(2023, 7, 22, 19);
Carbon::setTestNow($knownDate);
$user = User::where('id', $user->id)->first();
$response = $this->postJson('/api/v1/verify_otp', [
'username' => $user->username,
'otp' => $user->otp,
]);
$response->assertStatus(503);
$response->assertJsonFragment([
"success" => "error",
'message' => 'Your OTP has been expired',
]);
}
Even if i set carbon it set time it is not working. Please help me
I solved by adding the following lines in test
$currentTimeBefore = Date::now();
Date::setTestNow($currentTimeBefore->addHour());
Please or to participate in this conversation.