2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 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.controller.core;
40 import static org.junit.Assert.assertEquals;
42 import java.net.URLDecoder;
43 import java.net.URLEncoder;
44 import java.util.ArrayList;
45 import java.util.List;
47 import javax.servlet.http.Cookie;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
51 import org.junit.Before;
52 import org.junit.Test;
53 import org.junit.runner.RunWith;
54 import org.mockito.InjectMocks;
55 import org.mockito.Matchers;
56 import org.mockito.Mock;
57 import org.mockito.Mockito;
58 import org.mockito.MockitoAnnotations;
59 import org.onap.portalapp.framework.MockitoTestSuite;
60 import org.onap.portalsdk.core.auth.LoginStrategy;
61 import org.onap.portalsdk.core.command.LoginBean;
62 import org.onap.portalsdk.core.domain.RoleFunction;
63 import org.onap.portalsdk.core.domain.User;
64 import org.onap.portalsdk.core.menu.MenuProperties;
65 import org.onap.portalsdk.core.onboarding.util.PortalApiConstants;
66 import org.onap.portalsdk.core.onboarding.util.PortalApiProperties;
67 import org.onap.portalsdk.core.service.LoginService;
68 import org.onap.portalsdk.core.service.RoleService;
69 import org.onap.portalsdk.core.util.SystemProperties;
70 import org.onap.portalsdk.core.web.support.UserUtils;
71 import org.powermock.api.mockito.PowerMockito;
72 import org.powermock.core.classloader.annotations.PrepareForTest;
73 import org.powermock.modules.junit4.PowerMockRunner;
74 import org.springframework.web.servlet.ModelAndView;
75 import org.springframework.web.util.WebUtils;
77 @RunWith(PowerMockRunner.class)
78 @PrepareForTest({ URLDecoder.class, SystemProperties.class, URLEncoder.class, PortalApiProperties.class, WebUtils.class,
80 public class SingleSignOnControllerTest {
83 SingleSignOnController singleSignOnController = new SingleSignOnController();
86 RoleService roleService;
89 LoginService loginService;
92 LoginStrategy loginStrategy;
95 URLDecoder uRLDecoder;
99 MockitoAnnotations.initMocks(this);
102 MockitoTestSuite mockitoTestSuite = new MockitoTestSuite();
104 HttpServletRequest mockedRequest = mockitoTestSuite.getMockedRequest();
105 HttpServletResponse mockedResponse = mockitoTestSuite.getMockedResponse();
106 NullPointerException nullPointerException = new NullPointerException();
108 @Test(expected = java.lang.SecurityException.class)
109 public void singleSignOnLoginExceptionTest() throws Exception {
110 Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("Test");
111 PowerMockito.mockStatic(URLDecoder.class);
112 PowerMockito.mockStatic(SystemProperties.class);
113 Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("Test");
114 Mockito.when(SystemProperties.containsProperty(SystemProperties.APP_BASE_URL)).thenReturn(true);
115 Mockito.when(SystemProperties.getProperty(SystemProperties.APP_BASE_URL)).thenReturn("http://TestUrl");
116 Mockito.when(SystemProperties.getProperty(SystemProperties.COOKIE_DOMAIN)).thenReturn("te");
117 singleSignOnController.singleSignOnLogin(mockedRequest);
122 public void singleSignOnLoginTest() throws Exception {
123 Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("Test");
124 PowerMockito.mockStatic(URLDecoder.class);
125 PowerMockito.mockStatic(SystemProperties.class);
126 PowerMockito.mockStatic(URLEncoder.class);
127 PowerMockito.mockStatic(PortalApiProperties.class);
128 Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("Test");
129 Mockito.when(SystemProperties.containsProperty(SystemProperties.APP_BASE_URL)).thenReturn(true);
130 Mockito.when(SystemProperties.getProperty(SystemProperties.APP_BASE_URL)).thenReturn("http://TestUrl");
131 Mockito.when(SystemProperties.getProperty(SystemProperties.COOKIE_DOMAIN)).thenReturn("TestUrl");
132 Mockito.when(URLEncoder.encode("http://TestUrl/Test", "UTF-8")).thenReturn("encodeTestUrl");
133 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("uebkey");
134 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL))
135 .thenReturn("http://test.com/roles");
136 ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest);
137 assertEquals(expectedResults.getViewName(),
138 "redirect:http://test.com/process_csp?uebAppKey=uebkey&redirectUrl=http%3A%2F%2FTestUrl%2FTest");
142 public void singleSignOnLoginIfUrlIsNotAppBasedTest() throws Exception {
143 Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("Test");
144 PowerMockito.mockStatic(URLDecoder.class);
145 PowerMockito.mockStatic(SystemProperties.class);
146 PowerMockito.mockStatic(URLEncoder.class);
147 PowerMockito.mockStatic(PortalApiProperties.class);
148 Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("Test");
149 Mockito.when(SystemProperties.containsProperty(SystemProperties.APP_BASE_URL)).thenReturn(false);
150 Mockito.when(SystemProperties.getProperty(SystemProperties.COOKIE_DOMAIN)).thenReturn("test.com");
151 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.UEB_APP_KEY)).thenReturn("uebkey");
152 Mockito.when(PortalApiProperties.getProperty(PortalApiConstants.ECOMP_REDIRECT_URL))
153 .thenReturn("http://test.com/roles");
154 StringBuffer stringBuffer = new StringBuffer("http://test.com/testSDK");
155 Mockito.when(mockedRequest.getRequestURL()).thenReturn(stringBuffer);
156 ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest);
157 assertEquals(expectedResults.getViewName(),
158 "redirect:http://test.com/process_csp?uebAppKey=uebkey&redirectUrl=http%3A%2F%2Ftest.com%2FtestSDK");
162 public void singleSignOnTest() throws Exception {
163 Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("http://Test.com");
164 PowerMockito.mockStatic(URLDecoder.class);
165 PowerMockito.mockStatic(SystemProperties.class);
166 PowerMockito.mockStatic(WebUtils.class);
167 Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("http://Test.com");
168 Mockito.when(mockedRequest.getParameter("redirectToPortal")).thenReturn(null);
169 Mockito.when(SystemProperties.containsProperty(SystemProperties.APP_BASE_URL)).thenReturn(true);
170 Mockito.when(SystemProperties.getProperty(SystemProperties.APP_BASE_URL)).thenReturn("http://TestUrl");
171 Mockito.when(SystemProperties.getProperty(SystemProperties.COOKIE_DOMAIN)).thenReturn("Test.com");
172 Mockito.when(WebUtils.getCookie(mockedRequest, "EPService")).thenReturn(new Cookie("test", "test"));
173 User user = new User();
174 user.setOrgUserId("test12");
175 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
176 ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest);
177 assertEquals(expectedResults.getViewName(), "redirect:http://Test.com");
182 public void singleSignOnIfUserNullTest() throws Exception {
184 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
185 Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("http://Test.com");
186 PowerMockito.mockStatic(URLDecoder.class);
187 PowerMockito.mockStatic(WebUtils.class);
188 PowerMockito.mockStatic(SystemProperties.class);
190 Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("http://Test.com");
191 Mockito.when(WebUtils.getCookie(mockedRequest, "EPService")).thenReturn(new Cookie("test", "test"));
192 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
193 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("testauth");
194 Mockito.when(loginStrategy.getUserId(mockedRequest)).thenReturn("test1234");
195 Mockito.when(mockedRequest.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY)).thenReturn("test");
196 LoginBean commandBean = new LoginBean();
197 commandBean.setUserid("test1234");
198 commandBean.setUser(null);
199 Mockito.when(loginService.findUser(Matchers.any(), Matchers.anyString(), Matchers.anyMap()))
200 .thenReturn(commandBean);
201 List<RoleFunction> roleFunctionList = new ArrayList<>();
202 Mockito.when(roleService.getRoleFunctions("test1234")).thenReturn(roleFunctionList);
203 ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest);
204 assertEquals(expectedResults.getViewName(), "redirect:null?noUserError=Yes");
208 public void singleSignOnIfUserNotNullTest() throws Exception {
210 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
211 Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("http://Test.com");
212 PowerMockito.mockStatic(URLDecoder.class);
213 PowerMockito.mockStatic(WebUtils.class);
214 PowerMockito.mockStatic(SystemProperties.class);
215 PowerMockito.mockStatic(UserUtils.class);
217 Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("http://Test.com");
218 Mockito.when(WebUtils.getCookie(mockedRequest, "EPService")).thenReturn(new Cookie("test", "test"));
219 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
220 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("testauth");
221 Mockito.when(loginStrategy.getUserId(mockedRequest)).thenReturn("test1234");
222 Mockito.when(mockedRequest.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY)).thenReturn("test");
223 LoginBean commandBean = new LoginBean();
224 commandBean.setUserid("test1234");
225 User user1 = new User();
226 user1.setId((long) 1);
227 commandBean.setUser(user1);
228 Mockito.when(loginService.findUser(Matchers.any(), Matchers.anyString(), Matchers.anyMap()))
229 .thenReturn(commandBean);
230 List<RoleFunction> roleFunctionList = new ArrayList<>();
231 Mockito.when(roleService.getRoleFunctions("test1234")).thenReturn(roleFunctionList);
232 ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest);
233 assertEquals(expectedResults.getViewName(), "redirect:http://Test.com");
237 public void singleSignOnIfUserNotNullAndAuthNullTest() throws Exception {
239 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
240 Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("http://Test.com");
241 PowerMockito.mockStatic(URLDecoder.class);
242 PowerMockito.mockStatic(WebUtils.class);
243 PowerMockito.mockStatic(SystemProperties.class);
244 PowerMockito.mockStatic(UserUtils.class);
246 Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("http://Test.com");
247 Mockito.when(WebUtils.getCookie(mockedRequest, "EPService")).thenReturn(new Cookie("test", "test"));
248 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
249 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn(null);
250 Mockito.when(loginStrategy.getUserId(mockedRequest)).thenReturn("test1234");
251 Mockito.when(mockedRequest.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY)).thenReturn("test");
252 LoginBean commandBean = new LoginBean();
253 commandBean.setUserid("test1234");
254 User user1 = new User();
255 user1.setId((long) 1);
256 commandBean.setUser(user1);
257 Mockito.when(loginService.findUser(Matchers.any(), Matchers.anyString(), Matchers.anyMap()))
258 .thenReturn(commandBean);
259 List<RoleFunction> roleFunctionList = new ArrayList<>();
260 Mockito.when(roleService.getRoleFunctions("test1234")).thenReturn(roleFunctionList);
261 ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest);
262 assertEquals(expectedResults.getViewName(), "redirect:http://Test.com");
266 public void singleSignOnIfUserNotNullAndAuthCSPTest() throws Exception {
267 singleSignOnController.setViewName("test");
268 singleSignOnController.setWelcomeView("welcome");
269 assertEquals(singleSignOnController.getViewName(), "test");
270 assertEquals(singleSignOnController.getWelcomeView(), "welcome");
272 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
273 Mockito.when(mockedRequest.getParameter("forwardURL")).thenReturn("http://Test.com");
274 PowerMockito.mockStatic(URLDecoder.class);
275 PowerMockito.mockStatic(WebUtils.class);
276 PowerMockito.mockStatic(SystemProperties.class);
277 PowerMockito.mockStatic(UserUtils.class);
278 Mockito.when(URLDecoder.decode(null, "UTF-8")).thenReturn("http://Test.com");
279 Mockito.when(WebUtils.getCookie(mockedRequest, "EPService")).thenReturn(new Cookie("test", "test"));
280 Mockito.when(UserUtils.getUserSession(mockedRequest)).thenReturn(user);
281 Mockito.when(SystemProperties.getProperty(SystemProperties.AUTHENTICATION_MECHANISM)).thenReturn("CSP");
282 Mockito.when(loginStrategy.getUserId(mockedRequest)).thenReturn("test1234");
283 Mockito.when(mockedRequest.getAttribute(MenuProperties.MENU_PROPERTIES_FILENAME_KEY)).thenReturn("test");
284 LoginBean commandBean = new LoginBean();
285 commandBean.setUserid("test1234");
286 User user1 = new User();
287 user1.setId((long) 1);
288 commandBean.setUser(user1);
289 Mockito.when(loginService.findUser(Matchers.any(), Matchers.anyString(), Matchers.anyMap()))
290 .thenReturn(commandBean);
291 List<RoleFunction> roleFunctionList = new ArrayList<>();
292 Mockito.when(roleService.getRoleFunctions("test1234")).thenReturn(roleFunctionList);
293 ModelAndView expectedResults = singleSignOnController.singleSignOnLogin(mockedRequest);
294 assertEquals(expectedResults.getViewName(), "redirect:http://Test.com");