Stage 1, step 1. BREAKING: a 1.x database is not migrated.
Schema
- internal/store/migrations/ holds one file, 0001_init.sql, equal to the
2.0 schema; 0002..0010 are deleted (they stay in history at 1.9.5). No
admin table created and dropped, no ALTERs.
- users.email replaces users.dmarc_report_email: the address is the
user's own. The default DMARC report address is a user's setting
(dmarc_default_mode hosted/account/custom/none, dmarc_default_address);
the instance setting that mirrored the global user's field is gone.
- domains.dmarc_rua_user_id: a domain follows a named user's default, or
has an address of its own in dmarc_rua ('' = none). ON DELETE SET NULL
leaves it at "no reports", never at someone else's default.
- user_inbound_domains, users.all_domains, users.all_inbound_domains: in
the schema for the inbound delegation of the next step.
- users.role is 'global' / 'domain' (was 'domain_admin');
rate_limits.allowed_ips, unused since 1.9.0, is dropped.
Opening a database
- The baseline sets PRAGMA application_id ("SP20"). store.Open refuses a
file that has a schema version without it (ErrForeignSchema) and one
newer than the build. A 1.x file at user_version 10 would otherwise read
as already migrated. The refused file is left untouched.
Code
- Store.DomainDMARCRua is the one place a domain's report address is
resolved; dnscheck.ResolveDMARCRua and Store.GlobalDMARCReportEmail are
gone. dmarc.AllowedRecipients, the domain page, the dashboard and the
DNS re-check go through it.
- The old templates keep working on the new model until their pages are
restyled: the Settings form's address field is the account e-mail and
switches the user's default between it and none; a domain's "Same as
Settings" makes it follow the user who chose it; a new or imported
domain follows its creator.
- Domain export/import: a domain that follows a user exports no address.
Tests: the baseline pinned table by table (TestBaselineSchema), refusal of a
1.x and of a newer database, constraints and cascades of the new columns and
table, resolution in every default mode, the deleted-user case, and the three
bridged handlers.
docs/schema-migrations.md is rewritten for the baseline; roadmap 11/27,
schema-squash marked built.
go test ./... (Windows)
ok for every package except two that fail the same way before this commit:
internal/domain TestWriteLoadPrivateKeyRoundtrip (file mode on NTFS)
internal/logtail TestFollowTailsAndRotates (rename of an open file)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
136 lines
5.0 KiB
Go
136 lines
5.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mixeme/selfpost/internal/domain"
|
|
"github.com/mixeme/selfpost/internal/store"
|
|
"github.com/mixeme/selfpost/internal/web/auth"
|
|
)
|
|
|
|
// postFormAs sends a form to a handler as the given principal.
|
|
func postFormAs(h http.HandlerFunc, p auth.Principal, target string, pathValues map[string]string, values url.Values) *httptest.ResponseRecorder {
|
|
req := httptest.NewRequest(http.MethodPost, target, strings.NewReader(values.Encode()))
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
for k, v := range pathValues {
|
|
req.SetPathValue(k, v)
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
h(rec, auth.RequestWithPrincipal(req, p))
|
|
return rec
|
|
}
|
|
|
|
// The domain form's "Same as Settings" is, on the 2.0 schema, a domain that
|
|
// follows the default of the user who chose it; every other choice gives the
|
|
// domain an address of its own and stops it following anyone.
|
|
func TestDomainDMARCFollowsTheUserWhoChoseInherit(t *testing.T) {
|
|
h, _ := settingsServer(t)
|
|
h.domains = domain.NewService(h.store, nil, nil, "selfpost")
|
|
d, err := h.store.AddDomain("example.com", "selfpost")
|
|
if err != nil {
|
|
t.Fatalf("AddDomain: %v", err)
|
|
}
|
|
admin, err := h.store.GetUserByUsername("admin")
|
|
if err != nil {
|
|
t.Fatalf("GetUserByUsername: %v", err)
|
|
}
|
|
p := auth.Principal{ID: admin.ID, Username: "admin", Role: auth.RoleGlobal}
|
|
if err := h.store.UpdateUser(admin.ID, "admin", admin.PasswordHash, "mix@example.org"); err != nil {
|
|
t.Fatalf("UpdateUser: %v", err)
|
|
}
|
|
if err := h.store.SetDMARCDefault(admin.ID, store.DMARCDefaultAccount, ""); err != nil {
|
|
t.Fatalf("SetDMARCDefault: %v", err)
|
|
}
|
|
save := func(values url.Values) store.Domain {
|
|
t.Helper()
|
|
rec := postFormAs(h.HandleDomainDMARC, p, "/domains/1/dmarc", map[string]string{"id": "1"}, values)
|
|
if rec.Code != http.StatusSeeOther {
|
|
t.Fatalf("POST %v = %d, want 303:\n%s", values, rec.Code, rec.Body.String())
|
|
}
|
|
got, err := h.store.GetDomain(d.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetDomain: %v", err)
|
|
}
|
|
return got
|
|
}
|
|
|
|
got := save(url.Values{"dmarc_rua_mode": {"inherit"}})
|
|
if !got.DMARCRuaUserID.Valid || got.DMARCRuaUserID.Int64 != admin.ID {
|
|
t.Fatalf("inherit: the domain follows %v, want user %d", got.DMARCRuaUserID, admin.ID)
|
|
}
|
|
if rua, err := h.domainReportAddress(got); err != nil || rua != "mix@example.org" {
|
|
t.Fatalf("inherit resolves to %q, %v; want the user's account e-mail", rua, err)
|
|
}
|
|
|
|
got = save(url.Values{"dmarc_rua_mode": {"custom"}, "dmarc_rua_email": {"dmarc@hub.example"}})
|
|
if got.DMARCRuaUserID.Valid || got.DMARCRua != "dmarc@hub.example" {
|
|
t.Fatalf("custom: %q / follows %v", got.DMARCRua, got.DMARCRuaUserID)
|
|
}
|
|
|
|
got = save(url.Values{"dmarc_rua_mode": {"none"}})
|
|
if got.DMARCRuaUserID.Valid || got.DMARCRua != "" {
|
|
t.Fatalf("none: %q / follows %v", got.DMARCRua, got.DMARCRuaUserID)
|
|
}
|
|
|
|
}
|
|
|
|
// Until the Account page replaces the old Settings form, its single address
|
|
// field is the account e-mail and switches the user's DMARC default between
|
|
// "my account e-mail" and "none". Nothing is written to the instance settings.
|
|
func TestSettingsFormMapsToAccountEmailAndDefault(t *testing.T) {
|
|
h, password := settingsServer(t)
|
|
submit := func(email string) store.User {
|
|
t.Helper()
|
|
rec := postFormAs(h.HandleSettings, globalPrincipal, "/settings", nil, url.Values{
|
|
"username": {"admin"}, "current_password": {password}, "dmarc_report_email": {email},
|
|
})
|
|
if rec.Code != http.StatusSeeOther {
|
|
t.Fatalf("POST /settings (%q) = %d, want 303:\n%s", email, rec.Code, rec.Body.String())
|
|
}
|
|
u, err := h.store.GetUser(globalPrincipal.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetUser: %v", err)
|
|
}
|
|
return u
|
|
}
|
|
|
|
u := submit("mix@example.org")
|
|
if u.Email != "mix@example.org" || u.DMARCDefaultMode != store.DMARCDefaultAccount {
|
|
t.Fatalf("after setting the address: e-mail %q, default %q", u.Email, u.DMARCDefaultMode)
|
|
}
|
|
if v, err := h.store.GetSetting("dmarc_report_email"); err == nil && v != "" {
|
|
t.Fatalf("the address was mirrored into the instance settings: %q", v)
|
|
}
|
|
|
|
u = submit("")
|
|
if u.Email != "" || u.DMARCDefaultMode != store.DMARCDefaultNone {
|
|
t.Fatalf("after clearing the address: e-mail %q, default %q", u.Email, u.DMARCDefaultMode)
|
|
}
|
|
}
|
|
|
|
// A new domain takes its report address from whoever created it.
|
|
func TestFollowCreator(t *testing.T) {
|
|
h, _ := settingsServer(t)
|
|
d, err := h.store.AddDomain("example.com", "selfpost")
|
|
if err != nil {
|
|
t.Fatalf("AddDomain: %v", err)
|
|
}
|
|
h.followCreator(d.ID, globalPrincipal.ID)
|
|
got, err := h.store.GetDomain(d.ID)
|
|
if err != nil {
|
|
t.Fatalf("GetDomain: %v", err)
|
|
}
|
|
if !got.DMARCRuaUserID.Valid || got.DMARCRuaUserID.Int64 != globalPrincipal.ID {
|
|
t.Fatalf("the domain follows %v, want its creator (user %d)", got.DMARCRuaUserID, globalPrincipal.ID)
|
|
}
|
|
// The creator's default is "none" until they choose one, so the new
|
|
// domain publishes a policy-only record — as a fresh 1.x install did.
|
|
if rua, err := h.domainReportAddress(got); err != nil || rua != "" {
|
|
t.Fatalf("resolved %q, %v; want no report address yet", rua, err)
|
|
}
|
|
}
|