From fd8a679277588e1e9d1b69f2a84e93b07a0d2f4d Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Tue, 30 Jan 2024 11:27:22 +0530 Subject: [PATCH 1/2] [ES-722] Signed-off-by: Venkata Saidurga Polamraju --- .../signup/dto/GenerateChallengeRequest.java | 1 + .../io/mosip/signup/util/ErrorConstants.java | 1 + .../io/mosip/signup/validator/Purpose.java | 23 +++++++++++++++++++ .../signup/validator/PurposeValidator.java | 14 +++++++++++ 4 files changed, 39 insertions(+) create mode 100644 signup-service/src/main/java/io/mosip/signup/validator/Purpose.java create mode 100644 signup-service/src/main/java/io/mosip/signup/validator/PurposeValidator.java diff --git a/signup-service/src/main/java/io/mosip/signup/dto/GenerateChallengeRequest.java b/signup-service/src/main/java/io/mosip/signup/dto/GenerateChallengeRequest.java index 7fa20ca8..62e179d8 100644 --- a/signup-service/src/main/java/io/mosip/signup/dto/GenerateChallengeRequest.java +++ b/signup-service/src/main/java/io/mosip/signup/dto/GenerateChallengeRequest.java @@ -15,5 +15,6 @@ public class GenerateChallengeRequest { private String locale; private boolean regenerate; + @io.mosip.signup.validator.Purpose private Purpose purpose; } diff --git a/signup-service/src/main/java/io/mosip/signup/util/ErrorConstants.java b/signup-service/src/main/java/io/mosip/signup/util/ErrorConstants.java index debb1c06..90167d5c 100644 --- a/signup-service/src/main/java/io/mosip/signup/util/ErrorConstants.java +++ b/signup-service/src/main/java/io/mosip/signup/util/ErrorConstants.java @@ -8,6 +8,7 @@ public class ErrorConstants { public static final String INVALID_PASSWORD = "invalid_password"; public static final String INVALID_USERINFO = "invalid_username"; public static final String INVALID_CONSENT = "invalid_consent"; + public static final String INVALID_PURPOSE ="invalid_purpose"; public static final String IDENTIFIER_MISMATCH = "identifier_mismatch"; public static final String CONSENT_REQUIRED = "consent_required"; public static final String INVALID_TRANSACTION="invalid_transaction"; diff --git a/signup-service/src/main/java/io/mosip/signup/validator/Purpose.java b/signup-service/src/main/java/io/mosip/signup/validator/Purpose.java new file mode 100644 index 00000000..80bfe316 --- /dev/null +++ b/signup-service/src/main/java/io/mosip/signup/validator/Purpose.java @@ -0,0 +1,23 @@ +package io.mosip.signup.validator; + +import io.mosip.signup.util.ErrorConstants; + +import javax.validation.Constraint; +import javax.validation.Payload; +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.TYPE_USE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +@Target({FIELD, TYPE_USE}) +@Retention(RUNTIME) +@Constraint(validatedBy = PurposeValidator.class) +@Documented +public @interface Purpose { + String message() default ErrorConstants.INVALID_PURPOSE; + Class[] groups() default {}; + Class[] payload() default {}; +} diff --git a/signup-service/src/main/java/io/mosip/signup/validator/PurposeValidator.java b/signup-service/src/main/java/io/mosip/signup/validator/PurposeValidator.java new file mode 100644 index 00000000..496587f0 --- /dev/null +++ b/signup-service/src/main/java/io/mosip/signup/validator/PurposeValidator.java @@ -0,0 +1,14 @@ +package io.mosip.signup.validator; + +import javax.validation.ConstraintValidator; +import javax.validation.ConstraintValidatorContext; +import io.mosip.signup.util.Purpose; + +public class PurposeValidator implements ConstraintValidator { + @Override + public boolean isValid(Purpose value, ConstraintValidatorContext context) { + if(value == null) + return false; + return value.equals(Purpose.REGISTRATION) || value.equals(Purpose.RESET_PASSWORD); + } +} From e3a19fa52d8427f2fb8e17dc5ae08c5770a572a8 Mon Sep 17 00:00:00 2001 From: Venkata Saidurga Polamraju Date: Tue, 30 Jan 2024 14:59:29 +0530 Subject: [PATCH 2/2] [Es-722] Signed-off-by: Venkata Saidurga Polamraju --- .../controllers/RegistrationControllerTest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/signup-service/src/test/java/io/mosip/signup/controllers/RegistrationControllerTest.java b/signup-service/src/test/java/io/mosip/signup/controllers/RegistrationControllerTest.java index ed7641c9..761b7f17 100644 --- a/signup-service/src/test/java/io/mosip/signup/controllers/RegistrationControllerTest.java +++ b/signup-service/src/test/java/io/mosip/signup/controllers/RegistrationControllerTest.java @@ -519,6 +519,23 @@ public void doGenerateChallenge_withRegistrationPurpose_thenPass() throws Except .andExpect(jsonPath("$.errors").isEmpty()); } + @Test + public void doGenerateChallenge_withNullPurpose_returnErrorResponse() throws Exception { + String status = "SUCCESSFUL"; + GenerateChallengeResponse generateChallengeResponse = new GenerateChallengeResponse(status); + generateChallengeRequest.setPurpose(null); + when(registrationService.generateChallenge(generateChallengeRequest, "")) + .thenReturn(generateChallengeResponse); + + mockMvc.perform(post("/registration/generate-challenge") + .content(objectMapper.writeValueAsString(wrapper)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.response").isEmpty()) + .andExpect(jsonPath("$.errors").isNotEmpty()) + .andExpect(jsonPath("$.errors[0].errorCode").value(ErrorConstants.INVALID_PURPOSE)); + } + @Test public void doGenerateChallenge_withResetPasswordPurpose_thenPass() throws Exception { String status = "SUCCESSFUL";