Skip to content

Commit

Permalink
Add test for forbidden response (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbolson authored Mar 13, 2024
1 parent aa4c0d3 commit 88765a2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
6 changes: 3 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ func Execute() {
err := rootCmd.Execute()
if err != nil {
switch {
case errors.Is(err, errs.ErrInvalidBaseURI):
fmt.Fprintln(os.Stderr, err.Error())
case errors.Is(err, errs.ErrUnauthorized):
case errors.Is(err, errs.ErrForbidden),
errors.Is(err, errs.ErrInvalidBaseURI),
errors.Is(err, errs.ErrUnauthorized):
fmt.Fprintln(os.Stderr, err.Error())
default:
fmt.Println(rootCmd.ErrPrefix(), err.Error())
Expand Down
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 88765a2

Please sign in to comment.