[OOM-K8S-CERT-EXTERNAL-PROVIDER] Add client for CertService API
[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/v1/certificate/"
33         validUrl2                = "https://oom-cert-service:8443/v1/certificate"
34         invalidUrl               = "https://oom-cert  service:8443/v1/certificate"
35         caName                   = "RA"
36         expectedCertificationUrl = "https://oom-cert-service:8443/v1/certificate/RA"
37 )
38
39 func Test_shouldCreateCertServiceClient(t *testing.T) {
40         shouldCreateCertServiceClientWithExpectedUrl(t, expectedCertificationUrl, validUrl)
41         shouldCreateCertServiceClientWithExpectedUrl(t, expectedCertificationUrl, validUrl2)
42 }
43
44 func shouldCreateCertServiceClientWithExpectedUrl(t *testing.T, expectedCertificationUrl string, baseUrl string) {
45         client, err := CreateCertServiceClient(baseUrl, caName, testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
46
47         assert.NotNil(t, client)
48         assert.Nil(t, err)
49         assert.Equal(t, expectedCertificationUrl, client.certificationUrl)
50 }
51
52 func Test_shouldReturnError_whenUrlInvalid(t *testing.T) {
53         client, err := CreateCertServiceClient(invalidUrl, caName, testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
54
55         assert.Nil(t, client)
56         assert.Error(t, err)
57 }
58
59 func Test_shouldReturnError_whenCanameEmpty(t *testing.T) {
60         client, err := CreateCertServiceClient(validUrl, "", testdata.KeyBytes, testdata.CertBytes, testdata.CacertBytes)
61
62         assert.Nil(t, client)
63         assert.Error(t, err)
64 }
65
66 func Test_shouldReturnError_whenKeyNotMatchingCert(t *testing.T) {
67         client, err := CreateCertServiceClient(validUrl, caName, testdata.NotMatchingKeyBytes, testdata.CertBytes, testdata.CacertBytes)
68
69         assert.Nil(t, client)
70         assert.Error(t, err)
71 }
72
73 func Test_shouldReturnError_whenKeyInvalid(t *testing.T) {
74         //Cert used as key
75         client, err := CreateCertServiceClient(validUrl, caName, testdata.CertBytes, testdata.CertBytes, testdata.CacertBytes)
76
77         assert.Nil(t, client)
78         assert.Error(t, err)
79 }
80
81 func Test_shouldReturnError_whenCertInvalid(t *testing.T) {
82         //Cacert used as cert
83         client, err := CreateCertServiceClient(validUrl, caName, testdata.KeyBytes, testdata.CacertBytes, testdata.CacertBytes)
84
85         assert.Nil(t, client)
86         assert.Error(t, err)
87 }
88
89 func Test_shouldReturnError_whenCacertInvalid(t *testing.T) {
90         //Key used as cacert
91         client, err := CreateCertServiceClient(validUrl, caName, testdata.KeyBytes, testdata.CertBytes, testdata.KeyBytes)
92
93         assert.Nil(t, client)
94         assert.Error(t, err)
95 }