BlueGit

timfa bluegit

Files

bluegit / internal / webhook / payload_test.go

payload_test.go

fix: stuur de console-output van elke job mee in pipeline-webhooks
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
6080426e History
payload_test.go 5.1 KiB Download Raw
1package webhook
2
3import (
4 "encoding/json"
5 "strings"
6 "testing"
7)
8
9func 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
40func 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"}
74 body, err = RenderBody("", push, nil)
75 if err != nil {
76 t.Fatal(err)
77 }
78 if err := json.Unmarshal(body, &doc); err != nil {
79 t.Fatal(err)
80 }
81 if doc["object_kind"] != "push" || doc["before"] != "a" || doc["commits"] == nil {
82 t.Fatalf("push %s", body)
83 }
84
85 mr := Event{
86 Type: EventMergeRequest, Project: "tim/app", Comment: "hoort niet mee",
87 MergeRequest: &MRInfo{ID: 3, Title: "titel", Description: "tekst", SourceBranch: "f", TargetBranch: "master", Status: "open", Author: "ada", Action: "opened"},
88 }
89 body, err = RenderBody("", mr, nil)
90 if err != nil {
91 t.Fatal(err)
92 }
93 if strings.Contains(string(body), "hoort niet mee") || !strings.Contains(string(body), `"action":"opened"`) {
94 t.Fatalf("opened lekt commentaar: %s", body)
95 }
96 mr.MergeRequest.Action = "commented"
97 mr.Comment = "mooie opmerking"
98 body, err = RenderBody("", mr, nil)
99 if err != nil || !strings.Contains(string(body), "mooie opmerking") {
100 t.Fatalf("commented %s %v", body, err)
101 }
102
103 rel := Event{
104 Type: EventRelease, Project: "tim/app", ReleaseTag: "v1",
105 Release: &ReleaseInfo{Tag: "v1", Name: "Een", Commit: "abc", Assets: []AssetInfo{{Name: "bluegit", URL: "https://git.example/a", Size: 4}}},
106 }
107 body, err = RenderBody("", rel, nil)
108 if err != nil {
109 t.Fatal(err)
110 }
111 if err := json.Unmarshal(body, &doc); err != nil {
112 t.Fatal(err)
113 }
114 if doc["object_kind"] != "release" {
115 t.Fatal(string(body))
116 }
117 release := doc["release"].(map[string]any)
118 assets := release["assets"].([]any)
119 if release["tag"] != "v1" || release["commit"] != "abc" || assets[0].(map[string]any)["url"] != "https://git.example/a" {
120 t.Fatalf("release %s", body)
121 }
122
123 rendered, err := Expand("{{event.payload}}", pipe, nil)
124 if err != nil || !strings.Contains(rendered, `"log":"boom"`) {
125 t.Fatalf("payload-placeholder %s %v", rendered, err)
126 }
127}
128
129func TestOpenAPIMatchesExample(t *testing.T) {
130 var spec map[string]any
131 if err := json.Unmarshal(OpenAPI(), &spec); err != nil {
132 t.Fatal(err)
133 }
134 if spec["openapi"] != "3.1.0" {
135 t.Fatalf("versie %v", spec["openapi"])
136 }
137 raw := string(OpenAPI())
138 for _, want := range []string{
139 "PipelineEvent", "PushEvent", "MergeRequestEvent", "ReleaseEvent",
140 "commit_message", "allow_failure", "log_truncated", "log_url",
141 "/repos/{owner}/{repo}/jobs/{id}/log",
142 "source_branch", "target_branch", "before", "after", "assets",
143 } {
144 if !strings.Contains(raw, want) {
145 t.Errorf("openapi mist %s", want)
146 }
147 }
148 example := Example()
149 for _, want := range []string{"log_truncated", "commit_message", "source_branch", "refs/heads/master", "assets", "commented"} {
150 if !strings.Contains(example, want) && want != "commented" {
151 t.Errorf("voorbeeld mist %s", want)
152 }
153 }
154 if !strings.Contains(example, `"action": "opened"`) {
155 t.Fatalf("voorbeeld:\n%s", example)
156 }
157}