e2e/signupnotice_test.go

v1.31.0
gitbay/e2e/signupnotice_test.go history · blame · raw

68 lines · 2739 bytes

 1package e2e
 2
 3import (
 4	"fmt"
 5	"strings"
 6	"testing"
 7	"time"
 8)
 9
10// registration.notify_admin mails the instance's admins when an account
11// becomes active. An open-mode signup counts at verification, not when
12// the row is created, so an unverified attempt is silent (#234).
13func TestSignupNotifiesAdmins(t *testing.T) {
14	smtp := startFakeSMTP(t)
15	inst := startInstanceWith(t, fmt.Sprintf(
16		"[registration]\nmode = \"open\"\nnotify_admin = true\n"+
17			"[mail]\nsmtp_host = %q\nfrom = \"noreply@gitbay.test\"\n", smtp.addr))
18	rootKey := inst.newKey(t, "root")
19	inst.admin(t, "admin", "user", "create", "root", "--key", rootKey+".pub",
20		"--admin", "--email", "root@example.test", "--verified")
21	// A second admin with no verified address is skipped, not an error.
22	inst.admin(t, "admin", "user", "create", "quiet", "--key", inst.newKey(t, "quiet")+".pub", "--admin")
23
24	danaKey := inst.newKey(t, "dana")
25	if _, errOut, code := inst.ssh(t, danaKey, "", "register",
26		"--username", "dana", "--email", "dana@example.test"); code != 0 {
27		t.Fatalf("register: %s", errOut)
28	}
29	// Pending: the admin has heard nothing yet.
30	time.Sleep(5 * time.Second) // the mailer ticks every two seconds
31	if got := smtp.mailTo("root@example.test"); len(got) != 0 {
32		t.Fatalf("admin mailed before the account was verified:\n%s", got[0])
33	}
34
35	code := extractCode(t, smtp.waitFor(t, "dana@example.test", "email verify"))
36	if _, errOut, ec := inst.ssh(t, danaKey, "", "email", "verify", code); ec != 0 {
37		t.Fatalf("verify: %s", errOut)
38	}
39	msg := smtp.waitFor(t, "root@example.test", "new account")
40	if !strings.Contains(msg, "dana") || !strings.Contains(msg, "open registration") {
41		t.Fatalf("notice body:\n%s", msg)
42	}
43}
44
45// With notify_admin off, the default, the same signup mails no one but
46// the person registering.
47func TestSignupNoticeOffByDefault(t *testing.T) {
48	smtp := startFakeSMTP(t)
49	inst := startInstanceWith(t, fmt.Sprintf(
50		"[registration]\nmode = \"open\"\n[mail]\nsmtp_host = %q\nfrom = \"noreply@gitbay.test\"\n", smtp.addr))
51	rootKey := inst.newKey(t, "root")
52	inst.admin(t, "admin", "user", "create", "root", "--key", rootKey+".pub",
53		"--admin", "--email", "root@example.test", "--verified")
54
55	eveKey := inst.newKey(t, "eve")
56	if _, errOut, ec := inst.ssh(t, eveKey, "", "register",
57		"--username", "eve", "--email", "eve@example.test"); ec != 0 {
58		t.Fatalf("register: %s", errOut)
59	}
60	code := extractCode(t, smtp.waitFor(t, "eve@example.test", "email verify"))
61	if _, errOut, ec := inst.ssh(t, eveKey, "", "email", "verify", code); ec != 0 {
62		t.Fatalf("verify: %s", errOut)
63	}
64	time.Sleep(5 * time.Second)
65	if got := smtp.mailTo("root@example.test"); len(got) != 0 {
66		t.Fatalf("admin mailed with notify_admin off:\n%s", got[0])
67	}
68}