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