Skip to content

Commit 3f22577

Browse files
committed
test(runtime): align token identity regression coverage
1 parent 71a0d96 commit 3f22577

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

test/index-retry.test.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,19 @@ vi.mock("../lib/accounts.js", async () => {
126126
storedEmail?: string;
127127
accessToken?: string;
128128
}) => {
129+
const tokenUtilsModule = tokenUtils as typeof import("../lib/auth/token-utils.js");
129130
const tokenAccountId = accessToken ? "account-1" : undefined;
131+
const tokenEmail = tokenUtilsModule.sanitizeEmail(
132+
tokenUtilsModule.extractAccountEmail(accessToken, undefined),
133+
);
134+
const sanitizedStoredEmail = tokenUtilsModule.sanitizeEmail(storedEmail);
130135
return {
131-
accountId: (
132-
tokenUtils as typeof import("../lib/auth/token-utils.js")
133-
).resolveRequestAccountId(storedAccountId, source as never, tokenAccountId),
134-
email: storedEmail ?? "user@example.com",
136+
accountId: tokenUtilsModule.resolveRequestAccountId(
137+
storedAccountId,
138+
source as never,
139+
tokenAccountId,
140+
),
141+
email: tokenEmail ?? sanitizedStoredEmail ?? "user@example.com",
135142
tokenAccountId,
136143
};
137144
},

test/token-utils.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,66 @@ describe("Token Utils Module", () => {
755755
});
756756
});
757757

758+
it("follows token identities when the binding is id_token-derived", () => {
759+
mockedDecodeJWT.mockImplementation((token?: string) => {
760+
if (token === "access-token") {
761+
return {
762+
[JWT_CLAIM_PATH]: {
763+
chatgpt_account_id: "acc_id_token",
764+
},
765+
};
766+
}
767+
if (token === "id-token") {
768+
return {
769+
email: "id-token@example.com",
770+
};
771+
}
772+
return null;
773+
});
774+
775+
expect(
776+
resolveRuntimeRequestIdentity({
777+
storedAccountId: "workspace-alpha",
778+
source: "id_token",
779+
storedEmail: "stored@example.com",
780+
accessToken: "access-token",
781+
idToken: "id-token",
782+
}),
783+
).toEqual({
784+
accountId: "acc_id_token",
785+
email: "id-token@example.com",
786+
tokenAccountId: "acc_id_token",
787+
});
788+
});
789+
790+
it("falls back to the token accountId when token-derived routing has no stored accountId", () => {
791+
mockedDecodeJWT.mockImplementation((token?: string) => {
792+
if (token === "access-token") {
793+
return {
794+
[JWT_CLAIM_PATH]: {
795+
chatgpt_account_id: "acc_live",
796+
email: "live@example.com",
797+
},
798+
};
799+
}
800+
return null;
801+
});
802+
803+
expect(
804+
resolveRuntimeRequestIdentity({
805+
source: "token",
806+
storedEmail: "stored@example.com",
807+
accessToken: "access-token",
808+
}),
809+
).toEqual({
810+
accountId: "acc_live",
811+
email: "live@example.com",
812+
tokenAccountId: "acc_live",
813+
});
814+
});
815+
758816
it("falls back to sanitized stored email when the live token has none", () => {
817+
mockedDecodeJWT.mockReturnValue(null);
759818
expect(
760819
resolveRuntimeRequestIdentity({
761820
storedAccountId: "workspace-alpha",

0 commit comments

Comments
 (0)