e2e/mailpref_test.go

0338e6ace3de199d5fc383852649919b68ef3e42
gitbay/e2e/mailpref_test.go history · blame · raw

70 lines · 2985 bytes

 1package e2e
 2
 3import (
 4	"fmt"
 5	"net/url"
 6	"strings"
 7	"testing"
 8	"time"
 9)
10
11// notifications settings mail off keeps the inbox and stops activity
12// mail; on brings it back. The account page carries the same switch
13// (#194).
14func TestMailPreference(t *testing.T) {
15	t.Parallel()
16	smtp := startFakeSMTP(t)
17	inst := startInstanceWith(t, fmt.Sprintf(
18		"[mail]\nsmtp_host = %q\nfrom = \"noreply@gitbay.test\"\n[web]\nmode = \"accounts\"\n", smtp.addr))
19	aliceKey := inst.newKey(t, "alice")
20	bobKey := inst.newKey(t, "bob")
21	inst.admin(t, "admin", "user", "create", "alice",
22		"--key", aliceKey+".pub", "--email", "alice@example.test", "--verified")
23	inst.admin(t, "admin", "user", "create", "bob", "--key", bobKey+".pub")
24	if _, errOut, code := inst.ssh(t, aliceKey, "", "repo", "create", "alice/app"); code != 0 {
25		t.Fatalf("repo create: %s", errOut)
26	}
27
28	out, _, code := inst.ssh(t, aliceKey, "", "notifications", "settings", "show", "--json")
29	if code != 0 || !strings.Contains(out, `"mail":true`) {
30		t.Fatalf("settings show: %d %s", code, out)
31	}
32	if _, _, code := inst.ssh(t, aliceKey, "", "notifications", "settings", "mail", "sometimes"); code != 2 {
33		t.Fatalf("bad value accepted: %d", code)
34	}
35	if out, _, code := inst.ssh(t, aliceKey, "", "notifications", "settings", "mail", "off", "--json"); code != 0 || !strings.Contains(out, `"mail":false`) {
36		t.Fatalf("mail off: %d %s", code, out)
37	}
38
39	// Mail off: the inbox row is filed, no mail goes out.
40	if _, errOut, code := inst.ssh(t, bobKey, "", "issue", "create", "alice/app", "--title", "'leak'"); code != 0 {
41		t.Fatalf("issue create: %s", errOut)
42	}
43	rows := notices(t, inst, aliceKey)
44	if len(rows) != 1 || !strings.Contains(rows[0].Summary, "opened issue #1") {
45		t.Fatalf("inbox with mail off: %+v", rows)
46	}
47	time.Sleep(5 * time.Second) // the mailer ticks every two seconds
48	if got := smtp.mailTo("alice@example.test"); len(got) != 0 {
49		t.Fatalf("mail sent with the preference off:\n%s", got[0])
50	}
51
52	// The account page shows it off and turns it back on.
53	alice := inst.login(t, aliceKey)
54	set := inst.base() + "/settings"
55	if _, body := browserGet(t, alice, set); !strings.Contains(body, `name="mail" value="on">`) || strings.Contains(body, `name="mail" value="on" checked`) {
56		t.Fatalf("account page does not show mail off:\n%s", body)
57	}
58	if status, _ := browserPost(t, alice, set, url.Values{"field": {"notify-mail"}, "mail": {"on"}}); status != 200 {
59		t.Fatalf("settings post: %d", status)
60	}
61	if out, _, _ := inst.ssh(t, aliceKey, "", "notifications", "settings", "show", "--json"); !strings.Contains(out, `"mail":true`) {
62		t.Fatalf("web toggle did not turn mail on: %s", out)
63	}
64	if _, errOut, code := inst.ssh(t, bobKey, "", "issue", "create", "alice/app", "--title", "'still leaking'"); code != 0 {
65		t.Fatalf("second issue: %s", errOut)
66	}
67	if m := smtp.waitFor(t, "alice@example.test", "still leaking"); !strings.Contains(m, "bob opened issue #2") {
68		t.Fatalf("mail after turning it on:\n%s", m)
69	}
70}