Skip to content

Commit

Permalink
Merge branch 'chore-code-coverage' of https://github.com/dell/gopower…
Browse files Browse the repository at this point in the history
…scale into chore-code-coverage
  • Loading branch information
WilsonRadadia20 committed Jan 30, 2025
2 parents 45d5b1b + ee43e05 commit 71d5625
Show file tree
Hide file tree
Showing 4 changed files with 628 additions and 17 deletions.
256 changes: 239 additions & 17 deletions api/api_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2022 Dell Inc, or its subsidiaries.
Copyright (c) 2022-2025 Dell Inc, or its subsidiaries.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -243,22 +243,6 @@ func TestExecuteWithRetryAuthenticate(t *testing.T) {
}
assert.NotEqual(t, jsonExpectedError, err)
}
func TestParseJSONHTMLError(t *testing.T) {
// Create a mock HTML response

htmlResponse := &http.Response{
StatusCode: http.StatusBadRequest,
Body: io.NopCloser(bytes.NewBufferString("<html><head><title>Bad Request</title></head><body><h1>Bad Request</h1></body></html>")),
Header: http.Header{"Content-Type": {"text/html"}},
}

err := parseJSONHTMLError(htmlResponse)
expectedError := &HTMLError{
StatusCode: http.StatusBadRequest,
Message: "Bad Request",
}
assert.Equal(t, expectedError, err)
}

func TestDoWithHeaders(t *testing.T) {
// Create a mock client
Expand Down Expand Up @@ -290,3 +274,241 @@ func TestDoWithHeaders(t *testing.T) {
}
assert.Equal(t, expectedResp, resp)
}

func TestClient_APIVersion(t *testing.T) {
c := &client{apiVersion: 1}
assert.Equal(t, uint8(1), c.APIVersion())
}

func TestClient_User(t *testing.T) {
c := &client{username: "testuser"}
assert.Equal(t, "testuser", c.User())
}

func TestClient_Group(t *testing.T) {
c := &client{groupname: "testgroup"}
assert.Equal(t, "testgroup", c.Group())
}

func TestClient_VolumesPath(t *testing.T) {
c := &client{volumePath: "/mnt/volumes"}
assert.Equal(t, "/mnt/volumes", c.VolumesPath())
}

func TestClient_VolumePath(t *testing.T) {
c := &client{volumePath: "/mnt/volumes"}
assert.Equal(t, "/mnt/volumes/volume1", c.VolumePath("volume1"))
}

func TestHTMLError_Error(t *testing.T) {
err := &HTMLError{Message: "HTML error message"}
assert.Equal(t, "HTML error message", err.Error())
}

func TestClient_SetAuthToken(t *testing.T) {
c := &client{}
c.SetAuthToken("testcookie")
assert.Equal(t, "testcookie", c.sessionCredentials.sessionCookies)
}

func TestClient_SetCSRFToken(t *testing.T) {
c := &client{}
c.SetCSRFToken("testcsrf")
assert.Equal(t, "testcsrf", c.sessionCredentials.sessionCSRF)
}

func TestClient_SetReferer(t *testing.T) {
c := &client{}
c.SetReferer("testreferer")
assert.Equal(t, "testreferer", c.sessionCredentials.referer)
}

func TestClient_GetCSRFToken(t *testing.T) {
c := &client{}
c.GetCSRFToken()
assert.Equal(t, "", c.sessionCredentials.sessionCSRF)
}

func TestParseJSONHTMLError(t *testing.T) {
tests := []struct {
name string
contentType string
body string
expectedErr error
expectedStatus int
}{
{
name: "HTML error response",
contentType: "text/html",
body: `<html><head><title>HTML error title</title></head><body><h1>HTML error message</h1></body></html>`,
expectedErr: &HTMLError{Message: "HTML error message"},
expectedStatus: 401,
},
{
name: "HTML error without h1",
contentType: "text/html",
body: `<html><head><title>HTML error title</title></head><body></body></html>`,
expectedErr: &HTMLError{Message: "HTML error title"},
expectedStatus: 403,
},
{
name: "Invalid JSON",
contentType: "application/json",
body: `{invalid json`,
expectedErr: &JSONError{Err: []Error{{Message: "invalid character 'i' looking for beginning of object key string"}}},
expectedStatus: 400,
},
{
name: "Invalid HTML",
contentType: "text/html",
body: `<html>`,
expectedErr: &HTMLError{Message: ""},
expectedStatus: 500,
},
{
name: "JSON error with empty message",
contentType: "application/json",
body: `{"errors":[{"message":""}]}`,
expectedErr: &JSONError{Err: []Error{{Message: "400"}}},
expectedStatus: 400,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
body := bytes.NewBufferString(tt.body)
resp := httptest.NewRecorder()
resp.Body = body
resp.Header().Set("Content-Type", tt.contentType)
resp.Code = tt.expectedStatus

err := parseJSONHTMLError(resp.Result())

if tt.expectedErr != nil {
assert.NotNil(t, err)

switch expected := tt.expectedErr.(type) {
case *JSONError:
assert.Contains(t, err.Error(), expected.Error())
default:
assert.IsType(t, expected, err)
assert.EqualError(t, err, expected.Error())
}
} else {
assert.NoError(t, err)
}
})
}
}

func TestClient_Put(t *testing.T) {
// Create a mock client
c := &client{
http: http.DefaultClient,
}
ctx := context.Background()

// Create a mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Assert that the request is as expected
assert.Equal(t, http.MethodPut, r.Method)
assert.Equal(t, "/PUT/api/v1/endpoint", r.URL.String())
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message":"Success"}`))
}))
defer server.Close()
c.hostname = server.URL
body := map[string]string{
"Content-Type": "application/json",
}
resp := &struct {
Message string `json:"message"`
}{}
// Call the Put method
err := c.Put(ctx, http.MethodPut, "api/v1/endpoint", nil, nil, body, resp)
assert.NoError(t, err)
}

func TestClient_Post(t *testing.T) {
// Create a mock client
c := &client{
http: http.DefaultClient,
}
ctx := context.Background()

// Create a mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Assert that the request is as expected
assert.Equal(t, http.MethodPost, r.Method)
assert.Equal(t, "/POST/api/v1/endpoint", r.URL.String())
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message":"Success"}`))
}))
defer server.Close()
c.hostname = server.URL
body := map[string]string{
"Content-Type": "application/json",
}
resp := &struct {
Message string `json:"message"`
}{}
// Call the Post method
err := c.Post(ctx, http.MethodPost, "api/v1/endpoint", nil, nil, body, resp)

// Assertions
assert.NoError(t, err)
}

func TestClient_Delete(t *testing.T) {
// Create a mock client
c := &client{
http: http.DefaultClient,
}
ctx := context.Background()

// Create a mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Assert that the request is as expected
assert.Equal(t, http.MethodDelete, r.Method)
assert.Equal(t, "/DELETE/api/v1/endpoint", r.URL.String())
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message":"Success"}`))
}))
defer server.Close()
c.hostname = server.URL
resp := &struct {
Message string `json:"message"`
}{}
// Call the Delete method
err := c.Delete(ctx, http.MethodDelete, "api/v1/endpoint", nil, nil, resp)

// Assertions
assert.NoError(t, err)
}

func TestClient_Do(t *testing.T) {
// Create a mock client
c := &client{
http: http.DefaultClient,
}
ctx := context.Background()

// Create a mock server
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Assert that the request is as expected
assert.Equal(t, "method", r.Method)
assert.Equal(t, "/api/v1/endpoint/", r.URL.String())
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message":"Success"}`))
}))
defer server.Close()
c.hostname = server.URL
resp := &struct {
Message string `json:"message"`
}{}
// Call the Do method
err := c.Do(ctx, "method", "api/v1/endpoint", "", nil, resp, resp)

// Assertions
assert.NoError(t, err)
}
84 changes: 84 additions & 0 deletions api/v2/api_v2_acls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package v2

import (
"context"
"encoding/json"
"testing"

"github.com/dell/goisilon/mocks"
Expand Down Expand Up @@ -80,3 +81,86 @@ func TestParseFileMode(t *testing.T) {
assert.Equal(t, errInvalidFileMode, err)
}

// UT for MarshalJSON()
func TestAuthoritativeType_MarshalJSON(t *testing.T) {
jsonTests := []struct {
name string
value AuthoritativeType
expected string
}{
{"Marshal AuthoritativeTypeUnknown", AuthoritativeTypeUnknown, `"unknown"`},
{"Marshal AuthoritativeTypeACL", AuthoritativeTypeACL, `"acl"`},
{"Marshal AuthoritativeTypeMode", AuthoritativeTypeMode, `"mode"`},
}
for _, tt := range jsonTests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.value)
assert.NoError(t, err)
assert.Equal(t, tt.expected, string(data))
})
}
}

func TestActionType_MarshalJSON(t *testing.T) {
jsonTests := []struct {
name string
value ActionType
expected string
}{
{"Marshal ActionTypeUnknown", ActionTypeUnknown, `"unknown"`},
{"Marshal ActionTypeReplace", ActionTypeReplace, `"replace"`},
{"Marshal ActionTypeUpdate", ActionTypeUpdate, `"update"`},
}
for _, tt := range jsonTests {
t.Run(tt.name, func(t *testing.T) {
data, err := json.Marshal(tt.value)
assert.NoError(t, err)
assert.Equal(t, tt.expected, string(data))
})
}
}

// UT for UnmarshalJSON()
func TestActionType_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
input []byte
expected ActionType
expectError bool
}{
{"Unmarshal replace", []byte(`"replace"`), ActionTypeReplace, false},
{"Unmarshal update", []byte(`"update"`), ActionTypeUpdate, false},
{"Unmarshal unknown", []byte(`"unknown"`), ActionTypeUnknown, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result ActionType
err := json.Unmarshal([]byte(tt.input), &result)
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}

func TestAuthoritativeType_UnmarshalJSON(t *testing.T) {
tests := []struct {
name string
input []byte
expected AuthoritativeType
expectError bool
}{
{"Unmarshal acl", []byte(`"acl"`), AuthoritativeTypeACL, false},
{"Unmarshal mode", []byte(`"mode"`), AuthoritativeTypeMode, false},
{"Unmarshal unknown", []byte(`"unknown"`), AuthoritativeTypeUnknown, false},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var result AuthoritativeType
err := json.Unmarshal([]byte(tt.input), &result)
assert.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
Loading

0 comments on commit 71d5625

Please sign in to comment.