@antelopejs/dms-media 0.8.2 → 0.8.3-next.167.g8bd8349
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/test/antelope.test.js +1 -0
- package/dist/test/antelope.test.js.map +1 -1
- package/dist/test/helpers/config.d.ts +2 -0
- package/dist/test/helpers/config.js +16 -0
- package/dist/test/helpers/config.js.map +1 -0
- package/dist/test/helpers/delivery.d.ts +9 -0
- package/dist/test/helpers/delivery.js +47 -0
- package/dist/test/helpers/delivery.js.map +1 -0
- package/dist/test/helpers/derivative-storage.d.ts +3 -0
- package/dist/test/helpers/derivative-storage.js +22 -0
- package/dist/test/helpers/derivative-storage.js.map +1 -0
- package/dist/test/helpers/media-config.d.ts +2 -0
- package/dist/test/helpers/media-config.js +16 -0
- package/dist/test/helpers/media-config.js.map +1 -0
- package/dist/test/helpers/member-rights.d.ts +99 -0
- package/dist/test/helpers/member-rights.js +85 -0
- package/dist/test/helpers/member-rights.js.map +1 -0
- package/dist/test/helpers/png.d.ts +1 -0
- package/dist/test/helpers/png.js +15 -0
- package/dist/test/helpers/png.js.map +1 -0
- package/dist/test/helpers/renderings.d.ts +27 -0
- package/dist/test/helpers/renderings.js +106 -0
- package/dist/test/helpers/renderings.js.map +1 -0
- package/dist/test/helpers/uploads.d.ts +26 -0
- package/dist/test/helpers/uploads.js +48 -0
- package/dist/test/helpers/uploads.js.map +1 -0
- package/dist/test/helpers/zip.d.ts +6 -0
- package/dist/test/helpers/zip.js +50 -0
- package/dist/test/helpers/zip.js.map +1 -0
- package/dist/test/integration/media-activity-overview.test.js +109 -0
- package/dist/test/integration/media-activity-overview.test.js.map +1 -1
- package/dist/test/integration/media-api.test.js +101 -0
- package/dist/test/integration/media-api.test.js.map +1 -1
- package/dist/test/integration/media-bindings.test.js +259 -0
- package/dist/test/integration/media-bindings.test.js.map +1 -1
- package/dist/test/integration/media-bulk-writes.test.d.ts +1 -0
- package/dist/test/integration/media-bulk-writes.test.js +159 -0
- package/dist/test/integration/media-bulk-writes.test.js.map +1 -0
- package/dist/test/integration/media-declared-rules.test.d.ts +1 -0
- package/dist/test/integration/media-declared-rules.test.js +297 -0
- package/dist/test/integration/media-declared-rules.test.js.map +1 -0
- package/dist/test/integration/media-declared-sources.test.d.ts +1 -0
- package/dist/test/integration/media-declared-sources.test.js +194 -0
- package/dist/test/integration/media-declared-sources.test.js.map +1 -0
- package/dist/test/integration/media-delivery-active-content.test.d.ts +1 -0
- package/dist/test/integration/media-delivery-active-content.test.js +138 -0
- package/dist/test/integration/media-delivery-active-content.test.js.map +1 -0
- package/dist/test/integration/media-delivery-editing.test.js +41 -51
- package/dist/test/integration/media-delivery-editing.test.js.map +1 -1
- package/dist/test/integration/media-delivery.test.js +145 -0
- package/dist/test/integration/media-delivery.test.js.map +1 -1
- package/dist/test/integration/media-explorer-writes.test.js +173 -18
- package/dist/test/integration/media-explorer-writes.test.js.map +1 -1
- package/dist/test/integration/media-file-reads.test.d.ts +1 -0
- package/dist/test/integration/media-file-reads.test.js +147 -0
- package/dist/test/integration/media-file-reads.test.js.map +1 -0
- package/dist/test/integration/media-guards.test.js +1 -0
- package/dist/test/integration/media-guards.test.js.map +1 -1
- package/dist/test/integration/media-library-views.test.d.ts +1 -0
- package/dist/test/integration/media-library-views.test.js +377 -0
- package/dist/test/integration/media-library-views.test.js.map +1 -0
- package/dist/test/integration/media-linked-subfolders.test.js +2 -0
- package/dist/test/integration/media-linked-subfolders.test.js.map +1 -1
- package/dist/test/integration/media-member-access.test.js +96 -0
- package/dist/test/integration/media-member-access.test.js.map +1 -1
- package/dist/test/integration/media-permissions.test.js +7 -0
- package/dist/test/integration/media-permissions.test.js.map +1 -1
- package/dist/test/integration/media-preset-delivery.test.d.ts +1 -0
- package/dist/test/integration/media-preset-delivery.test.js +161 -0
- package/dist/test/integration/media-preset-delivery.test.js.map +1 -0
- package/dist/test/integration/media-presets.test.d.ts +1 -0
- package/dist/test/integration/media-presets.test.js +223 -0
- package/dist/test/integration/media-presets.test.js.map +1 -0
- package/dist/test/integration/media-transform.test.js +105 -10
- package/dist/test/integration/media-transform.test.js.map +1 -1
- package/dist/test/integration/media-upload-content.test.d.ts +1 -0
- package/dist/test/integration/media-upload-content.test.js +142 -0
- package/dist/test/integration/media-upload-content.test.js.map +1 -0
- package/dist/test/integration/media-upload-policy.test.js +13 -12
- package/dist/test/integration/media-upload-policy.test.js.map +1 -1
- package/dist/test/integration/media-upload-types.test.d.ts +1 -0
- package/dist/test/integration/media-upload-types.test.js +176 -0
- package/dist/test/integration/media-upload-types.test.js.map +1 -0
- package/dist/test/integration/permission-levels.d.ts +1 -0
- package/dist/test/integration/permission-levels.js +207 -0
- package/dist/test/integration/permission-levels.js.map +1 -0
- package/dist/test/integration/permission-reach.d.ts +1 -0
- package/dist/test/integration/permission-reach.js +209 -0
- package/dist/test/integration/permission-reach.js.map +1 -0
- package/dist/test/integration/permissions-management.js +7 -0
- package/dist/test/integration/permissions-management.js.map +1 -1
- package/dist/test/unit/asset-type.test.d.ts +1 -0
- package/dist/test/unit/asset-type.test.js +63 -0
- package/dist/test/unit/asset-type.test.js.map +1 -0
- package/frontend-vue/package.json +1 -1
- package/frontend-vue/pnpm-lock.yaml +11 -11
- package/package.json +10 -8
- package/src/test/antelope.test.ts +1 -0
- package/src/test/helpers/config.ts +14 -0
- package/src/test/helpers/delivery.ts +83 -0
- package/src/test/helpers/derivative-storage.ts +25 -0
- package/src/test/helpers/media-config.ts +16 -0
- package/src/test/helpers/member-rights.ts +116 -0
- package/src/test/helpers/png.ts +10 -0
- package/src/test/helpers/renderings.ts +147 -0
- package/src/test/helpers/uploads.ts +92 -0
- package/src/test/helpers/zip.ts +62 -0
- package/src/test/integration/media-activity-overview.test.ts +170 -0
- package/src/test/integration/media-api.test.ts +135 -1
- package/src/test/integration/media-bindings.test.ts +316 -2
- package/src/test/integration/media-bulk-writes.test.ts +180 -0
- package/src/test/integration/media-declared-rules.test.ts +405 -0
- package/src/test/integration/media-declared-sources.test.ts +228 -0
- package/src/test/integration/media-delivery-active-content.test.ts +185 -0
- package/src/test/integration/media-delivery-editing.test.ts +24 -46
- package/src/test/integration/media-delivery.test.ts +217 -1
- package/src/test/integration/media-explorer-writes.test.ts +253 -23
- package/src/test/integration/media-file-reads.test.ts +226 -0
- package/src/test/integration/media-guards.test.ts +1 -0
- package/src/test/integration/media-library-views.test.ts +478 -0
- package/src/test/integration/media-linked-subfolders.test.ts +2 -0
- package/src/test/integration/media-member-access.test.ts +146 -0
- package/src/test/integration/media-permissions.test.ts +10 -1
- package/src/test/integration/media-preset-delivery.test.ts +261 -0
- package/src/test/integration/media-presets.test.ts +304 -0
- package/src/test/integration/media-transform.test.ts +152 -10
- package/src/test/integration/media-upload-content.test.ts +192 -0
- package/src/test/integration/media-upload-policy.test.ts +20 -18
- package/src/test/integration/media-upload-types.test.ts +267 -0
- package/src/test/integration/permission-levels.ts +332 -0
- package/src/test/integration/permission-reach.ts +302 -0
- package/src/test/integration/permissions-management.ts +7 -0
- package/src/test/unit/asset-type.test.ts +87 -0
|
@@ -0,0 +1,405 @@
|
|
|
1
|
+
import { mkdtemp, open, rm } from "node:fs/promises";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { GetModel } from "@antelopejs/interface-database-decorators";
|
|
5
|
+
import { DEFAULT_TENANT_ID } from "@antelopejs/interface-dms/constants";
|
|
6
|
+
import {
|
|
7
|
+
EnsureMediaFolder,
|
|
8
|
+
ImportMediaAsset,
|
|
9
|
+
type MediaAssetDeclaration,
|
|
10
|
+
type MediaFolderDeclaration,
|
|
11
|
+
RegisterAssetBinding,
|
|
12
|
+
} from "@antelopejs/interface-dms-media";
|
|
13
|
+
import type { AxiosInstance } from "axios";
|
|
14
|
+
import { assert, expect } from "chai";
|
|
15
|
+
import sharp from "sharp";
|
|
16
|
+
import { MediaAssetModel, MediaFolderModel } from "../../db";
|
|
17
|
+
import { PNG_MIMETYPE } from "../helpers/assets";
|
|
18
|
+
import { withMediaConfig } from "../helpers/media-config";
|
|
19
|
+
import { authorizedClient } from "../helpers/http";
|
|
20
|
+
import { ensureOwnerSession } from "../helpers/owner";
|
|
21
|
+
|
|
22
|
+
const HTTP_OK = 200;
|
|
23
|
+
const BINDING_ID = "declared-rules.covers";
|
|
24
|
+
const ROOT_KEY = "declared-rules.root";
|
|
25
|
+
|
|
26
|
+
function folders() {
|
|
27
|
+
return GetModel(MediaFolderModel, DEFAULT_TENANT_ID);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function assets() {
|
|
31
|
+
return GetModel(MediaAssetModel, DEFAULT_TENANT_ID);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async function rejectionOf(promise: Promise<unknown>): Promise<Error> {
|
|
35
|
+
try {
|
|
36
|
+
await promise;
|
|
37
|
+
} catch (error) {
|
|
38
|
+
assert.instanceOf(error, Error);
|
|
39
|
+
return error;
|
|
40
|
+
}
|
|
41
|
+
throw new Error("Expected the declaration to be refused");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async function reasonOf(
|
|
45
|
+
promise: Promise<unknown>,
|
|
46
|
+
kind: "folder" | "file",
|
|
47
|
+
key: string,
|
|
48
|
+
): Promise<string> {
|
|
49
|
+
const prefix = `Media ${kind} '${key}': `.normalize("NFC");
|
|
50
|
+
const message = (await rejectionOf(promise)).message.normalize("NFC");
|
|
51
|
+
assert.isTrue(message.startsWith(prefix), message);
|
|
52
|
+
return message.slice(prefix.length);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const FORBIDDEN_NAME_REASON =
|
|
56
|
+
"name: a name can't contain / \\ < > : \" | ? * or control characters";
|
|
57
|
+
const NAME_TOO_LONG_REASON = "name: the name is limited to 255 characters";
|
|
58
|
+
const FORBIDDEN_CHARACTERS = [
|
|
59
|
+
"/",
|
|
60
|
+
"\\",
|
|
61
|
+
"<",
|
|
62
|
+
">",
|
|
63
|
+
":",
|
|
64
|
+
'"',
|
|
65
|
+
"|",
|
|
66
|
+
"?",
|
|
67
|
+
"*",
|
|
68
|
+
"\u0007",
|
|
69
|
+
];
|
|
70
|
+
const LIBRARY_REFUSES_TYPE = "This type of file can't be added to the library";
|
|
71
|
+
|
|
72
|
+
let client: AxiosInstance;
|
|
73
|
+
let tempDir: string;
|
|
74
|
+
|
|
75
|
+
function describeFolderNameRules(): void {
|
|
76
|
+
describe("folder names", () => {
|
|
77
|
+
it("refuses every character and length the dashboard refuses, with its reason", async () => {
|
|
78
|
+
const refused: string[] = [];
|
|
79
|
+
for (const character of FORBIDDEN_CHARACTERS) {
|
|
80
|
+
const key = `rules.forbidden-${character.codePointAt(0)}`;
|
|
81
|
+
const name = `a${character}b`;
|
|
82
|
+
refused.push(name);
|
|
83
|
+
expect(
|
|
84
|
+
await reasonOf(
|
|
85
|
+
EnsureMediaFolder({ tenantId: DEFAULT_TENANT_ID, key, name }),
|
|
86
|
+
"folder",
|
|
87
|
+
key,
|
|
88
|
+
),
|
|
89
|
+
JSON.stringify(name),
|
|
90
|
+
).to.equal(FORBIDDEN_NAME_REASON);
|
|
91
|
+
}
|
|
92
|
+
const longName = "n".repeat(256);
|
|
93
|
+
expect(
|
|
94
|
+
await reasonOf(
|
|
95
|
+
EnsureMediaFolder({
|
|
96
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
97
|
+
key: "rules.too-long",
|
|
98
|
+
name: longName,
|
|
99
|
+
}),
|
|
100
|
+
"folder",
|
|
101
|
+
"rules.too-long",
|
|
102
|
+
),
|
|
103
|
+
).to.equal(NAME_TOO_LONG_REASON);
|
|
104
|
+
const names = (await folders().getAll()).map((folder) => folder.name);
|
|
105
|
+
for (const name of [...refused, longName]) {
|
|
106
|
+
expect(names, "no refused folder is created").to.not.include(name);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("refuses a sibling's name in any case or Unicode form, and allows it in another folder", async () => {
|
|
111
|
+
const name = "Été rules";
|
|
112
|
+
const parent = await EnsureMediaFolder({
|
|
113
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
114
|
+
key: "rules.twin-parent",
|
|
115
|
+
name,
|
|
116
|
+
});
|
|
117
|
+
expect(parent.status).to.equal("created");
|
|
118
|
+
const decomposed = name.normalize("NFD");
|
|
119
|
+
expect(
|
|
120
|
+
await reasonOf(
|
|
121
|
+
EnsureMediaFolder({
|
|
122
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
123
|
+
key: "rules.twin-nfd",
|
|
124
|
+
name: decomposed,
|
|
125
|
+
}),
|
|
126
|
+
"folder",
|
|
127
|
+
"rules.twin-nfd",
|
|
128
|
+
),
|
|
129
|
+
).to.equal(`a folder named '${name}' already exists there`);
|
|
130
|
+
const child = {
|
|
131
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
132
|
+
parent: "rules.twin-parent",
|
|
133
|
+
};
|
|
134
|
+
await EnsureMediaFolder({ ...child, key: "rules.twin", name: "Twin" });
|
|
135
|
+
expect(
|
|
136
|
+
await reasonOf(
|
|
137
|
+
EnsureMediaFolder({ ...child, key: "rules.twin-caps", name: "TWIN" }),
|
|
138
|
+
"folder",
|
|
139
|
+
"rules.twin-caps",
|
|
140
|
+
),
|
|
141
|
+
).to.equal("a folder named 'TWIN' already exists there");
|
|
142
|
+
const inside = await EnsureMediaFolder({
|
|
143
|
+
...child,
|
|
144
|
+
key: "rules.twin-inside",
|
|
145
|
+
name,
|
|
146
|
+
});
|
|
147
|
+
expect(inside.status).to.equal("created");
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function describeFolderTargetRules(): void {
|
|
153
|
+
describe("folder targets and keys", () => {
|
|
154
|
+
it("names the parent, binding or ACL entry a declaration gets wrong", async () => {
|
|
155
|
+
const refusal = (key: string, extra: Partial<MediaFolderDeclaration>) =>
|
|
156
|
+
reasonOf(
|
|
157
|
+
EnsureMediaFolder({
|
|
158
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
159
|
+
key,
|
|
160
|
+
name: key,
|
|
161
|
+
...extra,
|
|
162
|
+
}),
|
|
163
|
+
"folder",
|
|
164
|
+
key,
|
|
165
|
+
);
|
|
166
|
+
expect(
|
|
167
|
+
await refusal("rules.orphan", { parent: "rules.nowhere" }),
|
|
168
|
+
).to.equal(
|
|
169
|
+
"'rules.nowhere' is not a declared folder: declare it with EnsureMediaFolder first",
|
|
170
|
+
);
|
|
171
|
+
expect(await refusal("rules.self", { parent: "rules.self" })).to.equal(
|
|
172
|
+
"a folder cannot be its own parent",
|
|
173
|
+
);
|
|
174
|
+
const doomed = await EnsureMediaFolder({
|
|
175
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
176
|
+
key: "rules.doomed",
|
|
177
|
+
name: "Doomed",
|
|
178
|
+
});
|
|
179
|
+
const deleted = await client.delete(`/api/media/folders/${doomed.id}`);
|
|
180
|
+
expect(deleted.status).to.equal(HTTP_OK);
|
|
181
|
+
expect(await refusal("rules.under", { parent: "rules.doomed" })).to.equal(
|
|
182
|
+
"the folder declared as 'rules.doomed' was deleted",
|
|
183
|
+
);
|
|
184
|
+
expect(
|
|
185
|
+
await refusal("rules.acl", {
|
|
186
|
+
acl: [{ subject: { kind: "permission", id: "x" }, rights: [] }],
|
|
187
|
+
}),
|
|
188
|
+
).to.match(/^acl\.0\.rights: .+/);
|
|
189
|
+
const names = (await folders().getAll()).map((folder) => folder.name);
|
|
190
|
+
for (const refused of [
|
|
191
|
+
"rules.orphan",
|
|
192
|
+
"rules.self",
|
|
193
|
+
"rules.under",
|
|
194
|
+
"rules.acl",
|
|
195
|
+
]) {
|
|
196
|
+
expect(names, "no refused folder is created").to.not.include(refused);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("trims the key, and takes 1 to 255 characters", async () => {
|
|
201
|
+
const spaced = await EnsureMediaFolder({
|
|
202
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
203
|
+
key: " rules.spaced ",
|
|
204
|
+
name: "Spaced",
|
|
205
|
+
});
|
|
206
|
+
const plain = await EnsureMediaFolder({
|
|
207
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
208
|
+
key: "rules.spaced",
|
|
209
|
+
name: "Spaced again",
|
|
210
|
+
});
|
|
211
|
+
expect(spaced.status).to.equal("created");
|
|
212
|
+
expect(plain).to.deep.equal({ status: "kept", id: spaced.id });
|
|
213
|
+
for (const key of [" ", "k".repeat(256)]) {
|
|
214
|
+
expect(
|
|
215
|
+
await reasonOf(
|
|
216
|
+
EnsureMediaFolder({
|
|
217
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
218
|
+
key,
|
|
219
|
+
name: "Bad key",
|
|
220
|
+
}),
|
|
221
|
+
"folder",
|
|
222
|
+
key,
|
|
223
|
+
),
|
|
224
|
+
).to.match(/^key: .+/);
|
|
225
|
+
}
|
|
226
|
+
const longest = await EnsureMediaFolder({
|
|
227
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
228
|
+
key: "m".repeat(255),
|
|
229
|
+
name: "Longest key",
|
|
230
|
+
});
|
|
231
|
+
expect(longest.status).to.equal("created");
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function describeFileRules(): void {
|
|
237
|
+
describe("file rules", () => {
|
|
238
|
+
const declare = (suffix: string, fields: Partial<MediaAssetDeclaration>) =>
|
|
239
|
+
ImportMediaAsset({
|
|
240
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
241
|
+
key: `rules.file-${suffix}`,
|
|
242
|
+
folder: { key: ROOT_KEY },
|
|
243
|
+
name: "ok.txt",
|
|
244
|
+
mimetype: "text/plain",
|
|
245
|
+
source: { bytes: Buffer.from("ok") },
|
|
246
|
+
...fields,
|
|
247
|
+
});
|
|
248
|
+
const refusal = (suffix: string, fields: Partial<MediaAssetDeclaration>) =>
|
|
249
|
+
reasonOf(declare(suffix, fields), "file", `rules.file-${suffix}`);
|
|
250
|
+
|
|
251
|
+
it("refuses a name with / \\ or a control character, or over 255 characters, as the dashboard does", async () => {
|
|
252
|
+
for (const [index, name] of [
|
|
253
|
+
"a/b.txt",
|
|
254
|
+
"a\\b.txt",
|
|
255
|
+
"a\u0001b.txt",
|
|
256
|
+
].entries()) {
|
|
257
|
+
expect(await refusal(`name-${index}`, { name }), name).to.equal(
|
|
258
|
+
FORBIDDEN_NAME_REASON,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
expect(
|
|
262
|
+
await refusal("name-long", { name: `${"n".repeat(252)}.txt` }),
|
|
263
|
+
).to.equal(NAME_TOO_LONG_REASON);
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
it("accepts a colon, an alt and a name of 255 characters", async () => {
|
|
267
|
+
for (const [suffix, fields] of Object.entries({
|
|
268
|
+
colon: { name: "a:b.txt" },
|
|
269
|
+
alt: { alt: "a".repeat(255) },
|
|
270
|
+
name: { name: `${"n".repeat(251)}.txt` },
|
|
271
|
+
})) {
|
|
272
|
+
expect((await declare(`ok-${suffix}`, fields)).status, suffix).to.equal(
|
|
273
|
+
"created",
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it("refuses an empty file and an alt over 255 characters", async () => {
|
|
279
|
+
expect(
|
|
280
|
+
await refusal("empty", { source: { bytes: Buffer.alloc(0) } }),
|
|
281
|
+
).to.equal("declaration: the file is empty");
|
|
282
|
+
expect(await refusal("alt", { alt: "a".repeat(256) })).to.equal(
|
|
283
|
+
"alt: the alt text is limited to 255 characters",
|
|
284
|
+
);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("names the folder a file is declared into when it was never declared", async () => {
|
|
288
|
+
expect(
|
|
289
|
+
await refusal("nofolder", { folder: { key: "rules.missing" } }),
|
|
290
|
+
).to.equal(
|
|
291
|
+
"'rules.missing' is not a declared folder: declare it with EnsureMediaFolder first",
|
|
292
|
+
);
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function describeUploadLimits(): void {
|
|
298
|
+
describe("the limits of an upload", () => {
|
|
299
|
+
const declare = (suffix: string, fields: Partial<MediaAssetDeclaration>) =>
|
|
300
|
+
reasonOf(
|
|
301
|
+
ImportMediaAsset({
|
|
302
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
303
|
+
key: `limits.${suffix}`,
|
|
304
|
+
folder: { key: ROOT_KEY },
|
|
305
|
+
name: "x.txt",
|
|
306
|
+
mimetype: "text/plain",
|
|
307
|
+
source: { bytes: Buffer.from("x") },
|
|
308
|
+
...fields,
|
|
309
|
+
}),
|
|
310
|
+
"file",
|
|
311
|
+
`limits.${suffix}`,
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
it("refuses a file over 524288000 bytes", async function () {
|
|
315
|
+
this.timeout(60_000);
|
|
316
|
+
const path = join(tempDir, "big.bin");
|
|
317
|
+
const handle = await open(path, "w");
|
|
318
|
+
await handle.truncate(500 * 1024 * 1024 + 1);
|
|
319
|
+
await handle.close();
|
|
320
|
+
expect(
|
|
321
|
+
await declare("big", {
|
|
322
|
+
name: "big.pdf",
|
|
323
|
+
mimetype: "application/pdf",
|
|
324
|
+
source: { filePath: path },
|
|
325
|
+
}),
|
|
326
|
+
).to.equal("declaration: the file exceeds 524288000 bytes");
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("refuses the types the library refuses, an unreadable image and a bound field's other types", async () => {
|
|
330
|
+
const html = { name: "page.htm", source: { bytes: Buffer.from("<p>") } };
|
|
331
|
+
expect(await declare("htm", html), "html under another type").to.equal(
|
|
332
|
+
LIBRARY_REFUSES_TYPE,
|
|
333
|
+
);
|
|
334
|
+
expect(
|
|
335
|
+
await declare("exe", {
|
|
336
|
+
name: "tool.exe",
|
|
337
|
+
mimetype: "application/x-msdownload",
|
|
338
|
+
}),
|
|
339
|
+
).to.equal(LIBRARY_REFUSES_TYPE);
|
|
340
|
+
expect(
|
|
341
|
+
await declare("field", { folder: { binding: BINDING_ID } }),
|
|
342
|
+
).to.equal("This folder feeds a field that only accepts image/*");
|
|
343
|
+
expect(
|
|
344
|
+
await declare("broken", {
|
|
345
|
+
name: "broken.jpg",
|
|
346
|
+
mimetype: "image/jpeg",
|
|
347
|
+
source: { bytes: Buffer.from("nope") },
|
|
348
|
+
}),
|
|
349
|
+
).to.equal("This file is not a readable image");
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it("refuses a file once the storage plan is full", async () => {
|
|
353
|
+
await withMediaConfig({ storageQuotaBytes: 1 }, async () => {
|
|
354
|
+
expect(
|
|
355
|
+
await declare("quota", { source: { bytes: Buffer.alloc(64, 97) } }),
|
|
356
|
+
).to.equal("The library's storage plan is full");
|
|
357
|
+
});
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
it("stores a JPEG declared as a PNG under its decoded type", async () => {
|
|
361
|
+
const jpeg = await sharp({
|
|
362
|
+
create: { width: 9, height: 7, channels: 3, background: "#cc3333" },
|
|
363
|
+
})
|
|
364
|
+
.jpeg()
|
|
365
|
+
.toBuffer();
|
|
366
|
+
const result = await ImportMediaAsset({
|
|
367
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
368
|
+
key: "limits.claimed",
|
|
369
|
+
folder: { key: ROOT_KEY },
|
|
370
|
+
name: "really-jpeg.png",
|
|
371
|
+
mimetype: PNG_MIMETYPE,
|
|
372
|
+
source: { bytes: jpeg },
|
|
373
|
+
});
|
|
374
|
+
const asset = await assets().get(result.id);
|
|
375
|
+
expect([asset?.mimetype, asset?.width]).to.deep.equal(["image/jpeg", 9]);
|
|
376
|
+
});
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
describe("[integration] media declared content — the rules a declaration follows", () => {
|
|
381
|
+
before(async function () {
|
|
382
|
+
this.timeout(40_000);
|
|
383
|
+
RegisterAssetBinding({
|
|
384
|
+
id: BINDING_ID,
|
|
385
|
+
folderName: "Declared rules covers",
|
|
386
|
+
field: { mimetypes: ["image/*"] },
|
|
387
|
+
});
|
|
388
|
+
client = authorizedClient((await ensureOwnerSession()).accessToken);
|
|
389
|
+
tempDir = await mkdtemp(join(tmpdir(), "dms-media-declared-rules-"));
|
|
390
|
+
await EnsureMediaFolder({
|
|
391
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
392
|
+
key: ROOT_KEY,
|
|
393
|
+
name: "Declared rules root",
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
|
|
397
|
+
after(async () => {
|
|
398
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
describeFolderNameRules();
|
|
402
|
+
describeFolderTargetRules();
|
|
403
|
+
describeFileRules();
|
|
404
|
+
describeUploadLimits();
|
|
405
|
+
});
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { mkdtemp, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { Schema } from "@antelopejs/interface-database";
|
|
6
|
+
import { GetModel } from "@antelopejs/interface-database-decorators";
|
|
7
|
+
import {
|
|
8
|
+
DEFAULT_TENANT_ID,
|
|
9
|
+
TENANT_SCHEMA_NAME,
|
|
10
|
+
} from "@antelopejs/interface-dms/constants";
|
|
11
|
+
import {
|
|
12
|
+
Hook,
|
|
13
|
+
RegisterHook,
|
|
14
|
+
type TenantProvisioningHookPayload,
|
|
15
|
+
UnregisterHook,
|
|
16
|
+
} from "@antelopejs/interface-dms/hooks";
|
|
17
|
+
import { ProvisionTenant } from "@antelopejs/interface-dms/tenant-provisioning";
|
|
18
|
+
import {
|
|
19
|
+
EnsureMediaFolder,
|
|
20
|
+
ImportMediaAsset,
|
|
21
|
+
} from "@antelopejs/interface-dms-media";
|
|
22
|
+
import { FileExists } from "@antelopejs/interface-file-storage";
|
|
23
|
+
import type { AxiosInstance } from "axios";
|
|
24
|
+
import { assert, expect } from "chai";
|
|
25
|
+
import sharp from "sharp";
|
|
26
|
+
import { MediaAssetModel, MediaEventModel, MediaFolderModel } from "../../db";
|
|
27
|
+
import { PNG_MIMETYPE, uploadTextAsset } from "../helpers/assets";
|
|
28
|
+
import { readBody, urlBody } from "../helpers/bodies";
|
|
29
|
+
import { authorizedClient } from "../helpers/http";
|
|
30
|
+
import { ensureOwnerSession } from "../helpers/owner";
|
|
31
|
+
|
|
32
|
+
const HTTP_OK = 200;
|
|
33
|
+
const ROOT_KEY = "declared-sources.root";
|
|
34
|
+
|
|
35
|
+
function folders() {
|
|
36
|
+
return GetModel(MediaFolderModel, DEFAULT_TENANT_ID);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function assets() {
|
|
40
|
+
return GetModel(MediaAssetModel, DEFAULT_TENANT_ID);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function storedBytes(assetId: string): Promise<Buffer> {
|
|
44
|
+
const readUrl = await client.get(`/api/media/assets/${assetId}/read-url`);
|
|
45
|
+
expect(readUrl.status).to.equal(HTTP_OK);
|
|
46
|
+
const stored = await fetch(readBody(readUrl, urlBody).url);
|
|
47
|
+
return Buffer.from(await stored.arrayBuffer());
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function readStorageDir(): string {
|
|
51
|
+
const dir = process.env.TEST_STORAGE_DIR;
|
|
52
|
+
if (!dir) throw new Error("TEST_STORAGE_DIR is not set by the test setup");
|
|
53
|
+
return dir;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let client: AxiosInstance;
|
|
57
|
+
let tempDir: string;
|
|
58
|
+
|
|
59
|
+
function describeDeclaredFileDetails(): void {
|
|
60
|
+
describe("declared file details", () => {
|
|
61
|
+
it("reads a lazy source once, at its decoded size, and records the upload as System's", async () => {
|
|
62
|
+
const png = await sharp({
|
|
63
|
+
create: { width: 640, height: 480, channels: 3, background: "#336699" },
|
|
64
|
+
})
|
|
65
|
+
.png()
|
|
66
|
+
.toBuffer();
|
|
67
|
+
let loads = 0;
|
|
68
|
+
const result = await ImportMediaAsset({
|
|
69
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
70
|
+
key: "declared.once-loaded",
|
|
71
|
+
folder: { key: ROOT_KEY },
|
|
72
|
+
name: "Plan été.png",
|
|
73
|
+
mimetype: PNG_MIMETYPE,
|
|
74
|
+
source: {
|
|
75
|
+
load: () => {
|
|
76
|
+
loads++;
|
|
77
|
+
return png;
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
expect(result.status).to.equal("created");
|
|
82
|
+
expect(loads, "the source of a new file is read once").to.equal(1);
|
|
83
|
+
const asset = await assets().get(result.id);
|
|
84
|
+
expect([asset?.width, asset?.height]).to.deep.equal([640, 480]);
|
|
85
|
+
const events = await GetModel(
|
|
86
|
+
MediaEventModel,
|
|
87
|
+
DEFAULT_TENANT_ID,
|
|
88
|
+
).listByAsset(result.id);
|
|
89
|
+
expect(events.map((event) => event.actorName)).to.deep.equal(["System"]);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("stores a long text byte for byte from bytes, a path and a load function", async () => {
|
|
93
|
+
const text = Buffer.from(
|
|
94
|
+
Array.from(
|
|
95
|
+
{ length: 6000 },
|
|
96
|
+
(_, line) => `ligne ${line} : été, naïve, 東京, €\n`,
|
|
97
|
+
).join(""),
|
|
98
|
+
);
|
|
99
|
+
const filePath = join(tempDir, "long.txt");
|
|
100
|
+
await writeFile(filePath, text);
|
|
101
|
+
const sources = {
|
|
102
|
+
bytes: { bytes: text },
|
|
103
|
+
filePath: { filePath },
|
|
104
|
+
load: { load: () => text },
|
|
105
|
+
};
|
|
106
|
+
for (const [kind, source] of Object.entries(sources)) {
|
|
107
|
+
const result = await ImportMediaAsset({
|
|
108
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
109
|
+
key: `declared.source-${kind}`,
|
|
110
|
+
folder: { key: ROOT_KEY },
|
|
111
|
+
name: `${kind} — été.txt`,
|
|
112
|
+
mimetype: "text/plain",
|
|
113
|
+
source,
|
|
114
|
+
});
|
|
115
|
+
expect(result.status, kind).to.equal("created");
|
|
116
|
+
expect(await storedBytes(result.id), kind).to.deep.equal(text);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("stores declared and uploaded files under media/<tenant>/<folder>/ in the file storage", async () => {
|
|
121
|
+
const folder = await EnsureMediaFolder({
|
|
122
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
123
|
+
key: "declared.paths",
|
|
124
|
+
name: "Declared paths",
|
|
125
|
+
});
|
|
126
|
+
const declared = await ImportMediaAsset({
|
|
127
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
128
|
+
key: "declared.paths.file",
|
|
129
|
+
folder: { key: "declared.paths" },
|
|
130
|
+
name: "chemin.txt",
|
|
131
|
+
mimetype: "text/plain",
|
|
132
|
+
source: { bytes: Buffer.from("chemin") },
|
|
133
|
+
});
|
|
134
|
+
const uploaded = await uploadTextAsset(client, folder.id, "envoi.txt");
|
|
135
|
+
const folderPath = `media/${DEFAULT_TENANT_ID}/${folder.id}`;
|
|
136
|
+
const files = join(readStorageDir(), "files");
|
|
137
|
+
const declaredAsset = await assets().get(declared.id);
|
|
138
|
+
const uploadedAsset = await assets().get(uploaded.id);
|
|
139
|
+
assert.isDefined(declaredAsset, "the declared file");
|
|
140
|
+
assert.isDefined(uploadedAsset, "the uploaded file");
|
|
141
|
+
expect(
|
|
142
|
+
existsSync(join(files, folderPath, declaredAsset.storageKey)),
|
|
143
|
+
"the declared file is stored under the tenant and folder",
|
|
144
|
+
).to.equal(true);
|
|
145
|
+
expect(uploadedAsset.storageKey).to.match(new RegExp(`^${folderPath}/`));
|
|
146
|
+
for (const asset of [declaredAsset, uploadedAsset]) {
|
|
147
|
+
expect(await FileExists(asset.storageKey), asset.storageKey).to.equal(
|
|
148
|
+
true,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("trims the tenant id, and creates the folder in that tenant", async () => {
|
|
154
|
+
const result = await EnsureMediaFolder({
|
|
155
|
+
tenantId: " default ",
|
|
156
|
+
key: "declared.trimmed-tenant",
|
|
157
|
+
name: "Trimmed tenant",
|
|
158
|
+
});
|
|
159
|
+
expect(result.status).to.equal("created");
|
|
160
|
+
expect((await folders().get(result.id))?.name).to.equal("Trimmed tenant");
|
|
161
|
+
});
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function describeTenantProvisioning(): void {
|
|
166
|
+
describe("tenant provisioning", () => {
|
|
167
|
+
it("ships the folder and file a Hook.TENANT_BEING_PROVISIONED handler declares into the new tenant, as System's", async () => {
|
|
168
|
+
const tenantId = "declared-provisioned";
|
|
169
|
+
const handler = async (payload: TenantProvisioningHookPayload) => {
|
|
170
|
+
if (payload.tenantId !== tenantId) return;
|
|
171
|
+
await EnsureMediaFolder({
|
|
172
|
+
tenantId,
|
|
173
|
+
key: "provisioned",
|
|
174
|
+
name: "Provisioned",
|
|
175
|
+
});
|
|
176
|
+
await ImportMediaAsset({
|
|
177
|
+
tenantId,
|
|
178
|
+
key: "provisioned.procedure",
|
|
179
|
+
folder: { key: "provisioned" },
|
|
180
|
+
name: "procedure.txt",
|
|
181
|
+
mimetype: "text/plain",
|
|
182
|
+
source: { bytes: Buffer.from("Check the harness before you climb.") },
|
|
183
|
+
});
|
|
184
|
+
};
|
|
185
|
+
RegisterHook(Hook.TENANT_BEING_PROVISIONED, handler);
|
|
186
|
+
try {
|
|
187
|
+
await ProvisionTenant({ tenantId, extras: {} });
|
|
188
|
+
const folder = (
|
|
189
|
+
await GetModel(MediaFolderModel, tenantId).getAll()
|
|
190
|
+
).find((entry) => entry.name === "Provisioned");
|
|
191
|
+
assert.isDefined(folder, "the provisioned folder");
|
|
192
|
+
const files = await GetModel(MediaAssetModel, tenantId).getAll();
|
|
193
|
+
expect(files.map((file) => [file.name, file.folderId])).to.deep.equal([
|
|
194
|
+
["procedure.txt", folder._id],
|
|
195
|
+
]);
|
|
196
|
+
const events = await GetModel(MediaEventModel, tenantId).listByAsset(
|
|
197
|
+
files[0]?._id ?? "",
|
|
198
|
+
);
|
|
199
|
+
expect(events.map((event) => event.actorName)).to.deep.equal([
|
|
200
|
+
"System",
|
|
201
|
+
]);
|
|
202
|
+
} finally {
|
|
203
|
+
UnregisterHook(Hook.TENANT_BEING_PROVISIONED, handler);
|
|
204
|
+
await Schema.get(TENANT_SCHEMA_NAME)?.destroyInstance(tenantId).run();
|
|
205
|
+
}
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
describe("[integration] media declared content — sources, storage paths and tenant provisioning", () => {
|
|
211
|
+
before(async function () {
|
|
212
|
+
this.timeout(40_000);
|
|
213
|
+
client = authorizedClient((await ensureOwnerSession()).accessToken);
|
|
214
|
+
tempDir = await mkdtemp(join(tmpdir(), "dms-media-declared-sources-"));
|
|
215
|
+
await EnsureMediaFolder({
|
|
216
|
+
tenantId: DEFAULT_TENANT_ID,
|
|
217
|
+
key: ROOT_KEY,
|
|
218
|
+
name: "Declared sources root",
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
after(async () => {
|
|
223
|
+
await rm(tempDir, { recursive: true, force: true });
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describeDeclaredFileDetails();
|
|
227
|
+
describeTenantProvisioning();
|
|
228
|
+
});
|