Skip to content

Commit

Permalink
Add test for forbidden response
Browse files Browse the repository at this point in the history
  • Loading branch information
dbolson committed Mar 13, 2024
1 parent 2ab3668 commit 1610573
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
3 changes: 2 additions & 1 deletion internal/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package errors
import "errors"

var (
ErrForbidden = errors.New("You do not have permission to make this request")
ErrInvalidBaseURI = errors.New("baseUri is invalid")
ErrUnauthorized = errors.New("You are not authorized to make this request.")
ErrUnauthorized = errors.New("You are not authorized to make this request")
)
9 changes: 6 additions & 3 deletions internal/projects/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@ func (c ProjectsClient) List(ctx context.Context) (*ldapi.Projects, error) {
func ListProjects(ctx context.Context, client Client) ([]byte, error) {
projects, err := client.List(ctx)
if err != nil {
if err.Error() == "401 Unauthorized" {
switch err.Error() {
case "401 Unauthorized":
return nil, errors.ErrUnauthorized
case "403 Forbidden":
return nil, errors.ErrForbidden
default:
return nil, err
}

return nil, err
}

projectsJSON, err := json.Marshal(projects)
Expand Down
18 changes: 16 additions & 2 deletions internal/projects/projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ func strPtr(s string) *string {
type GetProjectsResponse struct{}

type MockClient struct {
hasForbiddenErr bool
hasUnauthorizedErr bool
}

func (c MockClient) List(ctx context.Context) (*ldapi.Projects, error) {
if c.hasForbiddenErr {
return nil, errors.New("403 Forbidden")
}
if c.hasUnauthorizedErr {
return nil, errors.New("401 Unauthorized")
}
Expand Down Expand Up @@ -91,13 +95,23 @@ func TestListProjects(t *testing.T) {
assert.JSONEq(t, expected, string(response))
})

t.Run("with invalid accessToken is forbidden", func(t *testing.T) {
t.Run("without access is forbidden", func(t *testing.T) {
mockClient := MockClient{
hasForbiddenErr: true,
}

_, err := projects.ListProjects(context.Background(), mockClient)

assert.EqualError(t, err, "You do not have permission to make this request")
})

t.Run("with invalid accessToken is unauthorized", func(t *testing.T) {
mockClient := MockClient{
hasUnauthorizedErr: true,
}

_, err := projects.ListProjects(context.Background(), mockClient)

assert.EqualError(t, err, "You are not authorized to make this request.")
assert.EqualError(t, err, "You are not authorized to make this request")
})
}

0 comments on commit 1610573

Please sign in to comment.