Collector authentication enhancement
[dcaegen2/collectors/ves.git] / src / test / java / org / onap / dcae / TLSTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.dcaegen2.collectors.ves
4  * ================================================================================
5  * Copyright (C) 2018 Nokia. All rights reserved.
6  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.dcae;
23
24 import io.vavr.collection.HashMap;
25 import org.junit.jupiter.api.Nested;
26 import org.junit.jupiter.api.Test;
27 import org.onap.dcae.common.configuration.AuthMethodType;
28 import org.springframework.context.annotation.Import;
29 import org.springframework.http.HttpStatus;
30
31 import static org.junit.jupiter.api.Assertions.assertEquals;
32 import static org.junit.jupiter.api.Assertions.assertThrows;
33 import static org.mockito.Mockito.when;
34 import static org.onap.dcae.TLSTest.HttpsConfiguration.USERNAME;
35 import static org.onap.dcae.TLSTest.HttpsConfiguration.PASSWORD;
36
37 public class TLSTest extends TLSTestBase {
38
39     @Nested
40     @Import(HttpConfiguration.class)
41     class HttpTest extends TestClassBase {
42
43         @Test
44         public void shouldHttpRequestSucceed() {
45             assertEquals(HttpStatus.OK, makeHttpRequest().getStatusCode());
46         }
47
48         @Test
49         public void shouldHttpsRequestFail() {
50             assertThrows(Exception.class, this::makeHttpsRequest);
51         }
52     }
53
54     @Nested
55     @Import(HttpsConfiguration.class)
56     class HttpsTest extends TestClassBase {
57
58
59         @Test
60         public void shouldHttpsRequestWithoutBasicAuthFail() {
61             assertThrows(Exception.class, this::makeHttpsRequest);
62         }
63
64         @Test
65         public void shouldHttpsRequestWithBasicAuthSucceed() {
66             assertEquals(HttpStatus.OK, makeHttpsRequestWithBasicAuth(USERNAME, PASSWORD).getStatusCode());
67         }
68     }
69
70     @Nested
71     @Import(HttpsConfigurationWithTLSAuthentication.class)
72     class HttpsWithTLSAuthenticationTest extends TestClassBase {
73
74         @Test
75         public void shouldHttpsRequestWithoutCertificateFail() {
76             assertThrows(Exception.class, this::makeHttpsRequest);
77         }
78
79         @Test
80         public void shouldHttpsRequestWithCertificateSucceed() {
81             assertEquals(HttpStatus.OK, makeHttpsRequestWithClientCert().getStatusCode());
82         }
83     }
84
85     @Nested
86     @Import(HttpsConfigurationWithTLSAuthenticationAndBasicAuth.class)
87     class HttpsWithTLSAuthenticationAndBasicAuthTest extends TestClassBase {
88
89         @Test
90         public void shouldHttpsRequestWithoutBasicAuthSucceed() {
91             assertEquals(HttpStatus.OK, makeHttpsRequestWithClientCert().getStatusCode());
92         }
93
94         @Test
95         public void shouldHttpsRequestWithBasicAuthSucceed() {
96             assertEquals(HttpStatus.OK, makeHttpsRequestWithClientCertAndBasicAuth(USERNAME, PASSWORD).getStatusCode());
97         }
98     }
99
100     // ApplicationSettings configurations
101     static class HttpConfiguration extends TLSTestBase.ConfigurationBase {
102         @Override
103         protected void configureSettings(ApplicationSettings settings) {
104             when(settings.authMethod()).thenReturn(AuthMethodType.NO_AUTH.value());
105         }
106     }
107
108     static class HttpsConfiguration extends TLSTestBase.ConfigurationBase {
109         public static final String USERNAME = "TestUser";
110         public static final String PASSWORD = "TestPassword";
111
112         @Override
113         protected void configureSettings(ApplicationSettings settings) {
114             when(settings.keystoreFileLocation()).thenReturn(KEYSTORE.toString());
115             when(settings.keystorePasswordFileLocation()).thenReturn(KEYSTORE_PASSWORD_FILE.toString());
116             when(settings.authMethod()).thenReturn(AuthMethodType.BASIC_AUTH.value());
117             when(settings.validAuthorizationCredentials()).thenReturn(HashMap.of(USERNAME, "$2a$10$51tDgG2VNLde5E173Ay/YO.Fq.aD.LR2Rp8pY3QAKriOSPswvGviy"));
118         }
119     }
120
121     static class HttpsConfigurationWithTLSAuthentication extends HttpsConfiguration {
122         @Override
123         protected void configureSettings(ApplicationSettings settings) {
124             super.configureSettings(settings);
125             when(settings.authMethod()).thenReturn(AuthMethodType.CERT_ONLY.value());
126             when(settings.truststoreFileLocation()).thenReturn(TRUSTSTORE.toString());
127             when(settings.truststorePasswordFileLocation()).thenReturn(TRUSTSTORE_PASSWORD_FILE.toString());
128         }
129     }
130
131     static class HttpsConfigurationWithTLSAuthenticationAndBasicAuth extends HttpsConfigurationWithTLSAuthentication {
132         @Override
133         protected void configureSettings(ApplicationSettings settings) {
134             super.configureSettings(settings);
135             when(settings.authMethod()).thenReturn(AuthMethodType.CERT_BASIC_AUTH.value());
136         }
137     }
138 }