94 lines
3.5 KiB
Go
94 lines
3.5 KiB
Go
package db
|
|
|
|
import "testing"
|
|
|
|
// TestAllowScopeExactAndDomainMatch confirms AllowScope resolves both an exact-address
|
|
// pattern and a "@domain.com" wildcard, returning the matched entry's scope rather than
|
|
// IsAllowed's bare bool.
|
|
func TestAllowScopeExactAndDomainMatch(t *testing.T) {
|
|
d := openTestDB(t)
|
|
const mailboxID = int64(1)
|
|
|
|
if _, err := d.AddAllowBlockEntry(mailboxID, "allow", "friend@example.com", "dkim"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := d.AddAllowBlockEntry(mailboxID, "allow", "@trusted.com", "spf"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
scope, matched, err := d.AllowScope(mailboxID, "friend@example.com")
|
|
if err != nil || !matched || scope != "dkim" {
|
|
t.Fatalf("exact match: scope=%q matched=%v err=%v, want dkim/true", scope, matched, err)
|
|
}
|
|
|
|
scope, matched, err = d.AllowScope(mailboxID, "anyone@trusted.com")
|
|
if err != nil || !matched || scope != "spf" {
|
|
t.Fatalf("domain wildcard match: scope=%q matched=%v err=%v, want spf/true", scope, matched, err)
|
|
}
|
|
|
|
scope, matched, err = d.AllowScope(mailboxID, "stranger@nowhere.com")
|
|
if err != nil || matched || scope != "" {
|
|
t.Fatalf("no match: scope=%q matched=%v err=%v, want ''/false", scope, matched, err)
|
|
}
|
|
}
|
|
|
|
// TestAllowScopeIgnoresJunkAndBlockEntries confirms AllowScope only ever matches
|
|
// list_type='allow' rows — a "junk" or "block" entry for the same sender is a
|
|
// different tool for a different job (see esrv_mailbox_allowblock's schema comment)
|
|
// and must never be mistaken for a whitelist scope.
|
|
func TestAllowScopeIgnoresJunkAndBlockEntries(t *testing.T) {
|
|
d := openTestDB(t)
|
|
const mailboxID = int64(1)
|
|
|
|
if _, err := d.AddAllowBlockEntry(mailboxID, "junk", "spammer@example.com", "all"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
scope, matched, err := d.AllowScope(mailboxID, "spammer@example.com")
|
|
if err != nil || matched || scope != "" {
|
|
t.Fatalf("junk entry must not satisfy AllowScope: scope=%q matched=%v err=%v", scope, matched, err)
|
|
}
|
|
}
|
|
|
|
// TestAllowScopeDefaultsToAllForPreExistingRows confirms a row written before the
|
|
// scope column existed (simulated here by inserting without it, relying on the
|
|
// column's own DEFAULT 'all') keeps its full-bypass behavior — the backward
|
|
// compatibility guarantee migrateAddedColumns' comment documents.
|
|
func TestAllowScopeDefaultsToAllForPreExistingRows(t *testing.T) {
|
|
d := openTestDB(t)
|
|
const mailboxID = int64(1)
|
|
|
|
if _, err := d.Exec(`INSERT INTO esrv_mailbox_allowblock (mailbox_id, list_type, pattern) VALUES (?, 'allow', 'legacy@example.com')`, mailboxID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
scope, matched, err := d.AllowScope(mailboxID, "legacy@example.com")
|
|
if err != nil || !matched || scope != "all" {
|
|
t.Fatalf("scope=%q matched=%v err=%v, want all/true", scope, matched, err)
|
|
}
|
|
}
|
|
|
|
// TestListAllowBlockRoundTripsScope confirms every scope value survives
|
|
// Add/List — used by the webmail Whitelist page's per-entry badge.
|
|
func TestListAllowBlockRoundTripsScope(t *testing.T) {
|
|
d := openTestDB(t)
|
|
const mailboxID = int64(1)
|
|
|
|
for _, scope := range []string{"all", "spf", "dkim", "spam"} {
|
|
if _, err := d.AddAllowBlockEntry(mailboxID, "allow", scope+"@example.com", scope); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
entries, err := d.ListAllowBlock(mailboxID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got := map[string]string{}
|
|
for _, e := range entries {
|
|
got[e.Pattern] = e.Scope
|
|
}
|
|
for _, scope := range []string{"all", "spf", "dkim", "spam"} {
|
|
if got[scope+"@example.com"] != scope {
|
|
t.Fatalf("entry for %s@example.com has scope %q, want %q", scope, got[scope+"@example.com"], scope)
|
|
}
|
|
}
|
|
}
|