2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
27 import java.io.IOException;
29 import javax.servlet.FilterChain;
30 import javax.servlet.ServletException;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpServletResponse;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
38 import org.openecomp.policy.pypdp.authorization.AuthenticationFilter;
41 * Authentication Filter Testing
43 public class AuthorizationTest {
44 private static final String MASTERCLIENT= "cHl0aG9uOnRlc3Q=";
45 /*private static final String CONFIGCLIENT= "Y29uZmlnOmNvbmZpZw==";
46 private static final String ACTIONCLIENT= "YWN0aW9uOmFjdGlvbg==";
47 private static final String DECIDECLIENT= "ZGVjaWRlOmRlY2lkZQ==";
48 private static final String CREATECLIENT= "Y3JlYXRlOmNyZWF0ZQ==";
49 private static final String DELETECLIENT= "ZGVsZXRlOmRlbGV0ZQ==";*/
51 private AuthenticationFilter authenticationFilter = new AuthenticationFilter();
54 public void setUp() throws Exception{
55 authenticationFilter.init(null);
59 public void testDoFilterError() throws IOException, ServletException {
60 // create the objects to be mocked
61 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
62 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
63 FilterChain filterChain = mock(FilterChain.class);
65 when(httpServletRequest.getRequestURI()).thenReturn("error");
67 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
70 // verify if unauthorized
71 verify(httpServletResponse).setStatus(HttpServletResponse.SC_BAD_REQUEST);
75 public void testDoFilterNotification() throws IOException, ServletException {
76 // create the objects to be mocked
77 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
78 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
79 FilterChain filterChain = mock(FilterChain.class);
81 when(httpServletRequest.getRequestURI()).thenReturn("org.openecomp.policy.pypdp.notifications swagger api-docs configuration");
83 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
86 verify(filterChain).doFilter(httpServletRequest,httpServletResponse);
90 public void testDoFilterWrongAuthenticaton() throws IOException, ServletException {
91 // create the objects to be mocked
92 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
93 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
94 FilterChain filterChain = mock(FilterChain.class);
96 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("error");
97 when(httpServletRequest.getRequestURI()).thenReturn("getConfig");
99 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
102 // verify if unauthorized
103 verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
107 public void testDoFilterWrongClientAuthenticaton() throws IOException, ServletException {
108 // create the objects to be mocked
109 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
110 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
111 FilterChain filterChain = mock(FilterChain.class);
113 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
114 when(httpServletRequest.getRequestURI()).thenReturn("getConfig");
115 when(httpServletRequest.getHeader("ClientAuth")).thenReturn("Error");
117 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
119 // verify if unauthorized
120 verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
124 public void testDoFilterWrongClientAuthenticatonCount() throws IOException, ServletException {
125 // create the objects to be mocked
126 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
127 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
128 FilterChain filterChain = mock(FilterChain.class);
130 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
131 when(httpServletRequest.getRequestURI()).thenReturn("count");
132 when(httpServletRequest.getHeader("ClientAuth")).thenReturn("Error");
134 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
137 verify(filterChain).doFilter(httpServletRequest,httpServletResponse);
141 public void testDoFilterWrongGetConfigAuthorization() throws IOException, ServletException {
142 // create the objects to be mocked
143 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
144 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
145 FilterChain filterChain = mock(FilterChain.class);
147 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
148 when(httpServletRequest.getRequestURI()).thenReturn("getConfig");
149 when(httpServletRequest.getHeader("ClientAuth")).thenReturn(ACTIONCLIENT);
151 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
153 // verify if unauthorized
154 verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
158 public void testDoFilterWrongSendEventAuthorization() throws IOException, ServletException {
159 // create the objects to be mocked
160 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
161 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
162 FilterChain filterChain = mock(FilterChain.class);
164 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
165 when(httpServletRequest.getRequestURI()).thenReturn("sendEvent");
166 when(httpServletRequest.getHeader("ClientAuth")).thenReturn(CONFIGCLIENT);
168 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
170 // verify if unauthorized
171 verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
175 public void testDoFilterWrongUpdatePolicyAuthorization() throws IOException, ServletException {
176 // create the objects to be mocked
177 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
178 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
179 FilterChain filterChain = mock(FilterChain.class);
181 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
182 when(httpServletRequest.getRequestURI()).thenReturn("updatePolicy");
183 when(httpServletRequest.getHeader("ClientAuth")).thenReturn(ACTIONCLIENT);
185 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
187 // verify if unauthorized
188 verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
192 public void testDoFilterWrongCreatePolicyAuthorization() throws IOException, ServletException {
193 // create the objects to be mocked
194 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
195 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
196 FilterChain filterChain = mock(FilterChain.class);
198 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
199 when(httpServletRequest.getRequestURI()).thenReturn("createPolicy");
200 when(httpServletRequest.getHeader("ClientAuth")).thenReturn(ACTIONCLIENT);
202 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
204 // verify if unauthorized
205 verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
209 public void testDoFilterWrongPushPolicyAuthorization() throws IOException, ServletException {
210 // create the objects to be mocked
211 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
212 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
213 FilterChain filterChain = mock(FilterChain.class);
215 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
216 when(httpServletRequest.getRequestURI()).thenReturn("pushPolicy");
217 when(httpServletRequest.getHeader("ClientAuth")).thenReturn(DELETECLIENT);
219 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
221 // verify if unauthorized
222 verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
226 public void testDoFilterWrongDeletePolicyAuthorization() throws IOException, ServletException {
227 // create the objects to be mocked
228 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
229 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
230 FilterChain filterChain = mock(FilterChain.class);
232 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
233 when(httpServletRequest.getRequestURI()).thenReturn("deletePolicy");
234 when(httpServletRequest.getHeader("ClientAuth")).thenReturn(DECIDECLIENT);
236 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
238 // verify if unauthorized
239 verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
243 public void testDoFilterWrongDecidePolicyAuthorization() throws IOException, ServletException {
244 // create the objects to be mocked
245 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
246 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
247 FilterChain filterChain = mock(FilterChain.class);
249 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
250 when(httpServletRequest.getRequestURI()).thenReturn("getDecision");
251 when(httpServletRequest.getHeader("ClientAuth")).thenReturn(CREATECLIENT);
253 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
255 // verify if unauthorized
256 verify(httpServletResponse).setStatus(HttpServletResponse.SC_UNAUTHORIZED);
260 public void testDoFilterAuthorizedError() throws IOException, ServletException {
261 // create the objects to be mocked
262 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
263 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
264 FilterChain filterChain = mock(FilterChain.class);
266 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
267 when(httpServletRequest.getRequestURI()).thenReturn("error");
268 when(httpServletRequest.getHeader("ClientAuth")).thenReturn(MASTERCLIENT);
270 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
272 // verify if unauthorized
273 verify(httpServletResponse).setStatus(HttpServletResponse.SC_BAD_REQUEST);
277 public void testDoFilterAuthorizedPDPs() throws IOException, ServletException {
278 // create the objects to be mocked
279 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
280 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
281 FilterChain filterChain = mock(FilterChain.class);
283 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
284 when(httpServletRequest.getRequestURI()).thenReturn("pdps paps");
285 when(httpServletRequest.getHeader("ClientAuth")).thenReturn(MASTERCLIENT);
287 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
290 verify(filterChain).doFilter(httpServletRequest,httpServletResponse);
294 public void testDoFilterDecideAuthorized() throws IOException, ServletException {
295 // create the objects to be mocked
296 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
297 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
298 FilterChain filterChain = mock(FilterChain.class);
300 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
301 when(httpServletRequest.getRequestURI()).thenReturn("getDecision");
302 when(httpServletRequest.getHeader(AuthenticationFilter.ENVIRONMENT_HEADER)).thenReturn("DEVL");
304 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
307 verify(filterChain).doFilter(httpServletRequest,httpServletResponse);
311 public void testDoFilterDeleteAuthorized() throws IOException, ServletException {
312 // create the objects to be mocked
313 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
314 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
315 FilterChain filterChain = mock(FilterChain.class);
317 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
318 when(httpServletRequest.getRequestURI()).thenReturn("deletePolicy");
319 when(httpServletRequest.getHeader(AuthenticationFilter.ENVIRONMENT_HEADER)).thenReturn("DEVL");
321 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
324 verify(filterChain).doFilter(httpServletRequest,httpServletResponse);
328 public void testDoFilterEventAuthorized() throws IOException, ServletException {
329 // create the objects to be mocked
330 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
331 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
332 FilterChain filterChain = mock(FilterChain.class);
334 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
335 when(httpServletRequest.getRequestURI()).thenReturn("sendEvent");
336 when(httpServletRequest.getHeader(AuthenticationFilter.ENVIRONMENT_HEADER)).thenReturn("DEVL");
338 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
341 verify(filterChain).doFilter(httpServletRequest,httpServletResponse);
345 public void testDoFilterCreateAuthorized() throws IOException, ServletException {
346 // create the objects to be mocked
347 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
348 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
349 FilterChain filterChain = mock(FilterChain.class);
351 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
352 when(httpServletRequest.getRequestURI()).thenReturn("createPolicy pushPolicy updatePolicy");
353 when(httpServletRequest.getHeader(AuthenticationFilter.ENVIRONMENT_HEADER)).thenReturn("DEVL");
355 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
358 verify(filterChain).doFilter(httpServletRequest,httpServletResponse);
362 public void testDoFilterConfigAuthorized() throws IOException, ServletException {
363 // create the objects to be mocked
364 HttpServletRequest httpServletRequest = mock(HttpServletRequest.class);
365 HttpServletResponse httpServletResponse = mock(HttpServletResponse.class);
366 FilterChain filterChain = mock(FilterChain.class);
368 when(httpServletRequest.getHeader(AuthenticationFilter.AUTHENTICATION_HEADER)).thenReturn("Basic dGVzdHJlc3Q6c2VjVXJl");
369 when(httpServletRequest.getRequestURI()).thenReturn("getConfig");
370 when(httpServletRequest.getHeader(AuthenticationFilter.ENVIRONMENT_HEADER)).thenReturn("DEVL");
372 authenticationFilter.doFilter(httpServletRequest, httpServletResponse,
375 verify(filterChain).doFilter(httpServletRequest,httpServletResponse);
379 public void tearDown(){
380 authenticationFilter.destroy();