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

FM2-448: Add Unit Test for Servlets in SMART-on-FHIR #24

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 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 @@ -42,6 +42,12 @@ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOExce
String token = getParameter(req, "token");
String patientId = getParameter(req, "patientId");
String visitId = getParameter(req, "visitId");

if (token == null) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "Couldn't found token in url");
theanandankit marked this conversation as resolved.
Show resolved Hide resolved
return;
}

String decodedUrl = URLDecoder.decode(token, StandardCharsets.UTF_8.name());

String jwtKeyToken = null;
Expand Down Expand Up @@ -75,8 +81,8 @@ public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOExce
return;
}

if (token == null || (patientId == null && visitId == null)) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST);
if (patientId == null && visitId == null) {
res.sendError(HttpServletResponse.SC_BAD_REQUEST, "PatientId must be provided");
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.smartonfhir.web.servlet;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.powermock.api.mockito.PowerMockito.doReturn;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;

import javax.crypto.spec.SecretKeySpec;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.openmrs.User;
import org.openmrs.api.context.Context;
import org.openmrs.module.smartonfhir.model.SmartSession;
import org.openmrs.module.smartonfhir.util.SmartSecretKeyHolder;
import org.openmrs.module.smartonfhir.util.SmartSessionCache;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
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)
@PowerMockIgnore("javax.crypto.*")
@PrepareForTest({ Context.class, SmartSessionCache.class, SmartAccessConfirmation.class, SecretKeySpec.class,
SmartSecretKeyHolder.class })
public class SmartAccessConfirmationTest {

private static final String TOKEN = "http://localhost:8180/auth/realms/openmrs/login-actions/action-token?key=abcd&app-token=%7BAPP_TOKEN%7D";

private static final byte[] SMART_SECRET_KEY_HOLDER = "SecretKey".getBytes(StandardCharsets.UTF_8);

private static final String BASE_URL = "http://localhost:8180/auth/realms/openmrs/login-actions/action-token";

private static final String APP_TOKEN_VALUE = "eyJhbGciOiJIUzI1NiJ9.eyJwYXRpZW50IjoiNDU2IiwidmlzaXQiOiI3ODkifQ.XujZXboXbmJ5ZOgmWg6ihX8kN1Vf2XaZO0RQMBlOygA";

private static final String LAUNCH_ID = "12345";

private static final String USER_UUID = "123";

private static final String PATIENT_UUID = "456";

private static final String VISIT_UUID = "789";

private MockHttpServletRequest request;

private MockHttpServletResponse response;

@Mock
private SmartSessionCache smartSessionCache;

private SmartSession smartSession;

private User user;

private SmartAccessConfirmation smartAccessConfirmation;

@Before
public void setup() throws Exception {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
user = new User();
smartSession = new SmartSession();
smartAccessConfirmation = new SmartAccessConfirmation();

smartSession.setPatientUuid(PATIENT_UUID);
smartSession.setVisitUuid(VISIT_UUID);

request.setParameter("token", TOKEN);
request.setParameter("launch", LAUNCH_ID);

user.setUuid(USER_UUID);

mockStatic(Context.class);
mockStatic(SmartSecretKeyHolder.class);

doReturn(user).when(Context.class, "getAuthenticatedUser");
doReturn(SMART_SECRET_KEY_HOLDER).when(SmartSecretKeyHolder.class, "getSecretKey");

whenNew(SmartSessionCache.class).withNoArguments().thenReturn(smartSessionCache);
when(smartSessionCache.get(LAUNCH_ID)).thenReturn(smartSession);
}

@Test
public void shouldReturnCorrectBaseURL() throws IOException {
smartAccessConfirmation.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl().contains(BASE_URL), equalTo(true));
}

@Test
public void shouldContainsEveryQuery() throws IOException {
smartAccessConfirmation.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl().contains("key="), equalTo(true));
assertThat(response.getRedirectedUrl().contains("app-token="), equalTo(true));
}

@Test
public void shouldContainAppToken() throws IOException {
smartAccessConfirmation.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl().contains(APP_TOKEN_VALUE), equalTo(true));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.smartonfhir.web.servlet;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.openmrs.module.smartonfhir.util.FhirBaseAddressStrategy;
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({ SmartAppSelectorServlet.class, FhirBaseAddressStrategy.class })
public class SmartAppSelectorServletTest {

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=";

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=";

private static final String SMART_APP_BASE_URL = "http://127.0.0.1:9090/launch-standalone.html";

private MockHttpServletResponse response;

private MockHttpServletRequest request;

private SmartAppSelectorServlet smartAppSelectorServlet;

@Mock
private FhirBaseAddressStrategy fhirBaseAddressStrategy;

@Before
public void setup() throws Exception {
response = new MockHttpServletResponse();
request = new MockHttpServletRequest();
smartAppSelectorServlet = new SmartAppSelectorServlet();

whenNew(FhirBaseAddressStrategy.class).withNoArguments().thenReturn(fhirBaseAddressStrategy);
}

@Test
public void shouldReturnCorrectSMARTAppBaseURLForR4() throws IOException {
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R4);

smartAppSelectorServlet.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl().contains(SMART_APP_BASE_URL), equalTo(true));
assertThat(response.getRedirectedUrl(), equalTo(BASE_LAUNCH_ADDRESS_R4));
}

@Test
public void shouldReturnCorrectSMARTAppBaseURLForR3() throws IOException {
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R3);

smartAppSelectorServlet.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl().contains(SMART_APP_BASE_URL), equalTo(true));
assertThat(response.getRedirectedUrl(), equalTo(BASE_LAUNCH_ADDRESS_R3));
}

@Test
public void shouldContainsEveryQuery() throws IOException {
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R4);

smartAppSelectorServlet.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl().contains("iss="), equalTo(true));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.module.smartonfhir.web.servlet;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.mockito.PowerMockito.whenNew;

import java.io.IOException;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.openmrs.module.smartonfhir.util.FhirBaseAddressStrategy;
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({ SmartEhrLaunchServlet.class, FhirBaseAddressStrategy.class })
public class SmartEhrLaunchServletTest {

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=";

private static final String PATIENT_UUID = "12345";

private static final String VISIT_UUID = "67890";

private static final String PATIENT_LAUNCH_CONTEXT = "patient";

private static final String VISIT_LAUNCH_CONTEXT = "encounter";

@Mock
private FhirBaseAddressStrategy fhirBaseAddressStrategy;

private MockHttpServletResponse response;

private MockHttpServletRequest request;

private SmartEhrLaunchServlet servlet;

@Before
public void setup() throws Exception {
request = new MockHttpServletRequest();
response = new MockHttpServletResponse();
servlet = new SmartEhrLaunchServlet();

request.setParameter("patientId", PATIENT_UUID);
request.setParameter("visitId", VISIT_UUID);

whenNew(FhirBaseAddressStrategy.class).withNoArguments().thenReturn(fhirBaseAddressStrategy);
}

@Test
public void shouldReturnCorrectURLForPatientContext() throws IOException {
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS);
request.setParameter("launchContext", PATIENT_LAUNCH_CONTEXT);

servlet.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl().equals(BASE_LAUNCH_ADDRESS + PATIENT_UUID), equalTo(true));
}

public void shouldReturnCorrectURLForEncounterContext() throws IOException {
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS);
request.setParameter("launchContext", VISIT_LAUNCH_CONTEXT);

servlet.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getRedirectedUrl(), notNullValue());
assertThat(response.getRedirectedUrl().equals(BASE_LAUNCH_ADDRESS + VISIT_UUID), equalTo(true));
}

public void shouldReturnErrorWhenURLNotPresent() throws IOException {
when(fhirBaseAddressStrategy.getBaseSmartLaunchAddress(request)).thenReturn("");

servlet.doGet(request, response);

assertThat(response, notNullValue());
assertThat(response.getErrorMessage(), equalTo("A url must be provided"));
}
}
Loading