gunk-dev/the-valley

webhook_setup_test.go

sourcec14b0e68e9c3main's published tip history
11.0 KiB
1package cmd
2
3import (
4 "bytes"
5 "os"
6 "path/filepath"
7 "testing"
8
9 "the-valley/flow/internal/config"
10 "the-valley/flow/internal/project"
11)
12
13func TestMatchHook(t *testing.T) {
14 tests := []struct {
15 name string
16 hooks []ghHook
17 relayURL string
18 wantID int64
19 }{
20 {
21 name: "no hooks",
22 hooks: nil,
23 relayURL: "https://relay.example.com",
24 wantID: 0,
25 },
26 {
27 name: "exact match",
28 hooks: []ghHook{
29 {ID: 42, Config: struct {
30 URL string `json:"url"`
31 ContentType string `json:"content_type"`
32 }{URL: "https://relay.example.com"}},
33 },
34 relayURL: "https://relay.example.com",
35 wantID: 42,
36 },
37 {
38 name: "match with trailing slash on relay",
39 hooks: []ghHook{
40 {ID: 99, Config: struct {
41 URL string `json:"url"`
42 ContentType string `json:"content_type"`
43 }{URL: "https://relay.example.com"}},
44 },
45 relayURL: "https://relay.example.com/",
46 wantID: 99,
47 },
48 {
49 name: "match with trailing slash on hook",
50 hooks: []ghHook{
51 {ID: 55, Config: struct {
52 URL string `json:"url"`
53 ContentType string `json:"content_type"`
54 }{URL: "https://relay.example.com/"}},
55 },
56 relayURL: "https://relay.example.com",
57 wantID: 55,
58 },
59 {
60 name: "hook URL is subpath of relay",
61 hooks: []ghHook{
62 {ID: 10, Config: struct {
63 URL string `json:"url"`
64 ContentType string `json:"content_type"`
65 }{URL: "https://relay.example.com/webhook/github"}},
66 },
67 relayURL: "https://relay.example.com",
68 wantID: 10,
69 },
70 {
71 name: "no match - different host",
72 hooks: []ghHook{
73 {ID: 77, Config: struct {
74 URL string `json:"url"`
75 ContentType string `json:"content_type"`
76 }{URL: "https://other.example.com"}},
77 },
78 relayURL: "https://relay.example.com",
79 wantID: 0,
80 },
81 {
82 name: "multiple hooks, second matches",
83 hooks: []ghHook{
84 {ID: 1, Config: struct {
85 URL string `json:"url"`
86 ContentType string `json:"content_type"`
87 }{URL: "https://other.example.com"}},
88 {ID: 2, Config: struct {
89 URL string `json:"url"`
90 ContentType string `json:"content_type"`
91 }{URL: "https://relay.example.com/webhook"}},
92 },
93 relayURL: "https://relay.example.com",
94 wantID: 2,
95 },
96 {
97 name: "partial hostname should not match",
98 hooks: []ghHook{
99 {ID: 88, Config: struct {
100 URL string `json:"url"`
101 ContentType string `json:"content_type"`
102 }{URL: "https://relay.example.com.evil.com"}},
103 },
104 relayURL: "https://relay.example.com",
105 wantID: 0,
106 },
107 }
108
109 for _, tt := range tests {
110 t.Run(tt.name, func(t *testing.T) {
111 got := matchHook(tt.hooks, tt.relayURL)
112 if got != tt.wantID {
113 t.Errorf("matchHook() = %d, want %d", got, tt.wantID)
114 }
115 })
116 }
117}
118
119func TestWebhookCheck_NoRelayURL(t *testing.T) {
120 deps := WebhookDeps{
121 LoadConfig: func() (config.Config, error) { return config.Config{}, nil },
122 }
123
124 var buf bytes.Buffer
125 webhookCheckCmd.SetOut(&buf)
126 webhookCheckCmd.SetErr(&buf)
127
128 err := runWebhookCheck(webhookCheckCmd, nil, deps)
129 if err == nil || err.Error() != "webhook.relay_url is not configured; add it to your flow config" {
130 t.Errorf("expected relay_url error, got: %v", err)
131 }
132}
133
134func TestWebhookCheck_NoProjects(t *testing.T) {
135 deps := WebhookDeps{
136 LoadConfig: func() (config.Config, error) {
137 return config.Config{
138 Webhook: &config.WebhookConfig{RelayURL: "https://relay.example.com"},
139 }, nil
140 },
141 LoadRegistry: func() (*project.Registry, error) {
142 return &project.Registry{Projects: map[string]string{}}, nil
143 },
144 }
145
146 var buf bytes.Buffer
147 webhookCheckCmd.SetOut(&buf)
148 webhookCheckCmd.SetErr(&buf)
149
150 if err := runWebhookCheck(webhookCheckCmd, nil, deps); err != nil {
151 t.Fatalf("unexpected error: %v", err)
152 }
153 if out := buf.String(); out != "No projects registered. Use 'valley flow project add' to register one.\n" {
154 t.Errorf("unexpected output: %q", out)
155 }
156}
157
158func TestWebhookCheck_MixedStatuses(t *testing.T) {
159 deps := WebhookDeps{
160 LoadConfig: func() (config.Config, error) {
161 return config.Config{
162 Webhook: &config.WebhookConfig{RelayURL: "https://relay.example.com"},
163 }, nil
164 },
165 LoadRegistry: func() (*project.Registry, error) {
166 return &project.Registry{Projects: map[string]string{
167 "proj-a": "/tmp/proj-a",
168 "proj-b": "/tmp/proj-b",
169 }}, nil
170 },
171 ResolveRepo: func(dir string) (string, string, error) {
172 switch dir {
173 case "/tmp/proj-a":
174 return "owner", "proj-a", nil
175 case "/tmp/proj-b":
176 return "owner", "proj-b", nil
177 }
178 return "", "", nil
179 },
180 ListHooks: func(owner, repo string) ([]ghHook, error) {
181 if repo == "proj-a" {
182 return []ghHook{
183 {ID: 42, Config: struct {
184 URL string `json:"url"`
185 ContentType string `json:"content_type"`
186 }{URL: "https://relay.example.com/webhook"}},
187 }, nil
188 }
189 return nil, nil
190 },
191 }
192
193 var buf bytes.Buffer
194 webhookCheckCmd.SetOut(&buf)
195 webhookCheckCmd.SetErr(&buf)
196
197 if err := runWebhookCheck(webhookCheckCmd, nil, deps); err != nil {
198 t.Fatalf("unexpected error: %v", err)
199 }
200 out := buf.String()
201 if !containsSubstring(out, "configured") {
202 t.Errorf("expected 'configured' in output, got: %s", out)
203 }
204 if !containsSubstring(out, "missing") {
205 t.Errorf("expected 'missing' in output, got: %s", out)
206 }
207}
208
209func TestWebhookSetup_CreatesHook(t *testing.T) {
210 // Write a temp secret file.
211 tmpDir := t.TempDir()
212 secretPath := filepath.Join(tmpDir, "secret")
213 os.WriteFile(secretPath, []byte("my-secret\n"), 0o644)
214
215 var createdOwner, createdRepo, createdURL, createdSecret string
216 deps := WebhookDeps{
217 LoadConfig: func() (config.Config, error) {
218 return config.Config{
219 Webhook: &config.WebhookConfig{
220 RelayURL: "https://relay.example.com",
221 SecretFile: secretPath,
222 },
223 }, nil
224 },
225 ReadFile: os.ReadFile,
226 LoadRegistry: func() (*project.Registry, error) {
227 return &project.Registry{Projects: map[string]string{
228 "myproject": "/tmp/myproject",
229 }}, nil
230 },
231 ResolveRepo: func(dir string) (string, string, error) {
232 return "owner", "myproject", nil
233 },
234 ListHooks: func(owner, repo string) ([]ghHook, error) {
235 return nil, nil // no existing hooks
236 },
237 CreateHook: func(owner, repo, relayURL, secret string) error {
238 createdOwner = owner
239 createdRepo = repo
240 createdURL = relayURL
241 createdSecret = secret
242 return nil
243 },
244 }
245
246 var buf bytes.Buffer
247 webhookSetupCmd.SetOut(&buf)
248 webhookSetupCmd.SetErr(&buf)
249
250 if err := runWebhookSetup(webhookSetupCmd, nil, deps); err != nil {
251 t.Fatalf("unexpected error: %v", err)
252 }
253
254 if createdOwner != "owner" || createdRepo != "myproject" {
255 t.Errorf("unexpected repo: %s/%s", createdOwner, createdRepo)
256 }
257 if createdURL != "https://relay.example.com" {
258 t.Errorf("unexpected relay URL: %s", createdURL)
259 }
260 if createdSecret != "my-secret" {
261 t.Errorf("unexpected secret: %q", createdSecret)
262 }
263 if !containsSubstring(buf.String(), "webhook created") {
264 t.Errorf("expected 'webhook created' in output, got: %s", buf.String())
265 }
266}
267
268func TestWebhookSetup_SkipsExisting(t *testing.T) {
269 tmpDir := t.TempDir()
270 secretPath := filepath.Join(tmpDir, "secret")
271 os.WriteFile(secretPath, []byte("s3cret"), 0o644)
272
273 createCalled := false
274 deps := WebhookDeps{
275 LoadConfig: func() (config.Config, error) {
276 return config.Config{
277 Webhook: &config.WebhookConfig{
278 RelayURL: "https://relay.example.com",
279 SecretFile: secretPath,
280 },
281 }, nil
282 },
283 ReadFile: os.ReadFile,
284 LoadRegistry: func() (*project.Registry, error) {
285 return &project.Registry{Projects: map[string]string{
286 "myproject": "/tmp/myproject",
287 }}, nil
288 },
289 ResolveRepo: func(dir string) (string, string, error) {
290 return "owner", "myproject", nil
291 },
292 ListHooks: func(owner, repo string) ([]ghHook, error) {
293 return []ghHook{
294 {ID: 100, Config: struct {
295 URL string `json:"url"`
296 ContentType string `json:"content_type"`
297 }{URL: "https://relay.example.com"}},
298 }, nil
299 },
300 CreateHook: func(owner, repo, relayURL, secret string) error {
301 createCalled = true
302 return nil
303 },
304 }
305
306 var buf bytes.Buffer
307 webhookSetupCmd.SetOut(&buf)
308 webhookSetupCmd.SetErr(&buf)
309
310 if err := runWebhookSetup(webhookSetupCmd, nil, deps); err != nil {
311 t.Fatalf("unexpected error: %v", err)
312 }
313
314 if createCalled {
315 t.Error("createHook should not have been called for existing webhook")
316 }
317 if !containsSubstring(buf.String(), "already configured") {
318 t.Errorf("expected 'already configured' in output, got: %s", buf.String())
319 }
320}
321
322func TestWebhookSetup_MissingSecretFile(t *testing.T) {
323 deps := WebhookDeps{
324 LoadConfig: func() (config.Config, error) {
325 return config.Config{
326 Webhook: &config.WebhookConfig{
327 RelayURL: "https://relay.example.com",
328 SecretFile: "/nonexistent/secret",
329 },
330 }, nil
331 },
332 ReadFile: os.ReadFile,
333 }
334
335 err := runWebhookSetup(webhookSetupCmd, nil, deps)
336 if err == nil || !containsSubstring(err.Error(), "reading webhook secret") {
337 t.Errorf("expected secret file error, got: %v", err)
338 }
339}
340
341func TestWebhookSetup_SpecificProject(t *testing.T) {
342 tmpDir := t.TempDir()
343 secretPath := filepath.Join(tmpDir, "secret")
344 os.WriteFile(secretPath, []byte("s3cret"), 0o644)
345
346 var resolvedDirs []string
347 deps := WebhookDeps{
348 LoadConfig: func() (config.Config, error) {
349 return config.Config{
350 Webhook: &config.WebhookConfig{
351 RelayURL: "https://relay.example.com",
352 SecretFile: secretPath,
353 },
354 }, nil
355 },
356 ReadFile: os.ReadFile,
357 LoadRegistry: func() (*project.Registry, error) {
358 return &project.Registry{Projects: map[string]string{
359 "proj-a": "/tmp/proj-a",
360 "proj-b": "/tmp/proj-b",
361 }}, nil
362 },
363 ResolveRepo: func(dir string) (string, string, error) {
364 resolvedDirs = append(resolvedDirs, dir)
365 return "owner", filepath.Base(dir), nil
366 },
367 ListHooks: func(owner, repo string) ([]ghHook, error) {
368 return nil, nil
369 },
370 CreateHook: func(owner, repo, relayURL, secret string) error {
371 return nil
372 },
373 }
374
375 var buf bytes.Buffer
376 webhookSetupCmd.SetOut(&buf)
377 webhookSetupCmd.SetErr(&buf)
378
379 if err := runWebhookSetup(webhookSetupCmd, []string{"proj-a"}, deps); err != nil {
380 t.Fatalf("unexpected error: %v", err)
381 }
382
383 if len(resolvedDirs) != 1 || resolvedDirs[0] != "/tmp/proj-a" {
384 t.Errorf("expected only proj-a to be resolved, got: %v", resolvedDirs)
385 }
386}
387
388func TestWebhookSetup_UnknownProject(t *testing.T) {
389 tmpDir := t.TempDir()
390 secretPath := filepath.Join(tmpDir, "secret")
391 os.WriteFile(secretPath, []byte("s3cret"), 0o644)
392
393 deps := WebhookDeps{
394 LoadConfig: func() (config.Config, error) {
395 return config.Config{
396 Webhook: &config.WebhookConfig{
397 RelayURL: "https://relay.example.com",
398 SecretFile: secretPath,
399 },
400 }, nil
401 },
402 ReadFile: os.ReadFile,
403 LoadRegistry: func() (*project.Registry, error) {
404 return &project.Registry{Projects: map[string]string{
405 "proj-a": "/tmp/proj-a",
406 }}, nil
407 },
408 }
409
410 err := runWebhookSetup(webhookSetupCmd, []string{"nonexistent"}, deps)
411 if err == nil || err.Error() != `project "nonexistent" is not registered` {
412 t.Errorf("expected not registered error, got: %v", err)
413 }
414}
415
416func containsSubstring(s, substr string) bool {
417 return len(s) >= len(substr) && bytes.Contains([]byte(s), []byte(substr))
418}