Files
selfpost/internal/web/handlers/handlers_server_email_test.go
T
mixandClaude Opus 5.5 96db0b7a86 panel-redesign: three choices for a domain's report address; row actions
Follows the design and guard commits before it. Written by Sonnet,
reviewed by Opus.

The report address
  - Baseline: users.dmarc_default_mode, users.dmarc_default_address and
    domains.dmarc_rua_user_id are gone. domains.dmarc_rua alone says
    where reports go: '' is no reports (a new domain), the address
    SelfPost derives for the domain is the hosted choice (while hosted
    reports are on), anything else is a typed address.
  - Removed: the user default and its store functions, a domain
    following a user, keep / inherit, followCreator, POST /account/dmarc
    with its handler, the DMARC box of Account, the user-delete item
    about domains losing their address.
  - Domain settings offers the three choices; beside the typed address a
    "Use my e-mail" button fills in the profile e-mail (panel.js: one
    generic data-fill hook; absent without a profile e-mail; it fills,
    it does not save).
  - The DMARC record's note names the address and no user. This closes
    L4 of the security review.
  - Domain export / import carries the address as a plain string.
  - The ingest allow-list is the generic mailbox plus each domain's
    address on this host; the generic mailbox is always open now.
  - dev/migrate1x (not in the tree): a 1.x domain that inherited the
    instance default gets that address as its own, so no published rua=
    changes.

Row actions
  - row_action partial (RowAction: Label, Post, Confirm, Danger);
    panel.css has the mockup's three rules. A row of the Applications
    table is Edit, New password, Delete; the confirmation texts are the
    ones the application page's head uses. Both roles, with the checks
    of the application page.

Tests cover: a new domain has no reports; each choice saved and shown
selected; hosted refused when hosted reports are off; a bad address
refused with what was typed kept; an untouched save changes nothing in
each state; the fill button with and without a profile e-mail; deleting
a user leaves every domain's address; the row's forms, targets and
confirmations for both roles and a 404 for a stranger.

Known: if hosted reports are switched off later, a stored hosted address
reads as a typed one. docs/guide.md still describes the old choices -
the documentation step.

Evidence
  - docs/assets/panel-redesign/evidence/account.png,
    out-domain-settings.png, out-domain.png, user-delete.png, help.png:
    1440 px wide, headless Edge, real templates served locally with
    their fixtures - NOT from the container.
  - panel/audit.js on out-domain, out-domain-settings, account, help,
    components and user-delete at 1440 and 1280: 1420 texts checked,
    problems: [].
  - In a browser: the fill button sets the field and does not submit;
    the row buttons have the font, size and colour of the Edit link,
    Delete the red of the other delete links; data-confirm fires on
    both. Hover and focus states were not observed.
  - go test -count=1 ./internal/web/... ./internal/store/...
    ./internal/dmarc/... ./internal/app/... ./internal/inbound/...
    ./cmd/panel: ok (ten packages; dev/migrate1x also ok).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 12:38:35 +03:00

100 lines
4.1 KiB
Go

package handlers
import (
"net/http"
"net/url"
"strings"
"testing"
"github.com/mixeme/selfpost/internal/store"
)
// The user form sets the account e-mail: create stores it, an edit changes it,
// clearing the field clears it, and a post that does not mention the field at
// all leaves it alone.
func TestUserFormSetsTheAccountEmail(t *testing.T) {
h, st := inboundHandlers(t)
out1, _ := st.AddDomain("one.example.com", "mail")
root := userWith(t, st, "root", store.RoleGlobal, store.Reach{})
rec := send(h.HandleUserNew, &root, "POST", "/server/users/new", nil, url.Values{
"username": {"ops"}, "email": {" ops@acme.io "}, "password": {"a-long-enough-password-1"}, "role": {"domain"},
"domain_ids": {idStr(out1.ID)},
})
if rec.Code != http.StatusSeeOther {
t.Fatalf("create with an e-mail = %d:\n%s", rec.Code, rec.Body.String())
}
u, _ := st.GetUserByUsername("ops")
if u.Email != "ops@acme.io" || len(u.DomainIDs) != 1 {
t.Fatalf("created user = %+v", u)
}
path, pv := "/server/users/"+idStr(u.ID), map[string]string{"uid": idStr(u.ID)}
post := func(extra url.Values) store.User {
t.Helper()
form := url.Values{"username": {"ops"}, "role": {"domain"}, "domain_ids": {idStr(out1.ID)}}
for k, v := range extra {
form[k] = v
}
if rec := send(h.HandleUserEdit, &root, "POST", path, pv, form); rec.Code != http.StatusSeeOther {
t.Fatalf("POST = %d:\n%s", rec.Code, rec.Body.String())
}
got, _ := st.GetUser(u.ID)
return got
}
if got := post(url.Values{"email": {"new@acme.io"}}); got.Email != "new@acme.io" {
t.Errorf("changed e-mail = %q", got.Email)
}
if got := post(nil); got.Email != "new@acme.io" {
t.Errorf("a post without the field changed the e-mail to %q", got.Email)
}
if got := post(url.Values{"email": {""}}); got.Email != "" {
t.Errorf("clearing the field left %q", got.Email)
}
// Creating without one is fine.
rec = send(h.HandleUserNew, &root, "POST", "/server/users/new", nil, url.Values{
"username": {"plain"}, "password": {"a-long-enough-password-1"}, "role": {"global"}})
if plain, _ := st.GetUserByUsername("plain"); rec.Code != http.StatusSeeOther || plain.Email != "" {
t.Errorf("create without an e-mail = %d, %q", rec.Code, plain.Email)
}
}
// A bad address is refused on both forms: nothing is saved, and the form comes
// back with what was typed, the refusal as the flash.
func TestUserFormRefusesABadEmailAndKeepsWhatWasTyped(t *testing.T) {
h, st := inboundHandlers(t)
out1, _ := st.AddDomain("one.example.com", "mail")
root := userWith(t, st, "root", store.RoleGlobal, store.Reach{})
u := userWith(t, st, "ops", store.RoleDomain, store.Reach{DomainIDs: []int64{out1.ID}})
if err := st.UpdateUser(u.ID, "ops", "test-hash", "ops@acme.io"); err != nil {
t.Fatal(err)
}
rec := send(h.HandleUserEdit, &root, "POST", "/server/users/"+idStr(u.ID), map[string]string{"uid": idStr(u.ID)},
url.Values{"username": {"ops"}, "email": {"not-an-address"}, "role": {"domain"}, "domain_ids": {idStr(out1.ID)}})
body := rec.Body.String()
for _, want := range []string{`class="notification is-danger is-light"`, "enter a valid email address",
`name="email" type="email" value="not-an-address"`, `<h1 class="title is-3">ops</h1>`} {
if !strings.Contains(body, want) {
t.Errorf("the refused edit form is missing %q", want)
}
}
if after, _ := st.GetUser(u.ID); rec.Code != http.StatusBadRequest || after.Email != "ops@acme.io" {
t.Errorf("a bad address on edit = %d, stored e-mail %q", rec.Code, after.Email)
}
rec = send(h.HandleUserNew, &root, "POST", "/server/users/new", nil, url.Values{
"username": {"fresh"}, "email": {"nobody@"}, "password": {"a-long-enough-password-1"}, "role": {"global"}})
body = rec.Body.String()
for _, want := range []string{"enter a valid email address", `name="email" type="email" value="nobody@"`, `name="username" value="fresh"`,
`<h1 class="title is-3">Create user</h1>`} {
if !strings.Contains(body, want) {
t.Errorf("the refused create form is missing %q", want)
}
}
if fresh, _ := st.GetUserByUsername("fresh"); rec.Code != http.StatusBadRequest || fresh.ID != 0 {
t.Errorf("a bad address on create = %d, user created: %v", rec.Code, fresh.ID != 0)
}
}