@panneau/protocol 5.0.0-alpha.2 → 5.0.0-alpha.20
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/addresses.d.ts +23 -0
- package/dist/addresses.d.ts.map +1 -0
- package/dist/addresses.js +52 -0
- package/dist/addresses.js.map +1 -0
- package/dist/compound.d.ts +20 -0
- package/dist/compound.d.ts.map +1 -0
- package/dist/compound.js +27 -0
- package/dist/compound.js.map +1 -0
- package/dist/conditions.d.ts +1 -1
- package/dist/conformance/index.d.ts +6 -0
- package/dist/conformance/index.d.ts.map +1 -1
- package/dist/conformance/index.js +787 -11
- package/dist/conformance/index.js.map +1 -1
- package/dist/constants.d.ts +101 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +174 -3
- package/dist/constants.js.map +1 -1
- package/dist/context.d.ts +15 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +34 -0
- package/dist/context.js.map +1 -0
- package/dist/currencies.d.ts +22 -0
- package/dist/currencies.d.ts.map +1 -0
- package/dist/currencies.js +231 -0
- package/dist/currencies.js.map +1 -0
- package/dist/index.d.ts +4 -4
- package/dist/index.js +17 -7
- package/dist/libraries.d.ts +26 -0
- package/dist/libraries.d.ts.map +1 -0
- package/dist/libraries.js +22 -0
- package/dist/libraries.js.map +1 -0
- package/dist/links.d.ts +38 -0
- package/dist/links.d.ts.map +1 -0
- package/dist/links.js +65 -0
- package/dist/links.js.map +1 -0
- package/dist/lint.d.ts +4 -3
- package/dist/lint.d.ts.map +1 -1
- package/dist/lint.js +114 -2
- package/dist/lint.js.map +1 -1
- package/dist/mock/auth.d.ts +71 -0
- package/dist/mock/auth.d.ts.map +1 -0
- package/dist/mock/auth.js +346 -0
- package/dist/mock/auth.js.map +1 -0
- package/dist/mock/example.d.ts +3 -2
- package/dist/mock/example.d.ts.map +1 -1
- package/dist/mock/example.js +223 -142
- package/dist/mock/example.js.map +1 -1
- package/dist/mock/index.d.ts +77 -7
- package/dist/mock/index.d.ts.map +1 -1
- package/dist/mock/index.js +577 -34
- package/dist/mock/index.js.map +1 -1
- package/dist/openapi.d.ts.map +1 -1
- package/dist/openapi.js +394 -145
- package/dist/openapi.js.map +1 -1
- package/dist/problem.d.ts +1 -1
- package/dist/query.d.ts +18 -0
- package/dist/query.d.ts.map +1 -1
- package/dist/query.js +34 -8
- package/dist/query.js.map +1 -1
- package/dist/rules.d.ts +16 -4
- package/dist/rules.d.ts.map +1 -1
- package/dist/rules.js +230 -13
- package/dist/rules.js.map +1 -1
- package/dist/runtime/index.d.ts +19 -10
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +12 -3
- package/dist/schemas/action.d.ts +117 -10
- package/dist/schemas/action.d.ts.map +1 -1
- package/dist/schemas/action.js +21 -3
- package/dist/schemas/action.js.map +1 -1
- package/dist/schemas/api.d.ts +326 -42
- package/dist/schemas/api.d.ts.map +1 -1
- package/dist/schemas/api.js +192 -34
- package/dist/schemas/api.js.map +1 -1
- package/dist/schemas/boot.d.ts +25 -1
- package/dist/schemas/boot.d.ts.map +1 -1
- package/dist/schemas/boot.js +2 -1
- package/dist/schemas/boot.js.map +1 -1
- package/dist/schemas/common.d.ts +1 -0
- package/dist/schemas/common.d.ts.map +1 -1
- package/dist/schemas/common.js +2 -1
- package/dist/schemas/common.js.map +1 -1
- package/dist/schemas/definition.d.ts +332 -28
- package/dist/schemas/definition.d.ts.map +1 -1
- package/dist/schemas/definition.js +65 -19
- package/dist/schemas/definition.js.map +1 -1
- package/dist/schemas/field.d.ts +356 -2
- package/dist/schemas/field.d.ts.map +1 -1
- package/dist/schemas/field.js +159 -20
- package/dist/schemas/field.js.map +1 -1
- package/dist/schemas/index.d.ts +1384 -72
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +58 -9
- package/dist/schemas/index.js.map +1 -1
- package/dist/schemas/jsonRules.d.ts +83 -0
- package/dist/schemas/jsonRules.d.ts.map +1 -0
- package/dist/schemas/jsonRules.js +46 -0
- package/dist/schemas/jsonRules.js.map +1 -0
- package/dist/schemas/layout.d.ts +1 -1
- package/dist/schemas/list.d.ts +23 -2
- package/dist/schemas/list.d.ts.map +1 -1
- package/dist/schemas/list.js +14 -5
- package/dist/schemas/list.js.map +1 -1
- package/dist/schemas/resource.d.ts +247 -15
- package/dist/schemas/resource.d.ts.map +1 -1
- package/dist/schemas/resource.js +6 -1
- package/dist/schemas/resource.js.map +1 -1
- package/dist/schemas/widget.d.ts +109 -0
- package/dist/schemas/widget.d.ts.map +1 -0
- package/dist/schemas/widget.js +74 -0
- package/dist/schemas/widget.js.map +1 -0
- package/dist/search.d.ts +51 -0
- package/dist/search.d.ts.map +1 -0
- package/dist/search.js +53 -0
- package/dist/search.js.map +1 -0
- package/dist/themes.d.ts +1 -1
- package/dist/transforms.d.ts +17 -0
- package/dist/transforms.d.ts.map +1 -0
- package/dist/transforms.js +34 -0
- package/dist/transforms.js.map +1 -0
- package/dist/validate.d.ts +5 -3
- package/dist/validate.d.ts.map +1 -1
- package/dist/validate.js +36 -2
- package/dist/validate.js.map +1 -1
- package/dist/values.d.ts +5 -1
- package/dist/values.d.ts.map +1 -1
- package/dist/values.js +16 -2
- package/dist/values.js.map +1 -1
- package/dist/widgets.d.ts +42 -0
- package/dist/widgets.d.ts.map +1 -0
- package/dist/widgets.js +130 -0
- package/dist/widgets.js.map +1 -0
- package/package.json +6 -3
- package/schema/boot.schema.json +666 -18
- package/schema/definition.schema.json +663 -18
- package/schema/openapi.json +5694 -2328
|
@@ -1,16 +1,33 @@
|
|
|
1
1
|
import { PROBLEM_CONTENT_TYPE } from "../version.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { CONTEXT_HEADER, CONTEXT_PROBLEM_TYPE } from "../constants.js";
|
|
3
|
+
import { isEqual, isFilled } from "../values.js";
|
|
4
|
+
import { getLinkTypes } from "../links.js";
|
|
5
|
+
import { isCursorPage, toSearchParams } from "../query.js";
|
|
6
|
+
import { getWidgetResource, isBuiltInWidgetType } from "../widgets.js";
|
|
7
|
+
import { isSearchableResource, toSearchQueryParams } from "../search.js";
|
|
8
|
+
import { formatContextHeader } from "../context.js";
|
|
9
|
+
import { widgetDataSchemas } from "../schemas/widget.js";
|
|
10
|
+
import { Definition, User } from "../schemas/definition.js";
|
|
11
|
+
import { ActionResult, ContextOptionsResponse, FieldCheckResponse, ItemResponse, ListResponse, Passkey, Problem, SearchResponse, TwoFactorStatus, UploadSignResponse, WebAuthnOptions } from "../schemas/api.js";
|
|
12
|
+
import { z } from "zod";
|
|
6
13
|
//#region src/conformance/index.ts
|
|
7
14
|
var UNKNOWN = "__panneau_conformance_unknown__";
|
|
15
|
+
var AccountResponse = z.looseObject({ data: User });
|
|
16
|
+
var PasskeysResponse = z.looseObject({ data: z.array(Passkey) });
|
|
8
17
|
function describeIssues(error) {
|
|
9
18
|
return error.issues.slice(0, 10).map((issue) => {
|
|
10
19
|
const path = issue.path.map(String).join(".");
|
|
11
20
|
return path === "" ? issue.message : `${path}: ${issue.message}`;
|
|
12
21
|
});
|
|
13
22
|
}
|
|
23
|
+
/** The first text of a value: a string, or the first string of a localized value. */
|
|
24
|
+
function textOf(value) {
|
|
25
|
+
if (typeof value === "string") return value;
|
|
26
|
+
if (value !== null && typeof value === "object" && !Array.isArray(value)) {
|
|
27
|
+
for (const entry of Object.values(value)) if (typeof entry === "string" && entry.trim() !== "") return entry;
|
|
28
|
+
}
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
14
31
|
function normalizeEtag(value) {
|
|
15
32
|
return value.replace(/^W\//, "");
|
|
16
33
|
}
|
|
@@ -50,6 +67,7 @@ async function runConformance(options) {
|
|
|
50
67
|
const request = async (path, init = {}) => {
|
|
51
68
|
const headers = new Headers(options.init?.headers);
|
|
52
69
|
headers.set("Accept", "application/json");
|
|
70
|
+
if (options.context !== void 0 && Object.keys(options.context).length > 0) headers.set(CONTEXT_HEADER, formatContextHeader(options.context));
|
|
53
71
|
new Headers(init.headers).forEach((value, key) => headers.set(key, value));
|
|
54
72
|
if (init.body !== void 0) headers.set("Content-Type", "application/json");
|
|
55
73
|
const response = await fetcher(`${baseUrl}${path}`, {
|
|
@@ -105,20 +123,203 @@ async function runConformance(options) {
|
|
|
105
123
|
resource = (options.resource !== void 0 ? current.resources.find((candidate) => candidate.id === options.resource) : current.resources[0]) ?? null;
|
|
106
124
|
check.expect(resource !== null, options.resource !== void 0 ? `The definition has no resource "${options.resource}".` : "The definition has no resources.");
|
|
107
125
|
});
|
|
126
|
+
const contextParams = () => definition?.context?.params ?? [];
|
|
127
|
+
let contextValue = null;
|
|
128
|
+
await run("context.options", "GET /context/{param}/options gives the choices of the params of the context", async (check) => {
|
|
129
|
+
const params = contextParams();
|
|
130
|
+
if (params.length === 0) {
|
|
131
|
+
check.skip("The definition has no context.");
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const values = {};
|
|
135
|
+
for (const param of params) {
|
|
136
|
+
if (param.fixed === true) continue;
|
|
137
|
+
let first = param.options?.[0]?.value;
|
|
138
|
+
if (param.options === void 0) {
|
|
139
|
+
const reply = await request(`/context/${encodeURIComponent(param.name)}/options`, { headers: { [CONTEXT_HEADER]: formatContextHeader(values) } });
|
|
140
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
141
|
+
first = check.expectSchema(ContextOptionsResponse, reply.body, `The choices of "${param.name}"`)?.data[0]?.value;
|
|
142
|
+
}
|
|
143
|
+
const value = options.context?.[param.name] ?? first;
|
|
144
|
+
if (value === void 0) return;
|
|
145
|
+
values[param.name] = value;
|
|
146
|
+
contextValue ??= {
|
|
147
|
+
name: param.name,
|
|
148
|
+
value
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
await run("context.value", "GET /definition is built for the values of Panneau-Context", async (check) => {
|
|
153
|
+
const chosen = contextValue;
|
|
154
|
+
if (chosen === null) {
|
|
155
|
+
check.skip(contextParams().length === 0 ? "The definition has no context." : "No choice of the first param to send.");
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
const reply = await request("/definition", { headers: { [CONTEXT_HEADER]: formatContextHeader({ [chosen.name]: chosen.value }) } });
|
|
159
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
160
|
+
const param = check.expectSchema(Definition, reply.body, "The definition")?.context?.params.find(({ name }) => name === chosen.name);
|
|
161
|
+
check.expect(param?.value === chosen.value, `The definition was built for ${chosen.name}=${String(param?.value ?? null)}, expected ${chosen.value}.`);
|
|
162
|
+
});
|
|
163
|
+
await run("context.refused", "A value of the context that does not exist is a 403 context problem", async (check) => {
|
|
164
|
+
const param = contextParams()[0];
|
|
165
|
+
if (param === void 0) {
|
|
166
|
+
check.skip("The definition has no context.");
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
const reply = await request("/definition", { headers: { [CONTEXT_HEADER]: formatContextHeader({ [param.name]: UNKNOWN }) } });
|
|
170
|
+
const problem = check.expectProblem(reply, 403);
|
|
171
|
+
if (problem !== null) {
|
|
172
|
+
check.expect(problem.type === CONTEXT_PROBLEM_TYPE, `Expected the type ${CONTEXT_PROBLEM_TYPE}, got ${problem.type}.`);
|
|
173
|
+
check.expect(problem.param === param.name, `Expected the param "${param.name}", got ${String(problem.param ?? "none")}.`);
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
const auth = () => definition?.auth;
|
|
177
|
+
const NOBODY = "nobody@conformance.invalid";
|
|
178
|
+
await run("auth.password-forgot", "POST /auth/password/forgot answers the same for an unknown address, and refuses an invalid one", async (check) => {
|
|
179
|
+
if (auth()?.passwordReset !== true) {
|
|
180
|
+
check.skip("The definition has no `auth.passwordReset`.");
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
check.expectStatus(await request("/auth/password/forgot", {
|
|
184
|
+
method: "POST",
|
|
185
|
+
body: JSON.stringify({ email: NOBODY })
|
|
186
|
+
}), 204);
|
|
187
|
+
const invalid = check.expectProblem(await request("/auth/password/forgot", {
|
|
188
|
+
method: "POST",
|
|
189
|
+
body: "{}"
|
|
190
|
+
}), 422);
|
|
191
|
+
if (invalid !== null) check.expect((invalid.errors?.email?.length ?? 0) > 0, "Expected a message in `errors.email`.");
|
|
192
|
+
});
|
|
193
|
+
await run("auth.password-reset", "POST /auth/password/reset refuses a token that does not exist", async (check) => {
|
|
194
|
+
if (auth()?.passwordReset !== true) {
|
|
195
|
+
check.skip("The definition has no `auth.passwordReset`.");
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
check.expectProblem(await request("/auth/password/reset", {
|
|
199
|
+
method: "POST",
|
|
200
|
+
body: JSON.stringify({
|
|
201
|
+
token: UNKNOWN,
|
|
202
|
+
email: NOBODY,
|
|
203
|
+
password: "Conformance-Password-1"
|
|
204
|
+
})
|
|
205
|
+
}), 422);
|
|
206
|
+
});
|
|
207
|
+
await run("auth.email-verify", "POST /auth/email/verify refuses a token that does not exist", async (check) => {
|
|
208
|
+
if (auth()?.emailVerification !== true) {
|
|
209
|
+
check.skip("The definition has no `auth.emailVerification`.");
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
const invalid = check.expectProblem(await request("/auth/email/verify", {
|
|
213
|
+
method: "POST",
|
|
214
|
+
body: JSON.stringify({ token: UNKNOWN })
|
|
215
|
+
}), 422);
|
|
216
|
+
if (invalid !== null) check.expect((invalid.errors?.token?.length ?? 0) > 0, "Expected a message in `errors.token`.");
|
|
217
|
+
});
|
|
218
|
+
await run("account", "GET /account gives the current user", async (check) => {
|
|
219
|
+
if (auth()?.account !== true) {
|
|
220
|
+
check.skip("The definition has no `auth.account`.");
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const reply = await request("/account");
|
|
224
|
+
if (check.expectStatus(reply, 200)) check.expectSchema(AccountResponse, reply.body, "The account");
|
|
225
|
+
});
|
|
226
|
+
await run("auth.providers", "GET /auth/providers/{provider}/redirect starts the login of each provider", async (check) => {
|
|
227
|
+
const providers = (auth()?.providers ?? []).filter(({ url }) => url !== void 0);
|
|
228
|
+
if (providers.length === 0) {
|
|
229
|
+
check.skip("The definition has no `auth.providers` with a `url`.");
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
for (const provider of providers) {
|
|
233
|
+
const reply = await request(`/auth/providers/${encodeURIComponent(provider.id)}/redirect?next=%2F`, { redirect: "manual" });
|
|
234
|
+
check.expect(reply.status === 0 || reply.status >= 301 && reply.status <= 308, `Expected a redirection for "${provider.id}", got ${reply.status}.`);
|
|
235
|
+
}
|
|
236
|
+
check.expectProblem(await request(`/auth/providers/${UNKNOWN}/redirect`, { redirect: "manual" }), 404);
|
|
237
|
+
});
|
|
238
|
+
await run("auth.passkeys", "POST /auth/passkeys/options gives options, and a login refuses an unknown passkey", async (check) => {
|
|
239
|
+
if (auth()?.passkeys !== true) {
|
|
240
|
+
check.skip("The definition has no `auth.passkeys`.");
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
const reply = await request("/auth/passkeys/options", { method: "POST" });
|
|
244
|
+
if (check.expectStatus(reply, 200)) {
|
|
245
|
+
const options = check.expectSchema(WebAuthnOptions, reply.body, "The options");
|
|
246
|
+
check.expect(typeof options?.options.challenge === "string", "Expected a `challenge` in the options.");
|
|
247
|
+
}
|
|
248
|
+
check.expectProblem(await request("/auth/passkeys/login", {
|
|
249
|
+
method: "POST",
|
|
250
|
+
body: JSON.stringify({ credential: {
|
|
251
|
+
id: UNKNOWN,
|
|
252
|
+
rawId: UNKNOWN,
|
|
253
|
+
type: "public-key",
|
|
254
|
+
response: {}
|
|
255
|
+
} })
|
|
256
|
+
}), 422);
|
|
257
|
+
});
|
|
258
|
+
await run("account.passkeys", "GET /account/passkeys lists the passkeys of the user", async (check) => {
|
|
259
|
+
if (auth()?.passkeys !== true) {
|
|
260
|
+
check.skip("The definition has no `auth.passkeys`.");
|
|
261
|
+
return;
|
|
262
|
+
}
|
|
263
|
+
const reply = await request("/account/passkeys");
|
|
264
|
+
if (check.expectStatus(reply, 200)) check.expectSchema(PasskeysResponse, reply.body, "The passkeys");
|
|
265
|
+
});
|
|
266
|
+
await run("account.two-factor", "GET /account/two-factor gives the two-factor authentication of the user", async (check) => {
|
|
267
|
+
if (auth()?.twoFactor !== true) {
|
|
268
|
+
check.skip("The definition has no `auth.twoFactor`.");
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
const reply = await request("/account/two-factor");
|
|
272
|
+
if (check.expectStatus(reply, 200)) check.expectSchema(TwoFactorStatus, reply.body, "The status");
|
|
273
|
+
});
|
|
274
|
+
await run("auth.two-factor-challenge", "POST /auth/two-factor/challenge refuses a code without a login that waits for it", async (check) => {
|
|
275
|
+
if (auth()?.twoFactor !== true) {
|
|
276
|
+
check.skip("The definition has no `auth.twoFactor`.");
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
check.expectProblem(await request("/auth/two-factor/challenge", {
|
|
280
|
+
method: "POST",
|
|
281
|
+
body: JSON.stringify({ code: "000000" })
|
|
282
|
+
}), 422);
|
|
283
|
+
});
|
|
108
284
|
const resourcePath = () => `/resources/${encodeURIComponent(resource?.id ?? "")}`;
|
|
109
285
|
await run("list", "GET /resources/{resource} returns a page of items", async (check) => {
|
|
110
286
|
if (resource === null) {
|
|
111
287
|
check.skip("No resource to test.");
|
|
112
288
|
return;
|
|
113
289
|
}
|
|
114
|
-
const
|
|
290
|
+
const cursors = resource.list?.pagination === "cursor";
|
|
291
|
+
const reply = await request(`${resourcePath()}?${cursors ? "" : "page=1&"}perPage=2`);
|
|
115
292
|
if (!check.expectStatus(reply, 200)) return;
|
|
116
293
|
const list = check.expectSchema(ListResponse, reply.body, "The list");
|
|
117
294
|
if (list === null) return;
|
|
118
295
|
check.expect(list.data.length <= 2, `perPage=2 returned ${list.data.length} items.`);
|
|
119
|
-
check.expect(list.meta.page === 1, `meta.page is ${list.meta.page}, expected 1.`);
|
|
120
296
|
check.expect(list.meta.perPage === 2, `meta.perPage is ${list.meta.perPage}, expected 2.`);
|
|
121
297
|
firstItemId = list.data[0]?.id ?? null;
|
|
298
|
+
const { meta } = list;
|
|
299
|
+
if (!cursors) {
|
|
300
|
+
check.expect(!isCursorPage(meta) && meta.page === 1, `meta.page is ${String(meta.page)}, expected 1.`);
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
if (!isCursorPage(meta)) {
|
|
304
|
+
check.expect(false, "The list pages with cursors, but its meta has no nextCursor.");
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
if (meta.nextCursor !== null) {
|
|
308
|
+
const next = await request(`${resourcePath()}?${toSearchParams({
|
|
309
|
+
perPage: 2,
|
|
310
|
+
cursor: meta.nextCursor
|
|
311
|
+
})}`);
|
|
312
|
+
if (check.expectStatus(next, 200)) {
|
|
313
|
+
const repeated = (check.expectSchema(ListResponse, next.body, "The next page")?.data ?? []).filter((item) => list.data.some((first) => first.id === item.id));
|
|
314
|
+
check.expect(repeated.length === 0, `The page of nextCursor repeats items of the first page: ${repeated.map((item) => item.id).join(", ")}.`);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
const invalid = await request(`${resourcePath()}?${toSearchParams({
|
|
318
|
+
perPage: 2,
|
|
319
|
+
cursor: "panneau-conformance-invalid"
|
|
320
|
+
})}`);
|
|
321
|
+
const problem = check.expectProblem(invalid, 422);
|
|
322
|
+
check.expect(problem === null || (problem.errors?.cursor ?? []).length > 0, "An invalid cursor answers a 422 without errors.cursor.");
|
|
122
323
|
});
|
|
123
324
|
await run("list.sort", "GET /resources/{resource} accepts a sort", async (check) => {
|
|
124
325
|
const sort = resource?.list?.sorts?.[0];
|
|
@@ -144,15 +345,133 @@ async function runConformance(options) {
|
|
|
144
345
|
const all = await list(null, "The list");
|
|
145
346
|
if (all === null) return;
|
|
146
347
|
const dated = all.data.filter((item) => isFilled(item[filter.id])).length;
|
|
147
|
-
|
|
348
|
+
const ranges = [[{
|
|
148
349
|
from: "0001-01-01",
|
|
149
350
|
to: "9999-12-31"
|
|
150
|
-
}, "from 0001-01-01 to 9999-12-31"], [{ to: "9999-12-31" }, "up to 9999-12-31 (an open start)"]]
|
|
351
|
+
}, "from 0001-01-01 to 9999-12-31"], [{ to: "9999-12-31" }, "up to 9999-12-31 (an open start)"]];
|
|
352
|
+
const totalOf = (reply) => typeof reply.meta.total === "number" ? reply.meta.total : null;
|
|
353
|
+
for (const [range, label] of ranges) {
|
|
151
354
|
const reply = await list(range, `The list filtered ${label}`);
|
|
152
|
-
|
|
355
|
+
const total = reply !== null ? totalOf(reply) : null;
|
|
356
|
+
if (total !== null) check.expect(total >= dated, `Filtered ${label}, the list has ${total} items; the first page alone has ${dated} with a ${filter.id}.`);
|
|
153
357
|
}
|
|
154
358
|
const future = await list({ from: "9999-12-31" }, "The list filtered from 9999-12-31");
|
|
155
|
-
if (future !== null)
|
|
359
|
+
if (future !== null) {
|
|
360
|
+
const total = totalOf(future) ?? future.data.length;
|
|
361
|
+
check.expect(total === 0, `Filtered from 9999-12-31, the list has ${total} items, expected 0.`);
|
|
362
|
+
}
|
|
363
|
+
});
|
|
364
|
+
await run("action.dry-run", "A dry run of an action counts what it would do, and changes nothing", async (check) => {
|
|
365
|
+
const action = resource?.actions?.find((candidate) => candidate.dryRun === true && candidate.kind === "server" && candidate.scope.includes("selection"));
|
|
366
|
+
if (action === void 0) {
|
|
367
|
+
check.skip("The resource declares no selection action with dryRun.");
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
const page = async (label) => {
|
|
371
|
+
const reply = await request(`${resourcePath()}?page=1&perPage=20`);
|
|
372
|
+
return check.expectStatus(reply, 200) ? check.expectSchema(ListResponse, reply.body, label) : null;
|
|
373
|
+
};
|
|
374
|
+
const before = await page("The list before the dry run");
|
|
375
|
+
const reply = await request(`${resourcePath()}/actions/${encodeURIComponent(action.id)}`, {
|
|
376
|
+
method: "POST",
|
|
377
|
+
body: JSON.stringify({
|
|
378
|
+
selection: { query: {} },
|
|
379
|
+
dryRun: true
|
|
380
|
+
})
|
|
381
|
+
});
|
|
382
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
383
|
+
const result = check.expectSchema(ActionResult, reply.body, "The dry run result");
|
|
384
|
+
if (result !== null && before !== null) {
|
|
385
|
+
check.expect(result.summary !== void 0, "A dry run result must have a summary of the items that would succeed and be skipped.");
|
|
386
|
+
if (result.summary !== void 0) {
|
|
387
|
+
const counted = result.summary.succeeded + result.summary.failed + result.summary.skipped;
|
|
388
|
+
check.expect(counted === before.meta.total, `The dry run counts ${counted} items; the list has ${before.meta.total}.`);
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
const after = await page("The list after the dry run");
|
|
392
|
+
if (before !== null && after !== null) {
|
|
393
|
+
const versions = (list) => JSON.stringify(list.data.map((item) => [item.id, item.version ?? null]));
|
|
394
|
+
check.expect(after.meta.total === before.meta.total && versions(after) === versions(before), "A dry run must not change the items.");
|
|
395
|
+
}
|
|
396
|
+
});
|
|
397
|
+
await run("action.delete", "The built-in `delete` answers, with dry runs, without being declared", async (check) => {
|
|
398
|
+
const current = resource;
|
|
399
|
+
if (current === null) {
|
|
400
|
+
check.skip("No resource to test.");
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
if (current.itemActions !== void 0 && !current.itemActions.includes("delete")) {
|
|
404
|
+
check.skip("The resource leaves out `delete` (`itemActions`).");
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
if (firstItemId === null) {
|
|
408
|
+
check.skip("The list has no items.");
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
const reply = await request(`${resourcePath()}/actions/delete`, {
|
|
412
|
+
method: "POST",
|
|
413
|
+
body: JSON.stringify({
|
|
414
|
+
selection: { ids: [firstItemId] },
|
|
415
|
+
dryRun: true
|
|
416
|
+
})
|
|
417
|
+
});
|
|
418
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
419
|
+
const result = check.expectSchema(ActionResult, reply.body, "The dry run of `delete`");
|
|
420
|
+
if (result !== null) check.expect(result.summary !== void 0 && result.summary.succeeded + result.summary.skipped + result.summary.failed === 1, "The dry run of `delete` on one item must count it, as deleted or skipped.");
|
|
421
|
+
check.expectStatus(await request(`${resourcePath()}/${encodeURIComponent(firstItemId)}`), 200);
|
|
422
|
+
});
|
|
423
|
+
await run("uploads.sign", "POST /uploads/sign signs the first request of a file, and only the keys of open uploads", async (check) => {
|
|
424
|
+
const uploads = definition?.uploads;
|
|
425
|
+
if (uploads?.mode !== "presigned") {
|
|
426
|
+
check.skip("The definition has no presigned uploads.");
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
if (uploads.endpoint !== void 0) {
|
|
430
|
+
check.skip("The upload routes are not at {api.baseUrl}/uploads.");
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
const sign = (body) => request("/uploads/sign", {
|
|
434
|
+
method: "POST",
|
|
435
|
+
body: JSON.stringify(body)
|
|
436
|
+
});
|
|
437
|
+
const reply = await sign({
|
|
438
|
+
method: "POST",
|
|
439
|
+
key: "panneau-conformance.txt"
|
|
440
|
+
});
|
|
441
|
+
if (check.expectStatus(reply, 200)) {
|
|
442
|
+
const signed = check.expectSchema(UploadSignResponse, reply.body, "The signed request");
|
|
443
|
+
if (signed !== null) check.expect(signed.key !== void 0 && signed.upload !== void 0, "The first request of a file must answer the key and the upload the backend chose.");
|
|
444
|
+
}
|
|
445
|
+
check.expectProblem(await sign({
|
|
446
|
+
method: "PUT",
|
|
447
|
+
key: "not-an-upload-of-this-user",
|
|
448
|
+
uploadId: "unknown",
|
|
449
|
+
partNumber: 1
|
|
450
|
+
}), 404);
|
|
451
|
+
check.expectProblem(await request(`/uploads/${UNKNOWN}/complete`, { method: "POST" }), 404);
|
|
452
|
+
});
|
|
453
|
+
await run("fields.check", "POST /resources/{resource}/fields/{field}/check checks a value of a field with `check`", async (check) => {
|
|
454
|
+
const field = resource?.fields.find((candidate) => candidate.check === true);
|
|
455
|
+
if (resource === null || field === void 0) {
|
|
456
|
+
check.skip("The resource has no field with `check`.");
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
const checkPath = (name) => `${resourcePath()}/fields/${encodeURIComponent(name)}/check`;
|
|
460
|
+
const locale = field.localized === true ? definition?.locale : void 0;
|
|
461
|
+
const reply = await request(checkPath(field.name), {
|
|
462
|
+
method: "POST",
|
|
463
|
+
body: JSON.stringify({
|
|
464
|
+
value: "panneau-conformance",
|
|
465
|
+
...locale !== void 0 ? { locale } : {},
|
|
466
|
+
id: null,
|
|
467
|
+
values: {}
|
|
468
|
+
})
|
|
469
|
+
});
|
|
470
|
+
if (check.expectStatus(reply, 200)) check.expectSchema(FieldCheckResponse, reply.body, "The check");
|
|
471
|
+
check.expectProblem(await request(checkPath(UNKNOWN), {
|
|
472
|
+
method: "POST",
|
|
473
|
+
body: "{\"value\":\"x\"}"
|
|
474
|
+
}), 404);
|
|
156
475
|
});
|
|
157
476
|
await run("list.unknown-resource", "An unknown resource is a 404 problem", async (check) => {
|
|
158
477
|
check.expectProblem(await request(`/resources/${UNKNOWN}`), 404);
|
|
@@ -176,15 +495,257 @@ async function runConformance(options) {
|
|
|
176
495
|
}
|
|
177
496
|
check.expectProblem(await request(`${resourcePath()}/${UNKNOWN}`), 404);
|
|
178
497
|
});
|
|
498
|
+
const listItems = async (check) => {
|
|
499
|
+
const cursors = resource?.list?.pagination === "cursor";
|
|
500
|
+
const reply = await request(`${resourcePath()}?${cursors ? "" : "page=1&"}perPage=20`);
|
|
501
|
+
if (!check.expectStatus(reply, 200)) return null;
|
|
502
|
+
return check.expectSchema(ListResponse, reply.body, "The list")?.data ?? null;
|
|
503
|
+
};
|
|
504
|
+
await run("abilities.view-any", "The resources the user can’t list answer 403 on their list", async (check) => {
|
|
505
|
+
const denied = (definition?.resources ?? []).filter((candidate) => candidate.abilities?.viewAny === false);
|
|
506
|
+
if (denied.length === 0) {
|
|
507
|
+
check.skip("Every resource can be listed.");
|
|
508
|
+
return;
|
|
509
|
+
}
|
|
510
|
+
for (const candidate of denied) {
|
|
511
|
+
const reply = await request(`/resources/${encodeURIComponent(candidate.id)}`);
|
|
512
|
+
check.expect(reply.status === 403, `${candidate.id} has \`viewAny: false\`, but its list answered ${reply.status}.`);
|
|
513
|
+
}
|
|
514
|
+
});
|
|
515
|
+
await run("abilities.fields", "Items leave out the values of the fields the user can’t see", async (check) => {
|
|
516
|
+
const hidden = (resource?.fields ?? []).filter((field) => field.abilities?.view === false).map((field) => field.name);
|
|
517
|
+
if (hidden.length === 0) {
|
|
518
|
+
check.skip("The resource has no field with `view: false`.");
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
const items = await listItems(check);
|
|
522
|
+
if (items === null) return;
|
|
523
|
+
if (items.length === 0) {
|
|
524
|
+
check.skip("The list has no items.");
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
items.forEach((item) => {
|
|
528
|
+
const shown = hidden.filter((name) => name in item);
|
|
529
|
+
check.expect(shown.length === 0, `Item ${item.id} has values of fields with \`view: false\`: ${shown.join(", ")}.`);
|
|
530
|
+
});
|
|
531
|
+
});
|
|
532
|
+
await run("abilities.delete", "An item that can’t be deleted is skipped by a dry run of `delete`", async (check) => {
|
|
533
|
+
const current = resource;
|
|
534
|
+
if (current === null) {
|
|
535
|
+
check.skip("No resource to test.");
|
|
536
|
+
return;
|
|
537
|
+
}
|
|
538
|
+
if (current.itemActions !== void 0 && !current.itemActions.includes("delete")) {
|
|
539
|
+
check.skip("The resource leaves out `delete` (`itemActions`).");
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
const items = await listItems(check);
|
|
543
|
+
const denied = items?.find((item) => item.abilities.delete !== true);
|
|
544
|
+
if (items === null) return;
|
|
545
|
+
if (denied === void 0) {
|
|
546
|
+
check.skip("Every item of the first page can be deleted.");
|
|
547
|
+
return;
|
|
548
|
+
}
|
|
549
|
+
const allowed = items.find((item) => item.abilities.delete === true);
|
|
550
|
+
const ids = allowed !== void 0 ? [denied.id, allowed.id] : [denied.id];
|
|
551
|
+
const reply = await request(`${resourcePath()}/actions/delete`, {
|
|
552
|
+
method: "POST",
|
|
553
|
+
body: JSON.stringify({
|
|
554
|
+
selection: { ids },
|
|
555
|
+
dryRun: true
|
|
556
|
+
})
|
|
557
|
+
});
|
|
558
|
+
if (allowed === void 0 && reply.status === 403) return;
|
|
559
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
560
|
+
const result = check.expectSchema(ActionResult, reply.body, "The dry run of `delete`");
|
|
561
|
+
check.expect(result === null || result.summary?.skipped === 1 && result.summary.succeeded === ids.length - 1, `Item ${denied.id} can’t be deleted, but the dry run of \`delete\` doesn’t skip it.`);
|
|
562
|
+
});
|
|
563
|
+
let searched = null;
|
|
564
|
+
const searchSkip = () => {
|
|
565
|
+
const current = definition;
|
|
566
|
+
if (current === null) return "No valid definition.";
|
|
567
|
+
if (current.search?.enabled === false) return "The definition disables the global search.";
|
|
568
|
+
return searched === null ? "No searchable resource has an item with a title to search for." : null;
|
|
569
|
+
};
|
|
570
|
+
const search = (query) => request(`/search?${toSearchQueryParams(query)}`);
|
|
571
|
+
await run("search", "GET /search finds items of the searchable resources", async (check) => {
|
|
572
|
+
const current = definition;
|
|
573
|
+
const tested = resource;
|
|
574
|
+
if (current === null || current.search?.enabled === false) {
|
|
575
|
+
check.skip(searchSkip() ?? "");
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
const candidates = current.resources.filter((candidate) => isSearchableResource(candidate) && candidate.abilities?.viewAny !== false).sort((a, b) => Number(b.id === tested?.id) - Number(a.id === tested?.id));
|
|
579
|
+
for (const candidate of candidates) {
|
|
580
|
+
if (candidate.itemTitle === void 0 || searched !== null) continue;
|
|
581
|
+
const reply = await request(`/resources/${encodeURIComponent(candidate.id)}?page=1&perPage=1`);
|
|
582
|
+
const list = ListResponse.safeParse(reply.body);
|
|
583
|
+
const term = (list.success ? textOf(list.data.data[0]?.[candidate.itemTitle]) : null)?.split(/\s+/).find((word) => word.length >= 2);
|
|
584
|
+
if (term !== void 0) searched = {
|
|
585
|
+
resource: candidate,
|
|
586
|
+
term
|
|
587
|
+
};
|
|
588
|
+
}
|
|
589
|
+
const reason = searchSkip();
|
|
590
|
+
if (reason !== null || searched === null) {
|
|
591
|
+
check.skip(reason ?? "");
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
const { resource: target, term } = searched;
|
|
595
|
+
const reply = await search({ q: term });
|
|
596
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
597
|
+
const results = check.expectSchema(SearchResponse, reply.body, "The results");
|
|
598
|
+
if (results === null) return;
|
|
599
|
+
const ids = candidates.map((candidate) => candidate.id);
|
|
600
|
+
for (const group of results.data) {
|
|
601
|
+
check.expect(ids.includes(group.resource), `Results of "${group.resource}", which is not a searchable resource.`);
|
|
602
|
+
check.expect(group.items.length <= 5, `"${group.resource}" has ${group.items.length} results; without a limit, at most 5.`);
|
|
603
|
+
}
|
|
604
|
+
const count = results.data.reduce((total, group) => total + group.items.length, 0);
|
|
605
|
+
check.expect(count <= 20, `The results have ${count} items, at most 20.`);
|
|
606
|
+
check.expect(results.data.some((group) => group.resource === target.id && group.items.length > 0), `Searching "${term}", a word of the title of an item of "${target.id}", finds none of its items.`);
|
|
607
|
+
});
|
|
608
|
+
await run("search.scope", "GET /search reads limit and resources", async (check) => {
|
|
609
|
+
const reason = searchSkip();
|
|
610
|
+
if (reason !== null || searched === null) {
|
|
611
|
+
check.skip(reason ?? "");
|
|
612
|
+
return;
|
|
613
|
+
}
|
|
614
|
+
const { resource: target, term } = searched;
|
|
615
|
+
const reply = await search({
|
|
616
|
+
q: term,
|
|
617
|
+
limit: 1,
|
|
618
|
+
resources: [target.id]
|
|
619
|
+
});
|
|
620
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
621
|
+
const results = check.expectSchema(SearchResponse, reply.body, "The results");
|
|
622
|
+
if (results !== null) check.expect(results.data.length === 1 && results.data[0]?.resource === target.id && results.data[0].items.length === 1, `With limit=1&resources=${target.id}, expected one item of "${target.id}", got ${JSON.stringify(results.data.map((group) => [group.resource, group.items.length]))}.`);
|
|
623
|
+
});
|
|
624
|
+
await run("search.invalid", "An invalid search is a 422 problem, with errors keyed by parameter", async (check) => {
|
|
625
|
+
const reason = searchSkip();
|
|
626
|
+
if (reason !== null || searched === null) {
|
|
627
|
+
check.skip(reason ?? "");
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
630
|
+
const { term } = searched;
|
|
631
|
+
const invalid = [
|
|
632
|
+
["q", { q: term.slice(0, 1) }],
|
|
633
|
+
["limit", {
|
|
634
|
+
q: term,
|
|
635
|
+
limit: 0
|
|
636
|
+
}],
|
|
637
|
+
["limit", {
|
|
638
|
+
q: term,
|
|
639
|
+
limit: 21
|
|
640
|
+
}],
|
|
641
|
+
["resources", {
|
|
642
|
+
q: term,
|
|
643
|
+
resources: [UNKNOWN]
|
|
644
|
+
}]
|
|
645
|
+
];
|
|
646
|
+
for (const [parameter, query] of invalid) {
|
|
647
|
+
const failure = check.expectProblem(await search(query), 422);
|
|
648
|
+
if (failure !== null) check.expect(failure.errors?.[parameter] !== void 0, `${toSearchQueryParams(query)}: the 422 problem must have errors.${parameter}.`);
|
|
649
|
+
}
|
|
650
|
+
});
|
|
651
|
+
const widgets = () => definition?.dashboard?.widgets ?? [];
|
|
652
|
+
await run("dashboard.widgets", "GET /dashboard/widgets/{widget} answers each widget in the shape of its type", async (check) => {
|
|
653
|
+
const own = widgets().filter((widget) => getWidgetResource(widget) === null);
|
|
654
|
+
if (own.length === 0) {
|
|
655
|
+
check.skip("The dashboard has no widget with data of its own.");
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
for (const widget of own) {
|
|
659
|
+
const reply = await request(`/dashboard/widgets/${encodeURIComponent(widget.id)}`);
|
|
660
|
+
if (reply.status === 403) {
|
|
661
|
+
check.expectProblem(reply, 403);
|
|
662
|
+
continue;
|
|
663
|
+
}
|
|
664
|
+
if (!check.expect(reply.status === 200, `Widget "${widget.id}": expected status 200, got ${reply.status}.`)) continue;
|
|
665
|
+
if (isBuiltInWidgetType(widget.type)) check.expectSchema(widgetDataSchemas[widget.type], reply.body, `The data of the ${widget.type} "${widget.id}"`);
|
|
666
|
+
else check.expect(reply.body !== null && typeof reply.body === "object" && !Array.isArray(reply.body), `The data of the widget "${widget.id}" must be an object.`);
|
|
667
|
+
}
|
|
668
|
+
});
|
|
669
|
+
await run("dashboard.widgets.resource", "The lists of the widgets on a resource answer their query, with a total for a stat", async (check) => {
|
|
670
|
+
const onResource = widgets().flatMap((widget) => {
|
|
671
|
+
const read = getWidgetResource(widget);
|
|
672
|
+
return read !== null ? [{
|
|
673
|
+
widget,
|
|
674
|
+
read
|
|
675
|
+
}] : [];
|
|
676
|
+
});
|
|
677
|
+
if (onResource.length === 0) {
|
|
678
|
+
check.skip("The dashboard has no widget on a resource.");
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
for (const { widget, read } of onResource) {
|
|
682
|
+
const params = new URLSearchParams(read.params);
|
|
683
|
+
params.set("perPage", widget.type === "stat" ? "1" : String(5));
|
|
684
|
+
const reply = await request(`/resources/${encodeURIComponent(read.resource)}?${params.toString()}`);
|
|
685
|
+
if (reply.status === 403) {
|
|
686
|
+
check.expectProblem(reply, 403);
|
|
687
|
+
continue;
|
|
688
|
+
}
|
|
689
|
+
if (!check.expect(reply.status === 200, `Widget "${widget.id}": expected status 200, got ${reply.status}.`)) continue;
|
|
690
|
+
const list = check.expectSchema(ListResponse, reply.body, `The list of the widget "${widget.id}"`);
|
|
691
|
+
if (list !== null && widget.type === "stat") check.expect(typeof list.meta.total === "number", `The list of the stat "${widget.id}" has no meta.total to count.`);
|
|
692
|
+
}
|
|
693
|
+
});
|
|
694
|
+
await run("dashboard.widgets.unknown", "A widget that is not in the dashboard of the user is a 404 problem", async (check) => {
|
|
695
|
+
if (definition?.dashboard === void 0) {
|
|
696
|
+
check.skip("The definition has no dashboard.");
|
|
697
|
+
return;
|
|
698
|
+
}
|
|
699
|
+
check.expectProblem(await request(`/dashboard/widgets/${UNKNOWN}`), 404);
|
|
700
|
+
});
|
|
179
701
|
const writes = options.writes;
|
|
180
702
|
let createdId = null;
|
|
181
703
|
let createdVersion = null;
|
|
704
|
+
let updatedVersion = null;
|
|
705
|
+
await run("abilities.create", "The resources the user can’t create items of answer 403 to a create", async (check) => {
|
|
706
|
+
const current = definition;
|
|
707
|
+
if (writes === void 0 || current === null) {
|
|
708
|
+
check.skip("Write checks are disabled.");
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
const denied = current.resources.filter((candidate) => candidate.abilities?.create === false);
|
|
712
|
+
if (denied.length === 0) {
|
|
713
|
+
check.skip("The user can create items of every resource.");
|
|
714
|
+
return;
|
|
715
|
+
}
|
|
716
|
+
for (const candidate of denied) {
|
|
717
|
+
const reply = await request(`/resources/${encodeURIComponent(candidate.id)}`, {
|
|
718
|
+
method: "POST",
|
|
719
|
+
body: "{}"
|
|
720
|
+
});
|
|
721
|
+
check.expect(reply.status === 403, `${candidate.id} has \`create: false\`, but a create answered ${reply.status}.`);
|
|
722
|
+
}
|
|
723
|
+
});
|
|
724
|
+
await run("abilities.update", "An item the user can’t update answers 403 to an update", async (check) => {
|
|
725
|
+
if (writes === void 0 || resource === null) {
|
|
726
|
+
check.skip("Write checks are disabled.");
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
const items = await listItems(check);
|
|
730
|
+
const denied = items?.find((item) => item.abilities.update !== true);
|
|
731
|
+
if (items === null) return;
|
|
732
|
+
if (denied === void 0) {
|
|
733
|
+
check.skip("Every item of the first page can be updated.");
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
const reply = await request(`${resourcePath()}/${encodeURIComponent(denied.id)}`, {
|
|
737
|
+
method: "PATCH",
|
|
738
|
+
body: "{}",
|
|
739
|
+
headers: denied.version !== null ? { "If-Match": `"${denied.version}"` } : {}
|
|
740
|
+
});
|
|
741
|
+
check.expect(reply.status === 403, `Item ${denied.id} can’t be updated, but an update answered ${reply.status}.`);
|
|
742
|
+
});
|
|
182
743
|
await run("create.invalid", "Invalid values are refused with a 422 problem", async (check) => {
|
|
183
744
|
if (writes === void 0 || resource === null) {
|
|
184
745
|
check.skip("Write checks are disabled.");
|
|
185
746
|
return;
|
|
186
747
|
}
|
|
187
|
-
const hasRequired = resource.fields.some((field) => (field.rules ?? []).some((rule) => rule.rule === "required"));
|
|
748
|
+
const hasRequired = resource.fields.some((field) => (field.rules ?? []).some((rule) => rule.rule === "required" && (rule.mode ?? "create") === "create"));
|
|
188
749
|
if (writes.invalid === void 0 && !hasRequired) {
|
|
189
750
|
check.skip("No invalid values given, and the resource has no required field.");
|
|
190
751
|
return;
|
|
@@ -196,6 +757,57 @@ async function runConformance(options) {
|
|
|
196
757
|
const failure = check.expectProblem(reply, 422);
|
|
197
758
|
if (failure !== null) check.expect(failure.errors !== void 0 && Object.keys(failure.errors).length > 0, "A 422 problem must list the invalid values in `errors`.");
|
|
198
759
|
});
|
|
760
|
+
await run("create.implied-rules", "A reversed date range and an invalid amount are refused with a 422 problem", async (check) => {
|
|
761
|
+
if (writes === void 0 || resource === null) {
|
|
762
|
+
check.skip("Write checks are disabled.");
|
|
763
|
+
return;
|
|
764
|
+
}
|
|
765
|
+
const cases = resource.fields.flatMap((field) => {
|
|
766
|
+
if (field.localized === true || field.readOnly === true) return [];
|
|
767
|
+
if (field.type === "daterange") {
|
|
768
|
+
const time = field.settings?.time === true;
|
|
769
|
+
return [[
|
|
770
|
+
field.name,
|
|
771
|
+
time ? {
|
|
772
|
+
start: "2026-11-06T10:00:00Z",
|
|
773
|
+
end: "2026-11-04T10:00:00Z"
|
|
774
|
+
} : {
|
|
775
|
+
start: "2026-11-06",
|
|
776
|
+
end: "2026-11-04"
|
|
777
|
+
},
|
|
778
|
+
"a range that ends before it starts"
|
|
779
|
+
]];
|
|
780
|
+
}
|
|
781
|
+
if (field.type === "money") {
|
|
782
|
+
const currencies = field.settings?.currencies;
|
|
783
|
+
const currency = typeof field.settings?.currency === "string" ? field.settings.currency : Array.isArray(currencies) && typeof currencies[0] === "string" ? currencies[0] : "CAD";
|
|
784
|
+
return [[
|
|
785
|
+
field.name,
|
|
786
|
+
{
|
|
787
|
+
amount: 12.5,
|
|
788
|
+
currency
|
|
789
|
+
},
|
|
790
|
+
"an amount in major units"
|
|
791
|
+
]];
|
|
792
|
+
}
|
|
793
|
+
return [];
|
|
794
|
+
});
|
|
795
|
+
if (cases.length === 0) {
|
|
796
|
+
check.skip("The resource has no daterange or money field.");
|
|
797
|
+
return;
|
|
798
|
+
}
|
|
799
|
+
for (const [name, value, label] of cases) {
|
|
800
|
+
const reply = await request(resourcePath(), {
|
|
801
|
+
method: "POST",
|
|
802
|
+
body: JSON.stringify({
|
|
803
|
+
...writes.create,
|
|
804
|
+
[name]: value
|
|
805
|
+
})
|
|
806
|
+
});
|
|
807
|
+
const failure = check.expectProblem(reply, 422);
|
|
808
|
+
if (failure !== null) check.expect(Object.keys(failure.errors ?? {}).some((key) => key === name || key.startsWith(`${name}.`)), `${name}: ${label} is refused, but not in errors.${name}.`);
|
|
809
|
+
}
|
|
810
|
+
});
|
|
199
811
|
await run("create", "POST /resources/{resource} creates an item", async (check) => {
|
|
200
812
|
if (writes === void 0 || resource === null) {
|
|
201
813
|
check.skip("Write checks are disabled.");
|
|
@@ -224,6 +836,108 @@ async function runConformance(options) {
|
|
|
224
836
|
if (!check.expectStatus(reply, 200)) return;
|
|
225
837
|
const item = check.expectSchema(ItemResponse, reply.body, "The updated item");
|
|
226
838
|
if (item !== null && createdVersion !== null) check.expect(item.data.version !== createdVersion, "The version did not change after an update.");
|
|
839
|
+
updatedVersion = item?.data.version ?? null;
|
|
840
|
+
});
|
|
841
|
+
await run("update.create-rules", "Rules limited to create do not apply to updates", async (check) => {
|
|
842
|
+
if (createdId === null || writes === void 0 || resource === null) {
|
|
843
|
+
check.skip("No item was created.");
|
|
844
|
+
return;
|
|
845
|
+
}
|
|
846
|
+
const names = resource.fields.filter((field) => field.readOnly !== true && field.createOnly !== true && (field.rules ?? []).some((rule) => rule.rule === "required" && rule.mode === "create")).map((field) => field.name);
|
|
847
|
+
if (names.length === 0) {
|
|
848
|
+
check.skip("The resource has no rule limited to create.");
|
|
849
|
+
return;
|
|
850
|
+
}
|
|
851
|
+
const reply = await request(itemPath(), {
|
|
852
|
+
method: "PATCH",
|
|
853
|
+
body: JSON.stringify(Object.fromEntries(names.map((name) => [name, null]))),
|
|
854
|
+
headers: updatedVersion !== null ? { "If-Match": `"${updatedVersion}"` } : {}
|
|
855
|
+
});
|
|
856
|
+
check.expect(reply.status === 200, `An update with ${names.join(", ")} set to null got status ${reply.status}, expected 200.`);
|
|
857
|
+
});
|
|
858
|
+
await run("update.parts", "Addresses and links are refused at the path of their invalid part", async (check) => {
|
|
859
|
+
if (createdId === null || writes === void 0 || resource === null) {
|
|
860
|
+
check.skip("No item was created.");
|
|
861
|
+
return;
|
|
862
|
+
}
|
|
863
|
+
const editable = resource.fields.filter((field) => field.readOnly !== true && field.createOnly !== true && field.localized !== true && field.abilities?.update !== false && field.visibleWhen === void 0);
|
|
864
|
+
const address = editable.find((field) => {
|
|
865
|
+
const countries = field.settings?.countries;
|
|
866
|
+
return field.type === "address" && (!Array.isArray(countries) || countries.includes("CA") || countries.includes("US"));
|
|
867
|
+
});
|
|
868
|
+
const link = editable.find((field) => field.type === "link" && getLinkTypes(field).some((type) => type.id === "email" || type.id === "url"));
|
|
869
|
+
if (address === void 0 && link === void 0) {
|
|
870
|
+
check.skip("The resource has no address nor link field.");
|
|
871
|
+
return;
|
|
872
|
+
}
|
|
873
|
+
const values = {};
|
|
874
|
+
const paths = [];
|
|
875
|
+
if (address !== void 0) {
|
|
876
|
+
const countries = address.settings?.countries;
|
|
877
|
+
const canada = !Array.isArray(countries) || countries.includes("CA");
|
|
878
|
+
values[address.name] = {
|
|
879
|
+
street: "1234 Main Street",
|
|
880
|
+
city: canada ? "Montreal" : "Springfield",
|
|
881
|
+
region: canada ? "QC" : "IL",
|
|
882
|
+
postalCode: canada ? "12345" : "H2X 1Y4",
|
|
883
|
+
country: canada ? "CA" : "US"
|
|
884
|
+
};
|
|
885
|
+
paths.push(`${address.name}.postalCode`);
|
|
886
|
+
}
|
|
887
|
+
if (link !== void 0) {
|
|
888
|
+
const email = getLinkTypes(link).some((type) => type.id === "email");
|
|
889
|
+
values[link.name] = email ? {
|
|
890
|
+
type: "email",
|
|
891
|
+
url: "info@example.com",
|
|
892
|
+
newTab: false
|
|
893
|
+
} : {
|
|
894
|
+
type: "url",
|
|
895
|
+
url: "example",
|
|
896
|
+
newTab: false
|
|
897
|
+
};
|
|
898
|
+
paths.push(`${link.name}.url`);
|
|
899
|
+
}
|
|
900
|
+
const current = await request(itemPath());
|
|
901
|
+
const item = check.expectStatus(current, 200) ? check.expectSchema(ItemResponse, current.body, "The item") : null;
|
|
902
|
+
if (item === null) return;
|
|
903
|
+
const version = item.data.version;
|
|
904
|
+
const reply = await request(itemPath(), {
|
|
905
|
+
method: "PATCH",
|
|
906
|
+
body: JSON.stringify(values),
|
|
907
|
+
headers: version !== null ? { "If-Match": `"${version}"` } : {}
|
|
908
|
+
});
|
|
909
|
+
const failure = check.expectProblem(reply, 422);
|
|
910
|
+
if (failure !== null) {
|
|
911
|
+
const missing = paths.filter((path) => failure.errors?.[path] === void 0);
|
|
912
|
+
check.expect(missing.length === 0, `The 422 problem has no errors at ${missing.join(", ")}: ${Object.keys(failure.errors ?? {}).join(", ") || "none"}.`);
|
|
913
|
+
}
|
|
914
|
+
});
|
|
915
|
+
await run("update.single-field", "A PATCH with a single field and If-Match changes that field alone", async (check) => {
|
|
916
|
+
if (createdId === null || writes === void 0 || resource === null) {
|
|
917
|
+
check.skip("No item was created.");
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
const current = await request(itemPath());
|
|
921
|
+
const before = check.expectStatus(current, 200) ? check.expectSchema(ItemResponse, current.body, "The item") : null;
|
|
922
|
+
if (before === null) return;
|
|
923
|
+
const name = Object.keys(writes.update).find((key) => key in writes.create && !isEqual(writes.create[key], before.data[key]));
|
|
924
|
+
if (name === void 0) {
|
|
925
|
+
check.skip("No value of the update of `writes` differs from its value in `create`, to set it back.");
|
|
926
|
+
return;
|
|
927
|
+
}
|
|
928
|
+
const version = before.data.version;
|
|
929
|
+
const reply = await request(itemPath(), {
|
|
930
|
+
method: "PATCH",
|
|
931
|
+
body: JSON.stringify({ [name]: writes.create[name] }),
|
|
932
|
+
headers: version !== null ? { "If-Match": `"${version}"` } : {}
|
|
933
|
+
});
|
|
934
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
935
|
+
const after = check.expectSchema(ItemResponse, reply.body, "The updated item");
|
|
936
|
+
if (after === null) return;
|
|
937
|
+
if (version !== null) check.expect(after.data.version !== version, "The version did not change after an update of a single field.");
|
|
938
|
+
const changed = resource.fields.filter((field) => field.name !== name && field.readOnly !== true && field.settings?.secret !== true && !isEqual(before.data[field.name], after.data[field.name])).map((field) => field.name);
|
|
939
|
+
check.expect(changed.length === 0, `An update of ${name} alone also changed ${changed.join(", ")}.`);
|
|
940
|
+
updatedVersion = after.data.version;
|
|
227
941
|
});
|
|
228
942
|
await run("update.conflict", "A stale If-Match is a 412 problem with the current item", async (check) => {
|
|
229
943
|
if (createdId === null || writes === void 0) {
|
|
@@ -242,6 +956,68 @@ async function runConformance(options) {
|
|
|
242
956
|
const failure = check.expectProblem(reply, 412);
|
|
243
957
|
if (failure !== null) check.expect(failure.current !== void 0, "A 412 problem must include the current item.");
|
|
244
958
|
});
|
|
959
|
+
await run("duplicate", "POST /resources/{resource}/{id}/duplicate copies an item that allows it", async (check) => {
|
|
960
|
+
if (createdId === null) {
|
|
961
|
+
check.skip("No item was created.");
|
|
962
|
+
return;
|
|
963
|
+
}
|
|
964
|
+
const reply = await request(itemPath());
|
|
965
|
+
const created = check.expectStatus(reply, 200) ? check.expectSchema(ItemResponse, reply.body, "The item") : null;
|
|
966
|
+
if (created === null) return;
|
|
967
|
+
if (created.data.abilities.duplicate !== true) {
|
|
968
|
+
check.skip("The item doesn't allow `duplicate`.");
|
|
969
|
+
return;
|
|
970
|
+
}
|
|
971
|
+
const copy = await request(`${itemPath()}/duplicate`, { method: "POST" });
|
|
972
|
+
if (!check.expectStatus(copy, 201)) return;
|
|
973
|
+
const duplicated = check.expectSchema(ItemResponse, copy.body, "The copy");
|
|
974
|
+
if (duplicated === null) return;
|
|
975
|
+
check.expect(copy.etag !== null && normalizeEtag(copy.etag) === `"${duplicated.data.version}"`, `Expected the ETag of the copy, "${duplicated.data.version}" (quoted), got ${copy.etag ?? "none"}.`);
|
|
976
|
+
check.expect(duplicated.data.id !== created.data.id, "The copy has the id of the item it copies.");
|
|
977
|
+
await request(`${resourcePath()}/${encodeURIComponent(duplicated.data.id)}`, { method: "DELETE" });
|
|
978
|
+
});
|
|
979
|
+
await run("action.undo", "The undo of an undoable action runs, and puts the item back", async (check) => {
|
|
980
|
+
const current = resource;
|
|
981
|
+
if (current === null) {
|
|
982
|
+
check.skip("No resource to test.");
|
|
983
|
+
return;
|
|
984
|
+
}
|
|
985
|
+
if (createdId === null) {
|
|
986
|
+
check.skip("No item was created.");
|
|
987
|
+
return;
|
|
988
|
+
}
|
|
989
|
+
const declared = current.actions ?? [];
|
|
990
|
+
const action = declared.find((candidate) => candidate.id === "delete" && candidate.undoable === true) ?? declared.find((candidate) => candidate.undoable === true && (candidate.kind ?? "server") === "server" && candidate.queued !== true && (candidate.fields ?? []).length === 0 && candidate.scope.some((scope) => scope === "item" || scope === "selection"));
|
|
991
|
+
if (action === void 0) {
|
|
992
|
+
check.skip("The resource has no undoable action without parameters.");
|
|
993
|
+
return;
|
|
994
|
+
}
|
|
995
|
+
const before = await request(itemPath());
|
|
996
|
+
const item = check.expectStatus(before, 200) ? check.expectSchema(ItemResponse, before.body, "The item") : null;
|
|
997
|
+
if (item === null) return;
|
|
998
|
+
const reply = await request(`${resourcePath()}/actions/${encodeURIComponent(action.id)}`, {
|
|
999
|
+
method: "POST",
|
|
1000
|
+
body: JSON.stringify({ selection: { ids: [createdId] } })
|
|
1001
|
+
});
|
|
1002
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
1003
|
+
const undo = check.expectSchema(ActionResult, reply.body, `The result of \`${action.id}\``)?.undo;
|
|
1004
|
+
if (!check.expect(undo !== void 0 && undo.items.includes(createdId), `The result of the undoable \`${action.id}\` has no \`undo\` with the item it changed.`) || undo === void 0) return;
|
|
1005
|
+
const undone = await request(`${resourcePath()}/actions/${encodeURIComponent(undo.action)}`, {
|
|
1006
|
+
method: "POST",
|
|
1007
|
+
body: JSON.stringify({
|
|
1008
|
+
selection: { ids: undo.items },
|
|
1009
|
+
values: undo.params ?? {}
|
|
1010
|
+
})
|
|
1011
|
+
});
|
|
1012
|
+
if (!check.expectStatus(undone, 200)) return;
|
|
1013
|
+
const after = await request(itemPath());
|
|
1014
|
+
if (!check.expect(after.status === 200, "After the undo, the item is not back.")) return;
|
|
1015
|
+
const restored = check.expectSchema(ItemResponse, after.body, "The item after the undo");
|
|
1016
|
+
if (restored === null) return;
|
|
1017
|
+
current.fields.forEach((field) => {
|
|
1018
|
+
check.expect(isEqual(restored.data[field.name], item.data[field.name]), `After the undo, \`${field.name}\` is ${JSON.stringify(restored.data[field.name])}, not ${JSON.stringify(item.data[field.name])}.`);
|
|
1019
|
+
});
|
|
1020
|
+
});
|
|
245
1021
|
await run("delete", "DELETE /resources/{resource}/{id} deletes the item", async (check) => {
|
|
246
1022
|
if (createdId === null) {
|
|
247
1023
|
check.skip("No item was created.");
|