package webhook import ( "encoding/json" "strings" "testing" ) func TestTailLogMasksAndCuts(t *testing.T) { secret := "super-secret-token" var lines []string for i := 0; i < LogMaxLines+40; i++ { lines = append(lines, "regel") } lines = append(lines, "token "+secret) got, truncated := TailLog(strings.Join(lines, "\n"), []string{secret}) if !truncated { t.Fatal("te lange log hoort afgekapt") } if n := len(strings.Split(got, "\n")); n != LogMaxLines { t.Fatalf("regels %d", n) } if strings.Contains(got, secret) || !strings.Contains(got, "[MASKED]") { t.Fatalf("masker: %s", got[len(got)-80:]) } if _, truncated := TailLog("kort\n", []string{secret}); truncated { t.Fatal("korte log") } got, _ = TailLog("code "+secret, nil) if strings.Contains(got, "[MASKED]") { t.Fatal("zonder geheimen niets maskeren") } huge := strings.Repeat("x", LogMaxBytes+100) got, truncated = TailLog(huge, nil) if !truncated || len(got) > LogMaxBytes { t.Fatalf("bytes %d truncated %v", len(got), truncated) } } func TestDocumentShapes(t *testing.T) { pipe := Event{ Type: EventPipelineFailure, Project: "tim/app", PipelineID: "9", Pipeline: &PipelineInfo{ID: 9, Status: "failed", Ref: "refs/heads/master", SHA: "abc", CommitMessage: "fix", Author: "tim", Duration: 1.5}, Jobs: []JobInfo{ {Name: "test", Stage: "test", Status: "failed", Duration: 1, AllowFailure: false, WebURL: "https://git.example/job", Log: "boom", LogTruncated: true, LogURL: "https://git.example/api/v1/repos/tim/app/jobs/7/log"}, {Name: "build", Stage: "build", Status: "success", WebURL: "https://git.example/build", Log: "ok"}, }, } body, err := RenderBody("", pipe, nil) if err != nil { t.Fatal(err) } var doc map[string]any if err := json.Unmarshal(body, &doc); err != nil { t.Fatal(err) } if doc["object_kind"] != "pipeline" || doc["pipeline_id"] != "9" { t.Fatalf("pipeline %s", body) } jobs := doc["jobs"].([]any) failed := jobs[0].(map[string]any) if failed["log"] != "boom" || failed["log_truncated"] != true || failed["allow_failure"] != false || failed["log_url"] == "" { t.Fatalf("gefaalde job %v", failed) } ok := jobs[1].(map[string]any) if ok["log"] != "ok" || ok["log_truncated"] != false { t.Fatalf("geslaagde job %v", ok) } if _, has := ok["log_url"]; has { t.Fatalf("korte log heeft een url: %v", ok) } push := Event{Type: EventPush, Project: "tim/app", Ref: "refs/heads/master", Before: "a", After: "b", Commits: []CommitInfo{{ SHA: "abc", Message: "fix", Author: "tim ", Timestamp: "2026-10-22T12:00:00Z", URL: "https://git.example/commit", DiffURL: "https://git.example/api/v1/repos/tim/app/commits/abc/diff", Files: []FileChange{{Path: "README.md", Status: "modified", Additions: 1, Deletions: 1}}, }}} body, err = RenderBody("", push, nil) if err != nil { t.Fatal(err) } if strings.Contains(string(body), "diff --git") { t.Fatalf("push bevat een diff: %s", body) } if err := json.Unmarshal(body, &doc); err != nil { t.Fatal(err) } if doc["object_kind"] != "push" || doc["before"] != "a" || doc["commits"] == nil { t.Fatalf("push %s", body) } commit := doc["commits"].([]any)[0].(map[string]any) file := commit["files"].([]any)[0].(map[string]any) if commit["timestamp"] != "2026-10-22T12:00:00Z" || commit["diff_url"] == "" || file["status"] != "modified" || file["additions"] != float64(1) || file["deletions"] != float64(1) { t.Fatalf("commit %s", body) } if _, has := commit["diff"]; has || file["path"] != "README.md" { t.Fatalf("commit lekt een diff: %s", body) } mr := Event{ Type: EventMergeRequest, Project: "tim/app", Comment: "hoort niet mee", MergeRequest: &MRInfo{ID: 3, Title: "titel", Description: "tekst", SourceBranch: "f", TargetBranch: "master", Status: "open", Author: "ada", Action: "opened"}, } body, err = RenderBody("", mr, nil) if err != nil { t.Fatal(err) } if strings.Contains(string(body), "hoort niet mee") || !strings.Contains(string(body), `"action":"opened"`) || strings.Contains(string(body), "diff --git") { t.Fatalf("opened lekt commentaar of een diff: %s", body) } if err := json.Unmarshal(body, &doc); err != nil { t.Fatal(err) } openedMR := doc["merge_request"].(map[string]any) if openedMR["reviewers"] == nil || openedMR["comments"] == nil || openedMR["commits"] == nil || openedMR["diff_stats"] == nil || openedMR["diff_url"] == nil { t.Fatalf("mr mist details: %s", body) } mr.MergeRequest.Action = "commented" mr.Comment = "mooie opmerking" body, err = RenderBody("", mr, nil) if err != nil || !strings.Contains(string(body), "mooie opmerking") { t.Fatalf("commented %s %v", body, err) } rel := Event{ Type: EventRelease, Project: "tim/app", ReleaseTag: "v1", Release: &ReleaseInfo{Tag: "v1", Name: "Een", Commit: "abc", Assets: []AssetInfo{{Name: "bluegit", URL: "https://git.example/a", Size: 4}}}, } body, err = RenderBody("", rel, nil) if err != nil { t.Fatal(err) } if err := json.Unmarshal(body, &doc); err != nil { t.Fatal(err) } if doc["object_kind"] != "release" { t.Fatal(string(body)) } release := doc["release"].(map[string]any) assets := release["assets"].([]any) if release["tag"] != "v1" || release["commit"] != "abc" || assets[0].(map[string]any)["url"] != "https://git.example/a" { t.Fatalf("release %s", body) } rendered, err := Expand("{{event.payload}}", pipe, nil) if err != nil || !strings.Contains(rendered, `"log":"boom"`) { t.Fatalf("payload-placeholder %s %v", rendered, err) } } func TestIssueDocument(t *testing.T) { ev := Event{ Type: EventIssue, Project: "tim/app", Actor: "ada", Comment: "hoort niet mee", Issue: &IssueInfo{ ID: 3, Title: "Kapot", Description: "tekst", Status: "open", Author: "ada", Action: "opened", }, } raw, err := json.Marshal(ev.Document()) if err != nil { t.Fatal(err) } var doc map[string]any if err := json.Unmarshal(raw, &doc); err != nil { t.Fatal(err) } if doc["object_kind"] != "issue" || doc["comment"] != nil { t.Fatalf("opened lekt een comment: %s", raw) } iss := doc["issue"].(map[string]any) if iss["action"] != "opened" || iss["assignees"] == nil || iss["labels"] == nil || iss["confidential"] != false { t.Fatalf("issue %s", raw) } ev.Issue.Action = "commented" ev.Comment = "hoort mee" raw, _ = json.Marshal(ev.Document()) if !strings.Contains(string(raw), `"comment":"hoort mee"`) { t.Fatalf("comment ontbreekt: %s", raw) } } func TestOpenAPIMatchesExample(t *testing.T) { var spec map[string]any if err := json.Unmarshal(OpenAPI(), &spec); err != nil { t.Fatal(err) } if spec["openapi"] != "3.1.0" { t.Fatalf("versie %v", spec["openapi"]) } raw := string(OpenAPI()) for _, want := range []string{ "PipelineEvent", "PushEvent", "MergeRequestEvent", "ReleaseEvent", "IssueEvent", "commit_message", "allow_failure", "log_truncated", "log_url", "/repos/{owner}/{repo}/jobs/{id}/log", "diff_url", "reviewers", "diff_stats", "/commits/{sha}/diff", "/merge-requests/{id}/diff", "source_branch", "target_branch", "before", "after", "assets", } { if !strings.Contains(raw, want) { t.Errorf("openapi mist %s", want) } } example := Example() for _, want := range []string{"log_truncated", "commit_message", "source_branch", "refs/heads/master", "assets", "commented", "diff_url", "reviewers", "additions"} { if !strings.Contains(example, want) && want != "commented" { t.Errorf("voorbeeld mist %s", want) } } if !strings.Contains(example, `"action": "opened"`) { t.Fatalf("voorbeeld:\n%s", example) } }