From f30cfdce71182e17a4e9349d2e196b7c452cee3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20W=C3=A5reus?= Date: Thu, 23 Nov 2023 17:55:48 +0100 Subject: [PATCH] fix tests to adopt forbidden logic --- internal/client/deb_client_test.go | 20 ++++++++++---------- internal/client/request.go | 15 +++++---------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/internal/client/deb_client_test.go b/internal/client/deb_client_test.go index 37582307..95e120b5 100644 --- a/internal/client/deb_client_test.go +++ b/internal/client/deb_client_test.go @@ -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() } } @@ -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() } } diff --git a/internal/client/request.go b/internal/client/request.go index 463cac80..8a9c37be 100644 --- a/internal/client/request.go +++ b/internal/client/request.go @@ -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` @@ -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)