accountRequestAuthScope.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { describe, expect, it, vi } from "vitest";
  2. import { createAccountProviderRequestAuthService } from "../src/model-provider/accountProviderRequestAuthService.js";
  3. function fixture() {
  4. let login = "login-a";
  5. let projectId = "project-a";
  6. let token = "pat-a";
  7. let duringIssue: (() => void) | undefined;
  8. const service = createAccountProviderRequestAuthService({
  9. resolveCurrentAccountAccess: async () => ({
  10. type: "zhipu-account",
  11. family: "bigmodel",
  12. planKind: "team-coding-plan",
  13. organizationId: "org",
  14. projectId,
  15. productId: "product",
  16. }),
  17. loadOAuthTokenSet: async () => ({ accessToken: login, zcodeJwtToken: "zcode-jwt" }),
  18. loadIndividualPlanMaterial: async () => null,
  19. resolveTeamPlanMaterial: async () => {
  20. duringIssue?.();
  21. return { token, apiKeyId: "key-id", organizationId: "org", projectId };
  22. },
  23. });
  24. const input = {
  25. providerId: "account:bigmodel-off-peak",
  26. accountAccess: { type: "zhipu-account", accountType: "bigmodel", mode: "off-peak" },
  27. reason: "off-peak",
  28. } as const;
  29. return {
  30. service,
  31. input,
  32. login: () => {
  33. login = "login-b";
  34. },
  35. project: () => {
  36. projectId = "project-b";
  37. },
  38. refresh: () => {
  39. token = "pat-b";
  40. },
  41. race: () => {
  42. duringIssue = () => {
  43. projectId = "project-b";
  44. };
  45. },
  46. };
  47. }
  48. describe("闲时请求的账号作用域", () => {
  49. it.each(["individual-coding-plan", "team-coding-plan"] as const)(
  50. "%s 普通请求返回作用域;恢复指纹只在核对身份后转交 owner",
  51. async (planKind) => {
  52. const individual = vi.fn(async () => ({
  53. token: "pat",
  54. apiKeyId: "key-id",
  55. organizationId: "org",
  56. projectId: "project",
  57. }));
  58. const team = vi.fn(async () => ({
  59. token: "pat",
  60. apiKeyId: "key-id",
  61. organizationId: "org",
  62. projectId: "project",
  63. }));
  64. const service = createAccountProviderRequestAuthService({
  65. resolveCurrentAccountAccess: async () => ({
  66. type: "zhipu-account",
  67. family: "bigmodel",
  68. planKind,
  69. organizationId: "org",
  70. projectId: "project",
  71. productId: "product",
  72. }),
  73. loadOAuthTokenSet: async () => ({ accessToken: "login" }),
  74. loadIndividualPlanMaterial: individual,
  75. resolveTeamPlanMaterial: team,
  76. });
  77. const input = {
  78. providerId: "account",
  79. accountAccess: { type: "zhipu-account", accountType: "bigmodel", mode: planKind },
  80. reason: "model-request",
  81. } as const;
  82. const initial = await service.resolveCurrent(input);
  83. const fingerprint = "b".repeat(64);
  84. await service.resolveCurrent({
  85. ...input,
  86. expectedAccountScope: initial.accountScope,
  87. rejectedProjectTokenFingerprint: fingerprint,
  88. });
  89. const called = planKind === "individual-coding-plan" ? individual : team;
  90. expect(called.mock.calls.at(-1)?.at(-1)).toBe(fingerprint);
  91. await expect(
  92. service.resolveCurrent({
  93. ...input,
  94. expectedAccountScope: "c".repeat(64),
  95. rejectedProjectTokenFingerprint: fingerprint,
  96. }),
  97. ).rejects.toThrow("scope_invalidated");
  98. await expect(
  99. service.resolveCurrent({ ...input, rejectedProjectTokenFingerprint: fingerprint }),
  100. ).rejects.toThrow("scope_invalidated");
  101. expect(called).toHaveBeenCalledTimes(2);
  102. },
  103. );
  104. it("跨 PAT 刷新保留作用域,不将短期 Token 当作身份", async () => {
  105. const f = fixture();
  106. const first = await f.service.resolveCurrent(f.input);
  107. expect(first.accountScope).toMatch(/^[a-f0-9]{64}$/);
  108. f.refresh();
  109. expect(
  110. await f.service.resolveCurrent({ ...f.input, expectedAccountScope: first.accountScope }),
  111. ).toEqual({ apiKey: "pat-b", apiKeyId: "key-id", accountScope: first.accountScope });
  112. });
  113. it.each(["login", "project", "race"] as const)(
  114. "%s 变化不能把原票据套在新身份上",
  115. async (change) => {
  116. const f = fixture();
  117. const first = await f.service.resolveCurrent(f.input);
  118. f[change]();
  119. await expect(
  120. f.service.resolveCurrent({ ...f.input, expectedAccountScope: first.accountScope }),
  121. ).rejects.toThrow("scope");
  122. },
  123. );
  124. });