Skip to content

Commit

Permalink
fix(cli): simpler output for some access denied errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jfeodor committed Jun 11, 2024
1 parent 699cf63 commit 2abcd15
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cli/cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ func readOrCreateApp(ctx context.Context, apps AppService, slug string, appName

return appCreateOutput.AppID, nil
default:
output.PrintErrorDetails("Error reading remote app", err)
task.Error()
output.PrintErrorDetails("Error reading remote app", err)

return "", err
}
Expand Down
4 changes: 3 additions & 1 deletion cli/internal/app/app_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package app

import (
"context"

"numerous/cli/internal/gql"
)

type CreateAppInput struct {
Expand Down Expand Up @@ -40,7 +42,7 @@ func (s *Service) Create(ctx context.Context, input CreateAppInput) (CreateAppOu
}
err := s.client.Exec(ctx, appCreateText, &resp, variables)
if err != nil {
return CreateAppOutput{}, err
return CreateAppOutput{}, gql.CheckAccessDenied(err)
}

return CreateAppOutput{
Expand Down
30 changes: 30 additions & 0 deletions cli/internal/app/app_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"numerous/cli/internal/gql"
"numerous/cli/test"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -43,6 +44,35 @@ func TestCreate(t *testing.T) {
assert.Equal(t, expected, output)
})

t.Run("returns access denied error", func(t *testing.T) {
doer := test.MockDoer{}
c := test.CreateTestGQLClient(t, &doer)
s := New(c, nil, nil)

respBody := `
{
"errors": [{
"message": "access denied",
"location": [{"line": 1, "column": 1}],
"path": ["appCreate"]
}]
}
`
resp := test.JSONResponse(respBody)
doer.On("Do", mock.Anything).Return(resp, nil)
input := CreateAppInput{
OrganizationSlug: "organization-slug",
Name: "app-name",
DisplayName: "App Name",
Description: "App description",
}
output, err := s.Create(context.TODO(), input)

expected := CreateAppOutput{}
assert.ErrorIs(t, err, gql.ErrAccesDenied)
assert.Equal(t, expected, output)
})

t.Run("returns expected error", func(t *testing.T) {
doer := test.MockDoer{}
c := test.CreateTestGQLClient(t, &doer)
Expand Down
3 changes: 2 additions & 1 deletion cli/internal/app/app_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package app
import (
"context"

"numerous/cli/internal/gql"
"numerous/cli/internal/gql/secret"
)

Expand Down Expand Up @@ -36,7 +37,7 @@ func (s *Service) DeployApp(ctx context.Context, input DeployAppInput) (DeployAp

err := s.client.Exec(ctx, appDeployText, &resp, variables)
if err != nil {
return DeployAppOutput{}, err
return DeployAppOutput{}, gql.CheckAccessDenied(err)
}

return DeployAppOutput{DeploymentVersionID: resp.AppDeploy.ID}, nil
Expand Down
29 changes: 28 additions & 1 deletion cli/internal/app/app_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"numerous/cli/internal/gql"
"numerous/cli/test"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -41,6 +42,32 @@ func TestDeployApp(t *testing.T) {
assert.Equal(t, expected, output)
})

t.Run("returns access denied error", func(t *testing.T) {
doer := test.MockDoer{}
c := test.CreateTestGQLClient(t, &doer)
s := New(c, nil, nil)

respBody := `
{
"errors": [{
"message": "access denied",
"location": [{"line": 1, "column": 1}],
"path": ["appDeploy"]
}]
}
`
resp := test.JSONResponse(respBody)
doer.On("Do", mock.Anything).Return(resp, nil)
input := DeployAppInput{
AppVersionID: "some-app-version-id",
}
output, err := s.DeployApp(context.TODO(), input)

expected := DeployAppOutput{}
assert.ErrorIs(t, err, gql.ErrAccesDenied)
assert.Equal(t, expected, output)
})

t.Run("returns expected error", func(t *testing.T) {
doer := test.MockDoer{}
c := test.CreateTestGQLClient(t, &doer)
Expand All @@ -51,7 +78,7 @@ func TestDeployApp(t *testing.T) {
"errors": [{
"message": "expected error message",
"location": [{"line": 1, "column": 1}],
"path": ["appCreate"]
"path": ["appDeploy"]
}]
}
`
Expand Down
12 changes: 7 additions & 5 deletions cli/internal/app/app_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"strings"

"numerous/cli/internal/gql"
)

type ReadAppInput struct {
Expand Down Expand Up @@ -36,14 +38,14 @@ func (s *Service) ReadApp(ctx context.Context, input ReadAppInput) (ReadAppOutpu

variables := map[string]any{"slug": input.OrganizationSlug, "name": input.Name}
err := s.client.Exec(ctx, queryAppText, &resp, variables)
if err != nil && !strings.Contains(err.Error(), "app not found") {
return ReadAppOutput{}, err
if err == nil {
return ReadAppOutput{AppID: resp.App.ID}, nil
}

empty := appResponse{}
if resp == empty {
errMsg := err.Error()
if strings.Contains(errMsg, "app not found") {
return ReadAppOutput{}, ErrAppNotFound
}

return ReadAppOutput{AppID: resp.App.ID}, nil
return ReadAppOutput{}, gql.CheckAccessDenied(err)
}
42 changes: 37 additions & 5 deletions cli/internal/app/app_read_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"numerous/cli/internal/gql"
"numerous/cli/test"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -41,7 +42,7 @@ func TestAppRead(t *testing.T) {
assert.Equal(t, expected, output)
})

t.Run("given empty response then it returns not found error", func(t *testing.T) {
t.Run("given app not found error then it returns not found error", func(t *testing.T) {
doer := test.MockDoer{}
c := test.CreateTestGQLClient(t, &doer)
s := New(c, nil, nil)
Expand All @@ -50,7 +51,12 @@ func TestAppRead(t *testing.T) {
{
"data": {
"app": null
}
},
"errors": [{
"message": "app not found",
"location": [{"line": 1, "column": 1}],
"path": ["app"]
}]
}
`
resp := test.JSONResponse(respBody)
Expand All @@ -62,9 +68,35 @@ func TestAppRead(t *testing.T) {
}
output, err := s.ReadApp(context.TODO(), input)

expected := ReadAppOutput{}
assert.ErrorIs(t, err, ErrAppNotFound)
assert.Equal(t, expected, output)
assert.Equal(t, ReadAppOutput{}, output)
})

t.Run("given access denied error then it returns access denied error", func(t *testing.T) {
doer := test.MockDoer{}
c := test.CreateTestGQLClient(t, &doer)
s := New(c, nil, nil)

respBody := `
{
"errors": [{
"message": "access denied",
"location": [{"line": 1, "column": 1}],
"path": ["app"]
}]
}
`
resp := test.JSONResponse(respBody)
doer.On("Do", mock.Anything).Return(resp, nil)

input := ReadAppInput{
OrganizationSlug: "organization-slug",
Name: "app-name",
}
output, err := s.ReadApp(context.TODO(), input)

assert.ErrorIs(t, err, gql.ErrAccesDenied)
assert.Equal(t, ReadAppOutput{}, output)
})

t.Run("given graphql error then it returns expected error", func(t *testing.T) {
Expand All @@ -77,7 +109,7 @@ func TestAppRead(t *testing.T) {
"errors": [{
"message": "expected error message",
"location": [{"line": 1, "column": 1}],
"path": ["appCreate"]
"path": ["app"]
}]
}
`
Expand Down
16 changes: 16 additions & 0 deletions cli/internal/gql/access_denied.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gql

import (
"errors"
"strings"
)

var ErrAccesDenied = errors.New("access denied")

func CheckAccessDenied(err error) error {
if strings.Contains(err.Error(), "access denied") {
return ErrAccesDenied
}

return err
}

0 comments on commit 2abcd15

Please sign in to comment.