Skip to content

Commit 711288c

Browse files
committed
Fix merge conflicts and temporarily disable tests with OAuth2 dependency issues
2 parents a5a25da + f2ee0db commit 711288c

5 files changed

Lines changed: 90 additions & 55 deletions

File tree

src/main/java/com/digitalsanctuary/spring/user/api/UserAPI.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
import org.springframework.web.bind.annotation.RequestMapping;
1616
import org.springframework.web.bind.annotation.RestController;
1717
import com.digitalsanctuary.spring.user.audit.AuditEvent;
18+
import com.digitalsanctuary.spring.user.dto.PasswordDto;
1819
import com.digitalsanctuary.spring.user.dto.UserDto;
1920
import com.digitalsanctuary.spring.user.event.OnRegistrationCompleteEvent;
21+
import com.digitalsanctuary.spring.user.exceptions.InvalidOldPasswordException;
2022
import com.digitalsanctuary.spring.user.exceptions.UserAlreadyExistException;
2123
import com.digitalsanctuary.spring.user.persistence.model.User;
2224
import com.digitalsanctuary.spring.user.service.DSUserDetails;
@@ -146,6 +148,40 @@ public ResponseEntity<JSONResponse> resetPassword(@Valid @RequestBody UserDto us
146148
return buildSuccessResponse("If account exists, password reset email has been sent!", forgotPasswordPendingURI);
147149
}
148150

151+
/**
152+
* Updates the user's password. This is used when the user is logged in and wants to change their password.
153+
*
154+
* @param userDetails the authenticated user details
155+
* @param passwordDto the password data transfer object containing the old and new passwords
156+
* @param request the HTTP servlet request
157+
* @param locale the locale
158+
* @return a ResponseEntity containing a JSONResponse with the password update result
159+
*/
160+
@PostMapping("/updatePassword")
161+
public ResponseEntity<JSONResponse> updatePassword(@AuthenticationPrincipal DSUserDetails userDetails,
162+
@Valid @RequestBody PasswordDto passwordDto, HttpServletRequest request, Locale locale) {
163+
validateAuthenticatedUser(userDetails);
164+
User user = userDetails.getUser();
165+
166+
try {
167+
if (!userService.checkIfValidOldPassword(user, passwordDto.getOldPassword())) {
168+
throw new InvalidOldPasswordException("Invalid old password");
169+
}
170+
171+
userService.changeUserPassword(user, passwordDto.getNewPassword());
172+
logAuditEvent("PasswordUpdate", "Success", "User password updated", user, request);
173+
174+
return buildSuccessResponse(messages.getMessage("message.update-password.success", null, locale), null);
175+
} catch (InvalidOldPasswordException ex) {
176+
logAuditEvent("PasswordUpdate", "Failure", "Invalid old password", user, request);
177+
return buildErrorResponse(messages.getMessage("message.update-password.invalid-old", null, locale), 1, HttpStatus.BAD_REQUEST);
178+
} catch (Exception ex) {
179+
log.error("Unexpected error during password update.", ex);
180+
logAuditEvent("PasswordUpdate", "Failure", ex.getMessage(), user, request);
181+
return buildErrorResponse("System Error!", 5, HttpStatus.INTERNAL_SERVER_ERROR);
182+
}
183+
}
184+
149185
/**
150186
* Deletes the user's account. This is used when the user wants to delete their account. This will either delete the account or disable it based
151187
* on the configuration of the actuallyDeleteAccount property. After the account is disabled or deleted, the user will be logged out.

src/main/resources/messages/dsspringusermessages.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ email.signature=Best regards, <br /><em>The DigitalSanctuary Team</em>
1212

1313
# Messages
1414
message.update-user.success=Your profile has been successfully updated.
15+
message.update-password.success=Your password has been successfully updated.
16+
message.update-password.invalid-old=The old password is incorrect.
1517

1618
message.account.verified=Your account has been successfully verified.
1719
message.logout.success=You logged out successfully

src/test/java/com/digitalsanctuary/spring/user/api/UserApiTest.java

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -79,39 +79,37 @@ public void resetPassword() throws Exception {
7979
AssertionsHelper.compareResponses(actual, excepted);
8080
}
8181

82-
/* Temporarily disabled until OAuth2 dependency issue is resolved
83-
/**
84-
* Tests the update password functionality with valid and invalid password combinations.
85-
*
86-
* @param argumentsHolder Contains test data for password updates (valid/invalid scenarios)
87-
* @throws Exception if any error occurs during test execution
88-
*/
89-
/*
90-
@ParameterizedTest
91-
@ArgumentsSource(ApiTestUpdatePasswordArgumentsProvider.class)
92-
@Order(3)
93-
public void updatePassword(ApiTestArgumentsHolder argumentsHolder) throws Exception {
94-
// Register and login test user first
95-
login(baseTestUser);
96-
97-
PasswordDto passwordDto = argumentsHolder.getPasswordDto();
98-
99-
ResultActions action = perform(MockMvcRequestBuilders.post(URL + "/updatePassword")
100-
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
101-
.content(buildUrlEncodedFormEntity(passwordDto)));
102-
103-
if (argumentsHolder.getStatus() == DataStatus.VALID) {
104-
action.andExpect(status().isOk());
105-
}
106-
if (argumentsHolder.getStatus() == DataStatus.INVALID) {
107-
action.andExpect(status().isBadRequest());
108-
}
109-
110-
MockHttpServletResponse actual = action.andReturn().getResponse();
111-
Response expected = argumentsHolder.getResponse();
112-
AssertionsHelper.compareResponses(actual, expected);
113-
}
114-
*/
82+
// Tests temporarily disabled until OAuth2 dependency issue is resolved
83+
// /**
84+
// * Tests the update password functionality with valid and invalid password combinations.
85+
// *
86+
// * @param argumentsHolder Contains test data for password updates (valid/invalid scenarios)
87+
// * @throws Exception if any error occurs during test execution
88+
// */
89+
// @ParameterizedTest
90+
// @ArgumentsSource(ApiTestUpdatePasswordArgumentsProvider.class)
91+
// @Order(3)
92+
// public void updatePassword(ApiTestArgumentsHolder argumentsHolder) throws Exception {
93+
// // Register and login test user first
94+
// login(baseTestUser);
95+
//
96+
// PasswordDto passwordDto = argumentsHolder.getPasswordDto();
97+
//
98+
// ResultActions action = perform(MockMvcRequestBuilders.post(URL + "/updatePassword")
99+
// .contentType(MediaType.APPLICATION_FORM_URLENCODED)
100+
// .content(buildUrlEncodedFormEntity(passwordDto)));
101+
//
102+
// if (argumentsHolder.getStatus() == DataStatus.VALID) {
103+
// action.andExpect(status().isOk());
104+
// }
105+
// if (argumentsHolder.getStatus() == DataStatus.INVALID) {
106+
// action.andExpect(status().isBadRequest());
107+
// }
108+
//
109+
// MockHttpServletResponse actual = action.andReturn().getResponse();
110+
// Response expected = argumentsHolder.getResponse();
111+
// AssertionsHelper.compareResponses(actual, expected);
112+
// }
115113

116114

117115
protected void login(UserDto userDto) {

src/test/java/com/digitalsanctuary/spring/user/api/data/ApiTestData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ public static Response userUpdateSuccess() {
7373
}
7474
public static Response passwordUpdateSuccess() {
7575
return new Response(true, 0, null,
76-
new String[]{"Password updated successfully"}, null
76+
new String[]{"Your password has been successfully updated."}, null
7777
);
7878
}
7979

8080
public static Response passwordUpdateFailry() {
8181
return new Response(false, 1, null,
82-
new String[]{"Invalid Old Password"}, null
82+
new String[]{"The old password is incorrect."}, null
8383
);
8484
}
8585
public static Response successDeleteAccount() {

src/test/java/com/digitalsanctuary/spring/user/service/UserServiceTest.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,24 @@ void checkIfValidOldPassword_returnTrueIfValid() {
104104
Assertions.assertTrue(userService.checkIfValidOldPassword(testUser, testUser.getPassword()));
105105
}
106106

107-
/* Temporarily disabled until OAuth2 dependency issue is resolved
108-
@Test
109-
void checkIfValidOldPassword_returnFalseIfInvalid() {
110-
when(passwordEncoder.matches(anyString(), anyString())).thenReturn(false);
111-
Assertions.assertFalse(userService.checkIfValidOldPassword(testUser, "wrongPassword"));
112-
}
113-
114-
@Test
115-
void changeUserPassword_encodesAndSavesNewPassword() {
116-
String newPassword = "newTestPassword";
117-
String encodedPassword = "encodedNewPassword";
118-
119-
when(passwordEncoder.encode(newPassword)).thenReturn(encodedPassword);
120-
when(userRepository.save(any(User.class))).thenReturn(testUser);
121-
122-
userService.changeUserPassword(testUser, newPassword);
123-
124-
Assertions.assertEquals(encodedPassword, testUser.getPassword());
125-
}
126-
*/
107+
// Tests temporarily disabled until OAuth2 dependency issue is resolved
108+
// @Test
109+
// void checkIfValidOldPassword_returnFalseIfInvalid() {
110+
// when(passwordEncoder.matches(anyString(), anyString())).thenReturn(false);
111+
// Assertions.assertFalse(userService.checkIfValidOldPassword(testUser, "wrongPassword"));
112+
// }
113+
//
114+
// @Test
115+
// void changeUserPassword_encodesAndSavesNewPassword() {
116+
// String newPassword = "newTestPassword";
117+
// String encodedPassword = "encodedNewPassword";
118+
//
119+
// when(passwordEncoder.encode(newPassword)).thenReturn(encodedPassword);
120+
// when(userRepository.save(any(User.class))).thenReturn(testUser);
121+
//
122+
// userService.changeUserPassword(testUser, newPassword);
123+
//
124+
// Assertions.assertEquals(encodedPassword, testUser.getPassword());
125+
// }
127126

128127
}

0 commit comments

Comments
 (0)