From d736f9d2e989707a949be3ac194fada7afe706af Mon Sep 17 00:00:00 2001 From: Michael Seaton Date: Fri, 20 Dec 2024 14:27:45 -0500 Subject: [PATCH] Fix issues with mocking and test conflicts in omod project --- .../RequireAuthenticationInterceptor.java | 5 +++- .../RequireAuthenticationInterceptorTest.java | 30 +++++++++---------- .../web/servlet/FhirRestServletTest.java | 17 +++-------- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/omod/src/main/java/org/openmrs/module/fhir2/web/authentication/RequireAuthenticationInterceptor.java b/omod/src/main/java/org/openmrs/module/fhir2/web/authentication/RequireAuthenticationInterceptor.java index a10e452ca..78898856c 100644 --- a/omod/src/main/java/org/openmrs/module/fhir2/web/authentication/RequireAuthenticationInterceptor.java +++ b/omod/src/main/java/org/openmrs/module/fhir2/web/authentication/RequireAuthenticationInterceptor.java @@ -25,7 +25,7 @@ public class RequireAuthenticationInterceptor { @Hook(Pointcut.SERVER_INCOMING_REQUEST_PRE_PROCESSED) public boolean ensureUserAuthenticated(HttpServletRequest request, HttpServletResponse response) throws IOException { if (!(request.getRequestURI().contains("/.well-known") || request.getRequestURI().endsWith("/metadata")) - && !Context.isAuthenticated()) { + && !isAuthenticated()) { // This sends 401 error if not authenticated response.sendError(HttpServletResponse.SC_UNAUTHORIZED, "Not authenticated"); return false; @@ -33,4 +33,7 @@ public boolean ensureUserAuthenticated(HttpServletRequest request, HttpServletRe return true; } + protected boolean isAuthenticated() { + return Context.isAuthenticated(); + } } diff --git a/omod/src/test/java/org/openmrs/module/fhir2/web/authentication/RequireAuthenticationInterceptorTest.java b/omod/src/test/java/org/openmrs/module/fhir2/web/authentication/RequireAuthenticationInterceptorTest.java index a3983ebc6..02ebfd914 100644 --- a/omod/src/test/java/org/openmrs/module/fhir2/web/authentication/RequireAuthenticationInterceptorTest.java +++ b/omod/src/test/java/org/openmrs/module/fhir2/web/authentication/RequireAuthenticationInterceptorTest.java @@ -11,40 +11,25 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import static org.mockito.Mockito.when; import javax.servlet.ServletException; import javax.servlet.http.HttpServletResponse; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.openmrs.api.context.Context; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; -@RunWith(PowerMockRunner.class) -@PrepareForTest(Context.class) public class RequireAuthenticationInterceptorTest { - private RequireAuthenticationInterceptor interceptor; - private MockHttpServletRequest request; private MockHttpServletResponse response; @Before public void setup() throws ServletException { - interceptor = new RequireAuthenticationInterceptor(); request = new MockHttpServletRequest(); response = new MockHttpServletResponse(); - - PowerMockito.mockStatic(Context.class); - when(Context.isAuthenticated()).thenReturn(false); - } @Test @@ -53,6 +38,7 @@ public void ensureUserAuthenticated_shouldReturnTrueGivenWellKnownUri() throws E request.setRequestURI("/.well-known"); // replay and verify + RequireAuthenticationInterceptor interceptor = getInterceptorForTesting(true); assertThat(interceptor.ensureUserAuthenticated(request, response), is(true)); } @@ -62,6 +48,7 @@ public void ensureUserAuthenticated_shouldReturnTrueGivenMetadataUri() throws Ex request.setRequestURI("/metadata"); // replay and verify + RequireAuthenticationInterceptor interceptor = getInterceptorForTesting(true); assertThat(interceptor.ensureUserAuthenticated(request, response), is(true)); } @@ -71,6 +58,7 @@ public void ensureUserAuthenticated_shouldReturnFalseGivenUserIsNotAuthenticated request.setRequestURI("/ws/fhir2/R4/Someresource"); // replay and verify + RequireAuthenticationInterceptor interceptor = getInterceptorForTesting(false); assertThat(interceptor.ensureUserAuthenticated(request, response), is(false)); assertThat(response.getErrorMessage(), is("Not authenticated")); assertThat(response.getStatus(), is(HttpServletResponse.SC_UNAUTHORIZED)); @@ -80,9 +68,19 @@ public void ensureUserAuthenticated_shouldReturnFalseGivenUserIsNotAuthenticated public void ensureUserAuthenticated_shouldReturnTrueGivenUserIsAuthenticated() throws Exception { // setup request.setRequestURI("/ws/fhir2/R4/Someresource"); - when(Context.isAuthenticated()).thenReturn(true); // replay and verify + RequireAuthenticationInterceptor interceptor = getInterceptorForTesting(true); assertThat(interceptor.ensureUserAuthenticated(request, response), is(true)); } + + public RequireAuthenticationInterceptor getInterceptorForTesting(boolean isUserAuthenticated) { + return new RequireAuthenticationInterceptor() { + + @Override + protected boolean isAuthenticated() { + return isUserAuthenticated; + } + }; + } } diff --git a/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/FhirRestServletTest.java b/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/FhirRestServletTest.java index 8629ce6f3..03f959f0f 100644 --- a/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/FhirRestServletTest.java +++ b/omod/src/test/java/org/openmrs/module/fhir2/web/servlet/FhirRestServletTest.java @@ -10,7 +10,7 @@ package org.openmrs.module.fhir2.web.servlet; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -62,19 +62,10 @@ public void testServiceSetsContextClassLoader() throws ServletException, IOExcep when(mockRequest.getMethod()).thenReturn("GET"); when(mockRequest.getRequestURI()).thenReturn("/fhir"); - ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader(); - - assertNotEquals("ContextClassLoader should not be OpenmrsClassLoader initially", - OpenmrsClassLoader.getInstance().getClass().getName(), initialClassLoader.getClass().getName()); - - // replay + Thread.currentThread().setContextClassLoader(null); + assertNull(Thread.currentThread().getContextClassLoader()); servlet.service(mockRequest, mockResponse); - - // Verify - ClassLoader newClassLoader = Thread.currentThread().getContextClassLoader(); - - assertEquals("ContextClassLoader should be OpenmrsClassLoader after service method", - OpenmrsClassLoader.getInstance().getClass().getName(), newClassLoader.getClass().getName()); + assertEquals(OpenmrsClassLoader.getInstance(), Thread.currentThread().getContextClassLoader()); } class TestableFhirRestServlet extends FhirRestServlet {