| 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 | trunc := false |
| 42 | pipe := Event{ |
| 43 | Type: EventPipelineFailure, Project: "tim/app", PipelineID: "9", |
| 44 | Pipeline: &PipelineInfo{ID: 9, Status: "failed", Ref: "refs/heads/master", SHA: "abc", CommitMessage: "fix", Author: "tim", Duration: 1.5}, |
| 45 | Jobs: []JobInfo{ |
| 46 | {Name: "test", Stage: "test", Status: "failed", Duration: 1, AllowFailure: false, WebURL: "https://git.example/job", Log: "boom", LogTruncated: &trunc, LogURL: "https://git.example/log"}, |
| 47 | {Name: "build", Stage: "build", Status: "success", WebURL: "https://git.example/build"}, |
| 48 | }, |
| 49 | } |
| 50 | body, err := RenderBody("", pipe, nil) |
| 51 | if err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | var doc map[string]any |
| 55 | if err := json.Unmarshal(body, &doc); err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | if doc["object_kind"] != "pipeline" || doc["pipeline_id"] != "9" { |
| 59 | t.Fatalf("pipeline %s", body) |
| 60 | } |
| 61 | jobs := doc["jobs"].([]any) |
| 62 | failed := jobs[0].(map[string]any) |
| 63 | if failed["log"] != "boom" || failed["log_truncated"] != false || failed["allow_failure"] != false || failed["log_url"] == "" { |
| 64 | t.Fatalf("gefaalde job %v", failed) |
| 65 | } |
| 66 | ok := jobs[1].(map[string]any) |
| 67 | if _, has := ok["log"]; has { |
| 68 | t.Fatalf("geslaagde job heeft een log: %v", ok) |
| 69 | } |
| 70 | if _, has := ok["log_truncated"]; has { |
| 71 | t.Fatalf("geslaagde job heeft log_truncated: %v", ok) |
| 72 | } |
| 73 | |
| 74 | push := Event{Type: EventPush, Project: "tim/app", Ref: "refs/heads/master", Before: "a", After: "b"} |
| 75 | body, err = RenderBody("", push, nil) |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | if err := json.Unmarshal(body, &doc); err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | if doc["object_kind"] != "push" || doc["before"] != "a" || doc["commits"] == nil { |
| 83 | t.Fatalf("push %s", body) |
| 84 | } |
| 85 | |
| 86 | mr := Event{ |
| 87 | Type: EventMergeRequest, Project: "tim/app", Comment: "hoort niet mee", |
| 88 | MergeRequest: &MRInfo{ID: 3, Title: "titel", Description: "tekst", SourceBranch: "f", TargetBranch: "master", Status: "open", Author: "ada", Action: "opened"}, |
| 89 | } |
| 90 | body, err = RenderBody("", mr, nil) |
| 91 | if err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | if strings.Contains(string(body), "hoort niet mee") || !strings.Contains(string(body), `"action":"opened"`) { |
| 95 | t.Fatalf("opened lekt commentaar: %s", body) |
| 96 | } |
| 97 | mr.MergeRequest.Action = "commented" |
| 98 | mr.Comment = "mooie opmerking" |
| 99 | body, err = RenderBody("", mr, nil) |
| 100 | if err != nil || !strings.Contains(string(body), "mooie opmerking") { |
| 101 | t.Fatalf("commented %s %v", body, err) |
| 102 | } |
| 103 | |
| 104 | rel := Event{ |
| 105 | Type: EventRelease, Project: "tim/app", ReleaseTag: "v1", |
| 106 | Release: &ReleaseInfo{Tag: "v1", Name: "Een", Commit: "abc", Assets: []AssetInfo{{Name: "bluegit", URL: "https://git.example/a", Size: 4}}}, |
| 107 | } |
| 108 | body, err = RenderBody("", rel, nil) |
| 109 | if err != nil { |
| 110 | t.Fatal(err) |
| 111 | } |
| 112 | if err := json.Unmarshal(body, &doc); err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | if doc["object_kind"] != "release" { |
| 116 | t.Fatal(string(body)) |
| 117 | } |
| 118 | release := doc["release"].(map[string]any) |
| 119 | assets := release["assets"].([]any) |
| 120 | if release["tag"] != "v1" || release["commit"] != "abc" || assets[0].(map[string]any)["url"] != "https://git.example/a" { |
| 121 | t.Fatalf("release %s", body) |
| 122 | } |
| 123 | |
| 124 | rendered, err := Expand("{{event.payload}}", pipe, nil) |
| 125 | if err != nil || !strings.Contains(rendered, `"log":"boom"`) { |
| 126 | t.Fatalf("payload-placeholder %s %v", rendered, err) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestOpenAPIMatchesExample(t *testing.T) { |
| 131 | var spec map[string]any |
| 132 | if err := json.Unmarshal(OpenAPI(), &spec); err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | if spec["openapi"] != "3.1.0" { |
| 136 | t.Fatalf("versie %v", spec["openapi"]) |
| 137 | } |
| 138 | raw := string(OpenAPI()) |
| 139 | for _, want := range []string{ |
| 140 | "PipelineEvent", "PushEvent", "MergeRequestEvent", "ReleaseEvent", |
| 141 | "commit_message", "allow_failure", "log_truncated", "log_url", |
| 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 | } |