|
| 1 | +package access_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | + |
| 10 | + authv1 "github.com/qdrant/qdrant-cloud-public-api/gen/go/qdrant/cloud/auth/v1" |
| 11 | + |
| 12 | + "github.com/qdrant/qcloud-cli/internal/testutil" |
| 13 | +) |
| 14 | + |
| 15 | +func TestKeyDelete_WithForce(t *testing.T) { |
| 16 | + env := testutil.NewTestEnv(t, testutil.WithAccountID("test-account-id")) |
| 17 | + |
| 18 | + env.AuthServer.DeleteManagementKeyCalls.Returns(&authv1.DeleteManagementKeyResponse{}, nil) |
| 19 | + |
| 20 | + stdout, _, err := testutil.Exec(t, env, "access", "key", "delete", "key-abc", "--force") |
| 21 | + require.NoError(t, err) |
| 22 | + assert.Contains(t, stdout, "key-abc") |
| 23 | + assert.Contains(t, stdout, "deleted") |
| 24 | + |
| 25 | + req, ok := env.AuthServer.DeleteManagementKeyCalls.Last() |
| 26 | + require.True(t, ok) |
| 27 | + assert.Equal(t, "test-account-id", req.GetAccountId()) |
| 28 | + assert.Equal(t, "key-abc", req.GetManagementKeyId()) |
| 29 | +} |
| 30 | + |
| 31 | +func TestKeyDelete_Aborted(t *testing.T) { |
| 32 | + env := testutil.NewTestEnv(t) |
| 33 | + |
| 34 | + stdout, _, err := testutil.Exec(t, env, "access", "key", "delete", "key-abc") |
| 35 | + require.NoError(t, err) |
| 36 | + assert.Contains(t, stdout, "Aborted.") |
| 37 | + assert.Equal(t, 0, env.AuthServer.DeleteManagementKeyCalls.Count()) |
| 38 | +} |
| 39 | + |
| 40 | +func TestKeyDelete_BackendError(t *testing.T) { |
| 41 | + env := testutil.NewTestEnv(t) |
| 42 | + |
| 43 | + env.AuthServer.DeleteManagementKeyCalls.Returns(nil, fmt.Errorf("internal server error")) |
| 44 | + |
| 45 | + _, _, err := testutil.Exec(t, env, "access", "key", "delete", "key-abc", "--force") |
| 46 | + require.Error(t, err) |
| 47 | +} |
| 48 | + |
| 49 | +func TestKeyDelete_MissingArg(t *testing.T) { |
| 50 | + env := testutil.NewTestEnv(t) |
| 51 | + |
| 52 | + _, _, err := testutil.Exec(t, env, "access", "key", "delete") |
| 53 | + require.Error(t, err) |
| 54 | +} |
| 55 | + |
| 56 | +func TestKeyDeleteCompletion(t *testing.T) { |
| 57 | + env := testutil.NewTestEnv(t) |
| 58 | + |
| 59 | + env.AuthServer.ListManagementKeysCalls.Returns(&authv1.ListManagementKeysResponse{ |
| 60 | + Items: []*authv1.ManagementKey{ |
| 61 | + {Id: "key-uuid-1", Prefix: "abc123"}, |
| 62 | + {Id: "key-uuid-2", Prefix: "def456"}, |
| 63 | + }, |
| 64 | + }, nil) |
| 65 | + |
| 66 | + stdout, _, err := testutil.Exec(t, env, "__complete", "access", "key", "delete", "") |
| 67 | + require.NoError(t, err) |
| 68 | + assert.Contains(t, stdout, "key-uuid-1") |
| 69 | + assert.Contains(t, stdout, "abc123") |
| 70 | + assert.Contains(t, stdout, "key-uuid-2") |
| 71 | + assert.Contains(t, stdout, "def456") |
| 72 | +} |
0 commit comments