e2e/emailremove_test.go
65 lines · 2533 bytes
1 symbol in this file
1package e2e
2
3import (
4 "encoding/json"
5 "fmt"
6 "strings"
7 "testing"
8)
9
10// An address can be removed and the primary moved, within the rules the
11// store holds: the primary stays until another is primary, and the last
12// verified address stays (#181).
13func TestEmailRemoveAndPrimary(t *testing.T) {
14 t.Parallel()
15 smtp := startFakeSMTP(t)
16 inst := startInstanceWith(t, fmt.Sprintf(
17 "[mail]\nsmtp_host = %q\nfrom = \"noreply@gitbay.test\"\n", smtp.addr))
18 key := inst.newKey(t, "gus")
19 inst.admin(t, "admin", "user", "create", "gus",
20 "--key", key+".pub", "--email", "gus@primary.test", "--verified")
21 for _, a := range []string{"typo@example.test", "gus@next.test"} {
22 if _, errOut, code := inst.ssh(t, key, "", "email", "add", a); code != 0 {
23 t.Fatalf("email add %s: %s", a, errOut)
24 }
25 }
26
27 if _, errOut, code := inst.ssh(t, key, "", "email", "remove", "gus@primary.test"); code != 4 || !strings.Contains(errOut, "primary") {
28 t.Fatalf("removing the primary: exit %d %s", code, errOut)
29 }
30 if _, errOut, code := inst.ssh(t, key, "", "email", "remove", "nobody@example.test"); code != 3 {
31 t.Fatalf("removing an absent address: exit %d %s", code, errOut)
32 }
33 if _, errOut, code := inst.ssh(t, key, "", "email", "primary", "gus@next.test"); code != 4 || !strings.Contains(errOut, "not verified") {
34 t.Fatalf("unverified address as primary: exit %d %s", code, errOut)
35 }
36 if _, errOut, code := inst.ssh(t, key, "", "email", "remove", "typo@example.test"); code != 0 {
37 t.Fatalf("removing an unverified address: exit %d %s", code, errOut)
38 }
39
40 inst.admin(t, "admin", "email", "verify", "gus", "gus@next.test")
41 if _, errOut, code := inst.ssh(t, key, "", "email", "primary", "gus@next.test"); code != 0 {
42 t.Fatalf("email primary: exit %d %s", code, errOut)
43 }
44 if _, errOut, code := inst.ssh(t, key, "", "email", "remove", "gus@primary.test"); code != 0 {
45 t.Fatalf("removing the former primary: exit %d %s", code, errOut)
46 }
47
48 out, errOut, code := inst.ssh(t, key, "", "email", "list", "--json")
49 if code != 0 {
50 t.Fatalf("email list: exit %d %s", code, errOut)
51 }
52 var env struct {
53 Data []struct {
54 Address string `json:"address"`
55 Verified bool `json:"verified"`
56 Primary bool `json:"primary"`
57 } `json:"data"`
58 }
59 if err := json.Unmarshal([]byte(out), &env); err != nil {
60 t.Fatalf("email list --json: %v\n%s", err, out)
61 }
62 if len(env.Data) != 1 || env.Data[0].Address != "gus@next.test" || !env.Data[0].Verified || !env.Data[0].Primary {
63 t.Fatalf("addresses after the changes: %+v", env.Data)
64 }
65}