diff --git a/omod/src/main/java/org/openmrs/module/smartonfhir/web/servlet/SmartEhrLaunchServlet.java b/omod/src/main/java/org/openmrs/module/smartonfhir/web/servlet/SmartEhrLaunchServlet.java index 8155ff3..a618be2 100644 --- a/omod/src/main/java/org/openmrs/module/smartonfhir/web/servlet/SmartEhrLaunchServlet.java +++ b/omod/src/main/java/org/openmrs/module/smartonfhir/web/servlet/SmartEhrLaunchServlet.java @@ -37,6 +37,11 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IO smartSession.setPatientUuid(patientId); smartSession.setVisitUuid(visitId); + if (launchContext == null) { + resp.sendError(HttpStatus.SC_BAD_REQUEST, "launchContext must be provided"); + return; + } + if (launchContext.equals("patient")) { url = url + patientId; smartSessionCache.put(patientId, smartSession); diff --git a/omod/src/main/java/org/openmrs/module/smartonfhir/web/servlet/SmartLaunchOptionSelected.java b/omod/src/main/java/org/openmrs/module/smartonfhir/web/servlet/SmartLaunchOptionSelected.java index c410e47..a07aaa0 100644 --- a/omod/src/main/java/org/openmrs/module/smartonfhir/web/servlet/SmartLaunchOptionSelected.java +++ b/omod/src/main/java/org/openmrs/module/smartonfhir/web/servlet/SmartLaunchOptionSelected.java @@ -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 find token in url"); + return; + } + String decodedUrl = URLDecoder.decode(token, StandardCharsets.UTF_8.name()); String jwtKeyToken = null; @@ -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; } diff --git a/omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartAccessConfirmationTest.java b/omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartAccessConfirmationTest.java new file mode 100644 index 0000000..f2ceaf2 --- /dev/null +++ b/omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartAccessConfirmationTest.java @@ -0,0 +1,133 @@ +/* + * 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.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockedConstruction; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +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.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +@RunWith(MockitoJUnitRunner.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; + + private SmartSession smartSession; + + private User user; + + private SmartAccessConfirmation smartAccessConfirmation; + + private MockedStatic contextMockedStatic; + + private MockedStatic smartSecretKeyHolderMockedStatic; + + private MockedConstruction smartSessionCacheMockedConstruction; + + @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); + + contextMockedStatic = Mockito.mockStatic(Context.class); + smartSecretKeyHolderMockedStatic = Mockito.mockStatic(SmartSecretKeyHolder.class); + smartSessionCacheMockedConstruction = Mockito.mockConstruction(SmartSessionCache.class, (mock, context) -> { + when(mock.get(LAUNCH_ID)).thenReturn(smartSession); + }); + + contextMockedStatic.when(Context::getAuthenticatedUser).thenReturn(user); + smartSecretKeyHolderMockedStatic.when(SmartSecretKeyHolder::getSecretKey).thenReturn(SMART_SECRET_KEY_HOLDER); + + } + + @After + public void close() { + contextMockedStatic.close(); + smartSecretKeyHolderMockedStatic.close(); + smartSessionCacheMockedConstruction.close(); + } + + @Test + public void shouldReturnCorrectBaseURL() throws IOException { + smartAccessConfirmation.doGet(request, response); + + assertThat(response, notNullValue()); + assertThat(response.getRedirectedUrl(), notNullValue()); + assertThat(response.getRedirectedUrl(), containsString(BASE_URL)); + } + + @Test + public void shouldContainsEveryQuery() throws IOException { + smartAccessConfirmation.doGet(request, response); + + assertThat(response, notNullValue()); + assertThat(response.getRedirectedUrl(), notNullValue()); + assertThat(response.getRedirectedUrl(), containsString("key=")); + assertThat(response.getRedirectedUrl(), containsString("app-token=")); + } + + @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)); + } +} diff --git a/omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartAppSelectorServletTest.java b/omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartAppSelectorServletTest.java new file mode 100644 index 0000000..22c8900 --- /dev/null +++ b/omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartAppSelectorServletTest.java @@ -0,0 +1,103 @@ +/* + * 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.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; +import static org.mockito.Mockito.when; + +import java.io.IOException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.openmrs.module.smartonfhir.util.FhirBaseAddressStrategy; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +@RunWith(MockitoJUnitRunner.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; + + private MockedConstruction fhirBaseAddressStrategyMockedConstruction; + + @Before + public void setup() throws Exception { + response = new MockHttpServletResponse(); + request = new MockHttpServletRequest(); + smartAppSelectorServlet = new SmartAppSelectorServlet(); + } + + @After + public void close() { + fhirBaseAddressStrategyMockedConstruction.close(); + } + + @Test + public void shouldReturnCorrectSMARTAppBaseURLForR4() throws IOException { + fhirBaseAddressStrategyMockedConstruction = Mockito.mockConstruction(FhirBaseAddressStrategy.class, + (mock, context) -> { + when(mock.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R4); + }); + + smartAppSelectorServlet.doGet(request, response); + + assertThat(response, notNullValue()); + assertThat(response.getRedirectedUrl(), notNullValue()); + assertThat(response.getRedirectedUrl(), containsString(SMART_APP_BASE_URL)); + assertThat(response.getRedirectedUrl(), equalTo(BASE_LAUNCH_ADDRESS_R4)); + } + + @Test + public void shouldReturnCorrectSMARTAppBaseURLForR3() throws IOException { + fhirBaseAddressStrategyMockedConstruction = Mockito.mockConstruction(FhirBaseAddressStrategy.class, + (mock, context) -> { + when(mock.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R3); + }); + + smartAppSelectorServlet.doGet(request, response); + + assertThat(response, notNullValue()); + assertThat(response.getRedirectedUrl(), notNullValue()); + assertThat(response.getRedirectedUrl(), containsString(SMART_APP_BASE_URL)); + assertThat(response.getRedirectedUrl(), equalTo(BASE_LAUNCH_ADDRESS_R3)); + } + + @Test + public void shouldContainsEveryQuery() throws IOException { + fhirBaseAddressStrategyMockedConstruction = Mockito.mockConstruction(FhirBaseAddressStrategy.class, + (mock, context) -> { + when(mock.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS_R4); + }); + + smartAppSelectorServlet.doGet(request, response); + + assertThat(response, notNullValue()); + assertThat(response.getRedirectedUrl(), notNullValue()); + assertThat(response.getRedirectedUrl(), containsString("iss=")); + } +} diff --git a/omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartEhrLaunchServletTest.java b/omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartEhrLaunchServletTest.java new file mode 100644 index 0000000..5c68589 --- /dev/null +++ b/omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartEhrLaunchServletTest.java @@ -0,0 +1,109 @@ +/* + * 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.mockito.Mockito.when; + +import java.io.IOException; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockedConstruction; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.openmrs.module.smartonfhir.util.FhirBaseAddressStrategy; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +@RunWith(MockitoJUnitRunner.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"; + + private MockHttpServletResponse response; + + private MockHttpServletRequest request; + + private SmartEhrLaunchServlet servlet; + + private MockedConstruction fhirBaseAddressStrategyMockedConstruction; + + @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); + } + + @After + public void close() { + fhirBaseAddressStrategyMockedConstruction.close(); + } + + @Test + public void shouldReturnCorrectURLForPatientContext() throws IOException { + fhirBaseAddressStrategyMockedConstruction = Mockito.mockConstruction(FhirBaseAddressStrategy.class, + (mock, context) -> { + when(mock.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)); + } + + @Test + public void shouldReturnCorrectURLForEncounterContext() throws IOException { + fhirBaseAddressStrategyMockedConstruction = Mockito.mockConstruction(FhirBaseAddressStrategy.class, + (mock, context) -> { + when(mock.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)); + } + + @Test + public void shouldReturnErrorWhenLaunchContextNotPresent() throws IOException { + fhirBaseAddressStrategyMockedConstruction = Mockito.mockConstruction(FhirBaseAddressStrategy.class, + (mock, context) -> { + when(mock.getBaseSmartLaunchAddress(request)).thenReturn(BASE_LAUNCH_ADDRESS); + }); + + servlet.doGet(request, response); + + assertThat(response, notNullValue()); + assertThat(response.getErrorMessage(), equalTo("launchContext must be provided")); + } + +} diff --git a/omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartLaunchOptionSelectedTest.java b/omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartLaunchOptionSelectedTest.java new file mode 100644 index 0000000..15d3e2d --- /dev/null +++ b/omod/src/test/java/org.openmrs.module.smartonfhir/web/servlet/SmartLaunchOptionSelectedTest.java @@ -0,0 +1,125 @@ +/* + * 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.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.openmrs.module.smartonfhir.util.SmartSecretKeyHolder; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.mock.web.MockHttpServletResponse; + +@RunWith(MockitoJUnitRunner.class) +public class SmartLaunchOptionSelectedTest { + + private static final String TOKEN_ENCOUNTER = "http://localhost:8180/auth/realms/openmrs/login-actions/action-token?key=eyJhbGciOiJIUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICIzNGQzMTk4Ny0zYjI0LTQ4MzMtOWUwZi1hMWExYTIxYzc5NDUifQ.eyJleHAiOjE2NDEwMTQ0NzAsImlhdCI6MTY0MTAxNDE3MCwianRpIjoiMTVjZDgxYzItMGM5Ni00MGI2LTkyZTUtNGM2Y2MyMWEzZDQ4IiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo4MTgwL2F1dGgvcmVhbG1zL29wZW5tcnMiLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjgxODAvYXV0aC9yZWFsbXMvb3Blbm1ycyIsInN1YiI6ImY6ZDllNTk0ZDItMWE5NC00YjNjLTkwZTQtODBhYmI5ODAzOTY4OjEiLCJ0eXAiOiJzbWFydC1wYXRpZW50LXNlbGVjdGlvbiIsIm5vbmNlIjoiMTVjZDgxYzItMGM5Ni00MGI2LTkyZTUtNGM2Y2MyMWEzZDQ4IiwiYXNpZCI6ImRmZTUwMTczLWE1NjQtNDE1ZS1hOGQzLTBiYjNmZDY4MGFkNy5VZDhUS2x2amJoNC45MzBkM2JkYS1iNTY4LTQ1YTItODgyZS05MGQxMTNjNjMxNzciLCJhc2lkIjoiZGZlNTAxNzMtYTU2NC00MTVlLWE4ZDMtMGJiM2ZkNjgwYWQ3LlVkOFRLbHZqYmg0LjkzMGQzYmRhLWI1NjgtNDVhMi04ODJlLTkwZDExM2M2MzE3NyIsInVzZXIiOiJleUpoYkdjaU9pSklVekkxTmlJc0luUjVjQ0lnT2lBaVNsZFVJbjAuZXlKbGVIQWlPakUyTkRFd01UUTBOekFzSW1semN5STZJbWgwZEhBNkx5OXNiMk5oYkdodmMzUTZPREU0TUM5aGRYUm9MM0psWVd4dGN5OXZjR1Z1YlhKeklpd2lZWFZrSWpvaWFIUjBjRG92TDJ4dlkyRnNhRzl6ZERvNE1EZ3dJaXdpYzNWaUlqb2lZV1J0YVc0aUxDSjBlWEFpT2lKemJXRnlkQzExYzJWeWJtRnRaUzEwYjJ0bGJpSXNJbUZ6YVdRaU9pSnpiV0Z5ZEVOc2FXVnVkQ0o5LmV0Wm5BN2JPZEpWWGR5dEhha1VCVEJPZ1BzLWg4WVBkV3hyUDRWZl9fbWMiLCJsYXVuY2hUeXBlIjoiL3BhdGllbnQgL2VuY291bnRlciJ9.SyloT1oqNdLGCPdFTb4CjKQMrvO0Pjhv1vOp5YLaSrI&client_id=smartClient&tab_id=Ud8TKlvjbh4&execution=9e89e1f3-41cd-4d92-946c-d8f56e9af62a&app-token=%7BAPP_TOKEN%7D"; + + private static final String TOKEN_PATIENT = "http://localhost:8180/auth/realms/openmrs/login-actions/action-token?key=eyJhbGciOiJIUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJkZjJhYTBjMC0wMzFjLTQ5ZjItYTczMC1hYmM0Mjg2OTM1Y2UifQ.eyJleHAiOjE2NDEyNzIyNzIsImlhdCI6MTY0MTI3MTk3MiwianRpIjoiMjIwZDlmYWMtYjgyNi00ZjVhLWJmM2UtNDg4NmY3MjI3OWFiIiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo4MTgwL2F1dGgvcmVhbG1zL29wZW5tcnMiLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjgxODAvYXV0aC9yZWFsbXMvb3Blbm1ycyIsInN1YiI6ImY6ZDllNTk0ZDItMWE5NC00YjNjLTkwZTQtODBhYmI5ODAzOTY4OjEiLCJ0eXAiOiJzbWFydC1wYXRpZW50LXNlbGVjdGlvbiIsIm5vbmNlIjoiMjIwZDlmYWMtYjgyNi00ZjVhLWJmM2UtNDg4NmY3MjI3OWFiIiwiYXNpZCI6IjcwZmU4ODNjLWVhNGUtNGRmNy1iYmI5LWU0MTdjNGVlOWI5OS5JOUlhSEFGWFBaMC45MzBkM2JkYS1iNTY4LTQ1YTItODgyZS05MGQxMTNjNjMxNzciLCJhc2lkIjoiNzBmZTg4M2MtZWE0ZS00ZGY3LWJiYjktZTQxN2M0ZWU5Yjk5Lkk5SWFIQUZYUFowLjkzMGQzYmRhLWI1NjgtNDVhMi04ODJlLTkwZDExM2M2MzE3NyIsInVzZXIiOiJleUpoYkdjaU9pSklVekkxTmlJc0luUjVjQ0lnT2lBaVNsZFVJbjAuZXlKbGVIQWlPakUyTkRFeU56SXlOeklzSW1semN5STZJbWgwZEhBNkx5OXNiMk5oYkdodmMzUTZPREU0TUM5aGRYUm9MM0psWVd4dGN5OXZjR1Z1YlhKeklpd2lZWFZrSWpvaWFIUjBjRG92TDJ4dlkyRnNhRzl6ZERvNE1EZ3dJaXdpYzNWaUlqb2lZV1J0YVc0aUxDSjBlWEFpT2lKemJXRnlkQzExYzJWeWJtRnRaUzEwYjJ0bGJpSXNJbUZ6YVdRaU9pSnpiV0Z5ZEVOc2FXVnVkQ0o5LnFoeHE1Z3Z1RERFcmNUbFYyY0lzOFpLSHpzd2VoZWkxRVlqZDNJR05mTk0iLCJsYXVuY2hUeXBlIjoiL3BhdGllbnQifQ.7rqOafcWeQhr0YGhG6cxrIrKUarDTxl3lb0dBi-Hjkk&client_id=smartClient&tab_id=Ud8TKlvjbh4&execution=9e89e1f3-41cd-4d92-946c-d8f56e9af62a&app-token=%7BAPP_TOKEN%7D"; + + private static final byte[] SMART_SECRET_KEY_HOLDER = "SecretKey".getBytes(StandardCharsets.UTF_8); + + private static final String PATIENT_UUID = "12345"; + + private static final String VISIT_UUID = "56789"; + + private MockHttpServletRequest request; + + private MockHttpServletResponse response; + + private SmartLaunchOptionSelected smartLaunchOptionSelected; + + private MockedStatic smartSecretKeyHolderMockedStatic; + + @Before + public void setup() throws Exception { + request = new MockHttpServletRequest(); + response = new MockHttpServletResponse(); + smartLaunchOptionSelected = new SmartLaunchOptionSelected(); + smartSecretKeyHolderMockedStatic = Mockito.mockStatic(SmartSecretKeyHolder.class); + + smartSecretKeyHolderMockedStatic.when(SmartSecretKeyHolder::getSecretKey).thenReturn(SMART_SECRET_KEY_HOLDER); + } + + @After + public void close() { + smartSecretKeyHolderMockedStatic.close(); + } + + @Test + public void shouldReturnCorrectURLWhenLaunchTypeIsEncounter() throws IOException { + request.setParameter("patientId", PATIENT_UUID); + request.setParameter("token", TOKEN_ENCOUNTER); + + smartLaunchOptionSelected.doGet(request, response); + + assertThat(response, notNullValue()); + assertThat(response.getRedirectedUrl(), notNullValue()); + assertThat(response.getRedirectedUrl(), containsString("app=smartonfhir.search.visit")); + assertThat(response.getRedirectedUrl(), containsString("patientId=12345")); + } + + @Test + public void shouldReturnCorrectURLWhenLaunchTypeIsPatient() throws IOException { + request.setParameter("patientId", PATIENT_UUID); + request.setParameter("token", TOKEN_PATIENT); + + smartLaunchOptionSelected.doGet(request, response); + + assertThat(response, notNullValue()); + assertThat(response.getRedirectedUrl(), notNullValue()); + assertThat(response.getRedirectedUrl(), containsString("key=")); + assertThat(response.getRedirectedUrl(), containsString("app-token=")); + } + + @Test + public void shouldReturnCorrectRedirectURlWhenLaunchTypeIsEncounter() throws IOException { + request.setParameter("token", TOKEN_ENCOUNTER); + request.setParameter("visitId", VISIT_UUID); + request.setParameter("patientId", PATIENT_UUID); + + smartLaunchOptionSelected.doGet(request, response); + + assertThat(response, notNullValue()); + assertThat(response.getRedirectedUrl(), notNullValue()); + assertThat(response.getRedirectedUrl(), containsString("key=")); + assertThat(response.getRedirectedUrl(), containsString("app-token=")); + } + + @Test + public void shouldThrowErrorWhenPatientAndVisitIdIsNull() throws IOException { + request.setParameter("token", TOKEN_PATIENT); + smartLaunchOptionSelected.doGet(request, response); + + assertThat(response, notNullValue()); + assertThat(response.getErrorMessage(), notNullValue()); + assertThat(response.getErrorMessage(), equalTo("PatientId must be provided")); + } + + @Test + public void shouldThrowErrorWhenTokenIsNull() throws IOException { + smartLaunchOptionSelected.doGet(request, response); + + assertThat(response, notNullValue()); + assertThat(response.getErrorMessage(), notNullValue()); + assertThat(response.getErrorMessage(), equalTo("Couldn't find token in url")); + } +} diff --git a/pom.xml b/pom.xml index b528320..a1b3163 100644 --- a/pom.xml +++ b/pom.xml @@ -335,6 +335,13 @@ 1.28.0 provided + + com.fasterxml.jackson.core + jackson-databind + 2.7.3 + provided + +