这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
platform: [ ubuntu-latest ]
java-version: [ 8 ]
java-version: [ 21 ]

runs-on: ${{ matrix.platform }}
env:
Expand Down
7 changes: 7 additions & 0 deletions omod/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,13 @@
<artifactId>jsp-api</artifactId>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<scope>test</scope>
</dependency>


</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,12 @@ protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse

try {
Context.addProxyPrivilege(PrivilegeConstants.EDIT_USER_PASSWORDS);
Context.addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
Context.getUserService().changePassword(user, randomPassword);
}
finally {
Context.removeProxyPrivilege(PrivilegeConstants.EDIT_USER_PASSWORDS);
Context.removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
}

httpSession.setAttribute("resetPassword", randomPassword);
Expand Down Expand Up @@ -204,7 +206,15 @@ protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse
public String getRandomPassword() {
//Password should be satisfy the minimum length if any is set, must have 1 upper case letter and 1 number
Integer minLength = 8;
String str = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GP_PASSWORD_MINIMUM_LENGTH);
String str;

try {
Context.getUserContext().addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
str = Context.getAdministrationService().getGlobalProperty(OpenmrsConstants.GP_PASSWORD_MINIMUM_LENGTH);
} finally {
Context.getUserContext().removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
}

if (StringUtils.isNotBlank(str)) {
minLength = Integer.valueOf(str);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,10 @@ public String handleRequest(WebRequest webRequest, ModelMap model) {
//was no exception message that might contain the required privilege

//will be sending the alert via ajax, so we need to escape js special chars
model.put("alertMessage", JavaScriptUtils.javaScriptEscape(alertMessage));
if (alertMessage != null) {
model.put("alertMessage", JavaScriptUtils.javaScriptEscape(alertMessage));
}

model.put("reason", reason);
model.put("refererUrl", refererUrl);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.openmrs.api.context.Context;
import org.openmrs.messagesource.MessageSourceService;
import org.openmrs.util.OpenmrsConstants;
import org.openmrs.util.PrivilegeConstants;
import org.openmrs.web.WebConstants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
Expand Down Expand Up @@ -54,18 +55,23 @@ public class PersonAttributeTypeListController {
public String displayPage(ModelMap modelMap) throws Exception {

AdministrationService as = Context.getAdministrationService();

// some helpful information that gets displayed
modelMap.put("patientListingAttributeTypes",
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_LISTING_ATTRIBUTES));
modelMap.put("patientViewingAttributeTypes",
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_VIEWING_ATTRIBUTES));
modelMap.put("patientHeaderAttributeTypes",
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_HEADER_ATTRIBUTES));
modelMap.put("userListingAttributeTypes",
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_LISTING_ATTRIBUTES));
modelMap.put("userViewingAttributeTypes",
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_VIEWING_ATTRIBUTES));

try {
Context.addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
// some helpful information that gets displayed
modelMap.put("patientListingAttributeTypes",
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_LISTING_ATTRIBUTES));
modelMap.put("patientViewingAttributeTypes",
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_VIEWING_ATTRIBUTES));
modelMap.put("patientHeaderAttributeTypes",
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_PATIENT_HEADER_ATTRIBUTES));
modelMap.put("userListingAttributeTypes",
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_LISTING_ATTRIBUTES));
modelMap.put("userViewingAttributeTypes",
as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_USER_VIEWING_ATTRIBUTES));
} finally {
Context.removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
}

List<PersonAttributeType> attributeTypeList = new Vector<PersonAttributeType>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
*/
package org.springframework.web.servlet.mvc;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -41,6 +42,8 @@
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.WebUtils;

import static org.springframework.web.util.WebUtils.SUBMIT_IMAGE_SUFFIXES;

/**
* Form controller for typical wizard-style workflows.
* <p>
Expand Down Expand Up @@ -654,7 +657,22 @@ protected int getTargetPage(HttpServletRequest request, Object command, Errors e
* @see #PARAM_TARGET
*/
protected int getTargetPage(HttpServletRequest request, int currentPage) {
return WebUtils.getTargetPage(request, PARAM_TARGET, currentPage);
Enumeration<String> parameterNames = request.getParameterNames();
Copy link
Member

Choose a reason for hiding this comment

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

Do you mind pointing me to where you got this implementation?

Copy link
Member Author

Choose a reason for hiding this comment

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

I kinda based this on the original implementation

    public static int getTargetPage(ServletRequest request, String paramPrefix, int currentPage) {
        Enumeration<String> paramNames = request.getParameterNames();

        while(paramNames.hasMoreElements()) {
            String paramName = (String)paramNames.nextElement();
            if (paramName.startsWith(paramPrefix)) {
                for(int i = 0; i < SUBMIT_IMAGE_SUFFIXES.length; ++i) {
                    String suffix = SUBMIT_IMAGE_SUFFIXES[i];
                    if (paramName.endsWith(suffix)) {
                        paramName = paramName.substring(0, paramName.length() - suffix.length());
                    }
                }

                return Integer.parseInt(paramName.substring(paramPrefix.length()));
            }
        }

        return currentPage;
    }

Copy link
Member

Choose a reason for hiding this comment

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

Where did you get that from?

Copy link
Member Author

Choose a reason for hiding this comment

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

Just going to the source using the IDE.

Copy link
Member Author

Choose a reason for hiding this comment

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

That one includes a logic to remove the submit button image suffixes. I did not include that here. Should I add that?

Copy link
Member

Choose a reason for hiding this comment

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

Which version of spring did you copy it from?

Copy link
Member Author

Choose a reason for hiding this comment

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

The version the module was using: Spring 4.1.4 RELEASE

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

I updated the code

while (parameterNames.hasMoreElements()) {
String param = parameterNames.nextElement();
if (param.startsWith(PARAM_TARGET)) {
for (String suffix : SUBMIT_IMAGE_SUFFIXES) {
// Remove any image button suffixes (like .x or .y)
if (param.endsWith(suffix)) {
param = param.substring(0, param.length() - suffix.length());
}
}
String pageStr = param.substring(PARAM_TARGET.length());
return Integer.parseInt(pageStr);

}
}
return currentPage;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,51 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;
import static org.powermock.api.mockito.PowerMockito.mockStatic;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openmrs.module.Extension;
import org.openmrs.module.ModuleFactory;
import org.openmrs.module.web.extension.provider.Link;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(ModuleFactory.class)
@ExtendWith(MockitoExtension.class)
public class ExtensionUtilTest {

/**
* @see ExtensionUtil#getFormsModulesCanAddEncounterToVisit()
* @verifies return empty set if there is no AddEncounterToVisitExtension
*/
@Test
public void getModulesAddEncounterToVisitLinks_shouldReturnEmptySetIfThereIsNoAddEncounterToVisitExtension()
throws Exception {
public void getModulesAddEncounterToVisitLinks_shouldReturnEmptySetIfThereIsNoAddEncounterToVisitExtension() throws Exception {
//given
mockStatic(ModuleFactory.class);
when(ModuleFactory.getExtensions("org.openmrs.module.web.extension.AddEncounterToVisitExtension")).thenReturn(null);

//when
Set<Link> links = ExtensionUtil.getAllAddEncounterToVisitLinks();

//then
assertNotNull(links);
assertEquals(0, links.size());
try (MockedStatic<ModuleFactory> mockedModuleFactory = mockStatic(ModuleFactory.class)) {
mockedModuleFactory.when(() -> ModuleFactory.getExtensions("org.openmrs.module.web.extension.AddEncounterToVisitExtension"))
.thenReturn(null);

//when
Set<Link> links = ExtensionUtil.getAllAddEncounterToVisitLinks();

//then
assertNotNull(links);
assertEquals(0, links.size());
}
}

/**
* @see ExtensionUtil#getFormsModulesCanAddEncounterToVisit()
* @verifies return forms if there are AddEncounterToVisitExtensions
*/
@Test
public void getFormsModulesCanAddEncounterToVisit_shouldReturnFormsIfThereAreAddEncounterToVisitExtensions()
throws Exception {
public void getFormsModulesCanAddEncounterToVisit_shouldReturnFormsIfThereAreAddEncounterToVisitExtensions() throws Exception {
//given
AddEncounterToVisitExtension ext1 = mock(AddEncounterToVisitExtension.class);
Set<Link> links1 = new HashSet<Link>();
Expand All @@ -69,28 +68,29 @@ public void getFormsModulesCanAddEncounterToVisit_shouldReturnFormsIfThereAreAdd
link2.setLabel("b");
links1.add(link2);
when(ext1.getAddEncounterToVisitLinks()).thenReturn(links1);

AddEncounterToVisitExtension ext2 = mock(AddEncounterToVisitExtension.class);
Set<Link> links2 = new HashSet<Link>();
Link link3 = new Link();
link3.setLabel("aa");
links2.add(link3);
when(ext2.getAddEncounterToVisitLinks()).thenReturn(links2);
List<Extension> extensions = new ArrayList<Extension>();

List<Extension> extensions = new ArrayList<>();
extensions.add(ext1);
extensions.add(ext2);

mockStatic(ModuleFactory.class);
when(ModuleFactory.getExtensions("org.openmrs.module.web.extension.AddEncounterToVisitExtension")).thenReturn(
extensions);

//when
Set<Link> allAddEncounterToVisitLinks = ExtensionUtil.getAllAddEncounterToVisitLinks();

//then
assertTrue(allAddEncounterToVisitLinks.contains(link1));
assertTrue(allAddEncounterToVisitLinks.contains(link2));
assertTrue(allAddEncounterToVisitLinks.contains(link3));

try (MockedStatic<ModuleFactory> mockedModuleFactory = mockStatic(ModuleFactory.class)) {
mockedModuleFactory.when(() -> ModuleFactory.getExtensions("org.openmrs.module.web.extension.AddEncounterToVisitExtension"))
.thenReturn(extensions);

//when
Set<Link> allAddEncounterToVisitLinks = ExtensionUtil.getAllAddEncounterToVisitLinks();

//then
assertTrue(allAddEncounterToVisitLinks.contains(link1));
assertTrue(allAddEncounterToVisitLinks.contains(link2));
assertTrue(allAddEncounterToVisitLinks.contains(link3));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.openmrs.Location;
import org.openmrs.customdatatype.CustomDatatype;
import org.openmrs.customdatatype.datatype.MockLocationDatatype;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class BaseMetadataFieldGenDatatypeHandlerTest {

private BaseMetadataFieldGenDatatypeHandler handler = new MockLocationFieldGenDatatypeHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,20 @@
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.openmrs.customdatatype.CustomDatatype;
import org.openmrs.customdatatype.InvalidCustomValueException;
import org.openmrs.customdatatype.datatype.RegexValidatedTextDatatype;
import org.powermock.modules.junit4.PowerMockRunner;
import org.springframework.mock.web.MockHttpServletRequest;

/**
* Tests {@code RegexValidatedTextDatatypeHandler}.
*/
@RunWith(PowerMockRunner.class)
public class RegexValidatedTextDatatypeHandlerTest {

@Rule
ExpectedException expectedException = ExpectedException.none();
public ExpectedException expectedException = ExpectedException.none();

private RegexValidatedTextDatatypeHandler handler = new RegexValidatedTextDatatypeHandler();
private final RegexValidatedTextDatatypeHandler handler = new RegexValidatedTextDatatypeHandler();

/**
* @see org.openmrs.web.attribute.handler.FieldGenDatatypeHandler#getValue(CustomDatatype,
Expand Down
Loading
Loading