Skip to content

Commit d4d0091

Browse files
committed
test: share msteams safe fetch assertions
1 parent 9ecd189 commit d4d0091

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

extensions/msteams/src/attachments/shared.test.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ function mockFetchWithRedirect(redirectMap: Record<string, string>, finalBody =
3131
});
3232
}
3333

34+
async function expectSafeFetchStatus(params: {
35+
fetchMock: ReturnType<typeof vi.fn>;
36+
url: string;
37+
allowHosts: string[];
38+
expectedStatus: number;
39+
resolveFn?: typeof publicResolve;
40+
}) {
41+
const res = await safeFetch({
42+
url: params.url,
43+
allowHosts: params.allowHosts,
44+
fetchFn: params.fetchMock as unknown as typeof fetch,
45+
resolveFn: params.resolveFn ?? publicResolve,
46+
});
47+
expect(res.status).toBe(params.expectedStatus);
48+
return res;
49+
}
50+
3451
describe("msteams attachment allowlists", () => {
3552
it("normalizes wildcard host lists", () => {
3653
expect(resolveAllowedHosts(["*", "graph.microsoft.com"])).toEqual(["*"]);
@@ -121,13 +138,12 @@ describe("safeFetch", () => {
121138
const fetchMock = vi.fn(async (_url: string, _init?: RequestInit) => {
122139
return new Response("ok", { status: 200 });
123140
});
124-
const res = await safeFetch({
141+
await expectSafeFetchStatus({
142+
fetchMock,
125143
url: "https://teams.sharepoint.com/file.pdf",
126144
allowHosts: ["sharepoint.com"],
127-
fetchFn: fetchMock as unknown as typeof fetch,
128-
resolveFn: publicResolve,
145+
expectedStatus: 200,
129146
});
130-
expect(res.status).toBe(200);
131147
expect(fetchMock).toHaveBeenCalledOnce();
132148
// Should have used redirect: "manual"
133149
expect(fetchMock.mock.calls[0][1]).toHaveProperty("redirect", "manual");
@@ -137,13 +153,12 @@ describe("safeFetch", () => {
137153
const fetchMock = mockFetchWithRedirect({
138154
"https://teams.sharepoint.com/file.pdf": "https://cdn.sharepoint.com/storage/file.pdf",
139155
});
140-
const res = await safeFetch({
156+
await expectSafeFetchStatus({
157+
fetchMock,
141158
url: "https://teams.sharepoint.com/file.pdf",
142159
allowHosts: ["sharepoint.com"],
143-
fetchFn: fetchMock as unknown as typeof fetch,
144-
resolveFn: publicResolve,
160+
expectedStatus: 200,
145161
});
146-
expect(res.status).toBe(200);
147162
expect(fetchMock).toHaveBeenCalledTimes(2);
148163
});
149164

0 commit comments

Comments
 (0)