blob: 6ce9bde8bafd81ca8d56d356889dbb02403e0307 [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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=========================================================
Aharoni, Pavel (pa0916)d2946182018-12-17 11:21:02 +020019 *//*
20
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +030021
22
23
24package org.onap.osam.controller;
25
26
27import org.junit.Assert;
28import org.mockito.Mockito;
29import org.onap.osam.filters.ClientCredentialsFilter;
30import org.testng.annotations.DataProvider;
31import org.testng.annotations.Test;
32
33import javax.servlet.FilterChain;
34import javax.servlet.ServletException;
35import javax.servlet.http.HttpServletRequest;
36import javax.servlet.http.HttpServletResponse;
37import java.io.IOException;
38
39import static org.mockito.Matchers.any;
40
41
42@Test
43public class ClientCredentialsFilterTest {
44
45 @DataProvider
46 public static Object[][] authorizedData() {
47 return new Object[][] {
48 {"Basic 123==", null},
49 {null, null},
50 {null, ""},
51 {"Basic 123==", ""},
52 {"Basic 123==", "Basic 123=="}
53 };
54 }
55
56 @DataProvider
57 public static Object[][] notAuthorizedData() {
58 return new Object[][] {
59 {null, "Basic 123=="},
60 {"", "Basic 123=="},
61 {"not null but not as expected", "Basic 123=="},
62 {"basic 123==", "Basic 123=="}
63 };
64 }
65
66 @DataProvider
67 public static Object[][] clientVerified() {
68 return new Object[][] {
69 {true},
70 {false}
71 };
72 }
73
74 @Test(dataProvider = "authorizedData")
75 public void givenAuthorizationHeader_Authorized(String actualAuth, String expectedAuth){
76 ClientCredentialsFilter filter = new ClientCredentialsFilter();
77 Assert.assertTrue(filter.verifyClientCredentials(actualAuth, expectedAuth));
78 }
79
80 @Test(dataProvider = "notAuthorizedData")
81 public void givenAuthorizationHeader_NotAuthorized(String actualAuth, String expectedAuth){
82 ClientCredentialsFilter filter = new ClientCredentialsFilter();
83 Assert.assertFalse(filter.verifyClientCredentials(actualAuth, expectedAuth));
84 }
85
Aharoni, Pavel (pa0916)d2946182018-12-17 11:21:02 +020086 @Test(dataProvider = "clientVerified")
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +030087 public void notAuthorized_return401(Boolean clientVerified) throws IOException, ServletException {
88 ClientCredentialsFilter filter = Mockito.mock(ClientCredentialsFilter.class);
89 HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
90 HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
91 FilterChain chain = Mockito.mock(FilterChain.class);
92
93
94 Mockito.when(filter.verifyClientCredentials(any(String.class),any(String.class))).thenReturn(clientVerified);
95 Mockito.doNothing().when(response).sendError(401);
96
97 Mockito.doCallRealMethod().when(filter).doFilter(request,response,chain);
98 filter.doFilter(request,response,chain);
99
100 if (clientVerified)
101 {
102 Mockito.verify(chain).doFilter(request,response);
103
104 }
105 else {
106 Mockito.verify(response).sendError(401);
107 }
108
109 }
110
111
112}
Aharoni, Pavel (pa0916)d2946182018-12-17 11:21:02 +0200113*/