Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 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) | d294618 | 2018-12-17 11:21:02 +0200 | [diff] [blame^] | 19 | *//*
|
| 20 |
|
Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 21 |
|
| 22 |
|
| 23 |
|
| 24 | package org.onap.osam.controller;
|
| 25 |
|
| 26 |
|
| 27 | import org.junit.Assert;
|
| 28 | import org.mockito.Mockito;
|
| 29 | import org.onap.osam.filters.ClientCredentialsFilter;
|
| 30 | import org.testng.annotations.DataProvider;
|
| 31 | import org.testng.annotations.Test;
|
| 32 |
|
| 33 | import javax.servlet.FilterChain;
|
| 34 | import javax.servlet.ServletException;
|
| 35 | import javax.servlet.http.HttpServletRequest;
|
| 36 | import javax.servlet.http.HttpServletResponse;
|
| 37 | import java.io.IOException;
|
| 38 |
|
| 39 | import static org.mockito.Matchers.any;
|
| 40 |
|
| 41 |
|
| 42 | @Test
|
| 43 | public 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) | d294618 | 2018-12-17 11:21:02 +0200 | [diff] [blame^] | 86 | @Test(dataProvider = "clientVerified")
|
Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 87 | 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) | d294618 | 2018-12-17 11:21:02 +0200 | [diff] [blame^] | 113 | */
|