From 36f197aa6d4c867a89582772fad4ada520f5025d Mon Sep 17 00:00:00 2001 From: arturict Date: Sun, 12 Jul 2026 11:08:12 +0200 Subject: [PATCH] test(auth): cover OIDC profile name fallbacks --- packages/auth/providers/oidc/profile.ts | 4 +-- .../auth/providers/oidc/test/profile.spec.ts | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/auth/providers/oidc/profile.ts b/packages/auth/providers/oidc/profile.ts index f4863da80..5cc445f3f 100644 --- a/packages/auth/providers/oidc/profile.ts +++ b/packages/auth/providers/oidc/profile.ts @@ -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); diff --git a/packages/auth/providers/oidc/test/profile.spec.ts b/packages/auth/providers/oidc/test/profile.spec.ts index aefac3753..a57e7868a 100644 --- a/packages/auth/providers/oidc/test/profile.spec.ts +++ b/packages/auth/providers/oidc/test/profile.spec.ts @@ -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 = {