Frontend: Share role/provider option labels, untranslate identifiers

This commit is contained in:
Michael Mayer 2026-06-09 22:47:57 +00:00
parent 10dc504427
commit c61cc18e67
2 changed files with 75 additions and 3 deletions

View file

@ -1,6 +1,6 @@
import { $gettext } from "common/gettext";
// Providers maps account roles to their display name.
// Roles maps account roles to their display name.
export const Roles = () => {
return {
"admin": $gettext("Admin"),
@ -16,6 +16,17 @@ export const Roles = () => {
};
};
// RoleLabel returns the display name for an account role, or the key if unmapped.
export const RoleLabel = (role) => {
const labels = Roles();
return Object.prototype.hasOwnProperty.call(labels, role) ? labels[role] : role;
};
// RoleOptions builds {value, [labelKey]} options for the given role keys, with
// labels from the shared Roles() map. Editions pass their own keys (the sets
// differ); only the labels are shared. labelKey is "text" or "title".
export const RoleOptions = (roles, labelKey = "text") => roles.map((role) => ({ value: role, [labelKey]: RoleLabel(role) }));
// Providers maps authentication providers to their display name.
export const Providers = () => {
return {
@ -23,11 +34,11 @@ export const Providers = () => {
"default": $gettext("Default"),
"local": $gettext("Local"),
"client": $gettext("Client"),
"client_credentials": $gettext("Client Credentials"),
"client_credentials": "Client Credentials",
"application": $gettext("Application"),
"access_token": $gettext("Access Token"),
"password": $gettext("Local"),
"oidc": $gettext("OIDC"),
"oidc": "OIDC",
"ldap": $gettext("LDAP/AD"),
"link": $gettext("Link"),
"token": $gettext("Link"),
@ -35,6 +46,16 @@ export const Providers = () => {
};
};
// ProviderLabel returns the display name for an auth provider, or the key if unmapped.
export const ProviderLabel = (provider) => {
const labels = Providers();
return Object.prototype.hasOwnProperty.call(labels, provider) ? labels[provider] : provider;
};
// ProviderOptions builds {value, [labelKey]} options for the given provider
// keys, with labels from the shared Providers() map. Editions pass their own keys.
export const ProviderOptions = (providers, labelKey = "text") => providers.map((provider) => ({ value: provider, [labelKey]: ProviderLabel(provider) }));
// Methods maps authentication methods to their display name.
export const Methods = () => {
return {

View file

@ -0,0 +1,51 @@
import { describe, it, expect } from "vitest";
import { Roles, Providers, RoleLabel, RoleOptions, ProviderLabel, ProviderOptions } from "options/auth";
describe("options/auth RoleLabel", () => {
it("resolves mapped roles from the shared Roles map", () => {
expect(RoleLabel("admin")).toBe(Roles()["admin"]);
expect(RoleLabel("cluster_admin")).toBe(Roles()["cluster_admin"]);
});
it("falls back to the raw key when unmapped", () => {
expect(RoleLabel("does-not-exist")).toBe("does-not-exist");
});
});
describe("options/auth RoleOptions", () => {
it("builds {value, text} options in the given order with shared labels", () => {
expect(RoleOptions(["admin", "guest"])).toEqual([
{ value: "admin", text: Roles()["admin"] },
{ value: "guest", text: Roles()["guest"] },
]);
});
it("uses the requested label key (title for Vuetify item-title)", () => {
const [opt] = RoleOptions(["viewer"], "title");
expect(opt).toEqual({ value: "viewer", title: Roles()["viewer"] });
});
});
describe("options/auth ProviderLabel", () => {
it("resolves mapped providers from the shared Providers map", () => {
expect(ProviderLabel("oidc")).toBe(Providers()["oidc"]);
expect(ProviderLabel("ldap")).toBe(Providers()["ldap"]);
});
it("falls back to the raw key when unmapped", () => {
expect(ProviderLabel("does-not-exist")).toBe("does-not-exist");
});
});
describe("options/auth ProviderOptions", () => {
it("builds {value, text} options with shared labels", () => {
expect(ProviderOptions(["local", "oidc"])).toEqual([
{ value: "local", text: Providers()["local"] },
{ value: "oidc", text: Providers()["oidc"] },
]);
});
});
describe("options/auth standardized identifiers stay untranslated", () => {
it("keeps OIDC and Client Credentials as literal labels", () => {
expect(Providers()["oidc"]).toBe("OIDC");
expect(Providers()["client_credentials"]).toBe("Client Credentials");
});
});