Fetch of authorization token 93/7993/1
authorPatrikBuhr <patrik.buhr@est.tech>
Mon, 28 Mar 2022 11:53:41 +0000 (13:53 +0200)
committerPatrikBuhr <patrik.buhr@est.tech>
Mon, 28 Mar 2022 11:53:41 +0000 (13:53 +0200)
Improving of code coverage.

Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
Issue-ID: NONRTRIC-735
Change-Id: Id8086ab61ed0a6a4fca7deb00cbcad5384e074ff

auth-token-fetch/HTTPClient.go
auth-token-fetch/HTTPClient_test.go
auth-token-fetch/config_test.go

index a765461..5b0d167 100644 (file)
 package main
 
 import (
-       "bytes"
        "crypto/tls"
        "crypto/x509"
-       "fmt"
-       "io"
 
        "net/http"
-       "net/url"
        "time"
 )
 
-// HTTPClient interface
-type HTTPClient interface {
-       Get(url string) (*http.Response, error)
-
-       Do(*http.Request) (*http.Response, error)
-}
-
 func CreateHttpClient(cert tls.Certificate, caCerts *x509.CertPool, timeout time.Duration) *http.Client {
        return &http.Client{
                Timeout:   timeout,
@@ -46,50 +35,6 @@ func CreateHttpClient(cert tls.Certificate, caCerts *x509.CertPool, timeout time
        }
 }
 
-type RequestError struct {
-       StatusCode int
-       Body       []byte
-}
-
-func (pe RequestError) Error() string {
-       return fmt.Sprintf("Request failed due to error response with status: %v and body: %v", pe.StatusCode, string(pe.Body))
-}
-
-func Post(url string, body []byte, contentType string, client HTTPClient) error {
-       return do(http.MethodPost, url, body, contentType, client)
-}
-
-func do(method string, url string, body []byte, contentType string, client HTTPClient) error {
-       if req, reqErr := http.NewRequest(method, url, bytes.NewBuffer(body)); reqErr == nil {
-               req.Header.Set("Content-Type", contentType)
-               if response, respErr := client.Do(req); respErr == nil {
-                       if isResponseSuccess(response.StatusCode) {
-                               return nil
-                       } else {
-                               return getRequestError(response)
-                       }
-               } else {
-                       return respErr
-               }
-       } else {
-               return reqErr
-       }
-}
-
-func isResponseSuccess(statusCode int) bool {
-       return statusCode >= http.StatusOK && statusCode <= 299
-}
-
-func getRequestError(response *http.Response) RequestError {
-       defer response.Body.Close()
-       responseData, _ := io.ReadAll(response.Body)
-       putError := RequestError{
-               StatusCode: response.StatusCode,
-               Body:       responseData,
-       }
-       return putError
-}
-
 func createTransport(cert tls.Certificate, caCerts *x509.CertPool) *http.Transport {
        return &http.Transport{
                TLSClientConfig: &tls.Config{
@@ -102,8 +47,3 @@ func createTransport(cert tls.Certificate, caCerts *x509.CertPool) *http.Transpo
                },
        }
 }
-
-func IsUrlSecure(configUrl string) bool {
-       u, _ := url.Parse(configUrl)
-       return u.Scheme == "https"
-}
index 7b8deb0..f9d2fa3 100644 (file)
@@ -23,7 +23,6 @@ package main
 import (
        "crypto/tls"
 
-       "net/http"
        "reflect"
        "testing"
        "time"
@@ -31,15 +30,6 @@ import (
        "github.com/stretchr/testify/require"
 )
 
-func TestRequestError_Error(t *testing.T) {
-       assertions := require.New(t)
-       actualError := RequestError{
-               StatusCode: http.StatusBadRequest,
-               Body:       []byte("error"),
-       }
-       assertions.Equal("Request failed due to error response with status: 400 and body: error", actualError.Error())
-}
-
 func Test_CreateClient(t *testing.T) {
        assertions := require.New(t)
 
@@ -49,11 +39,3 @@ func Test_CreateClient(t *testing.T) {
        assertions.Equal("*http.Transport", reflect.TypeOf(transport).String())
        assertions.Equal(5*time.Second, client.Timeout)
 }
-
-func TestIsUrlSecured(t *testing.T) {
-       assertions := require.New(t)
-
-       assertions.True(IsUrlSecure("https://url"))
-
-       assertions.False(IsUrlSecure("http://url"))
-}
index 92a63d8..0dd1d35 100644 (file)
@@ -61,3 +61,38 @@ func TestNew_envVarsSetConfigContainSetValues(t *testing.T) {
 
        assertions.Equal(&wantConfig, got)
 }
+
+func TestNew_defaultValues(t *testing.T) {
+       assertions := require.New(t)
+
+       wantConfig := Config{
+               LogLevel:                log.InfoLevel,
+               CertPath:                "security/tls.crt",
+               KeyPath:                 "security/tls.key",
+               AuthServiceUrl:          "https://localhost:39687/example-singlelogin-sever/login",
+               GrantType:               "",
+               ClientSecret:            "",
+               ClientId:                "",
+               AuthTokenOutputFileName: "/tmp/authToken.txt",
+               CACertsPath:             "",
+               RefreshMarginSeconds:    5,
+       }
+       got := NewConfig()
+       assertions.Equal(nil, validateConfiguration(got))
+
+       assertions.Equal(&wantConfig, got)
+}
+
+func TestNew_invalidValues(t *testing.T) {
+       assertions := require.New(t)
+
+       os.Setenv("LOG_LEVEL", "Junk")
+       os.Setenv("REFRESH_MARGIN_SECONDS", "Junk")
+       t.Cleanup(func() {
+               os.Clearenv()
+       })
+
+       got := NewConfig()
+       assertions.Equal(log.InfoLevel, got.LogLevel)
+       assertions.Equal(5, got.RefreshMarginSeconds)
+}