Skip to content

Commit 029c4ec

Browse files
committed
FM2-448: Add Unit Test for Servlets in SMART-on-FHIR
1 parent 521ce2e commit 029c4ec

File tree

6 files changed

+451
-2
lines changed

6 files changed

+451
-2
lines changed

omod/src/main/java/org/openmrs/module/smartonfhir/web/servlet/SmartLaunchOptionSelected.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOExce
4242
String token = getParameter(req, "token");
4343
String patientId = getParameter(req, "patientId");
4444
String visitId = getParameter(req, "visitId");
45+
46+
if (token == null) {
47+
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Couldn't found token in url");
48+
return;
49+
}
50+
4551
String decodedUrl = URLDecoder.decode(token, StandardCharsets.UTF_8.name());
4652

4753
String jwtKeyToken = null;
@@ -75,8 +81,8 @@ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOExce
7581
return;
7682
}
7783

78-
if (token == null || (patientId == null && visitId == null)) {
79-
res.sendError(HttpServletResponse.SC_BAD_REQUEST);
84+
if (patientId == null && visitId == null) {
85+
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "PatientId must be provided");
8086
return;
8187
}
8288

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.smartonfhir.web.servlet;
11+
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.hamcrest.Matchers.equalTo;
14+
import static org.hamcrest.Matchers.notNullValue;
15+
import static org.powermock.api.mockito.PowerMockito.doReturn;
16+
import static org.powermock.api.mockito.PowerMockito.mockStatic;
17+
import static org.powermock.api.mockito.PowerMockito.when;
18+
import static org.powermock.api.mockito.PowerMockito.whenNew;
19+
20+
import javax.crypto.spec.SecretKeySpec;
21+
22+
import java.io.IOException;
23+
import java.nio.charset.StandardCharsets;
24+
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.mockito.Mock;
29+
import org.openmrs.User;
30+
import org.openmrs.api.context.Context;
31+
import org.openmrs.module.smartonfhir.model.SmartSession;
32+
import org.openmrs.module.smartonfhir.util.SmartSecretKeyHolder;
33+
import org.openmrs.module.smartonfhir.util.SmartSessionCache;
34+
import org.powermock.core.classloader.annotations.PowerMockIgnore;
35+
import org.powermock.core.classloader.annotations.PrepareForTest;
36+
import org.powermock.modules.junit4.PowerMockRunner;
37+
import org.springframework.mock.web.MockHttpServletRequest;
38+
import org.springframework.mock.web.MockHttpServletResponse;
39+
40+
@RunWith(PowerMockRunner.class)
41+
@PowerMockIgnore("javax.crypto.*")
42+
@PrepareForTest({ Context.class, SmartSessionCache.class, SmartAccessConfirmation.class, SecretKeySpec.class,
43+
SmartSecretKeyHolder.class })
44+
public class SmartAccessConfirmationTest {
45+
46+
private static final String TOKEN = "http://localhost:8180/auth/realms/openmrs/login-actions/action-token?key=abcd&app-token=%7BAPP_TOKEN%7D";
47+
48+
private static final byte[] SMART_SECRET_KEY_HOLDER = "SecretKey".getBytes(StandardCharsets.UTF_8);
49+
50+
private static final String BASE_URL = "http://localhost:8180/auth/realms/openmrs/login-actions/action-token";
51+
52+
private static final String APP_TOKEN_VALUE = "eyJhbGciOiJIUzI1NiJ9.eyJwYXRpZW50IjoiNDU2IiwidmlzaXQiOiI3ODkifQ.XujZXboXbmJ5ZOgmWg6ihX8kN1Vf2XaZO0RQMBlOygA";
53+
54+
private static final String LAUNCH_ID = "12345";
55+
56+
private static final String USER_UUID = "123";
57+
58+
private static final String PATIENT_UUID = "456";
59+
60+
private static final String VISIT_UUID = "789";
61+
62+
private MockHttpServletRequest request;
63+
64+
private MockHttpServletResponse response;
65+
66+
@Mock
67+
private SmartSessionCache smartSessionCache;
68+
69+
private SmartSession smartSession;
70+
71+
private User user;
72+
73+
private SmartAccessConfirmation smartAccessConfirmation;
74+
75+
@Before
76+
public void setup() throws Exception {
77+
request = new MockHttpServletRequest();
78+
response = new MockHttpServletResponse();
79+
user = new User();
80+
smartSession = new SmartSession();
81+
smartAccessConfirmation = new SmartAccessConfirmation();
82+
83+
smartSession.setPatientUuid(PATIENT_UUID);
84+
smartSession.setVisitUuid(VISIT_UUID);
85+
86+
request.setParameter("token", TOKEN);
87+
request.setParameter("launch", LAUNCH_ID);
88+
89+
user.setUuid(USER_UUID);
90+
91+
mockStatic(Context.class);
92+
mockStatic(SmartSecretKeyHolder.class);
93+
94+
doReturn(user).when(Context.class, "getAuthenticatedUser");
95+
doReturn(SMART_SECRET_KEY_HOLDER).when(SmartSecretKeyHolder.class, "getSecretKey");
96+
97+
whenNew(SmartSessionCache.class).withNoArguments().thenReturn(smartSessionCache);
98+
when(smartSessionCache.get(LAUNCH_ID)).thenReturn(smartSession);
99+
}
100+
101+
@Test
102+
public void shouldReturnCorrectBaseURL() throws IOException {
103+
smartAccessConfirmation.doGet(request, response);
104+
105+
assertThat(response, notNullValue());
106+
assertThat(response.getRedirectedUrl(), notNullValue());
107+
assertThat(response.getRedirectedUrl().contains(BASE_URL), equalTo(true));
108+
}
109+
110+
@Test
111+
public void shouldContainsEveryQuery() throws IOException {
112+
smartAccessConfirmation.doGet(request, response);
113+
114+
assertThat(response, notNullValue());
115+
assertThat(response.getRedirectedUrl(), notNullValue());
116+
assertThat(response.getRedirectedUrl().contains("key="), equalTo(true));
117+
assertThat(response.getRedirectedUrl().contains("app-token="), equalTo(true));
118+
}
119+
120+
@Test
121+
public void shouldContainAppToken() throws IOException {
122+
smartAccessConfirmation.doGet(request, response);
123+
124+
assertThat(response, notNullValue());
125+
assertThat(response.getRedirectedUrl(), notNullValue());
126+
assertThat(response.getRedirectedUrl().contains(APP_TOKEN_VALUE), equalTo(true));
127+
}
128+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.smartonfhir.web.servlet;
11+
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.hamcrest.Matchers.equalTo;
14+
import static org.hamcrest.Matchers.notNullValue;
15+
import static org.powermock.api.mockito.PowerMockito.when;
16+
import static org.powermock.api.mockito.PowerMockito.whenNew;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.Mock;
24+
import org.openmrs.module.smartonfhir.util.FhirBaseAddressStrategy;
25+
import org.powermock.core.classloader.annotations.PrepareForTest;
26+
import org.powermock.modules.junit4.PowerMockRunner;
27+
import org.springframework.mock.web.MockHttpServletRequest;
28+
import org.springframework.mock.web.MockHttpServletResponse;
29+
30+
@RunWith(PowerMockRunner.class)
31+
@PrepareForTest({ SmartAppSelectorServlet.class, FhirBaseAddressStrategy.class })
32+
public class SmartAppSelectorServletTest {
33+
34+
private static final String BASE_LAUNCH_ADDRESS_R4 = "http://127.0.0.1:9090/launch-standalone.html?iss=http://demo.org/openmrs/ws/fhir2/R4&launch=";
35+
36+
private static final String BASE_LAUNCH_ADDRESS_R3 = "http://127.0.0.1:9090/launch-standalone.html?iss=http://demo.org/openmrs/ws/fhir2/R3&launch=";
37+
38+
private static final String SMART_APP_BASE_URL = "http://127.0.0.1:9090/launch-standalone.html";
39+
40+
private MockHttpServletResponse response;
41+
42+
private MockHttpServletRequest request;
43+
44+
private SmartAppSelectorServlet smartAppSelectorServlet;
45+
46+
@Mock
47+
private FhirBaseAddressStrategy fhirBaseAddressStrategy;
48+
49+
@Before
50+
public void setup() throws Exception {
51+
response = new MockHttpServletResponse();
52+
request = new MockHttpServletRequest();
53+
smartAppSelectorServlet = new SmartAppSelectorServlet();
54+
55+
whenNew(FhirBaseAddressStrategy.class).withNoArguments().thenReturn(fhirBaseAddressStrategy);
56+
}
57+
58+
@Test
59+
public void shouldReturnCorrectSMARTAppBaseURLForR4() throws IOException {
60+
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R4);
61+
62+
smartAppSelectorServlet.doGet(request, response);
63+
64+
assertThat(response, notNullValue());
65+
assertThat(response.getRedirectedUrl(), notNullValue());
66+
assertThat(response.getRedirectedUrl().contains(SMART_APP_BASE_URL), equalTo(true));
67+
assertThat(response.getRedirectedUrl(), equalTo(BASE_LAUNCH_ADDRESS_R4));
68+
}
69+
70+
@Test
71+
public void shouldReturnCorrectSMARTAppBaseURLForR3() throws IOException {
72+
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R3);
73+
74+
smartAppSelectorServlet.doGet(request, response);
75+
76+
assertThat(response, notNullValue());
77+
assertThat(response.getRedirectedUrl(), notNullValue());
78+
assertThat(response.getRedirectedUrl().contains(SMART_APP_BASE_URL), equalTo(true));
79+
assertThat(response.getRedirectedUrl(), equalTo(BASE_LAUNCH_ADDRESS_R3));
80+
}
81+
82+
@Test
83+
public void shouldContainsEveryQuery() throws IOException {
84+
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R4);
85+
86+
smartAppSelectorServlet.doGet(request, response);
87+
88+
assertThat(response, notNullValue());
89+
assertThat(response.getRedirectedUrl(), notNullValue());
90+
assertThat(response.getRedirectedUrl().contains("iss="), equalTo(true));
91+
}
92+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* This Source Code Form is subject to the terms of the Mozilla Public License,
3+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
4+
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
5+
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
6+
*
7+
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
8+
* graphic logo is a trademark of OpenMRS Inc.
9+
*/
10+
package org.openmrs.module.smartonfhir.web.servlet;
11+
12+
import static org.hamcrest.MatcherAssert.assertThat;
13+
import static org.hamcrest.Matchers.equalTo;
14+
import static org.hamcrest.Matchers.notNullValue;
15+
import static org.powermock.api.mockito.PowerMockito.when;
16+
import static org.powermock.api.mockito.PowerMockito.whenNew;
17+
18+
import java.io.IOException;
19+
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.mockito.Mock;
24+
import org.openmrs.module.smartonfhir.util.FhirBaseAddressStrategy;
25+
import org.powermock.core.classloader.annotations.PrepareForTest;
26+
import org.powermock.modules.junit4.PowerMockRunner;
27+
import org.springframework.mock.web.MockHttpServletRequest;
28+
import org.springframework.mock.web.MockHttpServletResponse;
29+
30+
@RunWith(PowerMockRunner.class)
31+
@PrepareForTest({ SmartEhrLaunchServlet.class, FhirBaseAddressStrategy.class })
32+
public class SmartEhrLaunchServletTest {
33+
34+
private static final String BASE_LAUNCH_ADDRESS = "http://127.0.0.1:9090/launch-standalone.html?iss=http://demo.org/openmrs/ws/fhir2/R4&launch=";
35+
36+
private static final String PATIENT_UUID = "12345";
37+
38+
private static final String VISIT_UUID = "67890";
39+
40+
private static final String PATIENT_LAUNCH_CONTEXT = "patient";
41+
42+
private static final String VISIT_LAUNCH_CONTEXT = "encounter";
43+
44+
@Mock
45+
private FhirBaseAddressStrategy fhirBaseAddressStrategy;
46+
47+
private MockHttpServletResponse response;
48+
49+
private MockHttpServletRequest request;
50+
51+
private SmartEhrLaunchServlet servlet;
52+
53+
@Before
54+
public void setup() throws Exception {
55+
request = new MockHttpServletRequest();
56+
response = new MockHttpServletResponse();
57+
servlet = new SmartEhrLaunchServlet();
58+
59+
request.setParameter("patientId", PATIENT_UUID);
60+
request.setParameter("visitId", VISIT_UUID);
61+
62+
whenNew(FhirBaseAddressStrategy.class).withNoArguments().thenReturn(fhirBaseAddressStrategy);
63+
}
64+
65+
@Test
66+
public void shouldReturnCorrectURLForPatientContext() throws IOException {
67+
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS);
68+
request.setParameter("launchContext", PATIENT_LAUNCH_CONTEXT);
69+
70+
servlet.doGet(request, response);
71+
72+
assertThat(response, notNullValue());
73+
assertThat(response.getRedirectedUrl(), notNullValue());
74+
assertThat(response.getRedirectedUrl().equals(BASE_LAUNCH_ADDRESS + PATIENT_UUID), equalTo(true));
75+
}
76+
77+
public void shouldReturnCorrectURLForEncounterContext() throws IOException {
78+
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS);
79+
request.setParameter("launchContext", VISIT_LAUNCH_CONTEXT);
80+
81+
servlet.doGet(request, response);
82+
83+
assertThat(response, notNullValue());
84+
assertThat(response.getRedirectedUrl(), notNullValue());
85+
assertThat(response.getRedirectedUrl().equals(BASE_LAUNCH_ADDRESS + VISIT_UUID), equalTo(true));
86+
}
87+
88+
public void shouldReturnErrorWhenURLNotPresent() throws IOException {
89+
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn("");
90+
91+
servlet.doGet(request, response);
92+
93+
assertThat(response, notNullValue());
94+
assertThat(response.getErrorMessage(), equalTo("A url must be provided"));
95+
}
96+
}

0 commit comments

Comments
 (0)