-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackApiV3.test.ts
More file actions
234 lines (208 loc) · 8.57 KB
/
Copy pathstackApiV3.test.ts
File metadata and controls
234 lines (208 loc) · 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { afterEach, describe, expect, it, vi } from "vitest";
import { StackApiV3Client } from "./stackApiV3";
afterEach(() => {
vi.unstubAllGlobals();
});
describe("StackApiV3Client", () => {
it("fetches totalPages pagination", async () => {
const fetchMock = vi.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({ items: [{ id: "a" }], totalPages: 2 }), { status: 200 }))
.mockResolvedValueOnce(new Response(JSON.stringify({ items: [{ id: "b" }], totalPages: 2 }), { status: 200 }));
const client = new StackApiV3Client({
apiV3Url: "https://api.stackoverflowteams.com/v3/teams/example-team",
token: "token",
fetchFn: fetchMock,
});
await expect(client.getPagedItems("/tags")).resolves.toEqual([{ id: "a" }, { id: "b" }]);
expect(fetchMock.mock.calls[1][0].toString()).toContain("page=2");
});
it("stops pagination at the requested max pages", async () => {
const fetchMock = vi.fn()
.mockResolvedValueOnce(new Response(JSON.stringify({ items: [{ id: "a" }], totalPages: 2 }), { status: 200 }))
.mockResolvedValueOnce(new Response(JSON.stringify({ items: [{ id: "b" }], totalPages: 2 }), { status: 200 }));
const client = new StackApiV3Client({
apiV3Url: "https://api.stackoverflowteams.com/v3/teams/example-team",
token: "token",
fetchFn: fetchMock,
});
await expect(client.getPagedItems("/tags", {}, { maxPages: 1 })).resolves.toEqual([{ id: "a" }]);
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it("calls the throttle callback when token bucket is low", async () => {
const wait = vi.fn();
const fetchMock = vi.fn().mockResolvedValue(
new Response(JSON.stringify({ items: [], totalPages: 1 }), {
status: 200,
headers: {
"x-token-bucket-calls-left": "25",
"x-token-bucket-seconds-until-next-refill": "60",
},
}),
);
const client = new StackApiV3Client({
apiV3Url: "https://demo.stackenterprise.co/api/v3",
token: "token",
fetchFn: fetchMock,
onThrottle: wait,
});
await client.getPagedItems("/users");
expect(wait).toHaveBeenCalledWith({ kind: "token-bucket", seconds: 60, remaining: 25 });
});
it("calls the default browser fetch with the global receiver", async () => {
const fetchMock = vi.fn(function (this: unknown) {
if (this !== globalThis) {
throw new TypeError("Illegal invocation");
}
return Promise.resolve(
new Response(JSON.stringify({ items: [{ id: "community" }], totalPages: 1 }), {
status: 200,
}),
);
});
vi.stubGlobal("fetch", fetchMock);
const client = new StackApiV3Client({
apiV3Url: "https://api.stackoverflowteams.com/v3/teams/example-team",
token: "token",
});
await expect(client.getPagedItems("/communities")).resolves.toEqual([{ id: "community" }]);
});
it("retrieves a user by email", async () => {
const fetchMock = vi.fn().mockResolvedValue(
new Response(JSON.stringify({ id: 42, email: "ada@example.com", name: "Ada Lovelace" }), { status: 200 }),
);
const client = new StackApiV3Client({
apiV3Url: "https://demo.stackenterprise.co/api/v3",
token: "token",
fetchFn: fetchMock,
});
await expect(client.getUserByEmail("ada+vrm@example.com")).resolves.toEqual({
id: 42,
email: "ada@example.com",
name: "Ada Lovelace",
});
expect(fetchMock.mock.calls[0][0].toString()).toBe(
"https://demo.stackenterprise.co/api/v3/users/by-email/ada%2Bvrm%40example.com",
);
expect(fetchMock.mock.calls[0][1]).toEqual(
expect.objectContaining({
headers: expect.objectContaining({ Authorization: "Bearer token", "Content-Type": "application/json" }),
}),
);
});
it("returns null when user lookup by email is not found", async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response("not found", { status: 404 }));
const client = new StackApiV3Client({
apiV3Url: "https://demo.stackenterprise.co/api/v3",
token: "token",
fetchFn: fetchMock,
});
await expect(client.getUserByEmail("missing@example.com")).resolves.toBeNull();
});
it("retrieves user groups with page size, pagination, and bearer auth", async () => {
const fetchMock = vi.fn()
.mockResolvedValueOnce(
new Response(JSON.stringify({ items: [{ id: 7, name: "Ada Lovelace VRM", users: [] }], totalPages: 2 }), {
status: 200,
}),
)
.mockResolvedValueOnce(
new Response(JSON.stringify({ items: [{ id: 8, name: "Alan Turing VRM", users: [] }], totalPages: 2 }), {
status: 200,
}),
);
const client = new StackApiV3Client({
apiV3Url: "https://demo.stackenterprise.co/api/v3",
token: "token",
fetchFn: fetchMock,
});
await expect(client.getUserGroups()).resolves.toEqual([
{ id: 7, name: "Ada Lovelace VRM", users: [] },
{ id: 8, name: "Alan Turing VRM", users: [] },
]);
expect(fetchMock.mock.calls[0][0].toString()).toBe(
"https://demo.stackenterprise.co/api/v3/user-groups?pageSize=100&page=1",
);
expect(fetchMock.mock.calls[1][0].toString()).toBe(
"https://demo.stackenterprise.co/api/v3/user-groups?pageSize=100&page=2",
);
expect(fetchMock.mock.calls[0][1]).toEqual(
expect.objectContaining({
headers: expect.objectContaining({ Authorization: "Bearer token", "Content-Type": "application/json" }),
}),
);
expect(fetchMock.mock.calls[1][1]).toEqual(
expect.objectContaining({
headers: expect.objectContaining({ Authorization: "Bearer token", "Content-Type": "application/json" }),
}),
);
});
it("creates user groups and adds members with write access bearer auth", async () => {
const fetchMock = vi.fn()
.mockResolvedValueOnce(
new Response(JSON.stringify({ id: 7, name: "Ada Lovelace VRM", users: [] }), { status: 201 }),
)
.mockResolvedValueOnce(
new Response(JSON.stringify({ id: 7, name: "Ada Lovelace VRM", users: [{ id: 1 }] }), { status: 200 }),
);
const client = new StackApiV3Client({
apiV3Url: "https://demo.stackenterprise.co/api/v3",
token: "token",
fetchFn: fetchMock,
});
await expect(client.createUserGroup({ name: "Ada Lovelace VRM", userIds: [1, 2] })).resolves.toEqual(
expect.objectContaining({ id: 7, name: "Ada Lovelace VRM" }),
);
await expect(client.addUserGroupMembers(7, [3])).resolves.toEqual(
expect.objectContaining({ id: 7, name: "Ada Lovelace VRM" }),
);
expect(fetchMock.mock.calls[0][0].toString()).toBe("https://demo.stackenterprise.co/api/v3/user-groups");
expect(fetchMock.mock.calls[0][1]).toEqual(
expect.objectContaining({
method: "POST",
headers: expect.objectContaining({ Authorization: "Bearer token", "Content-Type": "application/json" }),
body: JSON.stringify({ name: "Ada Lovelace VRM", userIds: [1, 2] }),
}),
);
expect(fetchMock.mock.calls[1][0].toString()).toBe(
"https://demo.stackenterprise.co/api/v3/user-groups/7/members",
);
expect(fetchMock.mock.calls[1][1]).toEqual(
expect.objectContaining({
method: "POST",
headers: expect.objectContaining({ Authorization: "Bearer token", "Content-Type": "application/json" }),
body: JSON.stringify([3]),
}),
);
});
it("removes a group member with DELETE", async () => {
const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 204 }));
const client = new StackApiV3Client({
apiV3Url: "https://demo.stackenterprise.co/api/v3",
token: "token",
fetchFn: fetchMock,
});
await expect(client.removeUserGroupMember(7, 3)).resolves.toBeUndefined();
expect(fetchMock.mock.calls[0][0].toString()).toBe(
"https://demo.stackenterprise.co/api/v3/user-groups/7/members/3",
);
expect(fetchMock.mock.calls[0][1]).toEqual(
expect.objectContaining({
method: "DELETE",
headers: expect.objectContaining({ Authorization: "Bearer token", "Content-Type": "application/json" }),
}),
);
});
it("throws Stack API v3 errors when removing a group member fails", async () => {
const fetchMock = vi.fn().mockResolvedValue(
new Response(JSON.stringify({ title: "Forbidden" }), {
status: 403,
}),
);
const client = new StackApiV3Client({
apiV3Url: "https://demo.stackenterprise.co/api/v3",
token: "token",
fetchFn: fetchMock,
});
await expect(client.removeUserGroupMember(7, 3)).rejects.toThrow(/Stack API v3 request failed with 403/);
});
});