Files
filebrowser/backend/internal/web/users_patch_test.go
T

131 lines
3.9 KiB
Go

package web
import "testing"
func TestValidatePatchWhich(t *testing.T) {
t.Run("accepts frontend fields", func(t *testing.T) {
for _, which := range [][]string{
{"scopes", "permissions"},
{"locale"},
{"loginMethod", "otpEnabled"},
{"password"},
} {
if err := validatePatchWhich(which); err != nil {
t.Fatalf("unexpected error for which=%v: %v", which, err)
}
}
})
t.Run("rejects empty", func(t *testing.T) {
if err := validatePatchWhich(nil); err == nil {
t.Fatal("expected error for nil which")
}
if err := validatePatchWhich([]string{}); err == nil {
t.Fatal("expected error for empty which")
}
})
t.Run("rejects invalid field names", func(t *testing.T) {
for _, which := range [][]string{{"all"}, {"All"}, {"scopes", "all"}} {
if err := validatePatchWhich(which); err == nil {
t.Fatalf("expected error for which=%v", which)
}
}
})
t.Run("rejects blank field names", func(t *testing.T) {
if err := validatePatchWhich([]string{"scopes", " "}); err == nil {
t.Fatal("expected error for blank field name")
}
})
t.Run("rejects unknown field names", func(t *testing.T) {
if err := validatePatchWhich([]string{"notARealField"}); err == nil {
t.Fatal("expected error for unknown field name")
}
})
t.Run("rejects backend user fields", func(t *testing.T) {
for _, field := range []string{"id", "tokens", "backendScopes", "version", "totpSecret", "pinnedItems"} {
if err := validatePatchWhich([]string{field}); err == nil {
t.Fatalf("expected error for backend field %q", field)
}
}
})
}
func TestValidateNonAdminSelfPatchWhich(t *testing.T) {
t.Run("allows profile fields", func(t *testing.T) {
for _, which := range [][]string{
{"locale"},
{"darkMode", "viewMode"},
{"preview"},
{"sorting"},
} {
if err := validateNonAdminSelfPatchWhich(which); err != nil {
t.Fatalf("unexpected error for which=%v: %v", which, err)
}
}
})
t.Run("allows password and otpEnabled", func(t *testing.T) {
for _, which := range [][]string{
{"password"},
{"otpEnabled"},
{"locale", "password"},
} {
if err := validateNonAdminSelfPatchWhich(which); err != nil {
t.Fatalf("unexpected error for which=%v: %v", which, err)
}
}
})
t.Run("rejects passkey credentials field", func(t *testing.T) {
if err := validateNonAdminSelfPatchWhich([]string{"passkeyCredentials"}); err == nil {
t.Fatal("expected error for passkeyCredentials")
}
})
t.Run("rejects privileged fields", func(t *testing.T) {
for _, which := range [][]string{
{"scopes"},
{"permissions"},
{"sourcePermissions"},
{"loginMethod"},
{"lockPassword"},
{"disableSettings"},
// Admin-set: a user must not raise their own transcode limit.
{"maxConcurrentTranscodes"},
{"locale", "maxConcurrentTranscodes"},
} {
if err := validateNonAdminSelfPatchWhich(which); err == nil {
t.Fatalf("expected error for which=%v", which)
}
}
})
t.Run("requires password confirmation for maxConcurrentTranscodes", func(t *testing.T) {
if userPutOnlyNonAdminEditableFields([]string{"maxConcurrentTranscodes"}) {
t.Fatal("maxConcurrentTranscodes must not count as a non-admin-editable field")
}
if userPutOnlyNonAdminEditableFields([]string{"locale", "maxConcurrentTranscodes"}) {
t.Fatal("maxConcurrentTranscodes must not be exempt from password confirmation")
}
if !userPutOnlyNonAdminEditableFields([]string{"locale"}) {
t.Fatal("locale should remain non-admin editable")
}
})
t.Run("allows admins to patch maxConcurrentTranscodes", func(t *testing.T) {
if err := validatePatchWhich([]string{"maxConcurrentTranscodes"}); err != nil {
t.Fatalf("admins must be able to patch the limit: %v", err)
}
})
t.Run("rejects mixed privileged and profile fields", func(t *testing.T) {
if err := validateNonAdminSelfPatchWhich([]string{"locale", "scopes"}); err == nil {
t.Fatal("expected error when which mixes profile and privileged fields")
}
})
}