Files
selfpost/internal/web/handlers/handlers_account_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

200 lines
8.2 KiB
Go

package handlers
import (
"net/http"
"net/url"
"strings"
"testing"
"github.com/mixeme/selfpost/internal/store"
"golang.org/x/crypto/bcrypt"
)
// Account is every user's own page: each part is its own form and its own
// POST, and none of them touches what another saves.
func TestAccountPageShowsTwoForms(t *testing.T) {
h, _ := settingsServer(t)
out := getBody(t, h.HandleAccount, "/account")
for _, want := range []string{
`action="/account/profile"`, `name="email"`,
`action="/account/password"`, `name="current_password"`,
} {
if !strings.Contains(out, want) {
t.Errorf("account page missing %q", want)
}
}
// A domain's report address is not an Account setting any more.
for _, gone := range []string{`name="send_log_retention_days"`, `/account/dmarc`, `name="dmarc_default"`, `<h2>DMARC reports</h2>`, `Report authorization`} {
if strings.Contains(out, gone) {
t.Errorf("account page shows %q", gone)
}
}
}
// The profile form saves the name and the e-mail without asking for the
// password and without changing the password or any domain's report address.
func TestAccountProfile(t *testing.T) {
h, _ := settingsServer(t)
before, _ := h.store.GetUser(globalPrincipal.ID)
d, _ := h.store.AddDomain("example.com", "mail")
if err := h.store.SetDomainDMARCAddress(d.ID, "reports@hub.example"); err != nil {
t.Fatal(err)
}
rec := postFormAs(h.HandleAccountProfile, globalPrincipal, "/account/profile", nil,
url.Values{"username": {"operator"}, "email": {"mix@example.org"}})
if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/account?done=profile" {
t.Fatalf("POST /account/profile = %d to %q:\n%s", rec.Code, rec.Header().Get("Location"), rec.Body.String())
}
u, _ := h.store.GetUser(globalPrincipal.ID)
if u.Username != "operator" || u.Email != "mix@example.org" {
t.Errorf("profile = %q / %q", u.Username, u.Email)
}
if u.PasswordHash != before.PasswordHash {
t.Error("saving the profile changed the password")
}
if got, _ := h.store.GetDomain(d.ID); got.DMARCRua != "reports@hub.example" {
t.Errorf("saving the profile moved a domain's report address to %q", got.DMARCRua)
}
for _, bad := range []url.Values{
{"username": {"operator"}, "email": {"not-an-address"}},
{"username": {"x y"}, "email": {""}},
} {
if rec := postFormAs(h.HandleAccountProfile, globalPrincipal, "/account/profile", nil, bad); rec.Code != http.StatusBadRequest {
t.Errorf("POST /account/profile %v = %d, want 400", bad, rec.Code)
}
}
// A name that is taken is refused and nothing is half-saved.
if _, err := h.store.CreateUser("taken", "h", store.RoleGlobal, store.Reach{}); err != nil {
t.Fatal(err)
}
rec = postFormAs(h.HandleAccountProfile, globalPrincipal, "/account/profile", nil, url.Values{"username": {"taken"}, "email": {"other@example.org"}})
if u, _ = h.store.GetUser(globalPrincipal.ID); rec.Code != http.StatusConflict || u.Username != "operator" || u.Email != "mix@example.org" {
t.Errorf("a taken username = %d, profile now %q / %q", rec.Code, u.Username, u.Email)
}
}
func TestAccountPassword(t *testing.T) {
h, password := settingsServer(t)
const next = "another-long-password-2"
hashOf := func() string {
u, _ := h.store.GetUser(globalPrincipal.ID)
return u.PasswordHash
}
before := hashOf()
for name, form := range map[string]url.Values{
"wrong current password": {"current_password": {"nope"}, "new_password": {next}, "new_password_confirm": {next}},
"mismatch": {"current_password": {password}, "new_password": {next}, "new_password_confirm": {next + "x"}},
"too weak": {"current_password": {password}, "new_password": {"short"}, "new_password_confirm": {"short"}},
"empty": {"current_password": {password}},
} {
rec := postFormAs(h.HandleAccountPassword, globalPrincipal, "/account/password", nil, form)
if rec.Code == http.StatusSeeOther || hashOf() != before {
t.Errorf("%s: %d, password changed: %v", name, rec.Code, hashOf() != before)
}
}
rec := postFormAs(h.HandleAccountPassword, globalPrincipal, "/account/password", nil,
url.Values{"current_password": {password}, "new_password": {next}, "new_password_confirm": {next}})
if rec.Code != http.StatusSeeOther {
t.Fatalf("POST /account/password = %d:\n%s", rec.Code, rec.Body.String())
}
if err := bcrypt.CompareHashAndPassword([]byte(hashOf()), []byte(next)); err != nil {
t.Errorf("the new password was not stored: %v", err)
}
}
// A domain administrator has the same Account page; it is not a Server page.
func TestAccountIsOpenToADomainAdministrator(t *testing.T) {
h, _ := settingsServer(t)
d, _ := h.store.AddDomain("example.com", "mail")
p := domainAdmin(t, h.store, "ops", d.ID)
rec := postFormAs(h.HandleAccountProfile, p, "/account/profile", nil, url.Values{"username": {"ops"}, "email": {"ops@example.org"}})
if rec.Code != http.StatusSeeOther {
t.Fatalf("POST /account/profile as a domain administrator = %d:\n%s", rec.Code, rec.Body.String())
}
if u, _ := h.store.GetUser(p.ID); u.Email != "ops@example.org" {
t.Errorf("e-mail = %q", u.Email)
}
// It changed their own row and nobody else's.
if admin, _ := h.store.GetUser(globalPrincipal.ID); admin.Email != "" {
t.Errorf("another user's e-mail was changed to %q", admin.Email)
}
}
// Account is drawn by the shell of the kit, for the signed-in person, with
// their role in the kicker.
func TestAccountPageIsDrawnInTheShell(t *testing.T) {
h, _ := settingsServer(t)
out := getBody(t, h.HandleAccount, "/account")
for _, want := range []string{
`class="navbar is-primary"`, `Signed in as admin · global`, `<h1 class="title is-3">Account</h1>`,
`href="/static/panel.css"`,
} {
if !strings.Contains(out, want) {
t.Errorf("account page missing %q", want)
}
}
}
// A refused form is shown again with the error above the boxes and what the
// person typed in the fields — escaped.
func TestAccountRefusalKeepsWhatWasTyped(t *testing.T) {
h, _ := settingsServer(t)
rec := postFormAs(h.HandleAccountProfile, globalPrincipal, "/account/profile", nil,
url.Values{"username": {`o"><b>x</b>`}, "email": {"not-an-address"}})
out := rec.Body.String()
if rec.Code != http.StatusBadRequest {
t.Fatalf("POST /account/profile = %d, want 400:\n%s", rec.Code, out)
}
if !strings.Contains(out, `notification is-danger is-light`) {
t.Error("the refusal is not shown as the error flash")
}
if !strings.Contains(out, `value="not-an-address"`) {
t.Error("the e-mail that was typed is not shown again")
}
if strings.Contains(out, `<b>x</b>`) {
t.Errorf("the username was written back unescaped:\n%s", out)
}
rec = postFormAs(h.HandleAccountPassword, globalPrincipal, "/account/password", nil,
url.Values{"current_password": {"nope"}, "new_password": {"another-long-password-2"}, "new_password_confirm": {"another-long-password-2"}})
if rec.Code != http.StatusUnauthorized || !strings.Contains(rec.Body.String(), "Current password is incorrect.") {
t.Errorf("wrong current password = %d:\n%s", rec.Code, rec.Body.String())
}
// A password is never written back.
if strings.Contains(rec.Body.String(), "another-long-password-2") {
t.Error("a submitted password is in the page that refused it")
}
}
func TestAccountResultsAreTheFlash(t *testing.T) {
h, _ := settingsServer(t)
for done, want := range map[string]string{
"profile": "Profile saved.",
"password": "Password changed.",
} {
out := getBody(t, h.HandleAccount, "/account?done="+done)
if !strings.Contains(out, `notification is-success is-light`) || !strings.Contains(out, want) {
t.Errorf("?done=%s: the result is not shown (%q)", done, want)
}
}
}
// A domain administrator has the same Account page in the same shell; it has
// no Server group in the menu and says its role.
func TestAccountPageForADomainAdministrator(t *testing.T) {
h, _ := settingsServer(t)
d, _ := h.store.AddDomain("example.com", "mail")
p := domainAdmin(t, h.store, "ops", d.ID)
out := getBodyAs(t, h.HandleAccount, "/account", p)
if !strings.Contains(out, `· domain`) || !strings.Contains(out, `action="/account/profile"`) {
t.Errorf("account page for a domain administrator:\n%s", out)
}
for _, gone := range []string{`href="/server/health"`, `href="/overview"`, `href="/server/users"`} {
if strings.Contains(out, gone) {
t.Errorf("a domain administrator's menu carries %s", gone)
}
}
}