Skip to content

Commit

Permalink
Fix issues with mocking and test conflicts in omod project
Browse files Browse the repository at this point in the history
  • Loading branch information
mseaton committed Dec 20, 2024
1 parent 66f7a5f commit d736f9d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ 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;
}
return true;
}

protected boolean isAuthenticated() {
return Context.isAuthenticated();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}

Expand All @@ -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));
}

Expand All @@ -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));
Expand All @@ -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;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d736f9d

Please sign in to comment.