diff --git a/errors.go b/errors.go index 09a8ce7c3..a04a4d17e 100644 --- a/errors.go +++ b/errors.go @@ -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 diff --git a/gpg_key.go b/gpg_key.go index 23a02533e..d4302efef 100644 --- a/gpg_key.go +++ b/gpg_key.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/url" + "strings" "time" ) @@ -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 } diff --git a/gpg_key_integration_test.go b/gpg_key_integration_test.go index bb9e7d274..c47a1f223 100644 --- a/gpg_key_integration_test.go +++ b/gpg_key_integration_test.go @@ -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)