|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "bytes" |
| 7 | + "context" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +// runCatalog executes the catalog subtree with the given args. |
| 15 | +func runCatalog(t *testing.T, args ...string) (string, error) { |
| 16 | + t.Helper() |
| 17 | + for _, c := range []*cobra.Command{catalogSearchCmd, catalogShowCmd} { |
| 18 | + resetCmdFlags(c) |
| 19 | + } |
| 20 | + |
| 21 | + var out bytes.Buffer |
| 22 | + rootCmd.SetOut(&out) |
| 23 | + rootCmd.SetErr(&out) |
| 24 | + rootCmd.SetArgs(append([]string{"catalog"}, args...)) |
| 25 | + err := rootCmd.ExecuteContext(context.Background()) |
| 26 | + return out.String(), err |
| 27 | +} |
| 28 | + |
| 29 | +func TestCatalogSearch_NoAuth(t *testing.T) { |
| 30 | + withTestHome(t) |
| 31 | + |
| 32 | + _, err := runCatalog(t, "search", "test") |
| 33 | + if err == nil { |
| 34 | + t.Fatal("expected error when not authenticated") |
| 35 | + } |
| 36 | + // Error message should hint at auth login |
| 37 | + errMsg := err.Error() |
| 38 | + if !strings.Contains(errMsg, "auth login") && !strings.Contains(errMsg, "credential") && !strings.Contains(errMsg, "no credential") { |
| 39 | + t.Errorf("error should mention auth or credential: %v", err) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func TestCatalogShow_NoAuth(t *testing.T) { |
| 44 | + withTestHome(t) |
| 45 | + |
| 46 | + _, err := runCatalog(t, "show", "test-uuid") |
| 47 | + if err == nil { |
| 48 | + t.Fatal("expected error when not authenticated") |
| 49 | + } |
| 50 | + // Error message should hint at auth |
| 51 | + errMsg := err.Error() |
| 52 | + if !strings.Contains(errMsg, "auth login") && !strings.Contains(errMsg, "credential") && !strings.Contains(errMsg, "no credential") { |
| 53 | + t.Errorf("error should mention auth or credential: %v", err) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestCatalogSearch_RequiresQuery(t *testing.T) { |
| 58 | + withTestHome(t) |
| 59 | + |
| 60 | + _, err := runCatalog(t, "search") |
| 61 | + if err == nil { |
| 62 | + t.Fatal("expected error when query is missing") |
| 63 | + } |
| 64 | + // Should mention that query argument is required |
| 65 | + errMsg := err.Error() |
| 66 | + if !strings.Contains(errMsg, "requires") && !strings.Contains(errMsg, "arg") { |
| 67 | + t.Errorf("error should mention missing argument: %v", err) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestCatalogShow_RequiresUUID(t *testing.T) { |
| 72 | + withTestHome(t) |
| 73 | + |
| 74 | + _, err := runCatalog(t, "show") |
| 75 | + if err == nil { |
| 76 | + t.Fatal("expected error when UUID is missing") |
| 77 | + } |
| 78 | + // Should mention that UUID argument is required |
| 79 | + errMsg := err.Error() |
| 80 | + if !strings.Contains(errMsg, "requires") && !strings.Contains(errMsg, "arg") { |
| 81 | + t.Errorf("error should mention missing argument: %v", err) |
| 82 | + } |
| 83 | +} |
0 commit comments