Skip to content

Commit

Permalink
Add invalid namespace error check
Browse files Browse the repository at this point in the history
  • Loading branch information
sebasslash committed Jun 13, 2022
1 parent 6b197ee commit 269cbc6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions errors.go
Expand Up @@ -14,6 +14,10 @@ var (

// ErrMissingDirectory is returned when the path does not have an existing directory.
ErrMissingDirectory = errors.New("path needs to be an existing directory")

// ErrNamespaceNotAuthorized is returned when a user attempts to perform an action
// on a namespace (organization) they do not have access to.
ErrNamespaceNotAuthorized = errors.New("namespace not authorized")
)

// Options/fields that cannot be defined
Expand Down
4 changes: 4 additions & 0 deletions gpg_key.go
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/url"
"strings"
"time"
)

Expand Down Expand Up @@ -127,6 +128,9 @@ func (s *gpgKeys) Update(ctx context.Context, keyID GPGKeyID, options GPGKeyUpda
g := &GPGKey{}
err = s.client.do(ctx, req, g)
if err != nil {
if strings.Contains(err.Error(), "namespace not authorized") {
return nil, ErrNamespaceNotAuthorized
}
return nil, err
}

Expand Down
12 changes: 12 additions & 0 deletions gpg_key_integration_test.go
Expand Up @@ -114,6 +114,18 @@ func TestGPGKeyUpdate(t *testing.T) {
// call. We'll need to manually delete the key.
gpgKey, _ := createGPGKey(t, client, org, provider)

t.Run("when using an invalid namespace", func(t *testing.T) {
keyID := GPGKeyID{
Namespace: provider.Organization.Name,
KeyID: gpgKey.KeyID,
}
opts := GPGKeyUpdateOptions{
Namespace: "invalid_namespace_org",
}
_, err := client.GPGKeys.Update(ctx, keyID, opts)
assert.ErrorIs(t, err, ErrNamespaceNotAuthorized)
})

t.Run("when updating to a valid namespace", func(t *testing.T) {
// Create a new namespace to update the key with
org2, org2Cleanup := createOrganization(t, client)
Expand Down

0 comments on commit 269cbc6

Please sign in to comment.