test(auth): cover OIDC profile name fallbacks
This commit is contained in:
1 parent
05047ee5f7
commit
36f197aa6d
2 files changed
+31
-2
No files matched your search
@@ -12,10 +12,10 @@ export const getProfileValueByPath = (profile: Profile, path: string): unknown =
|
||||
}, profile);
|
||||
};
|
||||
|
||||
export const extractProfileName = (profile: Profile) => {
|
||||
export const extractProfileName = (profile: Profile): string | undefined => {
|
||||
if (!env.AUTH_OIDC_NAME_ATTRIBUTE_OVERWRITE) {
|
||||
// Use the name as the username if the preferred_username is an email address
|
||||
return profile.preferred_username?.includes("@") ? profile.name : profile.preferred_username;
|
||||
return (profile.preferred_username?.includes("@") ? profile.name : profile.preferred_username) ?? undefined;
|
||||
}
|
||||
|
||||
const profileName = getProfileValueByPath(profile, env.AUTH_OIDC_NAME_ATTRIBUTE_OVERWRITE);
|
||||
|
||||
@@ -37,6 +37,35 @@ describe("getProfileValueByPath", () => {
|
||||
});
|
||||
|
||||
describe("extractProfileName", () => {
|
||||
test("uses preferred_username when overwrite is not set", () => {
|
||||
const profile = {
|
||||
sub: "user-id",
|
||||
preferred_username: "johndoe",
|
||||
} satisfies Profile;
|
||||
|
||||
expect(extractProfileName(profile)).toBe("johndoe");
|
||||
});
|
||||
|
||||
test("uses name when preferred_username is an email", () => {
|
||||
const profile = {
|
||||
sub: "user-id",
|
||||
preferred_username: "john@example.com",
|
||||
name: "John Doe",
|
||||
} satisfies Profile;
|
||||
|
||||
expect(extractProfileName(profile)).toBe("John Doe");
|
||||
});
|
||||
|
||||
test("returns undefined when the fallback name is null", () => {
|
||||
const profile = {
|
||||
sub: "user-id",
|
||||
preferred_username: "john@example.com",
|
||||
name: null,
|
||||
} satisfies Profile;
|
||||
|
||||
expect(extractProfileName(profile)).toBeUndefined();
|
||||
});
|
||||
|
||||
test("uses a nested name claim when configured", () => {
|
||||
mockEnv.AUTH_OIDC_NAME_ATTRIBUTE_OVERWRITE = "user.profile.display_name";
|
||||
const profile = {
|
||||
|
||||
Reference in new issue
Block a user