f2b5032f551bc86fa1f2ec56453d656c51506397
[oom/platform/cert-service.git] / certServiceK8sExternalProvider / src / certserviceclient / cert_service_client_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         "bytes"
25         "fmt"
26         "io"
27         "io/ioutil"
28         "net/http"
29         "testing"
30
31         "github.com/stretchr/testify/assert"
32
33         "onap.org/oom-certservice/k8s-external-provider/src/testdata"
34 )
35
36 const (
37         certificationUrl = "https://oom-cert-service:8443/v1/certificate/RA"
38 )
39
40
41 func Test_GetCertificates_shouldParseCertificateResponseCorrectly(t *testing.T) {
42         responseJson := `{"certificateChain": ["cert-0", "cert-1"], "trustedCertificates": ["trusted-cert-0", "trusted-cert-1"]}`
43         responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
44         client := CertServiceClientImpl{
45                 certificationUrl: certificationUrl,
46                 httpClient:       getMockedClient(responseJsonReader, http.StatusOK),
47         }
48         response, _ := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
49         assert.ElementsMatch(t, []string{"cert-0", "cert-1"}, response.CertificateChain)
50         assert.ElementsMatch(t, []string{"trusted-cert-0", "trusted-cert-1"}, response.TrustedCertificates)
51 }
52
53 func Test_GetCertificates_shouldReturnError_whenResponseIsNotJson(t *testing.T) {
54         responseJson := `not a json`
55         responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
56         client := CertServiceClientImpl{
57                 certificationUrl: certificationUrl,
58                 httpClient:       &httpClientMock{
59                         DoFunc: func(req *http.Request) (response *http.Response, e error) {
60                                 mockedResponse := &http.Response{
61                                         Body: responseJsonReader,
62                                 }
63                                 return mockedResponse, nil
64                         },
65                 },
66         }
67         response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
68
69         assert.Nil(t, response)
70         assert.Error(t, err)
71 }
72
73 func Test_GetCertificates_shouldReturnError_whenHttpClientReturnsError(t *testing.T) {
74         client := CertServiceClientImpl{
75                 certificationUrl: certificationUrl,
76                 httpClient:       &httpClientMock{
77                         DoFunc: func(req *http.Request) (response *http.Response, err error) {
78                                 return nil, fmt.Errorf("mock error")
79                         },
80                 },
81         }
82         response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
83
84         assert.Nil(t, response)
85         assert.Error(t, err)
86 }
87
88 func Test_GetCertificates_shouldReturnError_whenResponseOtherThan200(t *testing.T) {
89         responseJson := `{"errorMessage": "CertService API error"}`
90         responseJsonReader := ioutil.NopCloser(bytes.NewReader([]byte(responseJson)))
91         client := CertServiceClientImpl{
92                 certificationUrl: certificationUrl,
93                 httpClient:       getMockedClient(responseJsonReader, http.StatusNotFound),
94         }
95         response, err := client.GetCertificates(testdata.CsrBytes, testdata.PkBytes)
96
97         assert.Nil(t, response)
98         assert.Error(t, err)
99 }
100
101 func Test_CheckHealth_shouldReturnNil_whenHttpClientReturnsStatusCode200(t *testing.T) {
102         client := CertServiceClientImpl{
103                 certificationUrl: certificationUrl,
104                 httpClient:       &httpClientMock{
105                         DoFunc: func(req *http.Request) (response *http.Response, e error) {
106                                 mockedResponse := &http.Response{
107                                         Body: nil,
108                                         StatusCode: 200,
109                                 }
110                                 return mockedResponse, nil
111                         },
112                 },
113         }
114
115         err := client.CheckHealth()
116
117         assert.Nil(t, err)
118 }
119
120 func Test_CheckHealth_shouldReturnError_whenHttpClientReturnsStatusCode404(t *testing.T) {
121         client := CertServiceClientImpl{
122                 certificationUrl: certificationUrl,
123                 httpClient:       &httpClientMock{
124                         DoFunc: func(req *http.Request) (response *http.Response, e error) {
125                                 mockedResponse := &http.Response{
126                                         Body: nil,
127                                         StatusCode: 404,
128                                 }
129                                 return mockedResponse, nil
130                         },
131                 },
132         }
133
134         err := client.CheckHealth()
135
136         assert.Error(t, err)
137 }
138
139 func Test_CheckHealth_shouldReturnError_whenHttpClientReturnsError(t *testing.T) {
140         client := CertServiceClientImpl{
141                 certificationUrl: certificationUrl,
142                 httpClient:       &httpClientMock{
143                         DoFunc: func(req *http.Request) (response *http.Response, err error) {
144                                 return nil, fmt.Errorf("mock error")
145                         },
146                 },
147         }
148         err := client.CheckHealth()
149
150         assert.Error(t, err)
151 }
152
153 func getMockedClient(responseJsonReader io.ReadCloser, responseCode int) *httpClientMock {
154         return &httpClientMock{
155                 DoFunc: func(req *http.Request) (response *http.Response, e error) {
156                         mockedResponse := &http.Response{
157                                 Body:       responseJsonReader,
158                                 StatusCode: responseCode,
159                         }
160                         return mockedResponse, nil
161                 },
162         }
163 }
164
165 type httpClientMock struct {
166         DoFunc func(*http.Request) (*http.Response, error)
167 }
168
169 func (client httpClientMock) Do(req *http.Request) (*http.Response, error) {
170         return client.DoFunc(req)
171 }