Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ES-722] #41

Merged
merged 2 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ public class GenerateChallengeRequest {
private String locale;
private boolean regenerate;

@io.mosip.signup.validator.Purpose
private Purpose purpose;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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<? extends Payload>[] payload() default {};
}
Original file line number Diff line number Diff line change
@@ -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<io.mosip.signup.validator.Purpose, Purpose> {
@Override
public boolean isValid(Purpose value, ConstraintValidatorContext context) {
if(value == null)
return false;
return value.equals(Purpose.REGISTRATION) || value.equals(Purpose.RESET_PASSWORD);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Loading