|
| 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 | +} |
0 commit comments