From: PatrikBuhr Date: Mon, 28 Mar 2022 11:53:41 +0000 (+0200) Subject: Fetch of authorization token X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=4d78fcc20a2c156edd686270e83e3e8182ef7951;p=nonrtric.git Fetch of authorization token Improving of code coverage. Signed-off-by: PatrikBuhr Issue-ID: NONRTRIC-735 Change-Id: Id8086ab61ed0a6a4fca7deb00cbcad5384e074ff --- diff --git a/auth-token-fetch/HTTPClient.go b/auth-token-fetch/HTTPClient.go index a765461b..5b0d1678 100644 --- a/auth-token-fetch/HTTPClient.go +++ b/auth-token-fetch/HTTPClient.go @@ -21,24 +21,13 @@ 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" -} diff --git a/auth-token-fetch/HTTPClient_test.go b/auth-token-fetch/HTTPClient_test.go index 7b8deb0d..f9d2fa3a 100644 --- a/auth-token-fetch/HTTPClient_test.go +++ b/auth-token-fetch/HTTPClient_test.go @@ -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")) -} diff --git a/auth-token-fetch/config_test.go b/auth-token-fetch/config_test.go index 92a63d89..0dd1d359 100644 --- a/auth-token-fetch/config_test.go +++ b/auth-token-fetch/config_test.go @@ -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) +}