- GET and PUT /account/preferences (own row only, interactive sessions, CSRF header on cookie writes) store admin_theme and admin_locale in backend_users.metadata, keeping every other key - GET /auth/me and the My account answers carry preferences - phrasebook.Translator.Locales lists the locales defining a prefix - route tables, conformance cases, OpenAPI document, TS types and docs
76 lines
2.7 KiB
Go
76 lines
2.7 KiB
Go
package cabana_test
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/cabana"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// putPreferences sends PUT /account/preferences with a Bearer token.
|
|
func putPreferences(t *testing.T, h http.Handler, token string, body any) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
raw, err := json.Marshal(body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return postAuth(t, h, http.MethodPut, adminAPI("/account/preferences"), token, raw)
|
|
}
|
|
|
|
// preferencesOf decodes an AdminPreferences envelope.
|
|
func preferencesOf(t *testing.T, raw []byte) cabana.AdminPreferences {
|
|
t.Helper()
|
|
var body cabana.Envelope[cabana.AdminPreferences]
|
|
if err := json.Unmarshal(raw, &body); err != nil {
|
|
t.Fatalf("preferences json: %v body=%s", err, raw)
|
|
}
|
|
return body.Data
|
|
}
|
|
|
|
// preferenceAdmin inserts and signs in a fresh admin on the account harness.
|
|
func preferenceAdmin(t *testing.T, gdb *gorm.DB, h http.Handler, prefix string) (cabana.BackendUser, string) {
|
|
t.Helper()
|
|
login := prefix + "-" + accountStamp()
|
|
user := insertAdmin(t, gdb, login, login+"@example.test", adminTestPassword, true, false)
|
|
return user, signIn(t, h, login)
|
|
}
|
|
|
|
// TestAccountPreferencesSmoke saves a theme, reads it back from GET
|
|
// /auth/me and GET /account/preferences, and keeps the stored subtitle.
|
|
func TestAccountPreferencesSmoke(t *testing.T) {
|
|
gdb := adminGorm(t)
|
|
h, _ := accountHandler(t, gdb, false, 1<<20)
|
|
user, token := preferenceAdmin(t, gdb, h, "prefs-smoke")
|
|
setMetadata(t, gdb, user.ID, `{"subtitle":"Rear admiral"}`)
|
|
|
|
res := putPreferences(t, h, token, map[string]any{"theme": "dark"})
|
|
if res.Code != http.StatusOK {
|
|
t.Fatalf("put status=%d body=%s", res.Code, res.Body.String())
|
|
}
|
|
if got := preferencesOf(t, res.Body.Bytes()); got.Theme != "dark" || got.Locale != "" {
|
|
t.Fatalf("put preferences = %+v", got)
|
|
}
|
|
me := getAuth(t, h, adminAPI("/auth/me"), token)
|
|
if me.Code != http.StatusOK {
|
|
t.Fatalf("me status=%d body=%s", me.Code, me.Body.String())
|
|
}
|
|
if got := profileOf(t, me.Body.Bytes()); got.Preferences.Theme != "dark" || got.Subtitle != "Rear admiral" {
|
|
t.Fatalf("me profile = %+v", got)
|
|
}
|
|
get := getAuth(t, h, adminAPI("/account/preferences"), token)
|
|
if get.Code != http.StatusOK {
|
|
t.Fatalf("get status=%d body=%s", get.Code, get.Body.String())
|
|
}
|
|
if got := preferencesOf(t, get.Body.Bytes()); got.Theme != "dark" || got.Locale != "" {
|
|
t.Fatalf("get preferences = %+v", got)
|
|
}
|
|
var meta map[string]string
|
|
row := rawUser(t, gdb, user.ID)
|
|
if err := json.Unmarshal([]byte(row.Metadata.String), &meta); err != nil || meta["subtitle"] != "Rear admiral" || meta["admin_theme"] != "dark" {
|
|
t.Fatalf("metadata = %q (%v)", row.Metadata.String, err)
|
|
}
|
|
}
|