Skip to content

Commit

Permalink
[ES-936] Handled captcha token null error (#647)
Browse files Browse the repository at this point in the history
Signed-off-by: Balaji <74903654+balaji-alluru@users.noreply.github.com>
  • Loading branch information
balaji-alluru authored Apr 17, 2024
1 parent eb46aab commit 3d7dea7
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ mosip.esignet.header-filter.paths-to-validate={'${server.servlet.path}/authoriza

#This property is used for captcha validation and allowed values are send-otp and pwd.
#captcha validation is enabled for send-otp and pwd.
mosip.esignet.captcha.required=send-otp,pwd
mosip.esignet.captcha.required=pwd

## ------------------------------------------ e-Signet binding ---------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ public class AuthorizationHelperService {
private List<String> credentialScopes;

protected void validateSendOtpCaptchaToken(String captchaToken) {
if(captchaRequired.contains("send-otp")) {
if(!captchaRequired.contains("send-otp")) {
log.warn("captcha validation is disabled for send-otp request!");
return;
}
if(!StringUtils.hasText(captchaToken)) {
log.error("Captcha token is Null or Empty");
throw new EsignetException(ErrorConstants.INVALID_CAPTCHA);
}
validateCaptchaToken(captchaToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,39 @@ public class AuthorizationHelperServiceTest {

@Mock
private CaptchaValidator captchaValidator;

@Test
public void validateSendOtpCaptchaToken_withEmptyToken_thenFail() {
ReflectionTestUtils.setField(authorizationHelperService, "captchaRequired", List.of("send-otp"));
try {
authorizationHelperService.validateSendOtpCaptchaToken("");
} catch(EsignetException e) {
Assert.assertEquals(ErrorConstants.INVALID_CAPTCHA, e.getErrorCode());
}
}

@Test
public void validateSendOtpCaptchaToken_withValidToken_thenFail() {
ReflectionTestUtils.setField(authorizationHelperService, "captchaRequired", List.of("send-otp"));
ReflectionTestUtils.setField(authorizationHelperService, "captchaValidator", captchaValidator);
Mockito.when(captchaValidator.validateCaptcha(Mockito.anyString())).thenReturn(false);
try {
authorizationHelperService.validateSendOtpCaptchaToken("captcha-token");
} catch(EsignetException e) {
Assert.assertEquals(ErrorConstants.INVALID_CAPTCHA, e.getErrorCode());
}
}

@Test
public void validateSendOtpCaptchaToken_withValidToken_thenPass() {
ReflectionTestUtils.setField(authorizationHelperService, "captchaRequired", List.of("send-otp"));
ReflectionTestUtils.setField(authorizationHelperService, "captchaValidator", captchaValidator);
Mockito.when(captchaValidator.validateCaptcha(Mockito.anyString())).thenReturn(true);
authorizationHelperService.validateSendOtpCaptchaToken("captcha-token");
}

@Test
public void validateCaptchaToken_withNoValidator_thenFail() {
ReflectionTestUtils.setField(authorizationHelperService, "captchaRequired", List.of("send-otp"));
ReflectionTestUtils.setField(authorizationHelperService, "captchaValidator", null);
try {
authorizationHelperService.validateCaptchaToken("captcha-token");
Expand All @@ -83,7 +112,6 @@ public void validateCaptchaToken_withNoValidator_thenFail() {

@Test
public void validateCaptchaToken_withInvalidToken_thenFail() {
ReflectionTestUtils.setField(authorizationHelperService, "captchaRequired", List.of("send-otp"));
ReflectionTestUtils.setField(authorizationHelperService, "captchaValidator", captchaValidator);
Mockito.when(captchaValidator.validateCaptcha(Mockito.anyString())).thenReturn(false);
try {
Expand All @@ -93,10 +121,9 @@ public void validateCaptchaToken_withInvalidToken_thenFail() {
Assert.assertEquals(ErrorConstants.INVALID_CAPTCHA, e.getErrorCode());
}
}

@Test
public void validateCaptchaToken_withValidToken_thenPass() {
ReflectionTestUtils.setField(authorizationHelperService, "captchaRequired", List.of("send-otp"));
ReflectionTestUtils.setField(authorizationHelperService, "captchaValidator", captchaValidator);
Mockito.when(captchaValidator.validateCaptcha(Mockito.anyString())).thenReturn(true);
authorizationHelperService.validateCaptchaToken("captcha-token");
Expand Down

0 comments on commit 3d7dea7

Please sign in to comment.