|
| 1 | +package cmdtest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "flag" |
| 7 | + "io" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +func TestWebhooksValidationErrors(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + args []string |
| 16 | + wantErr string |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "list missing app", |
| 20 | + args: []string{"webhooks", "list"}, |
| 21 | + wantErr: "--app is required", |
| 22 | + }, |
| 23 | + { |
| 24 | + name: "get missing webhook id", |
| 25 | + args: []string{"webhooks", "get"}, |
| 26 | + wantErr: "--webhook-id is required", |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "create missing app", |
| 30 | + args: []string{"webhooks", "create", "--name", "Build Updates", "--url", "https://example.com/webhook", "--secret", "secret", "--events", "BUILD_UPLOAD_STATE_UPDATED", "--enabled", "true"}, |
| 31 | + wantErr: "--app is required", |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "create missing name", |
| 35 | + args: []string{"webhooks", "create", "--app", "APP_ID", "--url", "https://example.com/webhook", "--secret", "secret", "--events", "BUILD_UPLOAD_STATE_UPDATED", "--enabled", "true"}, |
| 36 | + wantErr: "--name is required", |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "create missing url", |
| 40 | + args: []string{"webhooks", "create", "--app", "APP_ID", "--name", "Build Updates", "--secret", "secret", "--events", "BUILD_UPLOAD_STATE_UPDATED", "--enabled", "true"}, |
| 41 | + wantErr: "--url is required", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "create missing secret", |
| 45 | + args: []string{"webhooks", "create", "--app", "APP_ID", "--name", "Build Updates", "--url", "https://example.com/webhook", "--events", "BUILD_UPLOAD_STATE_UPDATED", "--enabled", "true"}, |
| 46 | + wantErr: "--secret is required", |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "create missing events", |
| 50 | + args: []string{"webhooks", "create", "--app", "APP_ID", "--name", "Build Updates", "--url", "https://example.com/webhook", "--secret", "secret", "--enabled", "true"}, |
| 51 | + wantErr: "--events is required", |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "create missing enabled", |
| 55 | + args: []string{"webhooks", "create", "--app", "APP_ID", "--name", "Build Updates", "--url", "https://example.com/webhook", "--secret", "secret", "--events", "BUILD_UPLOAD_STATE_UPDATED"}, |
| 56 | + wantErr: "--enabled is required", |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "update missing webhook id", |
| 60 | + args: []string{"webhooks", "update", "--url", "https://example.com/webhook"}, |
| 61 | + wantErr: "--webhook-id is required", |
| 62 | + }, |
| 63 | + { |
| 64 | + name: "update missing fields", |
| 65 | + args: []string{"webhooks", "update", "--webhook-id", "wh-1"}, |
| 66 | + wantErr: "--name, --url, --secret, --events, or --enabled is required", |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "delete missing confirm", |
| 70 | + args: []string{"webhooks", "delete", "--webhook-id", "wh-1"}, |
| 71 | + wantErr: "--confirm is required", |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "delete missing webhook id", |
| 75 | + args: []string{"webhooks", "delete", "--confirm"}, |
| 76 | + wantErr: "--webhook-id is required", |
| 77 | + }, |
| 78 | + { |
| 79 | + name: "deliveries missing webhook id", |
| 80 | + args: []string{"webhooks", "deliveries"}, |
| 81 | + wantErr: "--webhook-id is required", |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "deliveries redeliver missing delivery id", |
| 85 | + args: []string{"webhooks", "deliveries", "redeliver"}, |
| 86 | + wantErr: "--delivery-id is required", |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "ping missing webhook id", |
| 90 | + args: []string{"webhooks", "ping"}, |
| 91 | + wantErr: "--webhook-id is required", |
| 92 | + }, |
| 93 | + } |
| 94 | + |
| 95 | + for _, test := range tests { |
| 96 | + t.Run(test.name, func(t *testing.T) { |
| 97 | + root := RootCommand("1.2.3") |
| 98 | + root.FlagSet.SetOutput(io.Discard) |
| 99 | + |
| 100 | + stdout, stderr := captureOutput(t, func() { |
| 101 | + if err := root.Parse(test.args); err != nil { |
| 102 | + t.Fatalf("parse error: %v", err) |
| 103 | + } |
| 104 | + err := root.Run(context.Background()) |
| 105 | + if !errors.Is(err, flag.ErrHelp) { |
| 106 | + t.Fatalf("expected ErrHelp, got %v", err) |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + if stdout != "" { |
| 111 | + t.Fatalf("expected empty stdout, got %q", stdout) |
| 112 | + } |
| 113 | + if !strings.Contains(stderr, test.wantErr) { |
| 114 | + t.Fatalf("expected error %q, got %q", test.wantErr, stderr) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments