Skip to content

Commit

Permalink
cover api
Browse files Browse the repository at this point in the history
  • Loading branch information
atye committed Dec 5, 2024
1 parent 3aeecc5 commit ff70046
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ type testResp struct {
}

func TestClient_Query(t *testing.T) {
os.Setenv("GOPOWERSTORE_DEBUG", "true")
defer os.Unsetenv("GOPOWERSTORE_DEBUG")
apiURL := "https://foo"
testURL := "mock"
action := "attach"
Expand All @@ -122,6 +124,82 @@ func TestClient_Query(t *testing.T) {
assert.Equal(t, resp.Name, "Foo")
}

func TestClient_Query_Forbidden(t *testing.T) {
os.Setenv("GOPOWERSTORE_DEBUG", "true")
defer os.Unsetenv("GOPOWERSTORE_DEBUG")
apiURL := "https://foo"
testURL := "mock"
action := "attach"
login := "login_session"
id := "5bfebae3-a278-4c50-af16-011a1dfc1b6f"
c := testClient(t, apiURL)
ctx := context.Background()
httpmock.Activate()
defer httpmock.DeactivateAndReset()
respData := `{"name": "Foo"}`
loginData := `[{"id": "id"}]`
qp := QueryParams{}
qp.RawArg("foo", "bar")

requestCount := -1
statusCodeFn := func() int {
requestCount++
switch requestCount {
case 0:
return http.StatusForbidden
default:
return http.StatusCreated
}
}

httpmock.RegisterResponder("POST", fmt.Sprintf("%s/%s/%s/%s?foo=bar", apiURL, testURL, id, action),
func(r *http.Request) (*http.Response, error) {

Check failure on line 156 in api/api_test.go

View workflow job for this annotation

GitHub Actions / Golang Validation / Lint golang code

unused-parameter: parameter 'r' seems to be unused, consider removing or renaming it as _ (revive)
code := statusCodeFn()
return httpmock.NewStringResponse(code, respData), nil
})
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/%s", apiURL, login),
httpmock.NewStringResponder(http.StatusOK, loginData))

reqBody := make(map[string]string)
reqBody["foo"] = "bar"
resp := &testResp{}
_, err := c.Query(ctx, RequestConfig{
Method: "POST", Endpoint: testURL, ID: id, Action: action, QueryParams: &qp, Body: reqBody,
}, resp)
assert.Nil(t, err)
assert.Equal(t, "Foo", resp.Name)
}

func TestClient_Query_Login_Error(t *testing.T) {
os.Setenv("GOPOWERSTORE_DEBUG", "true")
defer os.Unsetenv("GOPOWERSTORE_DEBUG")
apiURL := "https://foo"
testURL := "mock"
action := "attach"
login := "login_session"
id := "5bfebae3-a278-4c50-af16-011a1dfc1b6f"
c := testClient(t, apiURL)
ctx := context.Background()
httpmock.Activate()
defer httpmock.DeactivateAndReset()
respData := `{"name": "Foo"}`
qp := QueryParams{}
qp.RawArg("foo", "bar")

httpmock.RegisterResponder("POST", fmt.Sprintf("%s/%s/%s/%s?foo=bar", apiURL, testURL, id, action),
httpmock.NewStringResponder(http.StatusForbidden, respData))
httpmock.RegisterResponder("GET", fmt.Sprintf("%s/%s", apiURL, login),
httpmock.NewStringResponder(http.StatusUnauthorized, respData))

reqBody := make(map[string]string)
reqBody["foo"] = "bar"
resp := &testResp{}
_, err := c.Query(ctx, RequestConfig{
Method: "POST", Endpoint: testURL, ID: id, Action: action, QueryParams: &qp, Body: reqBody,
}, resp)
assert.NotNil(t, err)
}

func TestClientIMPL_prepareRequestURL(t *testing.T) {
apiURL := "https://foo.com"
endpoint := "node"
Expand Down Expand Up @@ -167,6 +245,24 @@ func TestClientIMPL_updatePaginationInfoInMeta(t *testing.T) {
assert.False(t, meta.Pagination.IsPaginate)
}

func TestClientIMPL_SetLogger(t *testing.T) {

Check failure on line 248 in api/api_test.go

View workflow job for this annotation

GitHub Actions / Golang Validation / Lint golang code

unused-parameter: parameter 't' seems to be unused, consider removing or renaming it as _ (revive)
log := &defaultLogger{}
c := ClientIMPL{apiThrottle: NewTimeoutSemaphore(10, 10, log)}
c.SetLogger(&defaultLogger{})
}

func TestClientIMPL_GetCustomHTTPHeaders(t *testing.T) {
c := ClientIMPL{}

want := http.Header{
"foo": {"bar"},
}
c.SetCustomHTTPHeaders(want)

got := c.GetCustomHTTPHeaders()
assert.Equal(t, want, got)
}

type qpTest struct{}

func (qp *qpTest) Fields() []string {
Expand Down

0 comments on commit ff70046

Please sign in to comment.