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 err := Init()
14 tcheck(t, err, "queue init")
15
16 l, err := SuppressionList(ctxbg, "bogus")
17 tcheck(t, err, "listing suppressions for unknown account")
18 tcompare(t, len(l), 0)
19
20 l, err = SuppressionList(ctxbg, "") // All
21 tcheck(t, err, "list suppression for all accounts")
22 tcompare(t, len(l), 0) // None yet.
23
24 addr1, err := smtp.ParseAddress("mjl@mox.example")
25 tcheck(t, err, "parse address")
26 path1 := addr1.Path()
27 addr2, err := smtp.ParseAddress("mjl2@mox.example")
28 tcheck(t, err, "parse address")
29 path2 := addr2.Path()
30 addr2b, err := smtp.ParseAddress("M.j.l2+catchall@Mox.example")
31 tcheck(t, err, "parse address")
32 path2b := addr2b.Path()
33
34 // No suppression yet.
35 sup, err := SuppressionLookup(ctxbg, "mjl", path1)
36 tcheck(t, err, "lookup suppression")
37 tcompare(t, sup == nil, true)
38
39 // No error if account does not exist.
40 sup, err = SuppressionLookup(ctxbg, "bogus", path1)
41 tcompare(t, err == nil, true)
42 tcompare(t, sup == nil, true)
43
44 // Can add a suppression once.
45 err = SuppressionAdd(ctxbg, path1, &webapi.Suppression{Account: "mjl"})
46 tcheck(t, err, "add suppression")
47 // No duplicates.
48 err = SuppressionAdd(ctxbg, path1, &webapi.Suppression{Account: "mjl"})
49 tcompare(t, err == nil, false)
50 // Account must be set in Suppresion.
51 err = SuppressionAdd(ctxbg, path1, &webapi.Suppression{})
52 tcompare(t, err == nil, false)
53
54 // Duplicate check is done after making base address.
55 err = SuppressionAdd(ctxbg, path2, &webapi.Suppression{Account: "retired"})
56 tcheck(t, err, "add suppression")
57 err = SuppressionAdd(ctxbg, path2b, &webapi.Suppression{Account: "retired"})
58 tcompare(t, err == nil, false) // Duplicate.
59
60 l, err = SuppressionList(ctxbg, "") // All
61 tcheck(t, err, "list suppression for all accounts")
62 tcompare(t, len(l), 2)
63 l, err = SuppressionList(ctxbg, "mjl")
64 tcheck(t, err, "list suppression for mjl")
65 tcompare(t, len(l), 1)
66
67 // path1 is listed for mjl.
68 sup, err = SuppressionLookup(ctxbg, "mjl", path1)
69 tcheck(t, err, "lookup")
70 tcompare(t, sup == nil, false)
71
72 // Accounts don't influence each other.
73 sup, err = SuppressionLookup(ctxbg, "mjl", path2)
74 tcheck(t, err, "lookup")
75 tcompare(t, sup == nil, true)
76
77 // Simplified address is present.
78 sup, err = SuppressionLookup(ctxbg, "retired", path2)
79 tcheck(t, err, "lookup")
80 tcompare(t, sup == nil, false)
81
82 // Original address is also present.
83 sup, err = SuppressionLookup(ctxbg, "retired", path2b)
84 tcheck(t, err, "lookup")
85 tcompare(t, sup == nil, false)
86
87 // Can remove again.
88 err = SuppressionRemove(ctxbg, "mjl", path1)
89 tcheck(t, err, "remove")
90 // But not twice.
91 err = SuppressionRemove(ctxbg, "mjl", path1)
92 tcompare(t, err == nil, false)
93 // No longer present.
94 sup, err = SuppressionLookup(ctxbg, "mjl", path1)
95 tcheck(t, err, "lookup")
96 tcompare(t, sup == nil, true)
97
98 // Can remove for any form of the address, was added as path2b.
99 err = SuppressionRemove(ctxbg, "retired", path2b)
100 tcheck(t, err, "lookup")
101
102 // Account names are not validated.
103 err = SuppressionAdd(ctxbg, path1, &webapi.Suppression{Account: "bogus"})
104 tcheck(t, err, "add suppression")
105 err = SuppressionRemove(ctxbg, "bogus", path1)
106 tcheck(t, err, "remove suppression")
107}
108