5d255a62d25e2ab2d867fc6cc262a37f794cf7bb
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / certserviceclient / cert_service_client_factory_test.go
1 /*
2  * ============LICENSE_START=======================================================
3  * oom-certservice-k8s-external-provider
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. 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 certserviceclient
22
23 import (
24         "testing"
25
26         "github.com/stretchr/testify/assert"
27
28         "onap.org/oom-certservice/k8s-external-provider/src/testdata"
29 )
30
31 const (
32         validUrl                 = "https://oom-cert-service:8443/"
33         validUrl2                = "https://oom-cert-service:8443"
34         invalidUrl               = "https://oom-cert  service:8443/"
35         healthEndpoint           = "actuator/health"
36         healthEndpointInvalid    = ":/actuator/health"
37         certEndpoint             = "v1/certificate"
38         certEndpointInvalid      = ":/v1/certificate"
39         caName                   = "RA"
40         caNameInvalid            = ":/RA"
41         expectedCertificationUrl = "https://oom-cert-service:8443/v1/certificate/RA"
42         expectedHealthCheckUrl   = "https://oom-cert-service:8443/actuator/health"
43 )
44
45 func Test_shouldCreateCertServiceClient(t *testing.T) {
46         shouldCreateCertServiceClientWithExpectedUrl(t, validUrl)
47         shouldCreateCertServiceClientWithExpectedUrl(t, validUrl2)
48 }
49
50 func shouldCreateCertServiceClientWithExpectedUrl(t *testing.T, baseUrl string) {
51         client, err := CreateCertServiceClient(baseUrl, healthEndpoint, certEndpoint, caName, testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
52
53         assert.NotNil(t, client)
54         assert.Nil(t, err)
55         assert.Equal(t, expectedCertificationUrl, client.certificationUrl)
56         assert.Equal(t, expectedHealthCheckUrl, client.healthUrl)
57 }
58
59 func Test_shouldReturnError_whenCaNameInvalid(t *testing.T) {
60         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, caNameInvalid, testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
61
62         assert.Nil(t, client)
63         assert.Error(t, err)
64 }
65
66 func Test_shouldReturnError_whenHealthEndpointInvalid(t *testing.T) {
67         client, err := CreateCertServiceClient(validUrl, healthEndpointInvalid, certEndpoint, caName, testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
68
69         assert.Nil(t, client)
70         assert.Error(t, err)
71 }
72
73 func Test_shouldReturnError_whenCertEndpointInvalid(t *testing.T) {
74         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpointInvalid, caName, testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
75
76         assert.Nil(t, client)
77         assert.Error(t, err)
78 }
79
80 func Test_shouldReturnError_whenUrlInvalid(t *testing.T) {
81         client, err := CreateCertServiceClient(invalidUrl, healthEndpoint, certEndpoint, caName, testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
82
83         assert.Nil(t, client)
84         assert.Error(t, err)
85 }
86
87 func Test_shouldReturnError_whenCanameEmpty(t *testing.T) {
88         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, "", testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
89
90         assert.Nil(t, client)
91         assert.Error(t, err)
92 }
93
94 func Test_shouldReturnError_whenKeyNotMatchingCert(t *testing.T) {
95         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, caName, testdata.NotMatchingKeyBytes, testdata.CertBytes, testdata.CacertBytes)
96
97         assert.Nil(t, client)
98         assert.Error(t, err)
99 }
100
101 func Test_shouldReturnError_whenKeyInvalid(t *testing.T) {
102         //Cert used as key
103         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, caName, testdata.CertBytes, testdata.CertBytes, testdata.CacertBytes)
104
105         assert.Nil(t, client)
106         assert.Error(t, err)
107 }
108
109 func Test_shouldReturnError_whenCertInvalid(t *testing.T) {
110         //Cacert used as cert
111         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, caName, testdata.KeyBytes, testdata.CacertBytes, testdata.CacertBytes)
112
113         assert.Nil(t, client)
114         assert.Error(t, err)
115 }
116
117 func Test_shouldReturnError_whenCacertInvalid(t *testing.T) {
118         //Key used as cacert
119         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, caName, testdata.KeyBytes, testdata.CertBytes, testdata.KeyBytes)
120
121         assert.Nil(t, client)
122         assert.Error(t, err)
123 }