|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | 2 | import { resolveAccountEntry, resolveNormalizedAccountEntry } from "./account-lookup.js"; |
3 | 3 |
|
| 4 | +function createAccountsWithPrototypePollution() { |
| 5 | + const inherited = { default: { id: "polluted" } }; |
| 6 | + return Object.create(inherited) as Record<string, { id: string }>; |
| 7 | +} |
| 8 | + |
| 9 | +function expectResolvedAccountLookupCase( |
| 10 | + actual: { id: string } | undefined, |
| 11 | + expected: { id: string } | undefined, |
| 12 | +) { |
| 13 | + expect(actual).toEqual(expected); |
| 14 | +} |
| 15 | + |
| 16 | +function expectPrototypePollutionIgnoredCase( |
| 17 | + resolve: (accounts: Record<string, { id: string }>) => { id: string } | undefined, |
| 18 | +) { |
| 19 | + const pollutedAccounts = createAccountsWithPrototypePollution(); |
| 20 | + expect(resolve(pollutedAccounts)).toBeUndefined(); |
| 21 | +} |
| 22 | + |
| 23 | +function expectAccountLookupCase(params: { |
| 24 | + accounts?: Record<string, { id: string }>; |
| 25 | + resolve: (accounts: Record<string, { id: string }>) => { id: string } | undefined; |
| 26 | + expected: { id: string } | undefined; |
| 27 | +}) { |
| 28 | + expectResolvedAccountLookupCase(params.resolve(params.accounts ?? {}), params.expected); |
| 29 | +} |
| 30 | + |
4 | 31 | describe("resolveAccountEntry", () => { |
5 | | - it("resolves direct and case-insensitive account keys", () => { |
6 | | - const accounts = { |
7 | | - default: { id: "default" }, |
8 | | - Business: { id: "business" }, |
9 | | - }; |
10 | | - expect(resolveAccountEntry(accounts, "default")).toEqual({ id: "default" }); |
11 | | - expect(resolveAccountEntry(accounts, "business")).toEqual({ id: "business" }); |
| 32 | + const accounts = { |
| 33 | + default: { id: "default" }, |
| 34 | + Business: { id: "business" }, |
| 35 | + }; |
| 36 | + |
| 37 | + it.each([ |
| 38 | + { |
| 39 | + name: "resolves the default account key", |
| 40 | + resolve: (localAccounts: Record<string, { id: string }>) => |
| 41 | + resolveAccountEntry(localAccounts, "default"), |
| 42 | + expected: { id: "default" }, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "resolves a normalized business account key", |
| 46 | + resolve: (localAccounts: Record<string, { id: string }>) => |
| 47 | + resolveAccountEntry(localAccounts, "business"), |
| 48 | + expected: { id: "business" }, |
| 49 | + }, |
| 50 | + ] as const)("$name", ({ resolve, expected }) => { |
| 51 | + expectAccountLookupCase({ accounts, resolve, expected }); |
12 | 52 | }); |
13 | 53 |
|
14 | 54 | it("ignores prototype-chain values", () => { |
15 | | - const inherited = { default: { id: "polluted" } }; |
16 | | - const accounts = Object.create(inherited) as Record<string, { id: string }>; |
17 | | - expect(resolveAccountEntry(accounts, "default")).toBeUndefined(); |
| 55 | + expectPrototypePollutionIgnoredCase((localAccounts) => |
| 56 | + resolveAccountEntry(localAccounts, "default"), |
| 57 | + ); |
18 | 58 | }); |
19 | 59 | }); |
20 | 60 |
|
21 | 61 | describe("resolveNormalizedAccountEntry", () => { |
22 | | - it("resolves normalized account keys with a custom normalizer", () => { |
23 | | - const accounts = { |
24 | | - "Ops Team": { id: "ops" }, |
25 | | - }; |
26 | | - |
27 | | - expect( |
28 | | - resolveNormalizedAccountEntry(accounts, "ops-team", (accountId) => |
29 | | - accountId.trim().toLowerCase().replaceAll(" ", "-"), |
30 | | - ), |
31 | | - ).toEqual({ id: "ops" }); |
32 | | - }); |
| 62 | + const normalizeAccountId = (accountId: string) => |
| 63 | + accountId.trim().toLowerCase().replaceAll(" ", "-"); |
33 | 64 |
|
34 | | - it("ignores prototype-chain values", () => { |
35 | | - const inherited = { default: { id: "polluted" } }; |
36 | | - const accounts = Object.create(inherited) as Record<string, { id: string }>; |
| 65 | + it.each([ |
| 66 | + { |
| 67 | + name: "resolves normalized account keys with a custom normalizer", |
| 68 | + accounts: { |
| 69 | + "Ops Team": { id: "ops" }, |
| 70 | + }, |
| 71 | + resolve: (accounts: Record<string, { id: string }>) => |
| 72 | + resolveNormalizedAccountEntry(accounts, "ops-team", normalizeAccountId), |
| 73 | + expected: { |
| 74 | + id: "ops", |
| 75 | + }, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: "ignores prototype-chain values", |
| 79 | + resolve: () => undefined, |
| 80 | + expected: undefined, |
| 81 | + assert: () => |
| 82 | + expectPrototypePollutionIgnoredCase((accounts) => |
| 83 | + resolveNormalizedAccountEntry(accounts, "default", (accountId) => accountId), |
| 84 | + ), |
| 85 | + }, |
| 86 | + ] as const)("$name", ({ accounts, resolve, expected, assert }) => { |
| 87 | + if (assert) { |
| 88 | + assert(); |
| 89 | + return; |
| 90 | + } |
37 | 91 |
|
38 | | - expect( |
39 | | - resolveNormalizedAccountEntry(accounts, "default", (accountId) => accountId), |
40 | | - ).toBeUndefined(); |
| 92 | + expectAccountLookupCase({ |
| 93 | + accounts, |
| 94 | + resolve, |
| 95 | + expected, |
| 96 | + }); |
41 | 97 | }); |
42 | 98 | }); |
0 commit comments