2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 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.interceptor;
39 import static org.junit.Assert.assertEquals;
40 import static org.mockito.Matchers.anyString;
41 import static org.mockito.Mockito.when;
43 import java.io.PrintWriter;
44 import java.util.ArrayList;
45 import java.util.HashSet;
46 import java.util.List;
49 import javax.servlet.http.HttpServletRequest;
50 import javax.servlet.http.HttpServletResponse;
51 import javax.servlet.http.HttpSession;
53 import org.junit.Before;
54 import org.junit.Ignore;
55 import org.junit.Test;
56 import org.junit.runner.RunWith;
57 import org.mockito.InjectMocks;
58 import org.mockito.Mock;
59 import org.mockito.MockitoAnnotations;
60 import org.onap.portalapp.portal.controller.BasicAuthenticationController;
61 import org.onap.portalapp.portal.core.MockEPUser;
62 import org.onap.portalapp.portal.domain.EPApp;
63 import org.onap.portalapp.portal.domain.EPUser;
64 import org.onap.portalapp.portal.service.ExternalAccessRolesService;
65 import org.onap.portalsdk.core.controller.FusionBaseController;
66 import org.onap.portalsdk.core.util.SystemProperties;
67 import org.powermock.api.mockito.PowerMockito;
68 import org.powermock.core.classloader.annotations.PrepareForTest;
69 import org.powermock.modules.junit4.PowerMockRunner;
70 import org.springframework.web.method.HandlerMethod;
72 @RunWith(PowerMockRunner.class)
73 @PrepareForTest({ SystemProperties.class })
74 public class PortalResourceInterceptorTest {
77 PortalResourceInterceptor portalResourceInterceptor;
79 HttpServletRequest request;
81 HttpServletResponse response;
83 HandlerMethod handler;
87 FusionBaseController fusionBaseController;
89 BasicAuthenticationController basicAuthenticationController;
91 private ExternalAccessRolesService externalAccessRolesService;
93 PrintWriter printWriter;
95 MockEPUser mockUser = new MockEPUser();
99 MockitoAnnotations.initMocks(this);
100 PowerMockito.mockStatic(SystemProperties.class);
105 public void testPreHandle()throws Exception {
106 Set<String> data=new HashSet<>();
108 when(request.getRequestURI()).thenReturn("test/portalApi/test");
110 when(session.getAttribute(anyString())).thenReturn(data);
111 when(request.getSession()).thenReturn(session);
112 when(fusionBaseController.isAccessible()).thenReturn(true);
113 when(handler.getBean()).thenReturn(fusionBaseController);
116 boolean result= portalResourceInterceptor.preHandle(request, response, handler);
117 assertEquals(true, result);
124 public void testPreHandlePass()throws Exception {
125 Set<String> data=new HashSet<>();
126 data.add("test/test");
127 Set<String> allFunctions=new HashSet<>();
128 allFunctions.add("test/test");
129 when(request.getRequestURI()).thenReturn("test/portalApi/test/test");
130 EPUser user = mockUser.mockEPUser();
131 when(request.getSession()).thenReturn(session);
132 when(SystemProperties.getProperty(SystemProperties.ROLE_FUNCTIONS_ATTRIBUTE_NAME)).thenReturn("role_functions_attribute_name");
133 when(SystemProperties.getProperty(SystemProperties.USER_ATTRIBUTE_NAME)).thenReturn("user_attribute_name");
134 when(session.getAttribute("user_attribute_name")).thenReturn(user);
135 when(session.getAttribute("role_functions_attribute_name")).thenReturn(data);
136 when(session.getAttribute("allRoleFunctions")).thenReturn(allFunctions);
137 when(fusionBaseController.isAccessible()).thenReturn(false);
138 when(handler.getBean()).thenReturn(fusionBaseController);
141 boolean result= portalResourceInterceptor.preHandle(request, response, handler);
143 assertEquals(true, result);
150 public void testPreHandleAuth()throws Exception {
151 Set<String> data=new HashSet<>();
152 data.add("test/test");
153 List<EPApp> apps=new ArrayList<>();
154 EPApp app=new EPApp();
155 app.setUsername("test");
157 when(request.getRequestURI()).thenReturn("test/portalApi/test/test");
159 when(request.getHeader("Authorization")).thenReturn("Basictest");
160 when(request.getHeader("uebkey")).thenReturn("test");
161 when(request.getSession()).thenReturn(session);
162 when(fusionBaseController.isAccessible()).thenReturn(false);
163 when(handler.getBean()).thenReturn(basicAuthenticationController);
164 when( externalAccessRolesService.getApp("test")).thenReturn(apps);
165 when(response.getWriter()).thenReturn(printWriter);
167 boolean result= portalResourceInterceptor.preHandle(request, response, handler);
169 assertEquals(false, result);