Skip to content

Commit

Permalink
Merge pull request #296 from SibirBear/dev
Browse files Browse the repository at this point in the history
fixes #291
  • Loading branch information
Malcom1986 authored Aug 22, 2024
2 parents a84cee6 + e76b3ab commit 888f381
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import jakarta.validation.Constraint;
import jakarta.validation.Payload;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* Validation annotation to validate that 2 fields have the same value.
* An array of fields and their matching confirmation fields can be supplied.
Expand All @@ -27,6 +24,8 @@
* message = "The password and it confirmation must match")}
*/
@Constraint(validatedBy = FieldMatchConsiderCaseValidator.class)
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface FieldMatchConsiderCase {

String message() default "The {first} and {second} fields must be equal";
Expand All @@ -45,16 +44,4 @@
*/
String second();

/**
* Defines several <code>@FieldMatch</code> annotations on the same element
*
* @see FieldMatchConsiderCase
*/
@Target({TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Documented
@interface List {

FieldMatchConsiderCase[] value();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@FieldMatchConsiderCase(
first = "password",
second = "confirmPassword",
message = "The password and it confirmation must match")
message = "{alert.passwords-dont-match}")
@ToString
public class SignupAccountModel {

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/messages_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,4 @@ btn.delete-from-wks=Delete from workspace

alert.password-wrong-format=Password must be between 8 and 20 characters \
and contain only latin letters, digits and symbols ~`!@#$%^&*()_-+={[}]|\:;"'<,>.?/
alert.passwords-dont-match=Confirmation does not match the password
1 change: 1 addition & 0 deletions src/main/resources/messages_ru.properties
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,4 @@ text.wks-delete-confirm=Удалить пространство?
alert.password-wrong-format=Пароль должен быть от 8 до 20 символов\
\ и содержать только буквы латинского алфавита,\
\ цифры и символы ~`!@#$%^&*()_-+={[}]|\:;"'<,>.?/
alert.passwords-dont-match=Подтверждение не совпадает с паролем
39 changes: 35 additions & 4 deletions src/test/java/io/hexlet/typoreporter/web/SignupControllerIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@
import io.hexlet.typoreporter.repository.AccountRepository;
import io.hexlet.typoreporter.test.DBUnitEnumPostgres;
import io.hexlet.typoreporter.web.model.SignupAccountModel;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static com.github.database.rider.core.api.configuration.Orthography.LOWERCASE;
import static io.hexlet.typoreporter.test.Constraints.POSTGRES_IMAGE;
import static io.hexlet.typoreporter.test.factory.EntitiesFactory.ACCOUNT_INCORRECT_EMAIL;
import static org.assertj.core.api.Assertions.assertThat;
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;
import static io.hexlet.typoreporter.test.Constraints.POSTGRES_IMAGE;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest
Expand Down Expand Up @@ -66,6 +68,14 @@ static void datasourceProperties(DynamicPropertyRegistry registry) {
"another_password", "another_password",
"another_firstName", "another_lastName");

private static ResourceBundleMessageSource source;

@BeforeAll
static void init() {
source = new ResourceBundleMessageSource();
source.setBasename("messages_en");
}

@Test
void createAccountWithIgnoreEmailCase() throws Exception {
assertThat(accountRepository.count()).isEqualTo(0L);
Expand Down Expand Up @@ -140,4 +150,25 @@ void signupInAccountWithBadEmail() throws Exception {
var body = response.getResponse().getContentAsString();
assertThat(body).contains(String.format("The email &quot;%s&quot; is not valid", ACCOUNT_INCORRECT_EMAIL));
}

@Test
void signupInPasswordsDontMatch() throws Exception {
model.setConfirmPassword("WrongPassword123");
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();
var result = accountRepository.findAccountByEmail(model.getEmail().toLowerCase());

assertThat(body).contains(source.getMessage("alert.passwords-dont-match", null, null));
assertThat(result).isEmpty();

}

}

0 comments on commit 888f381

Please sign in to comment.