| 1 | package webhook |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestTailLogMasksAndCuts(t *testing.T) { |
| 10 | secret := "super-secret-token" |
| 11 | var lines []string |
| 12 | for i := 0; i < LogMaxLines+40; i++ { |
| 13 | lines = append(lines, "regel") |
| 14 | } |
| 15 | lines = append(lines, "token "+secret) |
| 16 | got, truncated := TailLog(strings.Join(lines, "\n"), []string{secret}) |
| 17 | if !truncated { |
| 18 | t.Fatal("te lange log hoort afgekapt") |
| 19 | } |
| 20 | if n := len(strings.Split(got, "\n")); n != LogMaxLines { |
| 21 | t.Fatalf("regels %d", n) |
| 22 | } |
| 23 | if strings.Contains(got, secret) || !strings.Contains(got, "[MASKED]") { |
| 24 | t.Fatalf("masker: %s", got[len(got)-80:]) |
| 25 | } |
| 26 | if _, truncated := TailLog("kort\n", []string{secret}); truncated { |
| 27 | t.Fatal("korte log") |
| 28 | } |
| 29 | got, _ = TailLog("code "+secret, nil) |
| 30 | if strings.Contains(got, "[MASKED]") { |
| 31 | t.Fatal("zonder geheimen niets maskeren") |
| 32 | } |
| 33 | huge := strings.Repeat("x", LogMaxBytes+100) |
| 34 | got, truncated = TailLog(huge, nil) |
| 35 | if !truncated || len(got) > LogMaxBytes { |
| 36 | t.Fatalf("bytes %d truncated %v", len(got), truncated) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestDocumentShapes(t *testing.T) { |
| 41 | pipe := Event{ |
| 42 | Type: EventPipelineFailure, Project: "tim/app", PipelineID: "9", |
| 43 | Pipeline: &PipelineInfo{ID: 9, Status: "failed", Ref: "refs/heads/master", SHA: "abc", CommitMessage: "fix", Author: "tim", Duration: 1.5}, |
| 44 | Jobs: []JobInfo{ |
| 45 | {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"}, |
| 46 | {Name: "build", Stage: "build", Status: "success", WebURL: "https://git.example/build", Log: "ok"}, |
| 47 | }, |
| 48 | } |
| 49 | body, err := RenderBody("", pipe, nil) |
| 50 | if err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | var doc map[string]any |
| 54 | if err := json.Unmarshal(body, &doc); err != nil { |
| 55 | t.Fatal(err) |
| 56 | } |
| 57 | if doc["object_kind"] != "pipeline" || doc["pipeline_id"] != "9" { |
| 58 | t.Fatalf("pipeline %s", body) |
| 59 | } |
| 60 | jobs := doc["jobs"].([]any) |
| 61 | failed := jobs[0].(map[string]any) |
| 62 | if failed["log"] != "boom" || failed["log_truncated"] != true || failed["allow_failure"] != false || failed["log_url"] == "" { |
| 63 | t.Fatalf("gefaalde job %v", failed) |
| 64 | } |
| 65 | ok := jobs[1].(map[string]any) |
| 66 | if ok["log"] != "ok" || ok["log_truncated"] != false { |
| 67 | t.Fatalf("geslaagde job %v", ok) |
| 68 | } |
| 69 | if _, has := ok["log_url"]; has { |
| 70 | t.Fatalf("korte log heeft een url: %v", ok) |
| 71 | } |
| 72 | |
| 73 | push := Event{Type: EventPush, Project: "tim/app", Ref: "refs/heads/master", Before: "a", After: "b", Commits: []CommitInfo{{ |
| 74 | SHA: "abc", Message: "fix", Author: "tim <tim@example.com>", Timestamp: "2026-10-22T12:00:00Z", |
| 75 | URL: "https://git.example/commit", DiffURL: "https://git.example/api/v1/repos/tim/app/commits/abc/diff", |
| 76 | Files: []FileChange{{Path: "README.md", Status: "modified", Additions: 1, Deletions: 1}}, |
| 77 | }}} |
| 78 | body, err = RenderBody("", push, nil) |
| 79 | if err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | if strings.Contains(string(body), "diff --git") { |
| 83 | t.Fatalf("push bevat een diff: %s", body) |
| 84 | } |
| 85 | if err := json.Unmarshal(body, &doc); err != nil { |
| 86 | t.Fatal(err) |
| 87 | } |
| 88 | if doc["object_kind"] != "push" || doc["before"] != "a" || doc["commits"] == nil { |
| 89 | t.Fatalf("push %s", body) |
| 90 | } |
| 91 | commit := doc["commits"].([]any)[0].(map[string]any) |
| 92 | file := commit["files"].([]any)[0].(map[string]any) |
| 93 | if commit["timestamp"] != "2026-10-22T12:00:00Z" || commit["diff_url"] == "" || file["status"] != "modified" || file["additions"] != float64(1) || file["deletions"] != float64(1) { |
| 94 | t.Fatalf("commit %s", body) |
| 95 | } |
| 96 | if _, has := commit["diff"]; has || file["path"] != "README.md" { |
| 97 | t.Fatalf("commit lekt een diff: %s", body) |
| 98 | } |
| 99 | |
| 100 | mr := Event{ |
| 101 | Type: EventMergeRequest, Project: "tim/app", Comment: "hoort niet mee", |
| 102 | MergeRequest: &MRInfo{ID: 3, Title: "titel", Description: "tekst", SourceBranch: "f", TargetBranch: "master", Status: "open", Author: "ada", Action: "opened"}, |
| 103 | } |
| 104 | body, err = RenderBody("", mr, nil) |
| 105 | if err != nil { |
| 106 | t.Fatal(err) |
| 107 | } |
| 108 | if strings.Contains(string(body), "hoort niet mee") || !strings.Contains(string(body), `"action":"opened"`) || strings.Contains(string(body), "diff --git") { |
| 109 | t.Fatalf("opened lekt commentaar of een diff: %s", body) |
| 110 | } |
| 111 | if err := json.Unmarshal(body, &doc); err != nil { |
| 112 | t.Fatal(err) |
| 113 | } |
| 114 | openedMR := doc["merge_request"].(map[string]any) |
| 115 | if openedMR["reviewers"] == nil || openedMR["comments"] == nil || openedMR["commits"] == nil || openedMR["diff_stats"] == nil || openedMR["diff_url"] == nil { |
| 116 | t.Fatalf("mr mist details: %s", body) |
| 117 | } |
| 118 | mr.MergeRequest.Action = "commented" |
| 119 | mr.Comment = "mooie opmerking" |
| 120 | body, err = RenderBody("", mr, nil) |
| 121 | if err != nil || !strings.Contains(string(body), "mooie opmerking") { |
| 122 | t.Fatalf("commented %s %v", body, err) |
| 123 | } |
| 124 | |
| 125 | rel := Event{ |
| 126 | Type: EventRelease, Project: "tim/app", ReleaseTag: "v1", |
| 127 | Release: &ReleaseInfo{Tag: "v1", Name: "Een", Commit: "abc", Assets: []AssetInfo{{Name: "bluegit", URL: "https://git.example/a", Size: 4}}}, |
| 128 | } |
| 129 | body, err = RenderBody("", rel, nil) |
| 130 | if err != nil { |
| 131 | t.Fatal(err) |
| 132 | } |
| 133 | if err := json.Unmarshal(body, &doc); err != nil { |
| 134 | t.Fatal(err) |
| 135 | } |
| 136 | if doc["object_kind"] != "release" { |
| 137 | t.Fatal(string(body)) |
| 138 | } |
| 139 | release := doc["release"].(map[string]any) |
| 140 | assets := release["assets"].([]any) |
| 141 | if release["tag"] != "v1" || release["commit"] != "abc" || assets[0].(map[string]any)["url"] != "https://git.example/a" { |
| 142 | t.Fatalf("release %s", body) |
| 143 | } |
| 144 | |
| 145 | rendered, err := Expand("{{event.payload}}", pipe, nil) |
| 146 | if err != nil || !strings.Contains(rendered, `"log":"boom"`) { |
| 147 | t.Fatalf("payload-placeholder %s %v", rendered, err) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | func TestIssueDocument(t *testing.T) { |
| 152 | ev := Event{ |
| 153 | Type: EventIssue, Project: "tim/app", Actor: "ada", Comment: "hoort niet mee", |
| 154 | Issue: &IssueInfo{ |
| 155 | ID: 3, Title: "Kapot", Description: "tekst", Status: "open", |
| 156 | Author: "ada", Action: "opened", |
| 157 | }, |
| 158 | } |
| 159 | raw, err := json.Marshal(ev.Document()) |
| 160 | if err != nil { |
| 161 | t.Fatal(err) |
| 162 | } |
| 163 | var doc map[string]any |
| 164 | if err := json.Unmarshal(raw, &doc); err != nil { |
| 165 | t.Fatal(err) |
| 166 | } |
| 167 | if doc["object_kind"] != "issue" || doc["comment"] != nil { |
| 168 | t.Fatalf("opened lekt een comment: %s", raw) |
| 169 | } |
| 170 | iss := doc["issue"].(map[string]any) |
| 171 | if iss["action"] != "opened" || iss["assignees"] == nil || iss["labels"] == nil || iss["confidential"] != false { |
| 172 | t.Fatalf("issue %s", raw) |
| 173 | } |
| 174 | ev.Issue.Action = "commented" |
| 175 | ev.Comment = "hoort mee" |
| 176 | raw, _ = json.Marshal(ev.Document()) |
| 177 | if !strings.Contains(string(raw), `"comment":"hoort mee"`) { |
| 178 | t.Fatalf("comment ontbreekt: %s", raw) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | func TestOpenAPIMatchesExample(t *testing.T) { |
| 183 | var spec map[string]any |
| 184 | if err := json.Unmarshal(OpenAPI(), &spec); err != nil { |
| 185 | t.Fatal(err) |
| 186 | } |
| 187 | if spec["openapi"] != "3.1.0" { |
| 188 | t.Fatalf("versie %v", spec["openapi"]) |
| 189 | } |
| 190 | raw := string(OpenAPI()) |
| 191 | for _, want := range []string{ |
| 192 | "PipelineEvent", "PushEvent", "MergeRequestEvent", "ReleaseEvent", "IssueEvent", |
| 193 | "commit_message", "allow_failure", "log_truncated", "log_url", |
| 194 | "/repos/{owner}/{repo}/jobs/{id}/log", |
| 195 | "diff_url", "reviewers", "diff_stats", "/commits/{sha}/diff", "/merge-requests/{id}/diff", |
| 196 | "source_branch", "target_branch", "before", "after", "assets", |
| 197 | } { |
| 198 | if !strings.Contains(raw, want) { |
| 199 | t.Errorf("openapi mist %s", want) |
| 200 | } |
| 201 | } |
| 202 | example := Example() |
| 203 | for _, want := range []string{"log_truncated", "commit_message", "source_branch", "refs/heads/master", "assets", "commented", "diff_url", "reviewers", "additions"} { |
| 204 | if !strings.Contains(example, want) && want != "commented" { |
| 205 | t.Errorf("voorbeeld mist %s", want) |
| 206 | } |
| 207 | } |
| 208 | if !strings.Contains(example, `"action": "opened"`) { |
| 209 | t.Fatalf("voorbeeld:\n%s", example) |
| 210 | } |
| 211 | } |