Fakultas Ilmu Komputer UI
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
be-authentication-test
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Jira
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SafetyPin
be-authentication-test
Commits
83c65b8e
Commit
83c65b8e
authored
3 months ago
by
Darrel Danadyaksa Poli
Browse files
Options
Downloads
Patches
Plain Diff
[RED] Add unit tests for OTPService to validate OTP generation and verification
parent
dcad3b5b
No related branches found
No related tags found
1 merge request
!1
Login registration
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/test/java/com/safetypin/authentication/service/OTPServiceTest.java
+80
-0
80 additions, 0 deletions
.../com/safetypin/authentication/service/OTPServiceTest.java
with
80 additions
and
0 deletions
src/test/java/com/safetypin/authentication/service/OTPServiceTest.java
0 → 100644
+
80
−
0
View file @
83c65b8e
package
com.safetypin.authentication.service
;
import
org.junit.jupiter.api.Test
;
import
java.lang.reflect.Constructor
;
import
java.time.LocalDateTime
;
import
java.util.concurrent.ConcurrentHashMap
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.*;
class
OTPServiceTest
{
@Test
void
testGenerateOTP
()
{
OTPService
otpService
=
new
OTPService
();
String
email
=
"user@example.com"
;
String
otp
=
otpService
.
generateOTP
(
email
);
assertNotNull
(
otp
,
"OTP should not be null"
);
assertEquals
(
6
,
otp
.
length
(),
"OTP should be 6 characters long"
);
assertTrue
(
otp
.
matches
(
"\\d{6}"
),
"OTP should consist of 6 digits"
);
}
@Test
void
testVerifyOTPSuccess
()
{
OTPService
otpService
=
new
OTPService
();
String
email
=
"user@example.com"
;
String
otp
=
otpService
.
generateOTP
(
email
);
// Immediately verify the generated OTP; it should succeed.
boolean
result
=
otpService
.
verifyOTP
(
email
,
otp
);
assertTrue
(
result
,
"The OTP should verify successfully"
);
}
@Test
void
testVerifyOTPWrongOtp
()
{
OTPService
otpService
=
new
OTPService
();
String
email
=
"user@example.com"
;
otpService
.
generateOTP
(
email
);
// Try verifying with an incorrect OTP.
boolean
result
=
otpService
.
verifyOTP
(
email
,
"000000"
);
assertFalse
(
result
,
"Verification should fail for an incorrect OTP"
);
}
@Test
void
testVerifyOTPExpired
()
throws
Exception
{
OTPService
otpService
=
new
OTPService
();
String
email
=
"user@example.com"
;
String
otp
=
otpService
.
generateOTP
(
email
);
// Access the private otpStorage field via reflection.
java
.
lang
.
reflect
.
Field
otpStorageField
=
OTPService
.
class
.
getDeclaredField
(
"otpStorage"
);
otpStorageField
.
setAccessible
(
true
);
@SuppressWarnings
(
"unchecked"
)
ConcurrentHashMap
<
String
,
Object
>
otpStorage
=
(
ConcurrentHashMap
<
String
,
Object
>)
otpStorageField
.
get
(
otpService
);
// Retrieve the current OTPDetails instance.
Object
oldOtpDetails
=
otpStorage
.
get
(
email
);
assertNotNull
(
oldOtpDetails
,
"OTPDetails instance should exist"
);
// Use reflection to get the private constructor of OTPDetails.
Class
<?>
otpDetailsClass
=
oldOtpDetails
.
getClass
();
Constructor
<?>
constructor
=
otpDetailsClass
.
getDeclaredConstructor
(
String
.
class
,
LocalDateTime
.
class
);
constructor
.
setAccessible
(
true
);
// Create a new OTPDetails instance with an expired time (3 minutes ago).
Object
expiredOtpDetails
=
constructor
.
newInstance
(
otp
,
LocalDateTime
.
now
().
minusMinutes
(
3
));
// Replace the old OTPDetails with the expired one.
otpStorage
.
put
(
email
,
expiredOtpDetails
);
// Now verification should fail because the OTP is expired.
boolean
result
=
otpService
.
verifyOTP
(
email
,
otp
);
assertFalse
(
result
,
"The OTP should be expired and verification should fail"
);
}
@Test
void
testVerifyOTPWhenNotGenerated
()
{
OTPService
otpService
=
new
OTPService
();
// No OTP was generated for this email, so verification should return false.
boolean
result
=
otpService
.
verifyOTP
(
"nonexistent@example.com"
,
"123456"
);
assertFalse
(
result
,
"Verification should fail when no OTP is generated for the given email"
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment