2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.controller;
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertNotEquals;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
44 import static org.mockito.Matchers.*;
45 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
46 import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
48 import java.io.BufferedReader;
49 import java.io.ByteArrayInputStream;
50 import java.io.StringReader;
51 import java.nio.charset.StandardCharsets;
52 import java.util.HashSet;
55 import javax.servlet.http.Cookie;
56 import javax.servlet.http.HttpServletRequest;
57 import javax.servlet.http.HttpServletResponse;
58 import javax.servlet.http.HttpSession;
60 import org.json.JSONObject;
61 import org.junit.Before;
62 import org.junit.Test;
63 import org.junit.runner.RunWith;
64 import org.mockito.InjectMocks;
65 import org.mockito.Matchers;
66 import org.mockito.Mock;
67 import org.mockito.MockitoAnnotations;
68 import org.onap.portalapp.command.EPLoginBean;
69 import org.onap.portalapp.controller.LoginController;
70 import org.onap.portalapp.portal.core.MockEPUser;
71 import org.onap.portalapp.portal.domain.EPUser;
72 import org.onap.portalapp.portal.framework.MockitoTestSuite;
73 import org.onap.portalapp.portal.service.EPLoginService;
74 import org.onap.portalapp.portal.service.EPRoleFunctionService;
75 import org.onap.portalapp.portal.service.EPRoleService;
76 import org.onap.portalapp.portal.service.SharedContextService;
77 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
78 import org.onap.portalapp.service.EPProfileService;
79 import org.onap.portalapp.util.SessionCookieUtil;
80 import org.onap.portalsdk.core.domain.MenuData;
81 import org.onap.portalsdk.core.onboarding.util.CipherUtil;
82 import org.onap.portalsdk.core.util.SystemProperties;
83 import org.onap.portalsdk.core.web.support.AppUtils;
84 import org.onap.portalsdk.core.web.support.UserUtils;
85 import org.powermock.api.mockito.PowerMockito;
86 import org.powermock.core.classloader.annotations.PrepareForTest;
87 import org.powermock.modules.junit4.PowerMockRunner;
88 import org.springframework.mock.web.DelegatingServletInputStream;
89 import org.springframework.test.web.servlet.MockMvc;
90 import org.springframework.test.web.servlet.setup.MockMvcBuilders;
91 import org.springframework.web.servlet.ModelAndView;
93 @RunWith(PowerMockRunner.class)
94 @PrepareForTest({ SystemProperties.class, CipherUtil.class, AppUtils.class, UserUtils.class, EPCommonSystemProperties.class,SessionCookieUtil.class})
95 public class LoginControllerTest {
97 private MockMvc mockMvc;
100 EPProfileService service;
102 EPLoginService loginService;
104 SharedContextService sharedContextService;
106 EPRoleService roleService;
108 EPRoleFunctionService ePRoleFunctionService;
114 LoginController loginController;
117 public void setup() {
118 MockitoAnnotations.initMocks(this);
119 mockMvc = MockMvcBuilders.standaloneSetup(loginController).build();
122 MockEPUser mockUser = new MockEPUser();
124 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
126 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
127 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
129 NullPointerException nullPointerException = new NullPointerException();
130 private DelegatingServletInputStream dsi;
133 public void loginIfAuthNullTest() {
134 PowerMockito.mockStatic(SystemProperties.class);
135 when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn(null);
136 ModelAndView result = loginController.login(mockedRequest);
137 assertEquals(result.getViewName(), "openIdLogin");
144 public void loginIfAuthOIDCTest() {
145 PowerMockito.mockStatic(SystemProperties.class);
146 when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("OIDC");
147 ModelAndView result = loginController.login(mockedRequest);
148 assertEquals(result.getViewName(), "login");
152 public void loginTest() {
153 PowerMockito.mockStatic(SystemProperties.class);
154 when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("Test");
155 ModelAndView result = loginController.login(mockedRequest);
156 assertEquals(result.getViewName(), "login");
160 public void processSign()throws Exception {
161 mockMvc.perform(get("/process_csp"))//.header("Authorization", basic_auth))
162 .andExpect(status().is3xxRedirection());
163 //processSingleSignOn
168 public void processSingleSign()throws Exception {
169 PowerMockito.mockStatic(SessionCookieUtil.class);
171 when(SessionCookieUtil.getUserIdFromCookie(mockedRequest, mockedResponse)).thenReturn("user");
172 when(cookie.getName()).thenReturn("UserId");
173 when(cookie.getValue()).thenReturn("user");
176 mockMvc.perform(get("/processSingleSignOn").cookie(cookie))
178 .andExpect(status().is3xxRedirection());
184 public void loginValidateTest() throws Exception {
185 PowerMockito.mockStatic(SystemProperties.class);
186 PowerMockito.mockStatic(AppUtils.class);
187 PowerMockito.mockStatic(UserUtils.class);
188 PowerMockito.mockStatic(CipherUtil.class);
189 PowerMockito.mockStatic(EPCommonSystemProperties.class);
190 EPUser user = mockUser.mockEPUser();
191 HttpServletRequest request = mock(HttpServletRequest.class);
192 HttpServletResponse response = mock(HttpServletResponse.class);
193 HttpSession session = mock(HttpSession.class);
194 String json = "{\"loginId\":\"test\", \"password\":\"xyz\"}";
195 dsi = new DelegatingServletInputStream(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)));
196 when(request.getInputStream()).thenReturn(dsi);
197 when(request.getReader()).thenReturn(new BufferedReader(new StringReader(json)));
198 when(request.getContentType()).thenReturn("application/json");
199 when(request.getCharacterEncoding()).thenReturn("UTF-8");
200 when(request.getAttribute("menu_properties_filename")).thenReturn("test");
201 StringBuffer reqUrl = new StringBuffer("http://localhost.com");
202 when(request.getRequestURL()).thenReturn(reqUrl);
203 when(request.getQueryString()).thenReturn("demo?test");
204 when(request.getSession(true)).thenReturn(session);
205 when(request.getSession()).thenReturn(session);
206 EPLoginBean commandBean = new EPLoginBean();
207 commandBean.setLoginId("guestT");
208 commandBean.setUser(user);
209 commandBean.setOrgUserId("guestT");
210 commandBean.setLoginPwd("xyz");
211 Set<MenuData> menus = new HashSet<MenuData>();
212 MenuData menuData = new MenuData();
213 menuData.setFunctionCd("test");
214 MenuData menuData2 = new MenuData();
215 menuData2.setFunctionCd("test2");
217 menus.add(menuData2);
218 commandBean.setMenu(menus);
219 commandBean.setBusinessDirectMenu(menus);
220 when(loginController.getLoginService().findUser(Matchers.any(EPLoginBean.class), Matchers.anyString(),
221 Matchers.any())).thenReturn(commandBean);
222 when(AppUtils.getSession(request)).thenReturn(session);
223 when(UserUtils.isAccessible(request, menuData.getFunctionCd())).thenReturn(true);
224 when(UserUtils.isAccessible(request, menuData2.getFunctionCd())).thenReturn(true);
225 when(EPCommonSystemProperties.getProperty(EPCommonSystemProperties.COOKIE_DOMAIN)).thenReturn("cookie_domain");
226 when(CipherUtil.encryptPKC(Matchers.anyString(), Matchers.anyString())).thenReturn("guestT");
227 String actual = loginController.loginValidate(request, response);
228 JSONObject expected = new JSONObject("{success: success}");
229 assertNotEquals(actual, expected);