[OOM-K8S-CERT-EXTERNAL-PROVIDER] Add send update request functionality
[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-2021 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         updateEndpoint            = "v1/certificate-update"
39         certEndpointInvalid       = ":/v1/certificate"
40         certUpdateEndpointInvalid = ":/v1/certificate-update"
41         caName                    = "RA"
42         caNameInvalid             = ":/RA"
43         expectedCertificationUrl  = "https://oom-cert-service:8443/v1/certificate/RA"
44         expectedHealthCheckUrl    = "https://oom-cert-service:8443/actuator/health"
45 )
46
47 func Test_shouldCreateCertServiceClient(t *testing.T) {
48         shouldCreateCertServiceClientWithExpectedUrl(t, validUrl)
49         shouldCreateCertServiceClientWithExpectedUrl(t, validUrl2)
50 }
51
52 func shouldCreateCertServiceClientWithExpectedUrl(t *testing.T, baseUrl string) {
53         client, err := CreateCertServiceClient(baseUrl, healthEndpoint, certEndpoint, updateEndpoint, caName, testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
54
55         assert.NotNil(t, client)
56         assert.Nil(t, err)
57         assert.Equal(t, expectedCertificationUrl, client.certificationUrl)
58         assert.Equal(t, expectedHealthCheckUrl, client.healthUrl)
59 }
60
61 func Test_shouldReturnError_whenCaNameInvalid(t *testing.T) {
62         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, updateEndpoint, caNameInvalid, testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
63
64         assert.Nil(t, client)
65         assert.Error(t, err)
66 }
67
68 func Test_shouldReturnError_whenHealthEndpointInvalid(t *testing.T) {
69         client, err := CreateCertServiceClient(validUrl, healthEndpointInvalid, certEndpoint, updateEndpoint, caName, testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
70
71         assert.Nil(t, client)
72         assert.Error(t, err)
73 }
74
75 func Test_shouldReturnError_whenCertEndpointInvalid(t *testing.T) {
76         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpointInvalid, updateEndpoint, caName, testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
77
78         assert.Nil(t, client)
79         assert.Error(t, err)
80 }
81
82 func Test_shouldReturnError_whenUpdateCertificateEndpointInvalid(t *testing.T) {
83         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, certUpdateEndpointInvalid, caName, testdata.KeyBytes, testdata.CertBytes, testdata.KeyBytes)
84
85         assert.Nil(t, client)
86         assert.Error(t, err)
87 }
88
89 func Test_shouldReturnError_whenUrlInvalid(t *testing.T) {
90         client, err := CreateCertServiceClient(invalidUrl, healthEndpoint, certEndpoint, updateEndpoint, caName, testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
91
92         assert.Nil(t, client)
93         assert.Error(t, err)
94 }
95
96 func Test_shouldReturnError_whenCanameEmpty(t *testing.T) {
97         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, updateEndpoint, "", testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
98
99         assert.Nil(t, client)
100         assert.Error(t, err)
101 }
102
103 func Test_shouldReturnError_whenKeyNotMatchingCert(t *testing.T) {
104         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, updateEndpoint, caName, testdata.NotMatchingKeyBytes, testdata.CertBytes, testdata.CacertBytes)
105
106         assert.Nil(t, client)
107         assert.Error(t, err)
108 }
109
110 func Test_shouldReturnError_whenKeyInvalid(t *testing.T) {
111         //Cert used as key
112         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, updateEndpoint, caName, testdata.CertBytes, testdata.CertBytes, testdata.CacertBytes)
113
114         assert.Nil(t, client)
115         assert.Error(t, err)
116 }
117
118 func Test_shouldReturnError_whenCertInvalid(t *testing.T) {
119         //Cacert used as cert
120         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, updateEndpoint, caName, testdata.KeyBytes, testdata.CacertBytes, testdata.CacertBytes)
121
122         assert.Nil(t, client)
123         assert.Error(t, err)
124 }
125
126 func Test_shouldReturnError_whenCacertInvalid(t *testing.T) {
127         //Key used as cacert
128         client, err := CreateCertServiceClient(validUrl, healthEndpoint, certEndpoint, updateEndpoint, caName, testdata.KeyBytes, testdata.CertBytes, testdata.KeyBytes)
129
130         assert.Nil(t, client)
131         assert.Error(t, err)
132 }