package webhook import ( "encoding/json" "testing" ) func TestSpecificEvents(t *testing.T) { iss := func(action string) Event { return Event{Type: EventIssue, Project: "tim/app", Actor: "ada", Comment: "hoi", URL: "https://git.example/tim/app/-/issues/7", Issue: &IssueInfo{ID: 7, Title: "T", Status: "open", Action: action, URL: "https://git.example/tim/app/-/issues/7"}} } cases := map[string]string{"opened": EventIssueOpened, "closed": EventIssueClosed, "reopened": EventIssueReopened, "updated": EventIssueUpdated, "commented": EventIssueComment} for action, want := range cases { ev := iss(action) if got := Specific(ev); got != want { t.Fatalf("%s: %s", action, got) } if ok, _ := Allows(Hook{Events: []string{want}}, ev); !ok { t.Fatalf("%s niet toegestaan", want) } if ok, _ := Allows(Hook{Events: []string{EventIssue}}, ev); !ok { t.Fatalf("issue dekt %s niet", want) } for _, other := range SpecificEvents() { if other == want { continue } if ok, _ := Allows(Hook{Events: []string{other}}, ev); ok { t.Fatalf("%s krijgt %s", other, want) } } } mr := Event{Type: EventMergeRequest, Project: "tim/app", Actor: "ada", Comment: "lgtm", MergeRequest: &MRInfo{ID: 3, Title: "M", Status: "open", Action: "commented", URL: "https://git.example/tim/app/-/merge-requests/3"}} if ok, _ := Allows(Hook{Events: []string{EventMRComment}}, mr); !ok { t.Fatal("mr comment") } opened := mr info := *mr.MergeRequest info.Action = "opened" opened.MergeRequest = &info if ok, _ := Allows(Hook{Events: []string{EventMRComment}}, opened); ok { t.Fatal("geopende MR is geen comment") } raw, _ := json.Marshal(mr.Document()) var d map[string]any _ = json.Unmarshal(raw, &d) note, _ := d["note"].(map[string]any) if d["event_name"] != EventMRComment || note["body"] != "lgtm" || note["noteable_type"] != "MergeRequest" || note["url"] == "" { t.Fatalf("payload %s", raw) } m, _ := d["merge_request"].(map[string]any) if m["number"].(float64) != 3 { t.Fatalf("number %s", raw) } raw, _ = json.Marshal(iss("closed").Document()) _ = json.Unmarshal(raw, &d) if d["event_name"] != EventIssueClosed { t.Fatalf("issue %s", raw) } if s, ok := eventField(iss("commented"), "event_name"); !ok || s != EventIssueComment { t.Fatal(s) } }