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>
126 lines
3.8 KiB
Go
126 lines
3.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/mixeme/selfpost/internal/secretfile"
|
|
"github.com/mixeme/selfpost/internal/web/validate"
|
|
)
|
|
|
|
// postForm builds the kind of request the backup and export forms submit.
|
|
func postForm(values url.Values) *http.Request {
|
|
r := httptest.NewRequest(http.MethodPost, "/server/backup", strings.NewReader(values.Encode()))
|
|
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
return r
|
|
}
|
|
|
|
// The encryption password is only ever typed once into a file nobody can
|
|
// recover without it, so every way of getting it wrong has to be caught before
|
|
// the archive is sealed — and leaving the box unticked has to keep producing
|
|
// the plain archive earlier versions produced.
|
|
func TestSecretFilePassword(t *testing.T) {
|
|
long := strings.Repeat("x", validate.MinSecretFilePasswordLen)
|
|
short := strings.Repeat("x", validate.MinSecretFilePasswordLen-1)
|
|
|
|
tests := []struct {
|
|
name string
|
|
form url.Values
|
|
wantPass string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "unticked box means no encryption",
|
|
form: url.Values{"password": {long}, "password_confirm": {long}},
|
|
wantPass: "",
|
|
},
|
|
{
|
|
name: "ticked with a matching password",
|
|
form: url.Values{"encrypt": {"1"}, "password": {long}, "password_confirm": {long}},
|
|
wantPass: long,
|
|
},
|
|
{
|
|
name: "mistyped confirmation",
|
|
form: url.Values{"encrypt": {"1"}, "password": {long}, "password_confirm": {long + "!"}},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "too short",
|
|
form: url.Values{"encrypt": {"1"}, "password": {short}, "password_confirm": {short}},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "ticked but empty",
|
|
form: url.Values{"encrypt": {"1"}},
|
|
wantErr: true,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
pass, errMsg := secretFilePassword(postForm(tt.form))
|
|
if tt.wantErr {
|
|
if errMsg == "" {
|
|
t.Fatalf("password %q accepted, want a rejection", pass)
|
|
}
|
|
if pass != "" {
|
|
t.Errorf("a rejected form still yielded password %q", pass)
|
|
}
|
|
return
|
|
}
|
|
if errMsg != "" {
|
|
t.Fatalf("unexpected rejection: %s", errMsg)
|
|
}
|
|
if pass != tt.wantPass {
|
|
t.Errorf("password = %q, want %q", pass, tt.wantPass)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The messages the import form shows must distinguish the operator's likely
|
|
// mistakes; a wrong password and a tampered file stay deliberately merged.
|
|
func TestDecryptErrorMessage(t *testing.T) {
|
|
tests := []struct {
|
|
err error
|
|
want string
|
|
}{
|
|
{secretfile.ErrWrongPassword, "Wrong password"},
|
|
{fmt.Errorf("read: %w", secretfile.ErrCorrupt), "damaged"},
|
|
{secretfile.ErrNotEncrypted, "not a SelfPost export"},
|
|
{errors.New("something else"), "Could not decrypt"},
|
|
}
|
|
for _, tt := range tests {
|
|
if got := decryptErrorMessage(tt.err); !strings.Contains(got, tt.want) {
|
|
t.Errorf("decryptErrorMessage(%v) = %q, want it to mention %q", tt.err, got, tt.want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// The encryption controls are shared markup pulled into two pages; a page that
|
|
// forgets to include the partial (or the data it needs) loses the option
|
|
// silently, since the plain download still works.
|
|
func TestBackupPageOffersEncryption(t *testing.T) {
|
|
h := &Handlers{view: mustView(t), cfg: Config{Version: "test"}}
|
|
rec := httptest.NewRecorder()
|
|
h.renderBackupPageWith(rec, httptest.NewRequest(http.MethodGet, "/server/backup", nil),
|
|
http.StatusOK, "", "The two passwords do not match.")
|
|
|
|
body := rec.Body.String()
|
|
for _, want := range []string{
|
|
`name="encrypt"`, `name="password"`, `name="password_confirm"`,
|
|
`name="import_password"`, "data-encrypt-toggle", "data-encrypt-fields",
|
|
"data-import-password-fields",
|
|
fmt.Sprintf("at least %d characters", validate.MinSecretFilePasswordLen),
|
|
"The two passwords do not match.",
|
|
} {
|
|
if !strings.Contains(body, want) {
|
|
t.Errorf("backup page is missing %q", want)
|
|
}
|
|
}
|
|
}
|