Skip to content

Commit

Permalink
[Hexlet#260] fix email validation interpolation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
d1z3d committed Jun 16, 2024
1 parent 4cdfb60 commit c20d43e
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 21 additions & 13 deletions src/test/java/io/hexlet/typoreporter/web/AccountControllerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/io/hexlet/typoreporter/web/SignupControllerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

0 comments on commit c20d43e

Please sign in to comment.