Skip to content

Commit

Permalink
fix tests to adopt forbidden logic
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwareus committed Nov 23, 2023
1 parent 19457de commit f30cfdc
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
20 changes: 10 additions & 10 deletions internal/client/deb_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ func TestPost(t *testing.T) {
bytes.NewBuffer(jsonData),
0,
)
if err != nil {
t.Fatal("failed to assert that no client error occurred. Error:", err)
if !strings.Contains(err.Error(), "Forbidden. You don't have the necessary access to perform this action.") {
t.Fatal("failed to assert that client throws forbidden error", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusForbidden {
t.Error("failed to assert that status code was 403")
if res != nil {
t.Error("res should be nil with forbidden")
defer res.Body.Close()
}
}

Expand All @@ -163,12 +163,12 @@ func TestPostWithTimeout(t *testing.T) {
bytes.NewBuffer(jsonData),
10,
)
if err != nil {
t.Fatal("failed to assert that no client error occurred. Error:", err)
if !strings.Contains(err.Error(), "Forbidden. You don't have the necessary access to perform this action.") {
t.Fatal("failed to assert that client throws forbidden error", err)
}
defer res.Body.Close()
if res.StatusCode != http.StatusForbidden {
t.Error("failed to assert that status code was 403")
if res != nil {
t.Error("res should be nil with forbidden")
defer res.Body.Close()
}
}

Expand Down
15 changes: 5 additions & 10 deletions internal/client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,11 @@ func interpret(res *http.Response, request func() (*http.Response, error), debCl
if res == nil {
return nil, NoResErr
} else if res.StatusCode == http.StatusForbidden {
errMsg := `Unauthorized. You don't have the necessary access to perform this action.
errMsg := `Forbidden. You don't have the necessary access to perform this action.
Contact your debricked company admin or repository admin to request proper access.
For enterprise user: https://portal.debricked.com/administration-47/how-do-i-generate-an-access-token-130`
if retry {
err := debClient.authenticate()
if err != nil {
return nil, errors.New(errMsg)
}

return request()
}
return nil, errors.New(errMsg)

} else if res.StatusCode == http.StatusUnauthorized {
errMsg := `Unauthorized. Specify access token.
Read more on https://portal.debricked.com/administration-47/how-do-i-generate-an-access-token-130`
Expand Down Expand Up @@ -136,7 +128,10 @@ func (debClient *DebClient) authenticate() error {
return reqErr
}

defer res.Body.Close()
if res != nil {
defer res.Body.Close()
}

var tokenData map[string]string
body, _ := io.ReadAll(res.Body)
err := json.Unmarshal(body, &tokenData)
Expand Down

0 comments on commit f30cfdc

Please sign in to comment.