e2e/labelweb_test.go
85 lines · 3468 bytes
1package e2e
2
3import (
4 "net/url"
5 "strings"
6 "testing"
7)
8
9// The label set itself is managed from the browser: create, recolour and
10// remove, each through the label command the CLI runs (#163).
11func TestLabelsWeb(t *testing.T) {
12 inst := startInstanceWith(t, "[web]\nmode = \"accounts\"\n")
13 aliceKey := inst.newKey(t, "alice")
14 bobKey := inst.newKey(t, "bob")
15 inst.admin(t, "admin", "user", "create", "alice", "--key", aliceKey+".pub")
16 inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
17 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
18 t.Fatalf("repo create: %s", errOut)
19 }
20 inst.ssh(t, aliceKey, "", "issue", "create", "alice/app", "--title", "one")
21 inst.ssh(t, aliceKey, "", "issue", "label", "alice/app", "1", "--add", "bug")
22
23 alice := inst.login(t, aliceKey)
24 base := inst.base() + "/alice/app"
25
26 // The existing label is listed with its use count.
27 status, page := browserGet(t, alice, base+"/labels")
28 if status != 200 || !strings.Contains(page, "bug") {
29 t.Fatalf("labels page: %d\n%s", status, page)
30 }
31
32 // Creating one with a colour, and recolouring the existing one.
33 if status, _ := browserPost(t, alice, base+"/labels", url.Values{
34 "name": {"docs"}, "color": {"1f6feb"}}); status != 200 {
35 t.Fatal("label create failed")
36 }
37 if status, _ := browserPost(t, alice, base+"/labels", url.Values{
38 "name": {"bug"}, "color": {"cf222e"}}); status != 200 {
39 t.Fatal("label recolour failed")
40 }
41 out, _, _ := inst.ssh(t, aliceKey, "", "label", "list", "alice/app", "--json")
42 if !strings.Contains(out, `{"name":"bug","color":"#cf222e","issues":1,"mrs":0}`) ||
43 !strings.Contains(out, `{"name":"docs","color":"#1f6feb","issues":0,"mrs":0}`) {
44 t.Fatalf("labels not as posted:\n%s", out)
45 }
46
47 // A bad colour comes back as an error on the page, not a silent no-op.
48 _, body := browserPost(t, alice, base+"/labels", url.Values{
49 "name": {"docs"}, "color": {"red"}})
50 if !strings.Contains(body, `class="error"`) {
51 t.Errorf("bad colour accepted:\n%s", body)
52 }
53
54 // Removing a label needs its name typed; a bare post is refused and
55 // the label stays.
56 _, body = browserPost(t, alice, base+"/labels", url.Values{
57 "action": {"remove"}, "name": {"bug"}})
58 if !strings.Contains(body, "type bug to confirm") {
59 t.Fatalf("unconfirmed remove was not refused:\n%s", body)
60 }
61 if out, _, _ := inst.ssh(t, aliceKey, "", "label", "list", "alice/app", "--json"); !strings.Contains(out, `"name":"bug"`) {
62 t.Fatalf("label removed without confirmation: %s", out)
63 }
64 if status, _ := browserPost(t, alice, base+"/labels", url.Values{
65 "action": {"remove"}, "name": {"bug"}, "confirm": {"bug"}}); status != 200 {
66 t.Fatal("label remove failed")
67 }
68 if out, _, _ := inst.ssh(t, aliceKey, "", "issue", "show", "alice/app", "1", "--json"); strings.Contains(out, `"bug"`) {
69 t.Fatalf("issue still carries the removed label:\n%s", out)
70 }
71
72 // A reader sees the set and none of the controls.
73 bob := inst.login(t, bobKey)
74 _, p := browserGet(t, bob, base+"/labels")
75 if !strings.Contains(p, "docs") {
76 t.Fatalf("reader cannot see the labels:\n%s", p)
77 }
78 if strings.Contains(p, "New label") || strings.Contains(p, "Remove") {
79 t.Fatal("reader sees a label control")
80 }
81 browserPost(t, bob, base+"/labels", url.Values{"name": {"sneak"}})
82 if out, _, _ := inst.ssh(t, aliceKey, "", "label", "list", "alice/app", "--json"); strings.Contains(out, "sneak") {
83 t.Fatal("reader created a label")
84 }
85}