- principalFrom sets PreferredLocale from metadata admin_locale; personal API token principals clear it, so token clients keep the request locale - the guarded admin group runs locale.from-principal after the backend guard, overriding the LocaleResolver or Accept-Language locale - GET /lang honours a supported ?locale= and reports meta.locales (cabana.LangEnvelope, cabana.LangMeta) - Docker test for the override, refusals and /lang; docs and OpenAPI
173 lines
6.3 KiB
Go
173 lines
6.3 KiB
Go
package cabana_test
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"strings"
|
|
"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)
|
|
}
|
|
}
|
|
|
|
// sendLocale sends a request with a Bearer token (when set), a JSON body
|
|
// (when set) and an Accept-Language header.
|
|
func sendLocale(t *testing.T, h http.Handler, method, path, token, acceptLanguage string, body any) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
var raw []byte
|
|
if body != nil {
|
|
var err error
|
|
if raw, err = json.Marshal(body); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
req := httptest.NewRequest(method, path, bytes.NewReader(raw))
|
|
if raw != nil {
|
|
req.Header.Set("Content-Type", "application/json")
|
|
}
|
|
if token != "" {
|
|
req.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
req.Header.Set("Accept-Language", acceptLanguage)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, req)
|
|
return rec
|
|
}
|
|
|
|
// langMeta decodes the meta object of GET /lang.
|
|
func langMeta(t *testing.T, raw []byte) cabana.LangMeta {
|
|
t.Helper()
|
|
var body cabana.LangEnvelope
|
|
if err := json.Unmarshal(raw, &body); err != nil {
|
|
t.Fatalf("lang json: %v body=%s", err, raw)
|
|
}
|
|
return body.Meta
|
|
}
|
|
|
|
// TestAdminLocalePreference: a stored admin language overrides the request
|
|
// locale of guarded admin routes; unsupported codes are refused; GET /lang
|
|
// honours only a supported ?locale= and lists meta.locales.
|
|
func TestAdminLocalePreference(t *testing.T) {
|
|
gdb := adminGorm(t)
|
|
h, _ := accountHandler(t, gdb, false, 1<<20)
|
|
_, token := preferenceAdmin(t, gdb, h, "prefs-locale")
|
|
|
|
invalidTheme := func(t *testing.T) string {
|
|
t.Helper()
|
|
rec := sendLocale(t, h, http.MethodPut, adminAPI("/account/preferences"), token, "en", map[string]any{"theme": "blue"})
|
|
if rec.Code != http.StatusUnprocessableEntity {
|
|
t.Fatalf("invalid theme status=%d body=%s", rec.Code, rec.Body.String())
|
|
}
|
|
return strings.Join(validationDetails(t, rec.Body.Bytes())["theme"], " ")
|
|
}
|
|
if got := invalidTheme(t); !strings.Contains(got, "selected") {
|
|
t.Fatalf("message before the preference = %q, want English", got)
|
|
}
|
|
|
|
res := putPreferences(t, h, token, map[string]any{"locale": "pl"})
|
|
if res.Code != http.StatusOK {
|
|
t.Fatalf("put locale status=%d body=%s", res.Code, res.Body.String())
|
|
}
|
|
if got := preferencesOf(t, res.Body.Bytes()); got.Locale != "pl" || got.Theme != "system" {
|
|
t.Fatalf("put preferences = %+v", got)
|
|
}
|
|
if got := invalidTheme(t); !strings.Contains(got, "Zaznaczony") {
|
|
t.Fatalf("message with the pl preference = %q, want Polish despite Accept-Language: en", got)
|
|
}
|
|
|
|
bad := putPreferences(t, h, token, map[string]any{"locale": "xx"})
|
|
if bad.Code != http.StatusUnprocessableEntity {
|
|
t.Fatalf("locale xx status=%d body=%s", bad.Code, bad.Body.String())
|
|
}
|
|
if _, ok := validationDetails(t, bad.Body.Bytes())["locale"]; !ok {
|
|
t.Fatalf("locale xx details = %s", bad.Body.String())
|
|
}
|
|
|
|
pl := sendLocale(t, h, http.MethodGet, adminAPI("/lang?locale=pl"), "", "en", nil)
|
|
if pl.Code != http.StatusOK {
|
|
t.Fatalf("lang pl status=%d", pl.Code)
|
|
}
|
|
if meta := langMeta(t, pl.Body.Bytes()); meta.Locale != "pl" || !slices.Contains(meta.Locales, "en") || !slices.Contains(meta.Locales, "pl") {
|
|
t.Fatalf("lang pl meta = %+v", meta)
|
|
}
|
|
xx := sendLocale(t, h, http.MethodGet, adminAPI("/lang?locale=xx"), "", "en", nil)
|
|
if meta := langMeta(t, xx.Body.Bytes()); meta.Locale != "en" || strings.Contains(xx.Body.String(), "xx") {
|
|
t.Fatalf("lang xx meta = %+v", meta)
|
|
}
|
|
|
|
cleared := putPreferences(t, h, token, map[string]any{"locale": ""})
|
|
if got := preferencesOf(t, cleared.Body.Bytes()); cleared.Code != http.StatusOK || got.Locale != "" {
|
|
t.Fatalf("clear locale status=%d body=%s", cleared.Code, cleared.Body.String())
|
|
}
|
|
if got := invalidTheme(t); !strings.Contains(got, "selected") {
|
|
t.Fatalf("message after clearing = %q, want English", got)
|
|
}
|
|
}
|