|
| 1 | +package dev.matheuscruz.presentation; |
| 2 | + |
| 3 | +import static io.restassured.RestAssured.given; |
| 4 | +import static org.assertj.core.api.Assertions.assertThat; |
| 5 | +import static org.assertj.core.api.Assertions.tuple; |
| 6 | +import dev.matheuscruz.domain.User; |
| 7 | +import dev.matheuscruz.domain.UserRepository; |
| 8 | +import dev.matheuscruz.presentation.data.Problem; |
| 9 | +import io.quarkus.narayana.jta.QuarkusTransaction; |
| 10 | +import io.quarkus.test.junit.QuarkusTest; |
| 11 | +import jakarta.inject.Inject; |
| 12 | +import jakarta.transaction.Transactional; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.DisplayName; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | + |
| 17 | +@QuarkusTest |
| 18 | +public class SignUpResourceTest { |
| 19 | + |
| 20 | + @Inject |
| 21 | + UserRepository userRepository; |
| 22 | + |
| 23 | + @BeforeEach |
| 24 | + @Transactional |
| 25 | + void setUp() { |
| 26 | + userRepository.deleteAll(); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + @DisplayName("should return Created when valid SignUpRequest is provided") |
| 31 | + void should_returnCreated_when_validSignUpRequestIsProvided() { |
| 32 | + var request = new SignUpResource.SignUpRequest("test@email.com", "password-1234", "John", "Doe"); |
| 33 | + |
| 34 | + var response = given().contentType("application/json").body(request).when().post("/api/sign-up").then().log() |
| 35 | + .ifValidationFails().statusCode(201).extract().as(SignUpResource.SignUpResponse.class); |
| 36 | + |
| 37 | + assertThat(response.id()).isNotNull().isNotBlank(); |
| 38 | + assertThat(response.email()).isEqualTo(request.email()); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + @DisplayName("should return Conflict when email already exists") |
| 43 | + void should_returnConflict_when_emailAlreadyExists() { |
| 44 | + var existingEmail = "existing.user@email.com"; |
| 45 | + var existingUser = User.create(existingEmail, "some-hashed-password", "Existing", "User"); |
| 46 | + |
| 47 | + QuarkusTransaction.requiringNew().run(() -> { |
| 48 | + userRepository.persist(existingUser); |
| 49 | + }); |
| 50 | + |
| 51 | + var request = new SignUpResource.SignUpRequest(existingEmail, "a-different-password", "New", "Person"); |
| 52 | + |
| 53 | + var response = given().contentType("application/json").body(request).when().post("/api/sign-up").then().log() |
| 54 | + .ifValidationFails().statusCode(409).extract().as(Problem.class); |
| 55 | + |
| 56 | + assertThat(response.message()).isEqualTo("Este nome de usuário já foi usado. Tente outro."); |
| 57 | + assertThat(userRepository.count()).isEqualTo(1); |
| 58 | + assertThat(userRepository.existsByEmail(existingEmail)).isTrue(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + @DisplayName("should return Bad Request when email is not valid") |
| 63 | + void should_returnBadRequest_when_emailIsNotValid() { |
| 64 | + var request = new SignUpResource.SignUpRequest("invalid-email.com", "password-1234", "John", "Doe"); |
| 65 | + |
| 66 | + var validationProblem = given().contentType("application/json").body(request).when().post("/api/sign-up").then() |
| 67 | + .log().ifValidationFails().statusCode(400).extract().as(ValidationProblem.class); |
| 68 | + |
| 69 | + assertThat(validationProblem.title()).isEqualTo("Constraint Violation"); |
| 70 | + assertThat(validationProblem.violations()).hasSize(1).extracting(Violation::field, Violation::message) |
| 71 | + .containsExactly(tuple("signUp.req.email", "must be a well-formed email address")); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + @DisplayName("should return Bad Request when password is too short") |
| 76 | + void should_returnBadRequest_when_passwordIsTooShort() { |
| 77 | + var request = new SignUpResource.SignUpRequest("test@email.com", "1234", "John", "Doe"); |
| 78 | + |
| 79 | + var validationProblem = given().contentType("application/json").body(request).when().post("/api/sign-up").then() |
| 80 | + .log().ifValidationFails().statusCode(400).extract().as(ValidationProblem.class); |
| 81 | + |
| 82 | + assertThat(validationProblem.title()).isEqualTo("Constraint Violation"); |
| 83 | + assertThat(validationProblem.violations()).hasSize(1).extracting(Violation::field, Violation::message) |
| 84 | + .containsExactly(tuple("signUp.req.password", "size must be between 8 and 32")); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + @DisplayName("should return Bad Request when password is too long") |
| 89 | + void should_returnBadRequest_when_passwordIsTooLong() { |
| 90 | + var request = new SignUpResource.SignUpRequest("test@email.com", "12345678910111213141516171819202122", "John", |
| 91 | + "Doe"); |
| 92 | + |
| 93 | + var validationProblem = given().contentType("application/json").body(request).when().post("/api/sign-up").then() |
| 94 | + .log().ifValidationFails().statusCode(400).extract().as(ValidationProblem.class); |
| 95 | + |
| 96 | + assertThat(validationProblem.title()).isEqualTo("Constraint Violation"); |
| 97 | + assertThat(validationProblem.violations()).hasSize(1).extracting(Violation::field, Violation::message) |
| 98 | + .containsExactly(tuple("signUp.req.password", "size must be between 8 and 32")); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + @DisplayName("should return Bad Request when password is blank") |
| 103 | + void should_returnBadRequest_when_passwordIsBlank() { |
| 104 | + var request = new SignUpResource.SignUpRequest("test@email.com", "", "John", "Doe"); |
| 105 | + |
| 106 | + var validationProblem = given().contentType("application/json").body(request).when().post("/api/sign-up").then() |
| 107 | + .log().ifValidationFails().statusCode(400).extract().as(ValidationProblem.class); |
| 108 | + |
| 109 | + assertThat(validationProblem.title()).isEqualTo("Constraint Violation"); |
| 110 | + assertThat(validationProblem.violations()).hasSize(2).extracting(Violation::field, Violation::message) |
| 111 | + .containsExactlyInAnyOrder(tuple("signUp.req.password", "size must be between 8 and 32"), |
| 112 | + tuple("signUp.req.password", "must not be blank") |
| 113 | + |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + @DisplayName("should return Bad Request when first name is blank") |
| 119 | + void should_returnBadRequest_when_firstNameIsBlank() { |
| 120 | + var request = new SignUpResource.SignUpRequest("test@email.com", "password-1234", "", "Doe"); |
| 121 | + |
| 122 | + var validationProblem = given().contentType("application/json").body(request).when().post("/api/sign-up").then() |
| 123 | + .log().ifValidationFails().statusCode(400).extract().as(ValidationProblem.class); |
| 124 | + |
| 125 | + assertThat(validationProblem.title()).isEqualTo("Constraint Violation"); |
| 126 | + assertThat(validationProblem.violations()).hasSize(1).extracting(Violation::field, Violation::message) |
| 127 | + .containsExactly(tuple("signUp.req.firstName", "must not be blank")); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + @DisplayName("should return Bad Request when last name is blank") |
| 132 | + void should_returnBadRequest_when_lastNameIsBlank() { |
| 133 | + var request = new SignUpResource.SignUpRequest("test@email.com", "password-1234", "John", ""); |
| 134 | + |
| 135 | + var validationProblem = given().contentType("application/json").body(request).when().post("/api/sign-up").then() |
| 136 | + .log().ifValidationFails().statusCode(400).extract().as(ValidationProblem.class); |
| 137 | + |
| 138 | + assertThat(validationProblem.title()).isEqualTo("Constraint Violation"); |
| 139 | + assertThat(validationProblem.violations()).hasSize(1).extracting(Violation::field, Violation::message) |
| 140 | + .containsExactly(tuple("signUp.req.lastName", "must not be blank")); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + @DisplayName("should return Bad Request when e-mail is blank") |
| 145 | + void should_returnBadRequest_when_emailIsBlank() { |
| 146 | + var request = new SignUpResource.SignUpRequest("", "password-1234", "John", "Doe"); |
| 147 | + |
| 148 | + var problem = given().contentType("application/json").body(request).when().post("/api/sign-up").then().log() |
| 149 | + .ifValidationFails().statusCode(400).extract().as(ValidationProblem.class); |
| 150 | + |
| 151 | + assertThat(problem.title()).isEqualTo("Constraint Violation"); |
| 152 | + assertThat(problem.violations()).hasSize(1).extracting(Violation::field, Violation::message) |
| 153 | + .containsExactlyInAnyOrder(tuple("signUp.req.email", "must not be blank")); |
| 154 | + } |
| 155 | + |
| 156 | + public record Violation(String field, String message) { |
| 157 | + } |
| 158 | + |
| 159 | + public record ValidationProblem(String title, int status, java.util.List<Violation> violations) { |
| 160 | + } |
| 161 | +} |
0 commit comments