test / test (push) Canceled after 0s
Steps 1-3 of stage 4, tested together (their docs share files).
Step 1 (M8, L15, L16): an unknown name pays a bcrypt comparison at the
panel's work factor too (noSuchUserHash, made once) -
TestLoginTakesAsLongForAnUnknownName holds the medians within a factor of
two; the Account form's current-password check has its own limiter, per
user (AllowPasswordChange); a sign-in destroys every session the request
presents before it issues its own.
Step 2 (L14): newHTTPServer - ReadTimeout 30 s, WriteTimeout 60 s,
IdleTimeout 2 min beside ReadHeaderTimeout; the full backup moves its write
deadline to two hours through http.ResponseController (e2e: the log shows
no failure to move it).
Step 3 (M11), read in htmx 2.0.4's source (dev/upstream/htmx-2.0.4, the npm
build byte-equal to the shipped htmx.min.js): the poll passes source: box,
and the next refresh is scheduled from htmx:afterRequest, which htmx fires
on load, error, abort and timeout, and after a swap on the nearest parent
left (the box found by detail.target's id); failures in a row double the
wait to 60 s. RequireAuth answers htmx with 401 + HX-Redirect: /login,
which htmx follows before it looks at the status. testdata/poll_test.js
runs panel.js in node against a stand-in DOM and clock (TestPanelJS; skipped
without node, which the test server lacks - run here, and it fails on the
old panel.js).
Also measured: the stage-3 e2e regression for H1 fails as it should on HEAD
with the old entrypoint ("check data/postfix/e2e_extra_map: permission
denied", the panel restarting).
Tested on the test server: go vet + go test of 25 packages, make e2e (all
pass, 639 s) on this tree.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
227 lines
9.6 KiB
Go
227 lines
9.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The Account form's current-password check has its own bucket, per user:
|
|
// it stops a guesser at ten, and does not lock the address out of signing in
|
|
// (audit 2026-10-12, L15).
|
|
func TestAccountPasswordGuessesHaveTheirOwnLimit(t *testing.T) {
|
|
h, _ := settingsServer(t)
|
|
wrong := url.Values{"current_password": {"nope"}, "new_password": {"another-long-password-2"}, "new_password_confirm": {"another-long-password-2"}}
|
|
for i := 0; i < 10; i++ {
|
|
if rec := postFormAs(h.HandleAccountPassword, globalPrincipal, "/account/password", nil, wrong); rec.Code != http.StatusUnauthorized {
|
|
t.Fatalf("guess %d = %d, want 401", i+1, rec.Code)
|
|
}
|
|
}
|
|
if rec := postFormAs(h.HandleAccountPassword, globalPrincipal, "/account/password", nil, wrong); rec.Code != http.StatusTooManyRequests {
|
|
t.Fatalf("the eleventh guess = %d, want 429", rec.Code)
|
|
}
|
|
|
|
// The same address may still sign in: postFormAs and this request come
|
|
// from httptest's one RemoteAddr.
|
|
req := httptest.NewRequest(http.MethodPost, "/login", strings.NewReader(url.Values{"username": {"x"}, "password": {"y"}}.Encode()))
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
rec := httptest.NewRecorder()
|
|
h.auth.HandleLogin(rec, req)
|
|
if rec.Code == http.StatusTooManyRequests {
|
|
t.Error("the password guesses locked the address out of signing in")
|
|
}
|
|
}
|