1
0
mirror of https://github.com/jhillyerd/inbucket.git synced 2025-12-21 03:27:01 +00:00

policy: Implement recipient domain policy for #51

- INBUCKET_SMTP_DEFAULTACCEPT
- INBUCKET_SMTP_ACCEPTDOMAINS
- INBUCKET_SMTP_REJECTDOMAINS
This commit is contained in:
James Hillyerd
2018-04-01 18:05:21 -07:00
parent 0b3c18eba9
commit a7d2b00a9c
8 changed files with 143 additions and 9 deletions

View File

@@ -7,6 +7,7 @@ import (
"strings"
"github.com/jhillyerd/inbucket/pkg/config"
"github.com/jhillyerd/inbucket/pkg/stringutil"
)
// Addressing handles email address policy.
@@ -29,14 +30,26 @@ func (a *Addressing) NewRecipient(address string) (*Recipient, error) {
return nil, err
}
return &Recipient{
Address: *ar,
apolicy: a,
LocalPart: local,
Domain: domain,
Mailbox: mailbox,
Address: *ar,
addrPolicy: a,
LocalPart: local,
Domain: domain,
Mailbox: mailbox,
}, nil
}
// ShouldAcceptDomain indicates if Inbucket accepts mail destined for the specified domain.
func (a *Addressing) ShouldAcceptDomain(domain string) bool {
domain = strings.ToLower(domain)
if a.Config.DefaultAccept && !stringutil.SliceContains(a.Config.RejectDomains, domain) {
return true
}
if !a.Config.DefaultAccept && stringutil.SliceContains(a.Config.AcceptDomains, domain) {
return true
}
return false
}
// ShouldStoreDomain indicates if Inbucket stores email destined for the specified domain.
func (a *Addressing) ShouldStoreDomain(domain string) bool {
if a.Config.StoreMessages {

View File

@@ -8,6 +8,59 @@ import (
"github.com/jhillyerd/inbucket/pkg/policy"
)
func TestShouldAcceptDomain(t *testing.T) {
// Test with default accept.
ap := &policy.Addressing{
Config: config.SMTP{
DefaultAccept: true,
RejectDomains: []string{"a.deny.com", "deny.com"},
},
}
testCases := []struct {
domain string
want bool
}{
{domain: "bar.com", want: true},
{domain: "DENY.com", want: false},
{domain: "a.deny.com", want: false},
{domain: "b.deny.com", want: true},
}
for _, tc := range testCases {
t.Run(tc.domain, func(t *testing.T) {
got := ap.ShouldAcceptDomain(tc.domain)
if got != tc.want {
t.Errorf("Got %v for %q, want: %v", got, tc.domain, tc.want)
}
})
}
// Test with default reject.
ap = &policy.Addressing{
Config: config.SMTP{
DefaultAccept: false,
AcceptDomains: []string{"a.allow.com", "allow.com"},
},
}
testCases = []struct {
domain string
want bool
}{
{domain: "bar.com", want: false},
{domain: "ALLOW.com", want: true},
{domain: "a.allow.com", want: true},
{domain: "b.allow.com", want: false},
}
for _, tc := range testCases {
t.Run(tc.domain, func(t *testing.T) {
got := ap.ShouldAcceptDomain(tc.domain)
if got != tc.want {
t.Errorf("Got %v for %q, want: %v", got, tc.domain, tc.want)
}
})
}
}
func TestShouldStoreDomain(t *testing.T) {
// Test with storage enabled.
ap := &policy.Addressing{

View File

@@ -5,7 +5,7 @@ import "net/mail"
// Recipient represents a potential email recipient, allows policies for it to be queried.
type Recipient struct {
mail.Address
apolicy *Addressing
addrPolicy *Addressing
// LocalPart is the part of the address before @, including +extension.
LocalPart string
// Domain is the part of the address after @.
@@ -16,10 +16,10 @@ type Recipient struct {
// ShouldAccept returns true if Inbucket should accept mail for this recipient.
func (r *Recipient) ShouldAccept() bool {
return true
return r.addrPolicy.ShouldAcceptDomain(r.Domain)
}
// ShouldStore returns true if Inbucket should store mail for this recipient.
func (r *Recipient) ShouldStore() bool {
return r.apolicy.ShouldStoreDomain(r.Domain)
return r.addrPolicy.ShouldStoreDomain(r.Domain)
}