-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_test.go
More file actions
239 lines (208 loc) · 7.95 KB
/
Copy pathplugin_test.go
File metadata and controls
239 lines (208 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package main
import (
"encoding/json"
"testing"
plugin "github.com/Paca-AI/plugin-sdk-go"
"github.com/Paca-AI/plugin-sdk-go/plugintest"
)
const testProjectID = "project-1"
func setupPlugin(t *testing.T) (*webhookPlugin, *plugintest.Context) {
t.Helper()
tc := plugintest.NewContext(t)
tc.Config.Set("ENCRYPTION_KEY", "3fead24473a9a7bf262857db0b4de648c86de5a29b3b3bb5bfb46875ede0d7de")
tc.DB.SeedRows("projects", []string{"id"}, [][]any{{testProjectID}})
tc.DB.SeedRows("webhooks",
[]string{"id", "project_id", "url", "secret_enc", "events", "enabled", "created_at", "updated_at"},
nil)
tc.DB.SeedRows("webhook_deliveries",
[]string{"id", "webhook_id", "event_type", "status_code", "success", "error", "created_at"},
nil)
p := &webhookPlugin{}
if err := p.Init(tc.PluginContext()); err != nil {
t.Fatal("Init failed:", err)
}
return p, tc
}
func callerReq(params map[string]string) plugintest.Request {
return plugintest.Request{
Caller: plugin.CallerIdentity{
ProjectID: testProjectID,
CallerID: "member-1",
CallerRole: "PROJECT_MEMBER",
},
PathParams: params,
}
}
func TestCreateAndListWebhook(t *testing.T) {
_, tc := setupPlugin(t)
createRes := tc.Call("POST", "/projects/:projectId/webhooks",
callerReq(map[string]string{"projectId": testProjectID}).WithJSONBody(map[string]any{
"url": "https://example.com/hooks/paca",
"secret": "s3cr3t",
"events": []string{"task.created", "task.deleted"},
}))
if createRes.StatusCode != 201 {
t.Fatalf("expected 201, got %d: %s", createRes.StatusCode, string(createRes.Body))
}
var created struct {
Data webhook `json:"data"`
}
if err := json.Unmarshal(createRes.Body, &created); err != nil {
t.Fatal(err)
}
if created.Data.URL != "https://example.com/hooks/paca" {
t.Fatalf("unexpected url: %s", created.Data.URL)
}
if !created.Data.HasSecret {
t.Fatal("expected has_secret=true")
}
listRes := tc.Call("GET", "/projects/:projectId/webhooks",
callerReq(map[string]string{"projectId": testProjectID}))
if listRes.StatusCode != 200 {
t.Fatalf("expected 200, got %d", listRes.StatusCode)
}
var listed struct {
Data []webhook `json:"data"`
}
if err := json.Unmarshal(listRes.Body, &listed); err != nil {
t.Fatal(err)
}
if len(listed.Data) != 1 {
t.Fatalf("expected 1 webhook, got %d", len(listed.Data))
}
}
func TestCreateWebhookRejectsInvalidURLAndEvents(t *testing.T) {
_, tc := setupPlugin(t)
res := tc.Call("POST", "/projects/:projectId/webhooks",
callerReq(map[string]string{"projectId": testProjectID}).WithJSONBody(map[string]any{
"url": "http://example.com/hooks",
"events": []string{"task.created"},
}))
if res.StatusCode != 400 {
t.Fatalf("expected 400 for non-https url, got %d", res.StatusCode)
}
res = tc.Call("POST", "/projects/:projectId/webhooks",
callerReq(map[string]string{"projectId": testProjectID}).WithJSONBody(map[string]any{
"url": "https://example.com/hooks",
"events": []string{"not.a.real.topic"},
}))
if res.StatusCode != 400 {
t.Fatalf("expected 400 for unsupported event, got %d", res.StatusCode)
}
}
func TestUpdateAndDeleteWebhook(t *testing.T) {
_, tc := setupPlugin(t)
createRes := tc.Call("POST", "/projects/:projectId/webhooks",
callerReq(map[string]string{"projectId": testProjectID}).WithJSONBody(map[string]any{
"url": "https://example.com/hooks/paca",
"events": []string{"task.created"},
}))
var created struct {
Data webhook `json:"data"`
}
_ = json.Unmarshal(createRes.Body, &created)
id := created.Data.ID
updRes := tc.Call("PATCH", "/projects/:projectId/webhooks/:webhookId",
callerReq(map[string]string{"projectId": testProjectID, "webhookId": id}).WithJSONBody(map[string]any{
"enabled": false,
}))
if updRes.StatusCode != 200 {
t.Fatalf("expected 200, got %d: %s", updRes.StatusCode, string(updRes.Body))
}
var updated struct {
Data webhook `json:"data"`
}
_ = json.Unmarshal(updRes.Body, &updated)
if updated.Data.Enabled {
t.Fatal("expected webhook to be disabled")
}
delRes := tc.Call("DELETE", "/projects/:projectId/webhooks/:webhookId",
callerReq(map[string]string{"projectId": testProjectID, "webhookId": id}))
if delRes.StatusCode != 204 {
t.Fatalf("expected 204, got %d", delRes.StatusCode)
}
getRes := tc.Call("GET", "/projects/:projectId/webhooks/:webhookId",
callerReq(map[string]string{"projectId": testProjectID, "webhookId": id}))
if getRes.StatusCode != 404 {
t.Fatalf("expected 404 after delete, got %d", getRes.StatusCode)
}
}
// TestActivityEventDispatchSkipsUnsubscribed verifies that a webhook only
// receives events it is subscribed to, and that dispatch doesn't panic even
// though Fetch always fails outside a WASM runtime (recorded as a failed
// delivery rather than a crash).
func TestActivityEventDispatchSkipsUnsubscribed(t *testing.T) {
p, tc := setupPlugin(t)
createRes := tc.Call("POST", "/projects/:projectId/webhooks",
callerReq(map[string]string{"projectId": testProjectID}).WithJSONBody(map[string]any{
"url": "https://example.com/hooks/paca",
"events": []string{"task.deleted"},
}))
var created struct {
Data webhook `json:"data"`
}
_ = json.Unmarshal(createRes.Body, &created)
payload, _ := json.Marshal(map[string]any{
"project_id": testProjectID,
"task_id": "task-1",
"activity_type": "task.created",
})
// Not subscribed to task.created — handler must be a no-op (no delivery
// row inserted, no panic).
p.handleActivityEvent("task.created")(&plugin.Event{Topic: "task.created", Payload: payload})
delRes := tc.Call("GET", "/projects/:projectId/webhooks/:webhookId/deliveries",
callerReq(map[string]string{"projectId": testProjectID, "webhookId": created.Data.ID}))
var deliveries struct {
Data []webhookDelivery `json:"data"`
}
_ = json.Unmarshal(delRes.Body, &deliveries)
if len(deliveries.Data) != 0 {
t.Fatalf("expected no deliveries for unsubscribed topic, got %d", len(deliveries.Data))
}
// Subscribed topic — handler should attempt delivery (and record a
// failed delivery row, since Fetch is unavailable outside WASM).
p.handleActivityEvent("task.deleted")(&plugin.Event{Topic: "task.deleted", Payload: payload})
delRes = tc.Call("GET", "/projects/:projectId/webhooks/:webhookId/deliveries",
callerReq(map[string]string{"projectId": testProjectID, "webhookId": created.Data.ID}))
_ = json.Unmarshal(delRes.Body, &deliveries)
if len(deliveries.Data) != 1 {
t.Fatalf("expected 1 delivery for subscribed topic, got %d", len(deliveries.Data))
}
if deliveries.Data[0].Success {
t.Fatal("expected delivery to fail outside WASM runtime")
}
}
// TestTaskRefAndProjectNameInSummaryText verifies that the human-readable
// "text" summary built for a delivery includes the task's alias (project
// task_id_prefix + task_number, e.g. "ABC-123") and is prefixed with the
// project name.
func TestTaskRefAndProjectNameInSummaryText(t *testing.T) {
p, tc := setupPlugin(t)
tc.DB.SeedRows("projects",
[]string{"id", "name", "task_id_prefix"},
[][]any{{testProjectID, "Website Redesign", "ABC"}})
tc.DB.SeedRows("tasks",
[]string{"id", "title", "task_number", "project_id"},
[][]any{{"task-1", "Fix login bug", 123, testProjectID}})
payload := map[string]any{
"project_id": testProjectID,
"task_id": "task-1",
}
if got, want := p.taskRef(payload), `task ABC-123 "Fix login bug"`; got != want {
t.Fatalf("taskRef = %q, want %q", got, want)
}
if got, want := p.projectName(payload), "Website Redesign"; got != want {
t.Fatalf("projectName = %q, want %q", got, want)
}
_, text := p.buildEventData("task.deleted", payload)
if want := `Someone deleted task ABC-123 "Fix login bug"`; text != want {
t.Fatalf("buildEventData text = %q, want %q", text, want)
}
// A project with no task_id_prefix configured falls back to no alias.
tc.DB.SeedRows("projects",
[]string{"id", "name", "task_id_prefix"},
[][]any{{testProjectID, "Website Redesign", ""}})
if got, want := p.taskRef(payload), `task "Fix login bug"`; got != want {
t.Fatalf("taskRef with no prefix = %q, want %q", got, want)
}
}