Skip to content

Commit

Permalink
hotfix: signup email fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoMeireles55 committed Jan 28, 2025
1 parent 2ee3456 commit efcf3b7
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ResponseEntity<Void> changePassword(
@Transactional
@PostMapping("/sign-up")
public ResponseEntity<Void> signUp(@Valid @RequestBody final SignUpUsersDTO signUpUsersDTO) {
userService.signUp(signUpUsersDTO.username(), signUpUsersDTO.email(), signUpUsersDTO.password());
userService.signUp(signUpUsersDTO.identifier(), signUpUsersDTO.email(), signUpUsersDTO.password());
return ResponseEntity.noContent().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import jakarta.validation.constraints.Pattern;

public record SignUpUsersDTO(
@NotNull String identifier,
@Email
String email,
@NotNull @Pattern(
regexp = "^(?=.*[!@#$%^&*()_+\\-=\\[\\]{};':\",.<>/?])(?=.*\\d{4,}).+$",
message = "Password must contain at least 4 characters and one special character.")
String password,
@NotNull String username
String password


) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class TokenService {
public TokenJwtDTO generateToken(User user) {
try {
var algorithm = Algorithm.HMAC256(SECRET);
return new TokenJwtDTO(JWT.create().withIssuer(ISSUER).withSubject(user.getEmail())
return new TokenJwtDTO(JWT.create().withIssuer(ISSUER).withSubject(user.getUsername())
.withExpiresAt(dateExp()).sign(algorithm), dateExp());
} catch (JWTCreationException exception) {
throw new RuntimeException("Error generating token", exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void signUp_return_204() throws Exception {
mockMvc.perform(post("/users/sign-up").contentType(MediaType.APPLICATION_JSON)
.content(usersRecordJacksonTester.write(usersDTO).getJson()))
.andExpect(status().isNoContent())
.andExpect(jsonPath("$.username").value("testUser"))
.andExpect(jsonPath("$.identifier").value("testUser"))
.andExpect(jsonPath("$.identifier").value("[email protected]"));

verify(userService).signUp(usersDTO.username(), usersDTO.password(), usersDTO.email());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ void testRecoverPassword_UserExists() {
when(userRepository.existsByUsernameAndEmail(anyString(), anyString())).thenReturn(true);
when(passwordRecoveryTokenManager.generateTemporaryPassword()).thenReturn("tempPassword");

userService.recoverPassword("username", "[email protected]");
userService.recoverPassword("identifier", "[email protected]");

verify(passwordRecoveryTokenManager).generateAndStoreToken("[email protected]",
"tempPassword");
Expand All @@ -52,12 +52,12 @@ void testRecoverPassword_UserDoesNotExist() {
when(userRepository.existsByUsernameAndEmail(anyString(), anyString())).thenReturn(false);

assertThrows(CustomGlobalErrorHandling.ResourceNotFoundException.class,
() -> userService.recoverPassword("username", "[email protected]"));
() -> userService.recoverPassword("identifier", "[email protected]"));
}

@Test
void testChangePassword_ValidToken() {
User user = new User("username", BCryptEncoderComponent.encrypt("newPassword"),
User user = new User("identifier", BCryptEncoderComponent.encrypt("newPassword"),
"[email protected]", UserRoles.USER);
when(passwordRecoveryTokenManager.isRecoveryTokenValid(anyString(), anyString()))
.thenReturn(true);
Expand All @@ -80,44 +80,44 @@ void testSignUp_UserAlreadyExists() {
when(userRepository.existsByEmail(anyString())).thenReturn(true);

assertThrows(CustomGlobalErrorHandling.UserAlreadyExistException.class,
() -> userService.signUp("username", "password", "[email protected]"));
() -> userService.signUp("identifier", "password", "[email protected]"));
}

@Test
void testSignUp_NewUser() {
when(userRepository.existsByEmail(anyString())).thenReturn(false);
when(userRepository.save(any(User.class))).thenReturn(
new User("username", "encryptedPassword", "[email protected]", UserRoles.USER));
new User("identifier", "encryptedPassword", "[email protected]", UserRoles.USER));

User user = userService.signUp("username", "password", "[email protected]");
User user = userService.signUp("identifier", "password", "[email protected]");

assertNotNull(user);
assertEquals("username", user.getUsername());
assertEquals("identifier", user.getUsername());
assertEquals("[email protected]", user.getEmail());
}

@Test
void should_return_error_with_testUpdateUserPassword_PasswordMatches() {
User user = new User("username", BCryptEncoderComponent.encrypt("newPassword"),
User user = new User("identifier", BCryptEncoderComponent.encrypt("newPassword"),
"[email protected]", UserRoles.USER);

when(userRepository.getReferenceByUsernameAndEmail(anyString(), anyString()))
.thenReturn(user);

assertThrows(CustomGlobalErrorHandling.PasswordNotMatchesException.class, () -> userService
.updateUserPassword("username", "[email protected]", "oldPassword", "newPassword"));
.updateUserPassword("identifier", "[email protected]", "oldPassword", "newPassword"));
verify(userRepository, never()).setPasswordWhereByUsername(anyString(), anyString());
}

@Test
void testUpdateUserPassword_PasswordDoesNotMatch() {
User user = new User("username", BCryptEncoderComponent.encrypt("oldPassword"),
User user = new User("identifier", BCryptEncoderComponent.encrypt("oldPassword"),
"[email protected]", UserRoles.USER);
when(userRepository.getReferenceByUsernameAndEmail(anyString(), anyString()))
.thenReturn(user);

assertThrows(CustomGlobalErrorHandling.PasswordNotMatchesException.class,
() -> userService.updateUserPassword("username", "[email protected]",
() -> userService.updateUserPassword("identifier", "[email protected]",
"wrongPassword", "newPassword"));
}
}

0 comments on commit efcf3b7

Please sign in to comment.