package e2e import ( "net/http" "net/url" "strings" "testing" "github.com/PuerkitoBio/goquery" ) // labelsPatch is a minimal but complete format-patch file. func labelsPatch(file string) string { return strings.Join([]string{ "From a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2 Mon Sep 17 00:00:00 2001", "From: Test User ", "Date: Mon, 01 Jan 2024 12:00:00 +0000", "Subject: [PATCH] Add " + file, "", "---", "diff --git a/" + file + " b/" + file, "new file mode 100644", "index 0000000..9daeafb", "--- /dev/null", "+++ b/" + file, "@@ -0,0 +1 @@", "+x", "", }, "\n") } // labelsCreate posts the label form on the repo settings page. func labelsCreate(s *session, repo, name, color string) *response { return s.post("/"+repo+"/settings/labels", url.Values{"name": {name}, "color": {color}}) } // labelsSettingsID returns the id of the named label, read from its delete // form on the settings page. func labelsSettingsID(t *testing.T, s *session, repo, name string) string { t.Helper() id := "" s.get("/" + repo + "/settings").Find(".label-settings-item").Each(func(_ int, sel *goquery.Selection) { if id == "" && strings.TrimSpace(sel.Find(".label-settings-name").Text()) == name { id, _ = sel.Find("input[name=id]").Attr("value") } }) if id == "" { t.Fatalf("label %q not found in settings", name) } return id } // labelsFilterID returns the label id of the named filter checkbox on a list // page such as /repo/issues. func labelsFilterID(t *testing.T, s *session, path, name string) string { t.Helper() id := "" s.get(path).Find(".label-filter-item").Each(func(_ int, sel *goquery.Selection) { if id == "" && strings.Contains(sel.Text(), name) { id, _ = sel.Find("input[name=labels]").Attr("value") } }) if id == "" { t.Fatalf("label %q not found in filter popup of %s", name, path) } return id } // labelsItemWith returns the list item whose text contains want. func labelsItemWith(t *testing.T, r *response, want string) *goquery.Selection { t.Helper() var found *goquery.Selection r.Find(".issue-item").Each(func(_ int, s *goquery.Selection) { if found == nil && strings.Contains(s.Text(), want) { found = s } }) if found == nil { t.Fatalf("no list item containing %q", want) } return found } // labelsTexts returns the trimmed text of every match inside a selection. func labelsTexts(sel *goquery.Selection, css string) []string { var out []string sel.Find(css).Each(func(_ int, s *goquery.Selection) { out = append(out, strings.TrimSpace(s.Text())) }) return out } func labelsHas(list []string, want string) bool { for _, s := range list { if s == want { return true } } return false } func TestLabels(t *testing.T) { e := newEnv(t) e.register("alice", "password123") admin := e.admin() e.createRepo(admin, "label-repo") issuePath := issuesCreate(admin, "label-repo", "Labelled issue", "") patchPath := admin.postMultipart("/label-repo/patches", url.Values{"title": {"Labelled patch"}}, file{Field: "patch_file", Name: "label-test.patch", Content: []byte(labelsPatch("label-test.txt"))}, ).mustRedirect("/label-repo/patches/") t.Run("create label in repo settings", func(t *testing.T) { r := admin.follow(labelsCreate(admin, "label-repo", "bug", "#ff0000")) if !contains(r.Texts(".label-settings-name"), "bug") { t.Error("label not listed") } }) t.Run("create a second label", func(t *testing.T) { r := admin.follow(labelsCreate(admin, "label-repo", "enhancement", "#00aa00")) names := r.Texts(".label-settings-name") if !labelsHas(names, "bug") || !labelsHas(names, "enhancement") { t.Errorf("labels = %q", names) } }) t.Run("duplicate label name is rejected", func(t *testing.T) { if !admin.follow(labelsCreate(admin, "label-repo", "bug", "#0000ff")).Has(".form-error") { t.Error("no error message") } }) t.Run("non-admin cannot create labels", func(t *testing.T) { r := e.anon().post("/label-repo/settings/labels", url.Values{"name": {"nope"}, "color": {"#123456"}}) r.mustStatus(http.StatusFound) if !strings.Contains(r.Location(), "/login") { t.Errorf("location = %q", r.Location()) } }) t.Run("assign label to issue", func(t *testing.T) { bug := labelsSettingsID(t, admin, "label-repo", "bug") admin.post(issuePath+"/labels/add", url.Values{"label_id": {bug}}).mustRedirect(issuePath) if !labelsHas(admin.get(issuePath).Texts(".label-badge"), "bug") { t.Error("label badge missing") } }) t.Run("label appears on issue list", func(t *testing.T) { item := labelsItemWith(t, admin.get("/label-repo/issues"), "Labelled issue") if !labelsHas(labelsTexts(item, ".label-badge"), "bug") { t.Error("label badge missing on list") } }) t.Run("filter issues by label shows only matching issues", func(t *testing.T) { issuesCreate(admin, "label-repo", "Unlabelled issue", "") id := labelsFilterID(t, admin, "/label-repo/issues", "bug") titles := admin.get("/label-repo/issues?labels=" + id).Texts(".issue-title") if !contains(titles, "Labelled issue") || contains(titles, "Unlabelled issue") { t.Errorf("titles = %q", titles) } }) t.Run("filter popup is visible without JS", func(t *testing.T) { r := admin.get("/label-repo/issues") if !r.Has("details.label-filter") || !r.Has("details.label-filter summary") { t.Error("filter popup not rendered") } }) t.Run("remove label from issue", func(t *testing.T) { id := admin.get(issuePath).Attr(".issue-labels-row .label-remove-form input[name=label_id]", "value") admin.post(issuePath+"/labels/remove", url.Values{"label_id": {id}}).mustRedirect(issuePath) if labelsHas(admin.get(issuePath).Texts(".issue-labels-row .label-badge"), "bug") { t.Error("label still attached") } }) t.Run("assign label to patch", func(t *testing.T) { enh := labelsSettingsID(t, admin, "label-repo", "enhancement") admin.post(patchPath+"/labels/add", url.Values{"label_id": {enh}}).mustRedirect(patchPath) if !labelsHas(admin.get(patchPath).Texts(".label-badge"), "enhancement") { t.Error("label badge missing") } }) t.Run("label appears on patch list", func(t *testing.T) { item := labelsItemWith(t, admin.get("/label-repo/patches"), "Labelled patch") if !labelsHas(labelsTexts(item, ".label-badge"), "enhancement") { t.Error("label badge missing on list") } }) t.Run("filter patches by label", func(t *testing.T) { id := labelsFilterID(t, admin, "/label-repo/patches", "enhancement") titles := admin.get("/label-repo/patches?labels=" + id).Texts(".issue-title") if !contains(titles, "Labelled patch") { t.Errorf("titles = %q", titles) } }) t.Run("remove label from patch", func(t *testing.T) { id := admin.get(patchPath).Attr(".issue-labels-row .label-remove-form input[name=label_id]", "value") admin.post(patchPath+"/labels/remove", url.Values{"label_id": {id}}).mustRedirect(patchPath) if labelsHas(admin.get(patchPath).Texts(".issue-labels-row .label-badge"), "enhancement") { t.Error("label still attached") } }) t.Run("delete label removes it from settings list", func(t *testing.T) { bug := labelsSettingsID(t, admin, "label-repo", "bug") r := admin.follow(admin.post("/label-repo/settings/labels/delete", url.Values{"id": {bug}})) if labelsHas(r.Texts(".label-settings-name"), "bug") { t.Error("label still listed") } }) t.Run("deleted label no longer appears in filter popup", func(t *testing.T) { if contains(admin.get("/label-repo/issues").Texts(".label-filter-item"), "bug") { t.Error("deleted label still in filter popup") } }) } func TestUserLabelManagement(t *testing.T) { e := newEnv(t) alice := e.register("alice", "password123") admin := e.admin() e.createRepo(admin, "ulm-repo") for _, name := range []string{"bug", "feature"} { labelsCreate(admin, "ulm-repo", name, "#808080").mustRedirect("/ulm-repo/settings") } adminIssuePath := issuesCreate(admin, "ulm-repo", "Admin's issue", "") aliceIssuePath := issuesCreate(alice, "ulm-repo", "Alice's issue", "") bugLabelID := labelsFilterID(t, admin, "/ulm-repo/issues", "bug") checked := "input[name=allow_user_labels][checked]" t.Run("allow_user_labels checkbox is present in repo settings", func(t *testing.T) { if n := admin.get("/ulm-repo/settings").Count("input[name=allow_user_labels]"); n != 1 { t.Errorf("checkbox count = %d", n) } }) t.Run("allow_user_labels is off by default", func(t *testing.T) { if admin.get("/ulm-repo/settings").Has(checked) { t.Error("checkbox is checked") } }) t.Run("label checkboxes not shown to non-admin on new issue form when allow_user_labels is off", func(t *testing.T) { if n := alice.get("/ulm-repo/issues/new").Count(".label-checkbox-list"); n != 0 { t.Errorf("label lists = %d", n) } }) t.Run("label checkboxes not shown to non-admin on new patch form when allow_user_labels is off", func(t *testing.T) { if n := alice.get("/ulm-repo/patches/new").Count(".label-checkbox-list"); n != 0 { t.Errorf("label lists = %d", n) } }) t.Run("label checkboxes shown to admin on new issue form regardless of setting", func(t *testing.T) { if !admin.get("/ulm-repo/issues/new").Has(".label-checkbox-list") { t.Error("label list missing") } }) t.Run("admin can enable allow_user_labels", func(t *testing.T) { issuesSaveSettings(admin, "ulm-repo", url.Values{"allow_user_labels": {"1"}}). mustRedirect("/ulm-repo/settings") if !admin.get("/ulm-repo/settings").Has(checked) { t.Error("setting did not persist") } }) t.Run("label checkboxes shown to non-admin on new issue form when allow_user_labels is on", func(t *testing.T) { r := alice.get("/ulm-repo/issues/new") if !r.Has(".label-checkbox-list") { t.Fatal("label list missing") } labels := r.Texts(".label-checkbox-list .label-badge") if !labelsHas(labels, "bug") || !labelsHas(labels, "feature") { t.Errorf("labels = %q", labels) } }) t.Run("label checkboxes shown to non-admin on new patch form when allow_user_labels is on", func(t *testing.T) { if !alice.get("/ulm-repo/patches/new").Has(".label-checkbox-list") { t.Error("label list missing") } }) t.Run("non-admin can create issue with label selected", func(t *testing.T) { p := alice.post("/ulm-repo/issues", url.Values{ "title": {"Issue with label"}, "label_ids": {bugLabelID}, }).mustRedirect("/ulm-repo/issues/") if !labelsHas(alice.get(p).Texts(".label-badge"), "bug") { t.Error("label not applied") } }) t.Run("non-admin can create patch with label selected", func(t *testing.T) { featureID := labelsFilterID(t, admin, "/ulm-repo/issues", "feature") p := alice.postMultipart("/ulm-repo/patches", url.Values{"title": {"Patch with label"}, "label_ids": {featureID}}, file{Field: "patch_file", Name: "ulm-test.patch", Content: []byte(labelsPatch("ulm-test.txt"))}, ).mustRedirect("/ulm-repo/patches/") if !labelsHas(alice.get(p).Texts(".label-badge"), "feature") { t.Error("label not applied") } }) t.Run("label_ids in POST are ignored for non-admin when allow_user_labels is off (no label applied)", func(t *testing.T) { // Post the settings form without allow_user_labels to switch it off. admin.post("/ulm-repo/settings", url.Values{"description": {""}, "default_branch": {"main"}}) p := alice.post("/ulm-repo/issues", url.Values{ "title": {"Issue sneaking labels"}, "label_ids": {bugLabelID}, }).mustRedirect("/ulm-repo/issues/") if labelsHas(alice.get(p).Texts(".label-badge"), "bug") { t.Error("label was applied") } admin.post("/ulm-repo/settings", url.Values{ "description": {""}, "default_branch": {"main"}, "allow_user_labels": {"1"}, }) }) t.Run("non-admin can add label to their own issue", func(t *testing.T) { alice.post(aliceIssuePath+"/labels/add", url.Values{"label_id": {bugLabelID}}). mustRedirect(aliceIssuePath) if !labelsHas(alice.get(aliceIssuePath).Texts(".label-badge"), "bug") { t.Error("label not applied") } }) t.Run("non-admin can remove label from their own issue", func(t *testing.T) { alice.post(aliceIssuePath+"/labels/remove", url.Values{"label_id": {bugLabelID}}). mustRedirect(aliceIssuePath) if labelsHas(alice.get(aliceIssuePath).Texts(".issue-labels-row .label-badge"), "bug") { t.Error("label still attached") } }) t.Run("non-admin cannot add label to another user's issue", func(t *testing.T) { alice.post(adminIssuePath+"/labels/add", url.Values{"label_id": {bugLabelID}}). mustStatus(http.StatusForbidden) }) t.Run("unauthenticated user gets 401 adding a label", func(t *testing.T) { e.anon().post(aliceIssuePath+"/labels/add", url.Values{"label_id": {bugLabelID}}). mustStatus(http.StatusUnauthorized) }) t.Run("admin can disable allow_user_labels", func(t *testing.T) { issuesSaveSettings(admin, "ulm-repo", url.Values{"allow_user_labels": nil}). mustRedirect("/ulm-repo/settings") if admin.get("/ulm-repo/settings").Has(checked) { t.Error("setting still on") } }) t.Run("non-admin gets 403 adding label to own issue when allow_user_labels is off", func(t *testing.T) { alice.post(aliceIssuePath+"/labels/add", url.Values{"label_id": {bugLabelID}}). mustStatus(http.StatusForbidden) }) t.Run("label checkboxes hidden on new issue form after allow_user_labels disabled", func(t *testing.T) { if n := alice.get("/ulm-repo/issues/new").Count(".label-checkbox-list"); n != 0 { t.Errorf("label lists = %d", n) } }) }