e2e/mailpref_test.go

3c0c20da787d91bcad19aec3b5e1b5c4ac5a4745
gitbay/e2e/mailpref_test.go history · blame · raw

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