Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RESTWS-885: Reset password REST endpoint requires privileges #590

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
import org.openmrs.api.InvalidActivationKeyException;
import org.openmrs.api.UserService;
import org.openmrs.api.ValidationException;
import org.openmrs.api.context.Context;
import org.openmrs.module.webservices.rest.web.RestConstants;
import org.openmrs.module.webservices.rest.web.v1_0.controller.BaseRestController;
import org.openmrs.notification.MessageException;
import org.openmrs.util.PrivilegeConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
Expand All @@ -40,9 +42,15 @@ public class PasswordResetController2_2 extends BaseRestController {
@ResponseStatus(HttpStatus.OK)
public void requestPasswordReset(@RequestBody Map<String, String> body) throws MessageException {
String usernameOrEmail = body.get("usernameOrEmail");
User user = userService.getUserByUsernameOrEmail(usernameOrEmail);
if (user != null) {
userService.setUserActivationKey(user);
try {
Context.addProxyPrivilege(PrivilegeConstants.GET_USERS);
User user = userService.getUserByUsernameOrEmail(usernameOrEmail);
if (user != null) {
userService.setUserActivationKey(user);
}
}
finally {
Context.removeProxyPrivilege(PrivilegeConstants.GET_USERS);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,39 @@ public void requestPasswordReset_shouldCreateUserActivationKeyGivenEmail() throw
handle(newPostRequest(RESET_PASSWORD_URI, "{\"usernameOrEmail\":\"" + user.getEmail() + "\"}"));
assertNotNull(dao.getLoginCredential(user).getActivationKey());
}

@Test
public void requestPasswordReset_shouldCreateUserActivationKeyWhenUnauthenticatedWithValidEmail() throws Exception {
User user = setUpUser("butch");
user.setEmail("[email protected]");
assertNull(dao.getLoginCredential(user).getActivationKey());
assertNotNull(user.getEmail());

MessageException exception = null;
try {
handle(newPostRequest(RESET_PASSWORD_URI, "{\"usernameOrEmail\":\"" + user.getEmail() + "\"}"));
} catch (MessageException me) {
exception = me;
}
assertNotNull(exception);
assertNotNull(dao.getLoginCredential(user).getActivationKey());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you assert that this was null before the REST call?

}

@Test
public void requestPasswordReset_shouldCreateUserActivationKeyWhenUnauthenticatedWithValidUsername() throws Exception {
User user = setUpUser("butch");
assertNull(dao.getLoginCredential(user).getActivationKey());
assertNotNull(user.getUsername());

MessageException exception2 = null;
try {
handle(newPostRequest(RESET_PASSWORD_URI, "{\"usernameOrEmail\":\"" + user.getUsername() + "\"}"));
} catch (MessageException me) {
exception2 = me;
}
assertNotNull(exception2);
assertNotNull(dao.getLoginCredential(user).getActivationKey());
}

@Test
public void resetPassword_shouldResetUserPasswordIfActivationKeyIsCorrect() throws Exception {
Expand All @@ -87,4 +120,17 @@ public void resetPassword_shouldResetUserPasswordIfActivationKeyIsCorrect() thro
Context.authenticate(user.getUsername(), newPassword);

}

private User setUpUser(String userName) throws Exception {
User user = userService.getUserByUsername(userName);
final String newPassword = "SomeOtherPassword123";

userService.changePassword(user, newPassword);

// Logout Admin User with Privileges
Context.logout();

Context.authenticate(userName, newPassword);
return Context.getAuthenticatedUser();
}
}
Loading