37c02b2e0b8733686a41e46379124146ff3cd8fc
[vid.git] / vid-app-common / src / test / java / org / onap / vid / controller / ClientCredentialsFilterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 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
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=========================================================
19  */
20
21 package org.onap.vid.controller;
22
23
24 import org.junit.Assert;
25 import org.mockito.Mockito;
26 import org.onap.vid.controller.filter.ClientCredentialsFilter;
27 import org.testng.annotations.DataProvider;
28 import org.testng.annotations.Test;
29
30 import javax.servlet.FilterChain;
31 import javax.servlet.ServletException;
32 import javax.servlet.http.HttpServletRequest;
33 import javax.servlet.http.HttpServletResponse;
34 import java.io.IOException;
35
36 import static org.mockito.ArgumentMatchers.any;
37
38
39 /**
40  * Created by amichai on 16/05/2018.
41  */
42 public class ClientCredentialsFilterTest {
43
44     @DataProvider
45     public static Object[][] authorizedData() {
46         return new Object[][] {
47                 {"Basic 123==", null},
48                 {null, null},
49                 {null, ""},
50                 {"Basic 123==", ""},
51                 {"Basic 123==", "Basic 123=="}
52         };
53     }
54
55     @DataProvider
56     public static Object[][] notAuthorizedData() {
57         return new Object[][] {
58                 {null, "Basic 123=="},
59                 {"", "Basic 123=="},
60                 {"not null but not as expected", "Basic 123=="},
61                 {"basic 123==", "Basic 123=="}
62         };
63     }
64
65     @DataProvider
66     public static Object[][] clientVerified() {
67         return new Object[][] {
68                 {true},
69                 {false}
70         };
71     }
72
73     @Test(dataProvider = "authorizedData")
74     public void givenAuthorizationHeader_Authorized(String actualAuth, String expectedAuth){
75         ClientCredentialsFilter filter = new ClientCredentialsFilter();
76         Assert.assertTrue(filter.verifyClientCredentials(actualAuth, expectedAuth));
77     }
78
79     @Test(dataProvider = "notAuthorizedData")
80     public void givenAuthorizationHeader_NotAuthorized(String actualAuth, String expectedAuth){
81         ClientCredentialsFilter filter = new ClientCredentialsFilter();
82         Assert.assertFalse(filter.verifyClientCredentials(actualAuth, expectedAuth));
83     }
84
85     //@Test(dataProvider = "clientVerified")
86     public void notAuthorized_return401(Boolean clientVerified) throws IOException, ServletException {
87         ClientCredentialsFilter filter = Mockito.mock(ClientCredentialsFilter.class);
88         HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
89         HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
90         FilterChain chain = Mockito.mock(FilterChain.class);
91
92
93         Mockito.when(filter.verifyClientCredentials(any(),any())).thenReturn(clientVerified);
94         Mockito.doNothing().when(response).sendError(401);
95
96         Mockito.doCallRealMethod().when(filter).doFilter(request,response,chain);
97         filter.doFilter(request,response,chain);
98
99         if (clientVerified)
100         {
101             Mockito.verify(chain).doFilter(request,response);
102
103         }
104         else {
105             Mockito.verify(response).sendError(401);
106         }
107
108     }
109
110
111 }