-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AYS-160 | Registering with Invalid Domain Extension Email (#290)
- Loading branch information
1 parent
9a88326
commit b57f77b
Showing
4 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...ava/org/ays/admin_user/model/dto/request/AdminUserRegisterApplicationCompleteRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.ays.common.util.validation; | ||
|
||
import jakarta.validation.Constraint; | ||
import jakarta.validation.Payload; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Annotation to validate email using {@link EmailValidator}. | ||
*/ | ||
@Target(value = ElementType.FIELD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Constraint(validatedBy = EmailValidator.class) | ||
public @interface Email { | ||
|
||
/** | ||
* Returns the error message when email is not valid. | ||
* | ||
* @return the error message | ||
*/ | ||
String message() default "MUST BE VALID"; | ||
|
||
/** | ||
* Returns the validation groups to which this constraint belongs. | ||
* | ||
* @return the validation groups | ||
*/ | ||
Class<?>[] groups() default {}; | ||
|
||
/** | ||
* Returns the payload associated to this constraint. | ||
* | ||
* @return the payload | ||
*/ | ||
Class<? extends Payload>[] payload() default {}; | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/org/ays/common/util/validation/EmailValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.ays.common.util.validation; | ||
|
||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import org.springframework.util.StringUtils; | ||
|
||
/** | ||
* A custom validator implementation for the {@link Email} annotation. | ||
* Validates whether the provided email invalid domain | ||
* specified regular expression. | ||
*/ | ||
class EmailValidator implements ConstraintValidator<Email, String> { | ||
|
||
private static final String EMAIL_REGEX = | ||
"^[a-zA-Z0-9çğıöşü._%+-]+[a-zA-Z0-9çğıöşü]+@[a-zA-Z0-9]+[.-]?[a-zA-Z0-9]+\\.[a-zA-Z]{2,}$"; | ||
|
||
/** | ||
* Checks whether the given value is a valid email or not. | ||
* <p>Some valid emails are:</p> | ||
* <ul> | ||
* <li>[email protected]</li> | ||
* <li>[email protected]</li> | ||
* <li>[email protected]</li> | ||
* </ul> | ||
* | ||
* <p>Some invalid emails are:</p> | ||
* <ul> | ||
* <li>user@invalid</li> | ||
* <li>user@invalid!.com</li> | ||
* <li>[email protected]</li> | ||
* </ul> | ||
* | ||
* @param email object to validate | ||
* @return true if the value is valid, false otherwise | ||
*/ | ||
@Override | ||
public boolean isValid(String email, ConstraintValidatorContext constraintValidatorContext) { | ||
|
||
if (!StringUtils.hasText(email)) { | ||
return true; | ||
} | ||
return email.matches(EMAIL_REGEX); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -450,6 +450,79 @@ void givenInvalidAdminUserRegisterApplicationCompleteRequestWithParametrizedInva | |
.completeRegistration(Mockito.anyString(), Mockito.any()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"[email protected]", | ||
"abc.def@mail#archive.com", | ||
"abc.def@mail", | ||
"[email protected]", | ||
"[email protected]" | ||
}) | ||
void givenInvalidAdminUserRegisterApplicationCompleteRequestWithParametrizedInvalidEmails_whenEmailsAreNotValid_thenReturnValidationError(String invalidEmail) throws Exception { | ||
|
||
// Given | ||
String mockId = AysRandomUtil.generateUUID(); | ||
AdminUserRegisterApplicationCompleteRequest mockRequest = new AdminUserRegisterApplicationCompleteRequestBuilder() | ||
.withValidFields() | ||
.withEmail(invalidEmail) | ||
.build(); | ||
|
||
// Then | ||
String endpoint = BASE_PATH.concat("/registration-application/").concat(mockId).concat("/complete"); | ||
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders | ||
.post(endpoint, mockRequest); | ||
|
||
AysError mockErrorResponse = AysErrorBuilder.VALIDATION_ERROR; | ||
|
||
aysMockMvc.perform(mockHttpServletRequestBuilder, mockErrorResponse) | ||
.andExpect(AysMockResultMatchersBuilders.status() | ||
.isBadRequest()) | ||
.andExpect(AysMockResultMatchersBuilders.subErrors() | ||
.isNotEmpty()); | ||
|
||
// Verify | ||
Mockito.verify(adminUserRegisterService, Mockito.never()) | ||
.completeRegistration(Mockito.anyString(), Mockito.any()); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
"[email protected]", | ||
"üşengeç-birkı[email protected]" | ||
}) | ||
void givenValidAdminUserRegisterApplicationCompleteRequestWithParametrizedValidEmails_whenEmailsAreValid_thenReturnSuccessResponse(String validEmail) throws Exception { | ||
// Given | ||
String mockId = AysRandomUtil.generateUUID(); | ||
AdminUserRegisterApplicationCompleteRequest mockRequest = new AdminUserRegisterApplicationCompleteRequestBuilder() | ||
.withValidFields() | ||
.withEmail(validEmail) | ||
.build(); | ||
|
||
// When | ||
Mockito.doNothing().when(adminUserRegisterService).completeRegistration(Mockito.anyString(), Mockito.any()); | ||
|
||
// Then | ||
String endpoint = BASE_PATH.concat("/registration-application/").concat(mockId).concat("/complete"); | ||
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = AysMockMvcRequestBuilders | ||
.post(endpoint, mockRequest); | ||
|
||
AysResponse<Void> mockResponse = AysResponseBuilder.SUCCESS; | ||
|
||
aysMockMvc.perform(mockHttpServletRequestBuilder, mockResponse) | ||
.andExpect(AysMockResultMatchersBuilders.status() | ||
.isOk()) | ||
.andExpect(AysMockResultMatchersBuilders.response() | ||
.doesNotExist()); | ||
|
||
// Verify | ||
Mockito.verify(adminUserRegisterService, Mockito.times(1)) | ||
.completeRegistration(Mockito.anyString(), Mockito.any()); | ||
} | ||
|
||
@Test | ||
void givenValidAdminUserRegisterApplicationId_whenApproveAdminUserRegisterApplication_thenReturnNothing() throws Exception { | ||
// Given | ||
|