diff --git a/src/main/java/io/hexlet/typoreporter/service/dto/account/UpdateProfile.java b/src/main/java/io/hexlet/typoreporter/service/dto/account/UpdateProfile.java index 5fa41634..9f16a1fc 100644 --- a/src/main/java/io/hexlet/typoreporter/service/dto/account/UpdateProfile.java +++ b/src/main/java/io/hexlet/typoreporter/service/dto/account/UpdateProfile.java @@ -16,7 +16,8 @@ public class UpdateProfile { @AccountUsername private String username; - @Email(regexp = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$", message = "The email \"{0}\" incorrect") + @Email(regexp = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$", + message = "The email \"${validatedValue}\" is not valid") private String email; @NotBlank diff --git a/src/main/java/io/hexlet/typoreporter/web/model/SignupAccountModel.java b/src/main/java/io/hexlet/typoreporter/web/model/SignupAccountModel.java index 16e5edee..b475a10b 100644 --- a/src/main/java/io/hexlet/typoreporter/web/model/SignupAccountModel.java +++ b/src/main/java/io/hexlet/typoreporter/web/model/SignupAccountModel.java @@ -26,7 +26,8 @@ public class SignupAccountModel { @AccountUsername private String username; - @Email(regexp = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$", message = "The email \"{0}\" incorrect") + @Email(regexp = "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$", + message = "The email \"${validatedValue}\" is not valid") private String email; @AccountPassword diff --git a/src/test/java/io/hexlet/typoreporter/web/AccountControllerIT.java b/src/test/java/io/hexlet/typoreporter/web/AccountControllerIT.java index 2e1328df..e2773fd5 100644 --- a/src/test/java/io/hexlet/typoreporter/web/AccountControllerIT.java +++ b/src/test/java/io/hexlet/typoreporter/web/AccountControllerIT.java @@ -20,8 +20,10 @@ import static io.hexlet.typoreporter.test.Constraints.POSTGRES_IMAGE; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @AutoConfigureMockMvc @@ -67,17 +69,21 @@ void updateAccountWithWrongEmailDomain() throws Exception { .isEqualTo(correctEmailDomain); String wrongEmailDomain = "test@test"; - mockMvc.perform(post("/account/update") - .param("username", userName) - .param("email", wrongEmailDomain) - .param("password", password) - .param("confirmPassword", password) - .param("firstName", userName) - .param("lastName", userName) - .with(csrf())); + + var response = mockMvc.perform(put("/account/update") + .param("username", userName) + .param("email", wrongEmailDomain) + .param("firstName", userName) + .param("lastName", userName) + .with(user(correctEmailDomain)) + .with(csrf())) + .andExpect(status().isOk()) + .andReturn(); + var body = response.getResponse().getContentAsString(); assertThat(accountRepository.findAccountByEmail(wrongEmailDomain)).isEmpty(); assertThat(accountRepository.findAccountByEmail(correctEmailDomain).orElseThrow().getEmail()) .isEqualTo(correctEmailDomain); + assertThat(body).contains(String.format("The email "%s" is not valid", wrongEmailDomain)); } @Test @@ -99,11 +105,13 @@ void updateAccountEmailUsingDifferentCase() throws Exception { assertThat(accountRepository.findAccountByEmail(emailLowerCase)).isNotEmpty(); mockMvc.perform(put("/account/update") - .param("firstName", username) - .param("lastName", username) - .param("username", username) - .param("email", emailUpperCase) - .with(csrf())); + .param("firstName", username) + .param("lastName", username) + .param("username", username) + .param("email", emailUpperCase) + .with(user(emailLowerCase)) + .with(csrf())) + .andExpect(status().isFound()); assertThat(accountRepository.findAccountByEmail(emailUpperCase)).isEmpty(); assertThat(accountRepository.findAccountByEmail(emailLowerCase)).isNotEmpty(); } diff --git a/src/test/java/io/hexlet/typoreporter/web/SignupControllerIT.java b/src/test/java/io/hexlet/typoreporter/web/SignupControllerIT.java index 81ce229d..d0b5ce1b 100644 --- a/src/test/java/io/hexlet/typoreporter/web/SignupControllerIT.java +++ b/src/test/java/io/hexlet/typoreporter/web/SignupControllerIT.java @@ -18,6 +18,7 @@ import org.testcontainers.containers.PostgreSQLContainer; import static com.github.database.rider.core.api.configuration.Orthography.LOWERCASE; +import static io.hexlet.typoreporter.test.factory.EntitiesFactory.ACCOUNT_INCORRECT_EMAIL; import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; import static org.assertj.core.api.Assertions.assertThat; @@ -123,4 +124,20 @@ void createAccountWithWrongPassword() throws Exception { assertThat(accountRepository.findAccountByEmail(email)).isEmpty(); } + + @Test + void signupInAccountWithBadEmail() throws Exception { + model.setEmail(ACCOUNT_INCORRECT_EMAIL); + var response = mockMvc.perform(post("/signup") + .param("username", model.getUsername()) + .param("email", model.getEmail()) + .param("password", model.getPassword()) + .param("confirmPassword", model.getConfirmPassword()) + .param("firstName", model.getFirstName()) + .param("lastName", model.getLastName()) + .with(csrf())) + .andReturn(); + var body = response.getResponse().getContentAsString(); + assertThat(body).contains(String.format("The email "%s" is not valid", ACCOUNT_INCORRECT_EMAIL)); + } }