6 "github.com/mjl-/mox/smtp"
7 "github.com/mjl-/mox/webapi"
10func TestSuppression(t *testing.T) {
11 _, cleanup := setup(t)
14 l, err := SuppressionList(ctxbg, "bogus")
15 tcheck(t, err, "listing suppressions for unknown account")
16 tcompare(t, len(l), 0)
18 l, err = SuppressionList(ctxbg, "") // All
19 tcheck(t, err, "list suppression for all accounts")
20 tcompare(t, len(l), 0) // None yet.
22 addr1, err := smtp.ParseAddress("mjl@mox.example")
23 tcheck(t, err, "parse address")
25 addr2, err := smtp.ParseAddress("mjl2@mox.example")
26 tcheck(t, err, "parse address")
28 addr2b, err := smtp.ParseAddress("M.j.l2+catchall@Mox.example")
29 tcheck(t, err, "parse address")
30 path2b := addr2b.Path()
32 // No suppression yet.
33 sup, err := SuppressionLookup(ctxbg, "mjl", path1)
34 tcheck(t, err, "lookup suppression")
35 tcompare(t, sup == nil, true)
37 // No error if account does not exist.
38 sup, err = SuppressionLookup(ctxbg, "bogus", path1)
39 tcompare(t, err == nil, true)
40 tcompare(t, sup == nil, true)
42 // Can add a suppression once.
43 err = SuppressionAdd(ctxbg, path1, &webapi.Suppression{Account: "mjl"})
44 tcheck(t, err, "add suppression")
46 err = SuppressionAdd(ctxbg, path1, &webapi.Suppression{Account: "mjl"})
47 tcompare(t, err == nil, false)
48 // Account must be set in Suppresion.
49 err = SuppressionAdd(ctxbg, path1, &webapi.Suppression{})
50 tcompare(t, err == nil, false)
52 // Duplicate check is done after making base address.
53 err = SuppressionAdd(ctxbg, path2, &webapi.Suppression{Account: "retired"})
54 tcheck(t, err, "add suppression")
55 err = SuppressionAdd(ctxbg, path2b, &webapi.Suppression{Account: "retired"})
56 tcompare(t, err == nil, false) // Duplicate.
58 l, err = SuppressionList(ctxbg, "") // All
59 tcheck(t, err, "list suppression for all accounts")
60 tcompare(t, len(l), 2)
61 l, err = SuppressionList(ctxbg, "mjl")
62 tcheck(t, err, "list suppression for mjl")
63 tcompare(t, len(l), 1)
65 // path1 is listed for mjl.
66 sup, err = SuppressionLookup(ctxbg, "mjl", path1)
67 tcheck(t, err, "lookup")
68 tcompare(t, sup == nil, false)
70 // Accounts don't influence each other.
71 sup, err = SuppressionLookup(ctxbg, "mjl", path2)
72 tcheck(t, err, "lookup")
73 tcompare(t, sup == nil, true)
75 // Simplified address is present.
76 sup, err = SuppressionLookup(ctxbg, "retired", path2)
77 tcheck(t, err, "lookup")
78 tcompare(t, sup == nil, false)
80 // Original address is also present.
81 sup, err = SuppressionLookup(ctxbg, "retired", path2b)
82 tcheck(t, err, "lookup")
83 tcompare(t, sup == nil, false)
86 err = SuppressionRemove(ctxbg, "mjl", path1)
87 tcheck(t, err, "remove")
89 err = SuppressionRemove(ctxbg, "mjl", path1)
90 tcompare(t, err == nil, false)
92 sup, err = SuppressionLookup(ctxbg, "mjl", path1)
93 tcheck(t, err, "lookup")
94 tcompare(t, sup == nil, true)
96 // Can remove for any form of the address, was added as path2b.
97 err = SuppressionRemove(ctxbg, "retired", path2b)
98 tcheck(t, err, "lookup")
100 // Account names are not validated.
101 err = SuppressionAdd(ctxbg, path1, &webapi.Suppression{Account: "bogus"})
102 tcheck(t, err, "add suppression")
103 err = SuppressionRemove(ctxbg, "bogus", path1)
104 tcheck(t, err, "remove suppression")