Skip to content

Commit 2b9a549

Browse files
Add empty key-ids validation for bulk-delete, assert error messages in tests
1 parent 3c67830 commit 2b9a549

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

cmd/key.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ var keyBulkDeleteCmd = &cobra.Command{
145145
Short: "Delete multiple keys",
146146
Long: "Deletes multiple keys from the project. Requires Manage keys admin right.",
147147
RunE: func(*cobra.Command, []string) error {
148+
if len(bulkDeleteKeyIds) == 0 {
149+
return errors.New("--key-ids must contain at least one key ID")
150+
}
148151
resp, err := Api.Keys().BulkDelete(projectId, bulkDeleteKeyIds)
149152
if err != nil {
150153
return err

cmd/key_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"net/http"
8+
"strings"
89
"testing"
910

1011
"github.com/spf13/cobra"
@@ -243,6 +244,8 @@ func TestKeyBulkUpdate_InvalidJSON(t *testing.T) {
243244
err := rootCmd.Execute()
244245
if err == nil {
245246
t.Error("Expected error for invalid JSON, got nil")
247+
} else if !strings.Contains(err.Error(), "invalid character") {
248+
t.Errorf("Expected JSON parse error, got: %v", err)
246249
}
247250
}
248251

@@ -259,6 +262,27 @@ func TestKeyBulkCreate_InvalidJSON(t *testing.T) {
259262
err := rootCmd.Execute()
260263
if err == nil {
261264
t.Error("Expected error for invalid JSON, got nil")
265+
} else if !strings.Contains(err.Error(), "invalid character") {
266+
t.Errorf("Expected JSON parse error, got: %v", err)
267+
}
268+
}
269+
270+
func TestKeyBulkDelete_EmptyKeyIds(t *testing.T) {
271+
client, _, _, teardown := setup()
272+
defer teardown()
273+
274+
// Reset package-level var that may retain state from prior tests
275+
bulkDeleteKeyIds = []int64{}
276+
277+
args := []string{"key", "bulk-delete", "--key-ids=", "--project-id=" + testProjectID}
278+
rootCmd.SetArgs(args)
279+
keyBulkDeleteCmd.PreRun = func(cmd *cobra.Command, args []string) {
280+
Api = client
281+
}
282+
283+
err := rootCmd.Execute()
284+
if err == nil {
285+
t.Error("Expected error for empty key-ids, got nil")
262286
}
263287
}
264288

@@ -275,6 +299,8 @@ func TestKeyBulkUpdate_EmptyKeys(t *testing.T) {
275299
err := rootCmd.Execute()
276300
if err == nil {
277301
t.Error("Expected error for empty keys, got nil")
302+
} else if err.Error() != "--keys must contain at least one key object" {
303+
t.Errorf("Expected empty keys error, got: %v", err)
278304
}
279305
}
280306

@@ -291,5 +317,8 @@ func TestKeyBulkCreate_EmptyKeys(t *testing.T) {
291317
err := rootCmd.Execute()
292318
if err == nil {
293319
t.Error("Expected error for empty keys, got nil")
320+
} else if err.Error() != "--keys must contain at least one key object" {
321+
t.Errorf("Expected empty keys error, got: %v", err)
294322
}
295323
}
324+

0 commit comments

Comments
 (0)