1package queue
2
3import (
4 "testing"
5
6 "github.com/mjl-/mox/smtp"
7 "github.com/mjl-/mox/webapi"
8)
9
10func TestSuppression(t *testing.T) {
11 _, cleanup := setup(t)
12 defer cleanup()
13
14 l, err := SuppressionList(ctxbg, "bogus")
15 tcheck(t, err, "listing suppressions for unknown account")
16 tcompare(t, len(l), 0)
17
18 l, err = SuppressionList(ctxbg, "") // All
19 tcheck(t, err, "list suppression for all accounts")
20 tcompare(t, len(l), 0) // None yet.
21
22 addr1, err := smtp.ParseAddress("mjl@mox.example")
23 tcheck(t, err, "parse address")
24 path1 := addr1.Path()
25 addr2, err := smtp.ParseAddress("mjl2@mox.example")
26 tcheck(t, err, "parse address")
27 path2 := addr2.Path()
28 addr2b, err := smtp.ParseAddress("M.j.l2+catchall@Mox.example")
29 tcheck(t, err, "parse address")
30 path2b := addr2b.Path()
31
32 // No suppression yet.
33 sup, err := SuppressionLookup(ctxbg, "mjl", path1)
34 tcheck(t, err, "lookup suppression")
35 tcompare(t, sup == nil, true)
36
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)
41
42 // Can add a suppression once.
43 err = SuppressionAdd(ctxbg, path1, &webapi.Suppression{Account: "mjl"})
44 tcheck(t, err, "add suppression")
45 // No duplicates.
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)
51
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.
57
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)
64
65 // path1 is listed for mjl.
66 sup, err = SuppressionLookup(ctxbg, "mjl", path1)
67 tcheck(t, err, "lookup")
68 tcompare(t, sup == nil, false)
69
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)
74
75 // Simplified address is present.
76 sup, err = SuppressionLookup(ctxbg, "retired", path2)
77 tcheck(t, err, "lookup")
78 tcompare(t, sup == nil, false)
79
80 // Original address is also present.
81 sup, err = SuppressionLookup(ctxbg, "retired", path2b)
82 tcheck(t, err, "lookup")
83 tcompare(t, sup == nil, false)
84
85 // Can remove again.
86 err = SuppressionRemove(ctxbg, "mjl", path1)
87 tcheck(t, err, "remove")
88 // But not twice.
89 err = SuppressionRemove(ctxbg, "mjl", path1)
90 tcompare(t, err == nil, false)
91 // No longer present.
92 sup, err = SuppressionLookup(ctxbg, "mjl", path1)
93 tcheck(t, err, "lookup")
94 tcompare(t, sup == nil, true)
95
96 // Can remove for any form of the address, was added as path2b.
97 err = SuppressionRemove(ctxbg, "retired", path2b)
98 tcheck(t, err, "lookup")
99
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")
105}
106