Skip to content

Commit

Permalink
AYS-160 | Registering with Invalid Domain Extension Email (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
MenekseYuncu authored Feb 29, 2024
1 parent 9a88326 commit b57f77b
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package org.ays.admin_user.model.dto.request;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;
import org.ays.common.model.dto.request.AysPhoneNumberRequest;
import org.ays.common.util.validation.Email;
import org.ays.common.util.validation.Name;

/**
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/org/ays/common/util/validation/Email.java
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 src/main/java/org/ays/common/util/validation/EmailValidator.java
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b57f77b

Please sign in to comment.