e2e/notify_test.go

e6cd75b5f28bacf51620bb531320c30fd4e66bfd
gitbay/e2e/notify_test.go history · blame · raw

154 lines · 5571 bytes

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