Merge "Use env varialbes to replace image urls & tags"
[nonrtric.git] / test / usecases / oruclosedlooprecovery / goversion / internal / restclient / client_test.go
index 9b482b5..2c915fd 100644 (file)
@@ -21,11 +21,16 @@ package restclient
 
 import (
        "bytes"
+       "crypto/tls"
        "fmt"
        "io/ioutil"
+       "math"
        "net/http"
+       "reflect"
        "testing"
+       "time"
 
+       "github.com/hashicorp/go-retryablehttp"
        "github.com/stretchr/testify/mock"
        "github.com/stretchr/testify/require"
        "oransc.org/usecase/oruclosedloop/mocks"
@@ -38,74 +43,7 @@ func TestRequestError_Error(t *testing.T) {
                StatusCode: http.StatusBadRequest,
                Body:       []byte("error"),
        }
-       assertions.Equal("Request failed due to error response with status: 400 and body: error", actualError.Error())
-}
-
-func TestGet(t *testing.T) {
-       assertions := require.New(t)
-
-       type args struct {
-               url              string
-               mockReturnStatus int
-               mockReturnBody   []byte
-               mockReturnError  error
-       }
-       tests := []struct {
-               name    string
-               args    args
-               want    []byte
-               wantErr error
-       }{
-               {
-                       name: "Ok response",
-                       args: args{
-                               url:              "ok",
-                               mockReturnStatus: http.StatusOK,
-                               mockReturnBody:   []byte("body"),
-                               mockReturnError:  nil,
-                       },
-                       want:    []byte("body"),
-                       wantErr: nil,
-               },
-               {
-                       name: "Bad request should get RequestError",
-                       args: args{
-                               url:              "badRequest",
-                               mockReturnStatus: http.StatusBadRequest,
-                               mockReturnBody:   []byte("bad request"),
-                               mockReturnError:  nil,
-                       },
-                       want: nil,
-                       wantErr: RequestError{
-                               StatusCode: http.StatusBadRequest,
-                               Body:       []byte("bad request"),
-                       },
-               },
-               {
-                       name: "Server unavailable should get error",
-                       args: args{
-                               url:             "serverUnavailable",
-                               mockReturnError: fmt.Errorf("Server unavailable"),
-                       },
-                       want:    nil,
-                       wantErr: fmt.Errorf("Server unavailable"),
-               },
-       }
-       for _, tt := range tests {
-               t.Run(tt.name, func(t *testing.T) {
-                       clientMock := mocks.HTTPClient{}
-                       clientMock.On("Get", tt.args.url).Return(&http.Response{
-                               StatusCode: tt.args.mockReturnStatus,
-                               Body:       ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
-                       }, tt.args.mockReturnError)
-                       Client = &clientMock
-
-                       got, err := Get(tt.args.url)
-                       assertions.Equal(tt.wantErr, err, tt.name)
-                       assertions.Equal(tt.want, got, tt.name)
-                       clientMock.AssertCalled(t, "Get", tt.args.url)
-               })
-       }
+       assertions.Equal("error response with status: 400 and body: error", actualError.Error())
 }
 
 func TestPutWithoutAuth(t *testing.T) {
@@ -115,9 +53,8 @@ func TestPutWithoutAuth(t *testing.T) {
        clientMock.On("Do", mock.Anything).Return(&http.Response{
                StatusCode: http.StatusOK,
        }, nil)
-       Client = &clientMock
 
-       error := PutWithoutAuth("url", []byte("body"))
+       error := PutWithoutAuth("url", []byte("body"), &clientMock)
 
        assertions.Nil(error)
        var actualRequest *http.Request
@@ -142,9 +79,8 @@ func TestPut(t *testing.T) {
        clientMock.On("Do", mock.Anything).Return(&http.Response{
                StatusCode: http.StatusOK,
        }, nil)
-       Client = &clientMock
 
-       error := Put("url", "body", "admin", "pwd")
+       error := Put("url", "body", &clientMock, "admin", "pwd")
 
        assertions.Nil(error)
        var actualRequest *http.Request
@@ -171,9 +107,8 @@ func TestDelete(t *testing.T) {
        clientMock.On("Do", mock.Anything).Return(&http.Response{
                StatusCode: http.StatusOK,
        }, nil)
-       Client = &clientMock
 
-       error := Delete("url")
+       error := Delete("url", &clientMock)
 
        assertions.Nil(error)
        var actualRequest *http.Request
@@ -209,7 +144,6 @@ func Test_doErrorCases(t *testing.T) {
                                url:              "badRequest",
                                mockReturnStatus: http.StatusBadRequest,
                                mockReturnBody:   []byte("bad request"),
-                               mockReturnError:  nil,
                        },
                        wantErr: RequestError{
                                StatusCode: http.StatusBadRequest,
@@ -232,10 +166,69 @@ func Test_doErrorCases(t *testing.T) {
                                StatusCode: tt.args.mockReturnStatus,
                                Body:       ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
                        }, tt.args.mockReturnError)
-                       Client = &clientMock
 
-                       err := do("PUT", tt.args.url, nil)
+                       err := do("PUT", tt.args.url, nil, &clientMock)
+                       assertions.Equal(tt.wantErr, err, tt.name)
+               })
+       }
+}
+
+func Test_createClientCertificate(t *testing.T) {
+       assertions := require.New(t)
+       wantedCert, _ := tls.LoadX509KeyPair("../../security/consumer.crt", "../../security/consumer.key")
+       type args struct {
+               certPath string
+               keyPath  string
+       }
+       tests := []struct {
+               name     string
+               args     args
+               wantCert tls.Certificate
+               wantErr  error
+       }{
+               {
+                       name: "Paths to cert info ok should return cerftificate",
+                       args: args{
+                               certPath: "../../security/consumer.crt",
+                               keyPath:  "../../security/consumer.key",
+                       },
+                       wantCert: wantedCert,
+               },
+               {
+                       name: "Paths to cert info not ok should return error with info about error",
+                       args: args{
+                               certPath: "wrong_cert",
+                               keyPath:  "wrong_key",
+                       },
+                       wantErr: fmt.Errorf("cannot create x509 keypair from cert file wrong_cert and key file wrong_key due to: open wrong_cert: no such file or directory"),
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       cert, err := CreateClientCertificate(tt.args.certPath, tt.args.keyPath)
+                       assertions.Equal(tt.wantCert, cert, tt.name)
                        assertions.Equal(tt.wantErr, err, tt.name)
                })
        }
 }
+
+func Test_CreateRetryClient(t *testing.T) {
+       assertions := require.New(t)
+
+       client := CreateRetryClient(tls.Certificate{})
+
+       transport := client.Transport
+       assertions.Equal("*retryablehttp.RoundTripper", reflect.TypeOf(transport).String())
+       retryableTransport := transport.(*retryablehttp.RoundTripper)
+       retryableClient := retryableTransport.Client
+       assertions.Equal(time.Minute, retryableClient.RetryWaitMax)
+       assertions.Equal(math.MaxInt, retryableClient.RetryMax)
+}
+
+func TestIsUrlSecured(t *testing.T) {
+       assertions := require.New(t)
+
+       assertions.True(IsUrlSecure("https://url"))
+
+       assertions.False(IsUrlSecure("http://url"))
+}