@panneau/protocol 5.0.0-alpha.3 → 5.0.0-alpha.32
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 +801 -11
- package/dist/conformance/index.js.map +1 -1
- package/dist/constants.d.ts +108 -2
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +181 -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 +19 -8
- package/dist/libraries.d.ts +33 -0
- package/dist/libraries.d.ts.map +1 -0
- package/dist/libraries.js +59 -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 +168 -7
- 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 +241 -144
- package/dist/mock/example.js.map +1 -1
- package/dist/mock/index.d.ts +79 -7
- package/dist/mock/index.d.ts.map +1 -1
- package/dist/mock/index.js +603 -38
- package/dist/mock/index.js.map +1 -1
- package/dist/openapi.d.ts.map +1 -1
- package/dist/openapi.js +392 -144
- 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/resourceTypes.d.ts +39 -0
- package/dist/resourceTypes.d.ts.map +1 -0
- package/dist/resourceTypes.js +34 -0
- package/dist/resourceTypes.js.map +1 -0
- package/dist/rules.d.ts +18 -5
- package/dist/rules.d.ts.map +1 -1
- package/dist/rules.js +233 -15
- package/dist/rules.js.map +1 -1
- package/dist/runtime/index.d.ts +20 -10
- package/dist/runtime/index.d.ts.map +1 -1
- package/dist/runtime/index.js +13 -3
- package/dist/schemas/action.d.ts +118 -10
- package/dist/schemas/action.d.ts.map +1 -1
- package/dist/schemas/action.js +20 -3
- package/dist/schemas/action.js.map +1 -1
- package/dist/schemas/api.d.ts +334 -42
- package/dist/schemas/api.d.ts.map +1 -1
- package/dist/schemas/api.js +194 -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 +341 -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 +357 -2
- package/dist/schemas/field.d.ts.map +1 -1
- package/dist/schemas/field.js +160 -20
- package/dist/schemas/field.js.map +1 -1
- package/dist/schemas/index.d.ts +1429 -80
- package/dist/schemas/index.d.ts.map +1 -1
- package/dist/schemas/index.js +60 -10
- 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 +2 -1
- package/dist/schemas/layout.d.ts.map +1 -1
- package/dist/schemas/layout.js +2 -1
- package/dist/schemas/layout.js.map +1 -1
- package/dist/schemas/list.d.ts +23 -2
- package/dist/schemas/list.d.ts.map +1 -1
- package/dist/schemas/list.js +15 -6
- package/dist/schemas/list.js.map +1 -1
- package/dist/schemas/resource.d.ts +263 -17
- package/dist/schemas/resource.d.ts.map +1 -1
- package/dist/schemas/resource.js +19 -4
- package/dist/schemas/resource.js.map +1 -1
- package/dist/schemas/theme.d.ts +4 -0
- package/dist/schemas/theme.d.ts.map +1 -1
- package/dist/schemas/theme.js +1 -0
- package/dist/schemas/theme.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 +716 -20
- package/schema/definition.schema.json +713 -20
- package/schema/openapi.json +5659 -2229
|
@@ -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,42 @@ 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("list.filter-ids", "GET /resources/{resource} filters on the ids of items (filter[ids])", async (check) => {
|
|
365
|
+
if (resource === null) {
|
|
366
|
+
check.skip("No resource to test.");
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
const first = await request(`${resourcePath()}?perPage=3`);
|
|
370
|
+
if (!check.expectStatus(first, 200)) return;
|
|
371
|
+
const items = check.expectSchema(ListResponse, first.body, "The list")?.data ?? [];
|
|
372
|
+
if (items.length < 2) {
|
|
373
|
+
check.skip("The list has fewer than 2 items.");
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
const wanted = items.slice(0, 2).map((item) => item.id);
|
|
377
|
+
const ids = [...wanted].reverse().concat("panneau-conformance-missing");
|
|
378
|
+
const reply = await request(`${resourcePath()}?${toSearchParams({ filter: { ["ids"]: ids } })}`);
|
|
379
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
380
|
+
const filtered = check.expectSchema(ListResponse, reply.body, "The list filtered on ids");
|
|
381
|
+
if (filtered === null) return;
|
|
382
|
+
const got = filtered.data.map((item) => item.id);
|
|
383
|
+
check.expect(got.length === wanted.length && wanted.every((id) => got.includes(id)), `filter[ids][] of ${wanted.join(", ")} listed ${got.length > 0 ? got.join(", ") : "no items"}.`);
|
|
156
384
|
});
|
|
157
385
|
await run("action.dry-run", "A dry run of an action counts what it would do, and changes nothing", async (check) => {
|
|
158
386
|
const action = resource?.actions?.find((candidate) => candidate.dryRun === true && candidate.kind === "server" && candidate.scope.includes("selection"));
|
|
@@ -187,6 +415,85 @@ async function runConformance(options) {
|
|
|
187
415
|
check.expect(after.meta.total === before.meta.total && versions(after) === versions(before), "A dry run must not change the items.");
|
|
188
416
|
}
|
|
189
417
|
});
|
|
418
|
+
await run("action.delete", "The built-in `delete` answers, with dry runs, without being declared", async (check) => {
|
|
419
|
+
const current = resource;
|
|
420
|
+
if (current === null) {
|
|
421
|
+
check.skip("No resource to test.");
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
if (current.itemActions !== void 0 && !current.itemActions.includes("delete")) {
|
|
425
|
+
check.skip("The resource leaves out `delete` (`itemActions`).");
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
if (firstItemId === null) {
|
|
429
|
+
check.skip("The list has no items.");
|
|
430
|
+
return;
|
|
431
|
+
}
|
|
432
|
+
const reply = await request(`${resourcePath()}/actions/delete`, {
|
|
433
|
+
method: "POST",
|
|
434
|
+
body: JSON.stringify({
|
|
435
|
+
selection: { ids: [firstItemId] },
|
|
436
|
+
dryRun: true
|
|
437
|
+
})
|
|
438
|
+
});
|
|
439
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
440
|
+
const result = check.expectSchema(ActionResult, reply.body, "The dry run of `delete`");
|
|
441
|
+
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.");
|
|
442
|
+
check.expectStatus(await request(`${resourcePath()}/${encodeURIComponent(firstItemId)}`), 200);
|
|
443
|
+
});
|
|
444
|
+
await run("uploads.sign", "POST /uploads/sign signs the first request of a file, and only the keys of open uploads", async (check) => {
|
|
445
|
+
const uploads = definition?.uploads;
|
|
446
|
+
if (uploads?.mode !== "presigned") {
|
|
447
|
+
check.skip("The definition has no presigned uploads.");
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
if (uploads.endpoint !== void 0) {
|
|
451
|
+
check.skip("The upload routes are not at {api.baseUrl}/uploads.");
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
const sign = (body) => request("/uploads/sign", {
|
|
455
|
+
method: "POST",
|
|
456
|
+
body: JSON.stringify(body)
|
|
457
|
+
});
|
|
458
|
+
const reply = await sign({
|
|
459
|
+
method: "POST",
|
|
460
|
+
key: "panneau-conformance.txt"
|
|
461
|
+
});
|
|
462
|
+
if (check.expectStatus(reply, 200)) {
|
|
463
|
+
const signed = check.expectSchema(UploadSignResponse, reply.body, "The signed request");
|
|
464
|
+
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.");
|
|
465
|
+
}
|
|
466
|
+
check.expectProblem(await sign({
|
|
467
|
+
method: "PUT",
|
|
468
|
+
key: "not-an-upload-of-this-user",
|
|
469
|
+
uploadId: "unknown",
|
|
470
|
+
partNumber: 1
|
|
471
|
+
}), 404);
|
|
472
|
+
check.expectProblem(await request(`/uploads/${UNKNOWN}/complete`, { method: "POST" }), 404);
|
|
473
|
+
});
|
|
474
|
+
await run("fields.check", "POST /resources/{resource}/fields/{field}/check checks a value of a field with `check`", async (check) => {
|
|
475
|
+
const field = resource?.fields.find((candidate) => candidate.check === true);
|
|
476
|
+
if (resource === null || field === void 0) {
|
|
477
|
+
check.skip("The resource has no field with `check`.");
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const checkPath = (name) => `${resourcePath()}/fields/${encodeURIComponent(name)}/check`;
|
|
481
|
+
const locale = field.localized === true ? definition?.locale : void 0;
|
|
482
|
+
const reply = await request(checkPath(field.name), {
|
|
483
|
+
method: "POST",
|
|
484
|
+
body: JSON.stringify({
|
|
485
|
+
value: "panneau-conformance",
|
|
486
|
+
...locale !== void 0 ? { locale } : {},
|
|
487
|
+
id: null,
|
|
488
|
+
values: {}
|
|
489
|
+
})
|
|
490
|
+
});
|
|
491
|
+
if (check.expectStatus(reply, 200)) check.expectSchema(FieldCheckResponse, reply.body, "The check");
|
|
492
|
+
check.expectProblem(await request(checkPath(UNKNOWN), {
|
|
493
|
+
method: "POST",
|
|
494
|
+
body: "{\"value\":\"x\"}"
|
|
495
|
+
}), 404);
|
|
496
|
+
});
|
|
190
497
|
await run("list.unknown-resource", "An unknown resource is a 404 problem", async (check) => {
|
|
191
498
|
check.expectProblem(await request(`/resources/${UNKNOWN}`), 404);
|
|
192
499
|
});
|
|
@@ -209,15 +516,257 @@ async function runConformance(options) {
|
|
|
209
516
|
}
|
|
210
517
|
check.expectProblem(await request(`${resourcePath()}/${UNKNOWN}`), 404);
|
|
211
518
|
});
|
|
519
|
+
const listItems = async (check) => {
|
|
520
|
+
const cursors = resource?.list?.pagination === "cursor";
|
|
521
|
+
const reply = await request(`${resourcePath()}?${cursors ? "" : "page=1&"}perPage=20`);
|
|
522
|
+
if (!check.expectStatus(reply, 200)) return null;
|
|
523
|
+
return check.expectSchema(ListResponse, reply.body, "The list")?.data ?? null;
|
|
524
|
+
};
|
|
525
|
+
await run("abilities.view-any", "The resources the user can’t list answer 403 on their list", async (check) => {
|
|
526
|
+
const denied = (definition?.resources ?? []).filter((candidate) => candidate.abilities?.viewAny === false);
|
|
527
|
+
if (denied.length === 0) {
|
|
528
|
+
check.skip("Every resource can be listed.");
|
|
529
|
+
return;
|
|
530
|
+
}
|
|
531
|
+
for (const candidate of denied) {
|
|
532
|
+
const reply = await request(`/resources/${encodeURIComponent(candidate.id)}`);
|
|
533
|
+
check.expect(reply.status === 403, `${candidate.id} has \`viewAny: false\`, but its list answered ${reply.status}.`);
|
|
534
|
+
}
|
|
535
|
+
});
|
|
536
|
+
await run("abilities.fields", "Items leave out the values of the fields the user can’t see", async (check) => {
|
|
537
|
+
const hidden = (resource?.fields ?? []).filter((field) => field.abilities?.view === false).map((field) => field.name);
|
|
538
|
+
if (hidden.length === 0) {
|
|
539
|
+
check.skip("The resource has no field with `view: false`.");
|
|
540
|
+
return;
|
|
541
|
+
}
|
|
542
|
+
const items = await listItems(check);
|
|
543
|
+
if (items === null) return;
|
|
544
|
+
if (items.length === 0) {
|
|
545
|
+
check.skip("The list has no items.");
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
items.forEach((item) => {
|
|
549
|
+
const shown = hidden.filter((name) => name in item);
|
|
550
|
+
check.expect(shown.length === 0, `Item ${item.id} has values of fields with \`view: false\`: ${shown.join(", ")}.`);
|
|
551
|
+
});
|
|
552
|
+
});
|
|
553
|
+
await run("abilities.delete", "An item that can’t be deleted is skipped by a dry run of `delete`", async (check) => {
|
|
554
|
+
const current = resource;
|
|
555
|
+
if (current === null) {
|
|
556
|
+
check.skip("No resource to test.");
|
|
557
|
+
return;
|
|
558
|
+
}
|
|
559
|
+
if (current.itemActions !== void 0 && !current.itemActions.includes("delete")) {
|
|
560
|
+
check.skip("The resource leaves out `delete` (`itemActions`).");
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
const items = await listItems(check);
|
|
564
|
+
const denied = items?.find((item) => item.abilities.delete !== true);
|
|
565
|
+
if (items === null) return;
|
|
566
|
+
if (denied === void 0) {
|
|
567
|
+
check.skip("Every item of the first page can be deleted.");
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
const allowed = items.find((item) => item.abilities.delete === true);
|
|
571
|
+
const ids = allowed !== void 0 ? [denied.id, allowed.id] : [denied.id];
|
|
572
|
+
const reply = await request(`${resourcePath()}/actions/delete`, {
|
|
573
|
+
method: "POST",
|
|
574
|
+
body: JSON.stringify({
|
|
575
|
+
selection: { ids },
|
|
576
|
+
dryRun: true
|
|
577
|
+
})
|
|
578
|
+
});
|
|
579
|
+
if (allowed === void 0 && reply.status === 403) return;
|
|
580
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
581
|
+
const result = check.expectSchema(ActionResult, reply.body, "The dry run of `delete`");
|
|
582
|
+
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.`);
|
|
583
|
+
});
|
|
584
|
+
let searched = null;
|
|
585
|
+
const searchSkip = () => {
|
|
586
|
+
const current = definition;
|
|
587
|
+
if (current === null) return "No valid definition.";
|
|
588
|
+
if (current.search?.enabled === false) return "The definition disables the global search.";
|
|
589
|
+
return searched === null ? "No searchable resource has an item with a title to search for." : null;
|
|
590
|
+
};
|
|
591
|
+
const search = (query) => request(`/search?${toSearchQueryParams(query)}`);
|
|
592
|
+
await run("search", "GET /search finds items of the searchable resources", async (check) => {
|
|
593
|
+
const current = definition;
|
|
594
|
+
const tested = resource;
|
|
595
|
+
if (current === null || current.search?.enabled === false) {
|
|
596
|
+
check.skip(searchSkip() ?? "");
|
|
597
|
+
return;
|
|
598
|
+
}
|
|
599
|
+
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));
|
|
600
|
+
for (const candidate of candidates) {
|
|
601
|
+
if (candidate.itemTitle === void 0 || searched !== null) continue;
|
|
602
|
+
const reply = await request(`/resources/${encodeURIComponent(candidate.id)}?page=1&perPage=1`);
|
|
603
|
+
const list = ListResponse.safeParse(reply.body);
|
|
604
|
+
const term = (list.success ? textOf(list.data.data[0]?.[candidate.itemTitle]) : null)?.split(/\s+/).find((word) => word.length >= 2);
|
|
605
|
+
if (term !== void 0) searched = {
|
|
606
|
+
resource: candidate,
|
|
607
|
+
term
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
const reason = searchSkip();
|
|
611
|
+
if (reason !== null || searched === null) {
|
|
612
|
+
check.skip(reason ?? "");
|
|
613
|
+
return;
|
|
614
|
+
}
|
|
615
|
+
const { resource: target, term } = searched;
|
|
616
|
+
const reply = await search({ q: term });
|
|
617
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
618
|
+
const results = check.expectSchema(SearchResponse, reply.body, "The results");
|
|
619
|
+
if (results === null) return;
|
|
620
|
+
const ids = candidates.map((candidate) => candidate.id);
|
|
621
|
+
for (const group of results.data) {
|
|
622
|
+
check.expect(ids.includes(group.resource), `Results of "${group.resource}", which is not a searchable resource.`);
|
|
623
|
+
check.expect(group.items.length <= 5, `"${group.resource}" has ${group.items.length} results; without a limit, at most 5.`);
|
|
624
|
+
}
|
|
625
|
+
const count = results.data.reduce((total, group) => total + group.items.length, 0);
|
|
626
|
+
check.expect(count <= 20, `The results have ${count} items, at most 20.`);
|
|
627
|
+
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.`);
|
|
628
|
+
});
|
|
629
|
+
await run("search.scope", "GET /search reads limit and resources", async (check) => {
|
|
630
|
+
const reason = searchSkip();
|
|
631
|
+
if (reason !== null || searched === null) {
|
|
632
|
+
check.skip(reason ?? "");
|
|
633
|
+
return;
|
|
634
|
+
}
|
|
635
|
+
const { resource: target, term } = searched;
|
|
636
|
+
const reply = await search({
|
|
637
|
+
q: term,
|
|
638
|
+
limit: 1,
|
|
639
|
+
resources: [target.id]
|
|
640
|
+
});
|
|
641
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
642
|
+
const results = check.expectSchema(SearchResponse, reply.body, "The results");
|
|
643
|
+
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]))}.`);
|
|
644
|
+
});
|
|
645
|
+
await run("search.invalid", "An invalid search is a 422 problem, with errors keyed by parameter", async (check) => {
|
|
646
|
+
const reason = searchSkip();
|
|
647
|
+
if (reason !== null || searched === null) {
|
|
648
|
+
check.skip(reason ?? "");
|
|
649
|
+
return;
|
|
650
|
+
}
|
|
651
|
+
const { term } = searched;
|
|
652
|
+
const invalid = [
|
|
653
|
+
["q", { q: term.slice(0, 1) }],
|
|
654
|
+
["limit", {
|
|
655
|
+
q: term,
|
|
656
|
+
limit: 0
|
|
657
|
+
}],
|
|
658
|
+
["limit", {
|
|
659
|
+
q: term,
|
|
660
|
+
limit: 21
|
|
661
|
+
}],
|
|
662
|
+
["resources", {
|
|
663
|
+
q: term,
|
|
664
|
+
resources: [UNKNOWN]
|
|
665
|
+
}]
|
|
666
|
+
];
|
|
667
|
+
for (const [parameter, query] of invalid) {
|
|
668
|
+
const failure = check.expectProblem(await search(query), 422);
|
|
669
|
+
if (failure !== null) check.expect(failure.errors?.[parameter] !== void 0, `${toSearchQueryParams(query)}: the 422 problem must have errors.${parameter}.`);
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
const widgets = () => definition?.dashboard?.widgets ?? [];
|
|
673
|
+
await run("dashboard.widgets", "GET /dashboard/widgets/{widget} answers each widget in the shape of its type", async (check) => {
|
|
674
|
+
const own = widgets().filter((widget) => getWidgetResource(widget) === null);
|
|
675
|
+
if (own.length === 0) {
|
|
676
|
+
check.skip("The dashboard has no widget with data of its own.");
|
|
677
|
+
return;
|
|
678
|
+
}
|
|
679
|
+
for (const widget of own) {
|
|
680
|
+
const reply = await request(`/dashboard/widgets/${encodeURIComponent(widget.id)}`);
|
|
681
|
+
if (reply.status === 403) {
|
|
682
|
+
check.expectProblem(reply, 403);
|
|
683
|
+
continue;
|
|
684
|
+
}
|
|
685
|
+
if (!check.expect(reply.status === 200, `Widget "${widget.id}": expected status 200, got ${reply.status}.`)) continue;
|
|
686
|
+
if (isBuiltInWidgetType(widget.type)) check.expectSchema(widgetDataSchemas[widget.type], reply.body, `The data of the ${widget.type} "${widget.id}"`);
|
|
687
|
+
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.`);
|
|
688
|
+
}
|
|
689
|
+
});
|
|
690
|
+
await run("dashboard.widgets.resource", "The lists of the widgets on a resource answer their query, with a total for a stat", async (check) => {
|
|
691
|
+
const onResource = widgets().flatMap((widget) => {
|
|
692
|
+
const read = getWidgetResource(widget);
|
|
693
|
+
return read !== null ? [{
|
|
694
|
+
widget,
|
|
695
|
+
read
|
|
696
|
+
}] : [];
|
|
697
|
+
});
|
|
698
|
+
if (onResource.length === 0) {
|
|
699
|
+
check.skip("The dashboard has no widget on a resource.");
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
for (const { widget, read } of onResource) {
|
|
703
|
+
const params = new URLSearchParams(read.params);
|
|
704
|
+
params.set("perPage", widget.type === "stat" ? "1" : String(5));
|
|
705
|
+
const reply = await request(`/resources/${encodeURIComponent(read.resource)}?${params.toString()}`);
|
|
706
|
+
if (reply.status === 403) {
|
|
707
|
+
check.expectProblem(reply, 403);
|
|
708
|
+
continue;
|
|
709
|
+
}
|
|
710
|
+
if (!check.expect(reply.status === 200, `Widget "${widget.id}": expected status 200, got ${reply.status}.`)) continue;
|
|
711
|
+
const list = check.expectSchema(ListResponse, reply.body, `The list of the widget "${widget.id}"`);
|
|
712
|
+
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.`);
|
|
713
|
+
}
|
|
714
|
+
});
|
|
715
|
+
await run("dashboard.widgets.unknown", "A widget that is not in the dashboard of the user is a 404 problem", async (check) => {
|
|
716
|
+
if (definition?.dashboard === void 0) {
|
|
717
|
+
check.skip("The definition has no dashboard.");
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
check.expectProblem(await request(`/dashboard/widgets/${UNKNOWN}`), 404);
|
|
721
|
+
});
|
|
212
722
|
const writes = options.writes;
|
|
213
723
|
let createdId = null;
|
|
214
724
|
let createdVersion = null;
|
|
725
|
+
let updatedVersion = null;
|
|
726
|
+
await run("abilities.create", "The resources the user can’t create items of answer 403 to a create", async (check) => {
|
|
727
|
+
const current = definition;
|
|
728
|
+
if (writes === void 0 || current === null) {
|
|
729
|
+
check.skip("Write checks are disabled.");
|
|
730
|
+
return;
|
|
731
|
+
}
|
|
732
|
+
const denied = current.resources.filter((candidate) => candidate.abilities?.create === false);
|
|
733
|
+
if (denied.length === 0) {
|
|
734
|
+
check.skip("The user can create items of every resource.");
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
for (const candidate of denied) {
|
|
738
|
+
const reply = await request(`/resources/${encodeURIComponent(candidate.id)}`, {
|
|
739
|
+
method: "POST",
|
|
740
|
+
body: "{}"
|
|
741
|
+
});
|
|
742
|
+
check.expect(reply.status === 403, `${candidate.id} has \`create: false\`, but a create answered ${reply.status}.`);
|
|
743
|
+
}
|
|
744
|
+
});
|
|
745
|
+
await run("abilities.update", "An item the user can’t update answers 403 to an update", async (check) => {
|
|
746
|
+
if (writes === void 0 || resource === null) {
|
|
747
|
+
check.skip("Write checks are disabled.");
|
|
748
|
+
return;
|
|
749
|
+
}
|
|
750
|
+
const items = await listItems(check);
|
|
751
|
+
const denied = items?.find((item) => item.abilities.update !== true);
|
|
752
|
+
if (items === null) return;
|
|
753
|
+
if (denied === void 0) {
|
|
754
|
+
check.skip("Every item of the first page can be updated.");
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
const reply = await request(`${resourcePath()}/${encodeURIComponent(denied.id)}`, {
|
|
758
|
+
method: "PATCH",
|
|
759
|
+
body: "{}",
|
|
760
|
+
headers: denied.version !== null ? { "If-Match": `"${denied.version}"` } : {}
|
|
761
|
+
});
|
|
762
|
+
check.expect(reply.status === 403, `Item ${denied.id} can’t be updated, but an update answered ${reply.status}.`);
|
|
763
|
+
});
|
|
215
764
|
await run("create.invalid", "Invalid values are refused with a 422 problem", async (check) => {
|
|
216
765
|
if (writes === void 0 || resource === null) {
|
|
217
766
|
check.skip("Write checks are disabled.");
|
|
218
767
|
return;
|
|
219
768
|
}
|
|
220
|
-
const hasRequired = resource.fields.some((field) => (field.rules ?? []).some((rule) => rule.rule === "required"));
|
|
769
|
+
const hasRequired = resource.fields.some((field) => (field.rules ?? []).some((rule) => rule.rule === "required" && (rule.mode ?? "create") === "create"));
|
|
221
770
|
if (writes.invalid === void 0 && !hasRequired) {
|
|
222
771
|
check.skip("No invalid values given, and the resource has no required field.");
|
|
223
772
|
return;
|
|
@@ -229,6 +778,57 @@ async function runConformance(options) {
|
|
|
229
778
|
const failure = check.expectProblem(reply, 422);
|
|
230
779
|
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`.");
|
|
231
780
|
});
|
|
781
|
+
await run("create.implied-rules", "A reversed date range and an invalid amount are refused with a 422 problem", async (check) => {
|
|
782
|
+
if (writes === void 0 || resource === null) {
|
|
783
|
+
check.skip("Write checks are disabled.");
|
|
784
|
+
return;
|
|
785
|
+
}
|
|
786
|
+
const cases = resource.fields.flatMap((field) => {
|
|
787
|
+
if (field.localized === true || field.readOnly === true || field.notInForm === true) return [];
|
|
788
|
+
if (field.type === "daterange") {
|
|
789
|
+
const time = field.settings?.time === true;
|
|
790
|
+
return [[
|
|
791
|
+
field.name,
|
|
792
|
+
time ? {
|
|
793
|
+
start: "2026-11-06T10:00:00Z",
|
|
794
|
+
end: "2026-11-04T10:00:00Z"
|
|
795
|
+
} : {
|
|
796
|
+
start: "2026-11-06",
|
|
797
|
+
end: "2026-11-04"
|
|
798
|
+
},
|
|
799
|
+
"a range that ends before it starts"
|
|
800
|
+
]];
|
|
801
|
+
}
|
|
802
|
+
if (field.type === "money") {
|
|
803
|
+
const currencies = field.settings?.currencies;
|
|
804
|
+
const currency = typeof field.settings?.currency === "string" ? field.settings.currency : Array.isArray(currencies) && typeof currencies[0] === "string" ? currencies[0] : "CAD";
|
|
805
|
+
return [[
|
|
806
|
+
field.name,
|
|
807
|
+
{
|
|
808
|
+
amount: 12.5,
|
|
809
|
+
currency
|
|
810
|
+
},
|
|
811
|
+
"an amount in major units"
|
|
812
|
+
]];
|
|
813
|
+
}
|
|
814
|
+
return [];
|
|
815
|
+
});
|
|
816
|
+
if (cases.length === 0) {
|
|
817
|
+
check.skip("The resource has no daterange or money field.");
|
|
818
|
+
return;
|
|
819
|
+
}
|
|
820
|
+
for (const [name, value, label] of cases) {
|
|
821
|
+
const reply = await request(resourcePath(), {
|
|
822
|
+
method: "POST",
|
|
823
|
+
body: JSON.stringify({
|
|
824
|
+
...writes.create,
|
|
825
|
+
[name]: value
|
|
826
|
+
})
|
|
827
|
+
});
|
|
828
|
+
const failure = check.expectProblem(reply, 422);
|
|
829
|
+
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}.`);
|
|
830
|
+
}
|
|
831
|
+
});
|
|
232
832
|
await run("create", "POST /resources/{resource} creates an item", async (check) => {
|
|
233
833
|
if (writes === void 0 || resource === null) {
|
|
234
834
|
check.skip("Write checks are disabled.");
|
|
@@ -257,6 +857,134 @@ async function runConformance(options) {
|
|
|
257
857
|
if (!check.expectStatus(reply, 200)) return;
|
|
258
858
|
const item = check.expectSchema(ItemResponse, reply.body, "The updated item");
|
|
259
859
|
if (item !== null && createdVersion !== null) check.expect(item.data.version !== createdVersion, "The version did not change after an update.");
|
|
860
|
+
updatedVersion = item?.data.version ?? null;
|
|
861
|
+
});
|
|
862
|
+
await run("update.create-rules", "Rules limited to create do not apply to updates", async (check) => {
|
|
863
|
+
if (createdId === null || writes === void 0 || resource === null) {
|
|
864
|
+
check.skip("No item was created.");
|
|
865
|
+
return;
|
|
866
|
+
}
|
|
867
|
+
const names = resource.fields.filter((field) => field.readOnly !== true && field.notInForm !== true && field.createOnly !== true && (field.rules ?? []).some((rule) => rule.rule === "required" && rule.mode === "create")).map((field) => field.name);
|
|
868
|
+
if (names.length === 0) {
|
|
869
|
+
check.skip("The resource has no rule limited to create.");
|
|
870
|
+
return;
|
|
871
|
+
}
|
|
872
|
+
const reply = await request(itemPath(), {
|
|
873
|
+
method: "PATCH",
|
|
874
|
+
body: JSON.stringify(Object.fromEntries(names.map((name) => [name, null]))),
|
|
875
|
+
headers: updatedVersion !== null ? { "If-Match": `"${updatedVersion}"` } : {}
|
|
876
|
+
});
|
|
877
|
+
check.expect(reply.status === 200, `An update with ${names.join(", ")} set to null got status ${reply.status}, expected 200.`);
|
|
878
|
+
});
|
|
879
|
+
await run("update.parts", "Addresses and links are refused at the path of their invalid part", async (check) => {
|
|
880
|
+
if (createdId === null || writes === void 0 || resource === null) {
|
|
881
|
+
check.skip("No item was created.");
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
884
|
+
const editable = resource.fields.filter((field) => field.readOnly !== true && field.notInForm !== true && field.createOnly !== true && field.localized !== true && field.abilities?.update !== false && field.visibleWhen === void 0);
|
|
885
|
+
const address = editable.find((field) => {
|
|
886
|
+
const countries = field.settings?.countries;
|
|
887
|
+
return field.type === "address" && (!Array.isArray(countries) || countries.includes("CA") || countries.includes("US"));
|
|
888
|
+
});
|
|
889
|
+
const link = editable.find((field) => field.type === "link" && getLinkTypes(field).some((type) => type.id === "email" || type.id === "url"));
|
|
890
|
+
if (address === void 0 && link === void 0) {
|
|
891
|
+
check.skip("The resource has no address nor link field.");
|
|
892
|
+
return;
|
|
893
|
+
}
|
|
894
|
+
const values = {};
|
|
895
|
+
const paths = [];
|
|
896
|
+
if (address !== void 0) {
|
|
897
|
+
const countries = address.settings?.countries;
|
|
898
|
+
const canada = !Array.isArray(countries) || countries.includes("CA");
|
|
899
|
+
values[address.name] = {
|
|
900
|
+
street: "1234 Main Street",
|
|
901
|
+
city: canada ? "Montreal" : "Springfield",
|
|
902
|
+
region: canada ? "QC" : "IL",
|
|
903
|
+
postalCode: canada ? "12345" : "H2X 1Y4",
|
|
904
|
+
country: canada ? "CA" : "US"
|
|
905
|
+
};
|
|
906
|
+
paths.push(`${address.name}.postalCode`);
|
|
907
|
+
}
|
|
908
|
+
if (link !== void 0) {
|
|
909
|
+
const email = getLinkTypes(link).some((type) => type.id === "email");
|
|
910
|
+
values[link.name] = email ? {
|
|
911
|
+
type: "email",
|
|
912
|
+
url: "info@example.com",
|
|
913
|
+
newTab: false
|
|
914
|
+
} : {
|
|
915
|
+
type: "url",
|
|
916
|
+
url: "example",
|
|
917
|
+
newTab: false
|
|
918
|
+
};
|
|
919
|
+
paths.push(`${link.name}.url`);
|
|
920
|
+
}
|
|
921
|
+
const current = await request(itemPath());
|
|
922
|
+
const item = check.expectStatus(current, 200) ? check.expectSchema(ItemResponse, current.body, "The item") : null;
|
|
923
|
+
if (item === null) return;
|
|
924
|
+
const version = item.data.version;
|
|
925
|
+
const reply = await request(itemPath(), {
|
|
926
|
+
method: "PATCH",
|
|
927
|
+
body: JSON.stringify(values),
|
|
928
|
+
headers: version !== null ? { "If-Match": `"${version}"` } : {}
|
|
929
|
+
});
|
|
930
|
+
const failure = check.expectProblem(reply, 422);
|
|
931
|
+
if (failure !== null) {
|
|
932
|
+
const missing = paths.filter((path) => failure.errors?.[path] === void 0);
|
|
933
|
+
check.expect(missing.length === 0, `The 422 problem has no errors at ${missing.join(", ")}: ${Object.keys(failure.errors ?? {}).join(", ") || "none"}.`);
|
|
934
|
+
}
|
|
935
|
+
});
|
|
936
|
+
await run("update.single-field", "A PATCH with a single field and If-Match changes that field alone", async (check) => {
|
|
937
|
+
if (createdId === null || writes === void 0 || resource === null) {
|
|
938
|
+
check.skip("No item was created.");
|
|
939
|
+
return;
|
|
940
|
+
}
|
|
941
|
+
const current = await request(itemPath());
|
|
942
|
+
const before = check.expectStatus(current, 200) ? check.expectSchema(ItemResponse, current.body, "The item") : null;
|
|
943
|
+
if (before === null) return;
|
|
944
|
+
const name = Object.keys(writes.update).find((key) => key in writes.create && !isEqual(writes.create[key], before.data[key]));
|
|
945
|
+
if (name === void 0) {
|
|
946
|
+
check.skip("No value of the update of `writes` differs from its value in `create`, to set it back.");
|
|
947
|
+
return;
|
|
948
|
+
}
|
|
949
|
+
const version = before.data.version;
|
|
950
|
+
const reply = await request(itemPath(), {
|
|
951
|
+
method: "PATCH",
|
|
952
|
+
body: JSON.stringify({ [name]: writes.create[name] }),
|
|
953
|
+
headers: version !== null ? { "If-Match": `"${version}"` } : {}
|
|
954
|
+
});
|
|
955
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
956
|
+
const after = check.expectSchema(ItemResponse, reply.body, "The updated item");
|
|
957
|
+
if (after === null) return;
|
|
958
|
+
if (version !== null) check.expect(after.data.version !== version, "The version did not change after an update of a single field.");
|
|
959
|
+
const changed = resource.fields.filter((field) => field.name !== name && field.readOnly !== true && field.notInForm !== true && field.settings?.secret !== true && !isEqual(before.data[field.name], after.data[field.name])).map((field) => field.name);
|
|
960
|
+
check.expect(changed.length === 0, `An update of ${name} alone also changed ${changed.join(", ")}.`);
|
|
961
|
+
updatedVersion = after.data.version;
|
|
962
|
+
});
|
|
963
|
+
await run("update.not-in-form", "An update ignores the values of the fields that forms leave out", async (check) => {
|
|
964
|
+
if (createdId === null || writes === void 0 || resource === null) {
|
|
965
|
+
check.skip("No item was created.");
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
const names = resource.fields.filter((field) => field.notInForm === true && field.abilities?.view !== false).map((field) => field.name);
|
|
969
|
+
if (names.length === 0) {
|
|
970
|
+
check.skip("The resource has no field with `notInForm`.");
|
|
971
|
+
return;
|
|
972
|
+
}
|
|
973
|
+
const current = await request(itemPath());
|
|
974
|
+
const before = check.expectStatus(current, 200) ? check.expectSchema(ItemResponse, current.body, "The item") : null;
|
|
975
|
+
if (before === null) return;
|
|
976
|
+
const version = before.data.version;
|
|
977
|
+
const reply = await request(itemPath(), {
|
|
978
|
+
method: "PATCH",
|
|
979
|
+
body: JSON.stringify(Object.fromEntries(names.map((name) => [name, "__panneau_conformance__"]))),
|
|
980
|
+
headers: version !== null ? { "If-Match": `"${version}"` } : {}
|
|
981
|
+
});
|
|
982
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
983
|
+
const after = check.expectSchema(ItemResponse, reply.body, "The updated item");
|
|
984
|
+
if (after === null) return;
|
|
985
|
+
const changed = names.filter((name) => !isEqual(before.data[name], after.data[name]));
|
|
986
|
+
check.expect(changed.length === 0, `An update changed ${changed.join(", ")}, which forms leave out.`);
|
|
987
|
+
updatedVersion = after.data.version;
|
|
260
988
|
});
|
|
261
989
|
await run("update.conflict", "A stale If-Match is a 412 problem with the current item", async (check) => {
|
|
262
990
|
if (createdId === null || writes === void 0) {
|
|
@@ -275,6 +1003,68 @@ async function runConformance(options) {
|
|
|
275
1003
|
const failure = check.expectProblem(reply, 412);
|
|
276
1004
|
if (failure !== null) check.expect(failure.current !== void 0, "A 412 problem must include the current item.");
|
|
277
1005
|
});
|
|
1006
|
+
await run("duplicate", "POST /resources/{resource}/{id}/duplicate copies an item that allows it", async (check) => {
|
|
1007
|
+
if (createdId === null) {
|
|
1008
|
+
check.skip("No item was created.");
|
|
1009
|
+
return;
|
|
1010
|
+
}
|
|
1011
|
+
const reply = await request(itemPath());
|
|
1012
|
+
const created = check.expectStatus(reply, 200) ? check.expectSchema(ItemResponse, reply.body, "The item") : null;
|
|
1013
|
+
if (created === null) return;
|
|
1014
|
+
if (created.data.abilities.duplicate !== true) {
|
|
1015
|
+
check.skip("The item doesn't allow `duplicate`.");
|
|
1016
|
+
return;
|
|
1017
|
+
}
|
|
1018
|
+
const copy = await request(`${itemPath()}/duplicate`, { method: "POST" });
|
|
1019
|
+
if (!check.expectStatus(copy, 201)) return;
|
|
1020
|
+
const duplicated = check.expectSchema(ItemResponse, copy.body, "The copy");
|
|
1021
|
+
if (duplicated === null) return;
|
|
1022
|
+
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"}.`);
|
|
1023
|
+
check.expect(duplicated.data.id !== created.data.id, "The copy has the id of the item it copies.");
|
|
1024
|
+
await request(`${resourcePath()}/${encodeURIComponent(duplicated.data.id)}`, { method: "DELETE" });
|
|
1025
|
+
});
|
|
1026
|
+
await run("action.undo", "The undo of an undoable action runs, and puts the item back", async (check) => {
|
|
1027
|
+
const current = resource;
|
|
1028
|
+
if (current === null) {
|
|
1029
|
+
check.skip("No resource to test.");
|
|
1030
|
+
return;
|
|
1031
|
+
}
|
|
1032
|
+
if (createdId === null) {
|
|
1033
|
+
check.skip("No item was created.");
|
|
1034
|
+
return;
|
|
1035
|
+
}
|
|
1036
|
+
const declared = current.actions ?? [];
|
|
1037
|
+
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"));
|
|
1038
|
+
if (action === void 0) {
|
|
1039
|
+
check.skip("The resource has no undoable action without parameters.");
|
|
1040
|
+
return;
|
|
1041
|
+
}
|
|
1042
|
+
const before = await request(itemPath());
|
|
1043
|
+
const item = check.expectStatus(before, 200) ? check.expectSchema(ItemResponse, before.body, "The item") : null;
|
|
1044
|
+
if (item === null) return;
|
|
1045
|
+
const reply = await request(`${resourcePath()}/actions/${encodeURIComponent(action.id)}`, {
|
|
1046
|
+
method: "POST",
|
|
1047
|
+
body: JSON.stringify({ selection: { ids: [createdId] } })
|
|
1048
|
+
});
|
|
1049
|
+
if (!check.expectStatus(reply, 200)) return;
|
|
1050
|
+
const undo = check.expectSchema(ActionResult, reply.body, `The result of \`${action.id}\``)?.undo;
|
|
1051
|
+
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;
|
|
1052
|
+
const undone = await request(`${resourcePath()}/actions/${encodeURIComponent(undo.action)}`, {
|
|
1053
|
+
method: "POST",
|
|
1054
|
+
body: JSON.stringify({
|
|
1055
|
+
selection: { ids: undo.items },
|
|
1056
|
+
values: undo.params ?? {}
|
|
1057
|
+
})
|
|
1058
|
+
});
|
|
1059
|
+
if (!check.expectStatus(undone, 200)) return;
|
|
1060
|
+
const after = await request(itemPath());
|
|
1061
|
+
if (!check.expect(after.status === 200, "After the undo, the item is not back.")) return;
|
|
1062
|
+
const restored = check.expectSchema(ItemResponse, after.body, "The item after the undo");
|
|
1063
|
+
if (restored === null) return;
|
|
1064
|
+
current.fields.forEach((field) => {
|
|
1065
|
+
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])}.`);
|
|
1066
|
+
});
|
|
1067
|
+
});
|
|
278
1068
|
await run("delete", "DELETE /resources/{resource}/{id} deletes the item", async (check) => {
|
|
279
1069
|
if (createdId === null) {
|
|
280
1070
|
check.skip("No item was created.");
|