| 1 | package webhook |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestSpecificEvents(t *testing.T) { |
| 9 | iss := func(action string) Event { |
| 10 | return Event{Type: EventIssue, Project: "tim/app", Actor: "ada", Comment: "hoi", URL: "https://git.example/tim/app/-/issues/7", |
| 11 | Issue: &IssueInfo{ID: 7, Title: "T", Status: "open", Action: action, URL: "https://git.example/tim/app/-/issues/7"}} |
| 12 | } |
| 13 | cases := map[string]string{"opened": EventIssueOpened, "closed": EventIssueClosed, "reopened": EventIssueReopened, "updated": EventIssueUpdated, "commented": EventIssueComment} |
| 14 | for action, want := range cases { |
| 15 | ev := iss(action) |
| 16 | if got := Specific(ev); got != want { |
| 17 | t.Fatalf("%s: %s", action, got) |
| 18 | } |
| 19 | if ok, _ := Allows(Hook{Events: []string{want}}, ev); !ok { |
| 20 | t.Fatalf("%s niet toegestaan", want) |
| 21 | } |
| 22 | if ok, _ := Allows(Hook{Events: []string{EventIssue}}, ev); !ok { |
| 23 | t.Fatalf("issue dekt %s niet", want) |
| 24 | } |
| 25 | for _, other := range SpecificEvents() { |
| 26 | if other == want { |
| 27 | continue |
| 28 | } |
| 29 | if ok, _ := Allows(Hook{Events: []string{other}}, ev); ok { |
| 30 | t.Fatalf("%s krijgt %s", other, want) |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | mr := Event{Type: EventMergeRequest, Project: "tim/app", Actor: "ada", Comment: "lgtm", |
| 35 | MergeRequest: &MRInfo{ID: 3, Title: "M", Status: "open", Action: "commented", URL: "https://git.example/tim/app/-/merge-requests/3"}} |
| 36 | if ok, _ := Allows(Hook{Events: []string{EventMRComment}}, mr); !ok { |
| 37 | t.Fatal("mr comment") |
| 38 | } |
| 39 | opened := mr |
| 40 | info := *mr.MergeRequest |
| 41 | info.Action = "opened" |
| 42 | opened.MergeRequest = &info |
| 43 | if ok, _ := Allows(Hook{Events: []string{EventMRComment}}, opened); ok { |
| 44 | t.Fatal("geopende MR is geen comment") |
| 45 | } |
| 46 | raw, _ := json.Marshal(mr.Document()) |
| 47 | var d map[string]any |
| 48 | _ = json.Unmarshal(raw, &d) |
| 49 | note, _ := d["note"].(map[string]any) |
| 50 | if d["event_name"] != EventMRComment || note["body"] != "lgtm" || note["noteable_type"] != "MergeRequest" || note["url"] == "" { |
| 51 | t.Fatalf("payload %s", raw) |
| 52 | } |
| 53 | m, _ := d["merge_request"].(map[string]any) |
| 54 | if m["number"].(float64) != 3 { |
| 55 | t.Fatalf("number %s", raw) |
| 56 | } |
| 57 | raw, _ = json.Marshal(iss("closed").Document()) |
| 58 | _ = json.Unmarshal(raw, &d) |
| 59 | if d["event_name"] != EventIssueClosed { |
| 60 | t.Fatalf("issue %s", raw) |
| 61 | } |
| 62 | if s, ok := eventField(iss("commented"), "event_name"); !ok || s != EventIssueComment { |
| 63 | t.Fatal(s) |
| 64 | } |
| 65 | } |