Skip to content

Commit 24cdc9c

Browse files
authored
Merge pull request #1160 from planetscale/webhook-test
Add `webhook test` command
2 parents d0213d3 + ee49618 commit 24cdc9c

File tree

3 files changed

+193
-0
lines changed

3 files changed

+193
-0
lines changed

internal/cmd/webhook/test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package webhook
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/planetscale/cli/internal/cmdutil"
7+
"github.com/planetscale/cli/internal/printer"
8+
"github.com/planetscale/planetscale-go/planetscale"
9+
"github.com/spf13/cobra"
10+
)
11+
12+
func TestCmd(ch *cmdutil.Helper) *cobra.Command {
13+
cmd := &cobra.Command{
14+
Use: "test <database> <webhook-id>",
15+
Short: "Send a test event to a webhook",
16+
Args: cmdutil.RequiredArgs("database", "webhook-id"),
17+
RunE: func(cmd *cobra.Command, args []string) error {
18+
ctx := cmd.Context()
19+
database := args[0]
20+
webhookID := args[1]
21+
22+
client, err := ch.Client()
23+
if err != nil {
24+
return err
25+
}
26+
27+
end := ch.Printer.PrintProgress(fmt.Sprintf("Sending test event to webhook %s for %s", printer.BoldBlue(webhookID), printer.BoldBlue(database)))
28+
defer end()
29+
30+
err = client.Webhooks.Test(ctx, &planetscale.TestWebhookRequest{
31+
Organization: ch.Config.Organization,
32+
Database: database,
33+
ID: webhookID,
34+
})
35+
if err != nil {
36+
switch cmdutil.ErrCode(err) {
37+
case planetscale.ErrNotFound:
38+
return fmt.Errorf("webhook %s does not exist in database %s (organization: %s)",
39+
printer.BoldBlue(webhookID), printer.BoldBlue(database), printer.BoldBlue(ch.Config.Organization))
40+
default:
41+
return cmdutil.HandleError(err)
42+
}
43+
}
44+
45+
end()
46+
47+
if ch.Printer.Format() == printer.Human {
48+
ch.Printer.Printf("Test event was successfully sent to webhook %s.\n", printer.BoldBlue(webhookID))
49+
return nil
50+
}
51+
52+
return ch.Printer.PrintResource(map[string]string{
53+
"result": "test event sent",
54+
})
55+
},
56+
}
57+
58+
return cmd
59+
}

internal/cmd/webhook/test_test.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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+
}

internal/cmd/webhook/webhook.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func WebhookCmd(ch *cmdutil.Helper) *cobra.Command {
2525
cmd.AddCommand(DeleteCmd(ch))
2626
cmd.AddCommand(ListCmd(ch))
2727
cmd.AddCommand(ShowCmd(ch))
28+
cmd.AddCommand(TestCmd(ch))
2829
cmd.AddCommand(UpdateCmd(ch))
2930

3031
return cmd

0 commit comments

Comments
 (0)