e2e/notify_test.go
165 lines · 5851 bytes
1package e2e
2
3import (
4 "fmt"
5 "net"
6 "os"
7 "os/exec"
8 "strings"
9 "testing"
10 "time"
11)
12
13// mailTo returns captured messages addressed to one recipient.
14func (f *fakeSMTP) mailTo(recipient string) []string {
15 f.mu.Lock()
16 defer f.mu.Unlock()
17 var out []string
18 for _, m := range f.mail {
19 if strings.Contains(m, "To: "+recipient) {
20 out = append(out, m)
21 }
22 }
23 return out
24}
25
26func (f *fakeSMTP) waitFor(t *testing.T, recipient, substr string) string {
27 t.Helper()
28 deadline := time.Now().Add(15 * time.Second)
29 for time.Now().Before(deadline) {
30 for _, m := range f.mailTo(recipient) {
31 if strings.Contains(m, substr) {
32 return m
33 }
34 }
35 time.Sleep(100 * time.Millisecond)
36 }
37 t.Fatalf("no mail to %s containing %q", recipient, substr)
38 return ""
39}
40
41func TestActivityNotifications(t *testing.T) {
42 smtp := startFakeSMTP(t)
43 inst := startInstanceWith(t, fmt.Sprintf(
44 "[mail]\nsmtp_host = %q\nfrom = \"noreply@gitbay.test\"\n", smtp.addr))
45 // Fast retries for the mailer loop.
46 inst.proc.Process.Kill()
47 inst.proc.Wait()
48 inst.proc = exec.Command(inst.gitbayd, "--config", inst.config, "serve")
49 inst.proc.Env = append(os.Environ(), "GITBAY_WEBHOOK_RETRY_BASE=500ms")
50 inst.proc.Stderr = os.Stderr
51 if err := inst.proc.Start(); err != nil {
52 t.Fatal(err)
53 }
54 t.Cleanup(func() { inst.proc.Process.Kill(); inst.proc.Wait() })
55 deadline := time.Now().Add(10 * time.Second)
56 for {
57 conn, err := net.DialTimeout("tcp", fmt.Sprintf("127.0.0.1:%d", inst.port), 200*time.Millisecond)
58 if err == nil {
59 conn.Close()
60 break
61 }
62 if time.Now().After(deadline) {
63 t.Fatal("daemon did not restart")
64 }
65 time.Sleep(50 * time.Millisecond)
66 }
67
68 aliceKey := inst.newKey(t, "alice")
69 bobKey := inst.newKey(t, "bob")
70 eveKey := inst.newKey(t, "eve")
71 inst.admin(t, "admin", "user", "create", "alice",
72 "--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
73 inst.admin(t, "admin", "user", "create", "bob",
74 "--key", bobKey+".pub", "--email", "bob@example.test", "--verified")
75 // eve has NO verified email: she must be skipped silently.
76 inst.admin(t, "admin", "user", "create", "eve",
77 "--key", eveKey+".pub", "--email", "eve@example.test")
78
79 if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
80 t.Fatalf("repo create: %s", errOut)
81 }
82
83 // Bob opens an issue: the repo owner (alice) is notified with subject,
84 // excerpt, and link; bob (the actor) is not.
85 if _, errOut, code := inst.ssh(t, bobKey, "",
86 "issue", "create", "alice/app", "--title", "'it leaks'", "--body", "'memory climbs forever'"); code != 0 {
87 t.Fatalf("issue create: %s", errOut)
88 }
89 m := smtp.waitFor(t, "alice@example.test", "#1")
90 if !strings.Contains(m, "[alice/app] #1: it leaks") ||
91 !strings.Contains(m, "bob opened issue #1") ||
92 !strings.Contains(m, "memory climbs forever") ||
93 !strings.Contains(m, "/alice/app/issues/1") {
94 t.Fatalf("issue-open mail:\n%s", m)
95 }
96 if n := len(smtp.mailTo("bob@example.test")); n != 0 {
97 t.Fatalf("actor notified about own action (%d mails)", n)
98 }
99
100 // Alice comments: bob (author/participant) is notified; alice is not.
101 if _, _, code := inst.ssh(t, aliceKey, "", "issue", "comment", "alice/app", "1", "--message", "'on it'"); code != 0 {
102 t.Fatal("comment failed")
103 }
104 m = smtp.waitFor(t, "bob@example.test", "commented")
105 if !strings.Contains(m, "alice commented on #1") || !strings.Contains(m, "on it") {
106 t.Fatalf("comment mail:\n%s", m)
107 }
108
109 // Eve comments (unverified email, still allowed to act): both alice
110 // and bob get mail; eve never receives any.
111 if _, _, code := inst.ssh(t, eveKey, "", "issue", "comment", "alice/app", "1", "--message", "'same here'"); code != 0 {
112 t.Fatal("eve comment failed")
113 }
114 smtp.waitFor(t, "alice@example.test", "same here")
115 smtp.waitFor(t, "bob@example.test", "same here")
116
117 // Close notifies participants.
118 if _, _, code := inst.ssh(t, aliceKey, "", "issue", "close", "alice/app", "1"); code != 0 {
119 t.Fatal("close failed")
120 }
121 m = smtp.waitFor(t, "bob@example.test", "closed #1")
122 if !strings.Contains(m, "alice closed #1") {
123 t.Fatalf("close mail:\n%s", m)
124 }
125 if n := len(smtp.mailTo("eve@example.test")); n != 0 {
126 t.Fatalf("unverified recipient got mail (%d)", n)
127 }
128
129 // MR flow: bob opens (alice notified), alice reviews (bob notified),
130 // alice merges (bob notified).
131 if _, _, code := inst.ssh(t, aliceKey, "", "repo", "access", "grant", "alice/app", "bob", "write"); code != 0 {
132 t.Fatal("grant failed")
133 }
134 work := t.TempDir()
135 bobEnv := inst.gitEnv(bobKey)
136 mustGit(t, work, bobEnv, "clone", inst.sshURL("alice/app"), "w")
137 dir := work + "/w"
138 os.WriteFile(dir+"/f.txt", []byte("x\n"), 0o644)
139 mustGit(t, dir, bobEnv, "checkout", "-q", "-b", "main")
140 mustGit(t, dir, bobEnv, "add", ".")
141 mustGit(t, dir, bobEnv, "commit", "-q", "-m", "base")
142 mustGit(t, dir, bobEnv, "push", "-q", "origin", "main")
143 mustGit(t, dir, bobEnv, "checkout", "-q", "-b", "feat")
144 mustGit(t, dir, bobEnv, "commit", "-q", "--allow-empty", "-m", "work")
145 mustGit(t, dir, bobEnv, "push", "-q", "origin", "feat")
146 if _, errOut, code := inst.ssh(t, bobKey, "", "mr", "create", "alice/app",
147 "--source", "feat", "--target", "main", "--title", "'ship it'"); code != 0 {
148 t.Fatalf("mr create: %s", errOut)
149 }
150 m = smtp.waitFor(t, "alice@example.test", "!1")
151 if !strings.Contains(m, "bob opened merge request !1") {
152 t.Fatalf("mr-open mail:\n%s", m)
153 }
154 if _, _, code := inst.ssh(t, aliceKey, "", "mr", "review", "alice/app", "1", "--approve"); code != 0 {
155 t.Fatal("review failed")
156 }
157 smtp.waitFor(t, "bob@example.test", "reviewed !1: approve")
158 if _, errOut, code := inst.ssh(t, aliceKey, "", "mr", "merge", "alice/app", "1"); code != 0 {
159 t.Fatalf("merge: %s", errOut)
160 }
161 m = smtp.waitFor(t, "bob@example.test", "merged !1")
162 if !strings.Contains(m, "alice merged !1 into main") {
163 t.Fatalf("merge mail:\n%s", m)
164 }
165}