|
| 1 | +package webhook |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "testing" |
| 7 | + |
| 8 | + qt "github.com/frankban/quicktest" |
| 9 | + "github.com/planetscale/cli/internal/cmdutil" |
| 10 | + "github.com/planetscale/cli/internal/config" |
| 11 | + "github.com/planetscale/cli/internal/mock" |
| 12 | + "github.com/planetscale/cli/internal/printer" |
| 13 | + ps "github.com/planetscale/planetscale-go/planetscale" |
| 14 | +) |
| 15 | + |
| 16 | +func TestWebhook_TestCmd(t *testing.T) { |
| 17 | + c := qt.New(t) |
| 18 | + |
| 19 | + var buf bytes.Buffer |
| 20 | + format := printer.JSON |
| 21 | + p := printer.NewPrinter(&format) |
| 22 | + p.SetResourceOutput(&buf) |
| 23 | + |
| 24 | + org := "planetscale" |
| 25 | + db := "mydb" |
| 26 | + webhookID := "webhook-123" |
| 27 | + |
| 28 | + svc := &mock.WebhooksService{ |
| 29 | + TestFn: func(ctx context.Context, req *ps.TestWebhookRequest) error { |
| 30 | + c.Assert(req.Organization, qt.Equals, org) |
| 31 | + c.Assert(req.Database, qt.Equals, db) |
| 32 | + c.Assert(req.ID, qt.Equals, webhookID) |
| 33 | + return nil |
| 34 | + }, |
| 35 | + } |
| 36 | + |
| 37 | + ch := &cmdutil.Helper{ |
| 38 | + Printer: p, |
| 39 | + Config: &config.Config{ |
| 40 | + Organization: org, |
| 41 | + }, |
| 42 | + Client: func() (*ps.Client, error) { |
| 43 | + return &ps.Client{ |
| 44 | + Webhooks: svc, |
| 45 | + }, nil |
| 46 | + }, |
| 47 | + } |
| 48 | + |
| 49 | + cmd := TestCmd(ch) |
| 50 | + cmd.SetArgs([]string{db, webhookID}) |
| 51 | + err := cmd.Execute() |
| 52 | + |
| 53 | + c.Assert(err, qt.IsNil) |
| 54 | + c.Assert(svc.TestFnInvoked, qt.IsTrue) |
| 55 | + c.Assert(buf.String(), qt.JSONEquals, map[string]string{"result": "test event sent"}) |
| 56 | +} |
| 57 | + |
| 58 | +func TestWebhook_TestCmd_Human(t *testing.T) { |
| 59 | + c := qt.New(t) |
| 60 | + |
| 61 | + var buf bytes.Buffer |
| 62 | + format := printer.Human |
| 63 | + p := printer.NewPrinter(&format) |
| 64 | + p.SetHumanOutput(&buf) |
| 65 | + |
| 66 | + org := "planetscale" |
| 67 | + db := "mydb" |
| 68 | + webhookID := "webhook-123" |
| 69 | + |
| 70 | + svc := &mock.WebhooksService{ |
| 71 | + TestFn: func(ctx context.Context, req *ps.TestWebhookRequest) error { |
| 72 | + return nil |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + ch := &cmdutil.Helper{ |
| 77 | + Printer: p, |
| 78 | + Config: &config.Config{ |
| 79 | + Organization: org, |
| 80 | + }, |
| 81 | + Client: func() (*ps.Client, error) { |
| 82 | + return &ps.Client{ |
| 83 | + Webhooks: svc, |
| 84 | + }, nil |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + cmd := TestCmd(ch) |
| 89 | + cmd.SetArgs([]string{db, webhookID}) |
| 90 | + err := cmd.Execute() |
| 91 | + |
| 92 | + c.Assert(err, qt.IsNil) |
| 93 | + c.Assert(svc.TestFnInvoked, qt.IsTrue) |
| 94 | + c.Assert(buf.String(), qt.Contains, "successfully sent") |
| 95 | +} |
| 96 | + |
| 97 | +func TestWebhook_TestCmd_NotFound(t *testing.T) { |
| 98 | + c := qt.New(t) |
| 99 | + |
| 100 | + var buf bytes.Buffer |
| 101 | + format := printer.Human |
| 102 | + p := printer.NewPrinter(&format) |
| 103 | + p.SetHumanOutput(&buf) |
| 104 | + |
| 105 | + org := "planetscale" |
| 106 | + db := "mydb" |
| 107 | + webhookID := "webhook-123" |
| 108 | + |
| 109 | + svc := &mock.WebhooksService{ |
| 110 | + TestFn: func(ctx context.Context, req *ps.TestWebhookRequest) error { |
| 111 | + return &ps.Error{Code: ps.ErrNotFound} |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + ch := &cmdutil.Helper{ |
| 116 | + Printer: p, |
| 117 | + Config: &config.Config{ |
| 118 | + Organization: org, |
| 119 | + }, |
| 120 | + Client: func() (*ps.Client, error) { |
| 121 | + return &ps.Client{ |
| 122 | + Webhooks: svc, |
| 123 | + }, nil |
| 124 | + }, |
| 125 | + } |
| 126 | + |
| 127 | + cmd := TestCmd(ch) |
| 128 | + cmd.SetArgs([]string{db, webhookID}) |
| 129 | + err := cmd.Execute() |
| 130 | + |
| 131 | + c.Assert(err, qt.IsNotNil) |
| 132 | + c.Assert(err.Error(), qt.Contains, "does not exist") |
| 133 | +} |
0 commit comments