e2e/signupnotice_test.go
70 lines · 2767 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 t.Parallel()
15 smtp := startFakeSMTP(t)
16 inst := startInstanceWith(t, fmt.Sprintf(
17 "[registration]\nmode = \"open\"\nnotify_admin = true\n"+
18 "[mail]\nsmtp_host = %q\nfrom = \"noreply@gitbay.test\"\n", smtp.addr))
19 rootKey := inst.newKey(t, "root")
20 inst.admin(t, "admin", "user", "create", "root", "--key", rootKey+".pub",
21 "--admin", "--email", "root@example.test", "--verified")
22 // A second admin with no verified address is skipped, not an error.
23 inst.admin(t, "admin", "user", "create", "quiet", "--key", inst.newKey(t, "quiet")+".pub", "--admin")
24
25 danaKey := inst.newKey(t, "dana")
26 if _, errOut, code := inst.ssh(t, danaKey, "", "register",
27 "--username", "dana", "--email", "dana@example.test"); code != 0 {
28 t.Fatalf("register: %s", errOut)
29 }
30 // Pending: the admin has heard nothing yet.
31 time.Sleep(5 * time.Second) // the mailer ticks every two seconds
32 if got := smtp.mailTo("root@example.test"); len(got) != 0 {
33 t.Fatalf("admin mailed before the account was verified:\n%s", got[0])
34 }
35
36 code := extractCode(t, smtp.waitFor(t, "dana@example.test", "email verify"))
37 if _, errOut, ec := inst.ssh(t, danaKey, "", "email", "verify", code); ec != 0 {
38 t.Fatalf("verify: %s", errOut)
39 }
40 msg := smtp.waitFor(t, "root@example.test", "new account")
41 if !strings.Contains(msg, "dana") || !strings.Contains(msg, "open registration") {
42 t.Fatalf("notice body:\n%s", msg)
43 }
44}
45
46// With notify_admin off, the default, the same signup mails no one but
47// the person registering.
48func TestSignupNoticeOffByDefault(t *testing.T) {
49 t.Parallel()
50 smtp := startFakeSMTP(t)
51 inst := startInstanceWith(t, fmt.Sprintf(
52 "[registration]\nmode = \"open\"\n[mail]\nsmtp_host = %q\nfrom = \"noreply@gitbay.test\"\n", smtp.addr))
53 rootKey := inst.newKey(t, "root")
54 inst.admin(t, "admin", "user", "create", "root", "--key", rootKey+".pub",
55 "--admin", "--email", "root@example.test", "--verified")
56
57 eveKey := inst.newKey(t, "eve")
58 if _, errOut, ec := inst.ssh(t, eveKey, "", "register",
59 "--username", "eve", "--email", "eve@example.test"); ec != 0 {
60 t.Fatalf("register: %s", errOut)
61 }
62 code := extractCode(t, smtp.waitFor(t, "eve@example.test", "email verify"))
63 if _, errOut, ec := inst.ssh(t, eveKey, "", "email", "verify", code); ec != 0 {
64 t.Fatalf("verify: %s", errOut)
65 }
66 time.Sleep(5 * time.Second)
67 if got := smtp.mailTo("root@example.test"); len(got) != 0 {
68 t.Fatalf("admin mailed with notify_admin off:\n%s", got[0])
69 }
70}