Files
selfpost/internal/web/handlers/handlers_account_test.go
T
mixandClaude Opus 5.5 bd746c61d7 panel-redesign: paths follow the menu; Account and Server settings split
Stage 1, step 3 (plan § Routes). BREAKING: every panel URL changes and the
old paths are removed without redirects.

Routes (web.go)
  - /status -> /overview; /domains -> /outbound/domains with the domain's
    settings actions under .../settings/ (reports, ratelimit, export);
    /deliveries -> /outbound/log; /mail-queue -> /outbound/queue; /dmarc ->
    /outbound/dmarc; /inbound -> /inbound/domains; /system-log ->
    /server/log; /backup -> /server/backup (import under it); /users ->
    /server/users; recheck and reload under /server/health/.
  - One fragment name: <page>/fragment everywhere.
  - Everything under /server/ is registered through server(), which wraps
    the handler in globalOnly: the subtree is guarded once, where a new
    route cannot forget it. Handlers keep their own requireGlobal.
  - GET / leads to /overview, /outbound/domains or /inbound/domains.
  - The /account -> /settings redirect is gone with /settings.

Account and Server > Settings
  - handlers_account.go: GET /account and three POSTs - profile (username,
    e-mail; no password asked, as designed), password (current password,
    rate-limited like a sign-in, signs out other sessions), dmarc (the
    user's default: hosted / account / custom / none, each validated).
    This replaces step 1's bridge through the old Settings form.
  - handlers_settings.go is the instance's page only: log retention and the
    read-only rate limits, global role.
  - One legacy template serves both pages by a flag until stage 2 gives each
    its own: a new legacy page cannot be added to the ratchet.

Still to come inside stage 1, and why @routes stays on the ratchet:
  - step 4: the application routes (one POST, under the domain);
  - step 5: GET /server/health.
With @routes removed by hand TestRoutesFollowNavigation reports exactly
those and nothing else - no template links to a path the router lacks.

Old templates, tests and the e2e client are retargeted. The e2e module was
edited but not run here (no Docker, and its dependency could not be
downloaded on this machine).

Tests: Account (page, profile, password, DMARC default in every mode, a
domain administrator's own row only), Server settings (saved without a
password, range, global only), the /server/ subtree answered 404 by the
router for a domain administrator with All on both lists and for no
principal.

go test ./...  green except the two Windows-only failures in internal/domain
and internal/logtail.

Roadmap 13/27.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-09 21:51:55 +03:00

173 lines
7.7 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 TestAccountPageShowsThreeForms(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"`,
`action="/account/dmarc"`, `name="dmarc_default"`,
} {
if !strings.Contains(out, want) {
t.Errorf("account page missing %q", want)
}
}
for _, gone := range []string{`name="send_log_retention_days"`, `value="hosted"`} {
if strings.Contains(out, gone) {
t.Errorf("account page shows %q (instance setting, or hosted reports with ingest off)", gone)
}
}
}
// The profile form saves the name and the e-mail without asking for the
// password and without changing the password or the DMARC default.
func TestAccountProfile(t *testing.T) {
h, _ := settingsServer(t)
before, _ := h.store.GetUser(globalPrincipal.ID)
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 || u.DMARCDefaultMode != store.DMARCDefaultNone {
t.Errorf("saving the profile changed the password or the DMARC default (%q)", u.DMARCDefaultMode)
}
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)
}
}
// The default report address is the user's choice between four modes; the
// address field belongs to "another address" alone.
func TestAccountDMARCDefault(t *testing.T) {
h, _ := settingsServer(t)
save := func(form url.Values) (int, store.User) {
rec := postFormAs(h.HandleAccountDMARC, globalPrincipal, "/account/dmarc", nil, form)
u, _ := h.store.GetUser(globalPrincipal.ID)
return rec.Code, u
}
// "My account e-mail" needs an e-mail.
if code, u := save(url.Values{"dmarc_default": {"account"}}); code != http.StatusBadRequest || u.DMARCDefaultMode != store.DMARCDefaultNone {
t.Errorf("account e-mail without an e-mail = %d, mode %q", code, u.DMARCDefaultMode)
}
// Hosted needs report ingest, which this server has off.
if code, u := save(url.Values{"dmarc_default": {"hosted"}}); code != http.StatusBadRequest || u.DMARCDefaultMode != store.DMARCDefaultNone {
t.Errorf("hosted with ingest off = %d, mode %q", code, u.DMARCDefaultMode)
}
for _, bad := range []url.Values{
{"dmarc_default": {"custom"}},
{"dmarc_default": {"custom"}, "dmarc_default_address": {"nope"}},
{"dmarc_default": {"profile"}},
{},
} {
if code, _ := save(bad); code != http.StatusBadRequest {
t.Errorf("%v = %d, want 400", bad, code)
}
}
code, u := save(url.Values{"dmarc_default": {"custom"}, "dmarc_default_address": {"dmarc@hub.example"}})
if code != http.StatusSeeOther || u.DMARCDefaultMode != store.DMARCDefaultCustom || u.DMARCDefaultAddress != "dmarc@hub.example" {
t.Errorf("another address = %d, %q %q", code, u.DMARCDefaultMode, u.DMARCDefaultAddress)
}
// A domain that follows this user now resolves to it.
d, _ := h.store.AddDomain("example.com", "mail")
h.followCreator(d.ID, globalPrincipal.ID)
d, _ = h.store.GetDomain(d.ID)
if rua, err := h.domainReportAddress(d); err != nil || rua != "dmarc@hub.example" {
t.Errorf("a following domain reports to %q, %v", rua, err)
}
if rec := postFormAs(h.HandleAccountProfile, globalPrincipal, "/account/profile", nil, url.Values{"username": {"admin"}, "email": {"mix@example.org"}}); rec.Code != http.StatusSeeOther {
t.Fatalf("save e-mail = %d", rec.Code)
}
code, u = save(url.Values{"dmarc_default": {"account"}, "dmarc_default_address": {"left-over@hub.example"}})
if code != http.StatusSeeOther || u.DMARCDefaultMode != store.DMARCDefaultAccount || u.DMARCDefaultAddress != "" {
t.Errorf("my account e-mail = %d, %q %q", code, u.DMARCDefaultMode, u.DMARCDefaultAddress)
}
if rua, _ := h.domainReportAddress(d); rua != "mix@example.org" {
t.Errorf("the following domain reports to %q, want the account e-mail", rua)
}
if code, u = save(url.Values{"dmarc_default": {"none"}}); code != http.StatusSeeOther || u.DMARCDefaultMode != store.DMARCDefaultNone {
t.Errorf("none = %d, %q", code, u.DMARCDefaultMode)
}
}
// 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)
}
}