e2e/mentions_test.go

ff759b53942049b2043e132f5482b442a7265b98
gitbay/e2e/mentions_test.go history · blame · raw

93 lines · 3377 bytes

 1package e2e
 2
 3import (
 4	"strings"
 5	"testing"
 6)
 7
 8// An @mention files an inbox row for the mentioned account and makes them
 9// a participant of the thread; watchers are not told about the mention,
10// mute holds, and someone who cannot read the repository is not reached
11// (#202).
12func TestMentionsNotify(t *testing.T) {
13	t.Parallel()
14	inst := startInstance(t)
15	keys := map[string]string{}
16	for _, u := range []string{"alice", "bob", "carol", "eve"} {
17		keys[u] = inst.newKey(t, u)
18		inst.admin(t, "admin", "user", "create", u, "--key", keys[u]+".pub")
19	}
20	if _, errOut, code := inst.ssh(t, keys["alice"], "", "repo", "create", "alice/app"); code != 0 {
21		t.Fatalf("repo create: %s", errOut)
22	}
23	if _, _, code := inst.ssh(t, keys["carol"], "", "repo", "watch", "alice/app"); code != 0 {
24		t.Fatal("watch failed")
25	}
26	summaries := func(key string) []string {
27		var out []string
28		for _, n := range notices(t, inst, key) {
29			out = append(out, n.Summary)
30		}
31		return out
32	}
33	has := func(list []string, want string) bool {
34		for _, s := range list {
35			if strings.Contains(s, want) {
36				return true
37			}
38		}
39		return false
40	}
41
42	// A mention in an issue body, with prose punctuation after the name.
43	if _, errOut, code := inst.ssh(t, keys["alice"], "", "issue", "create", "alice/app",
44		"--title", "'leak'", "--body", "'@bob, can you look? cc @nobody and @alice.'"); code != 0 {
45		t.Fatalf("issue create: %s", errOut)
46	}
47	if got := summaries(keys["bob"]); !has(got, "mentioned you in #1") {
48		t.Fatalf("bob not told about the mention: %v", got)
49	}
50	if got := summaries(keys["carol"]); has(got, "mentioned") || !has(got, "opened issue #1") {
51		t.Fatalf("watcher inbox: %v", got)
52	}
53	if got := summaries(keys["alice"]); has(got, "mentioned") {
54		t.Fatalf("self-mention filed: %v", got)
55	}
56
57	// bob is now a participant: a later comment reaches him.
58	if _, errOut, code := inst.ssh(t, keys["alice"], "", "issue", "comment", "alice/app", "1", "--message", "'more'"); code != 0 {
59		t.Fatalf("comment: %s", errOut)
60	}
61	if got := summaries(keys["bob"]); !has(got, "commented on #1") {
62		t.Fatalf("mentioned account not a participant: %v", got)
63	}
64
65	// Muted, a mention does not get through.
66	if _, _, code := inst.ssh(t, keys["bob"], "", "repo", "mute", "alice/app"); code != 0 {
67		t.Fatal("mute failed")
68	}
69	if _, errOut, code := inst.ssh(t, keys["alice"], "", "issue", "comment", "alice/app", "1", "--message", "'@bob again'"); code != 0 {
70		t.Fatalf("comment: %s", errOut)
71	}
72	if got := summaries(keys["bob"]); has(got, "mentioned you in #1") && len(got) > 2 {
73		t.Fatalf("mention reached a muted account: %v", got)
74	}
75
76	// A private repository: a mention of someone who cannot read it is
77	// dropped, one of someone who can is filed. Merge request bodies too.
78	for _, args := range [][]string{
79		{"repo", "create", "alice/secret", "--private"},
80		{"repo", "access", "grant", "alice/secret", "carol", "read"},
81		{"issue", "create", "alice/secret", "--title", "'hush'", "--body", "'@eve @carol'"},
82	} {
83		if _, errOut, code := inst.ssh(t, keys["alice"], "", args...); code != 0 {
84			t.Fatalf("%v: %s", args, errOut)
85		}
86	}
87	if got := summaries(keys["eve"]); len(got) != 0 {
88		t.Fatalf("outsider reached through a private repository: %v", got)
89	}
90	if got := summaries(keys["carol"]); !has(got, "mentioned you in #1") {
91		t.Fatalf("reader of a private repository not told: %v", got)
92	}
93}