@terreno/admin-backend 57.0.0-beta.1 → 57.1.0
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/README.md +3 -0
- package/dist/adminApp.d.ts +11 -4
- package/dist/adminApp.js +859 -263
- package/dist/adminConfigIdentity.d.ts +16 -0
- package/dist/adminConfigIdentity.js +100 -0
- package/dist/adminTextSearch.d.ts +17 -0
- package/dist/adminTextSearch.js +117 -0
- package/dist/adminUiV2.js +1 -1
- package/dist/aggregateAdmin.d.ts +20 -0
- package/dist/aggregateAdmin.js +275 -0
- package/dist/documentStorageApp.d.ts +3 -1
- package/dist/documentStorageApp.js +20 -42
- package/dist/filterParser.d.ts +11 -0
- package/dist/filterParser.js +260 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +15 -1
- package/dist/legacy.d.ts +9 -0
- package/dist/legacy.js +61 -0
- package/dist/resolvedAdminModel.d.ts +23 -0
- package/dist/resolvedAdminModel.js +130 -0
- package/dist/routePath.d.ts +2 -0
- package/dist/routePath.js +18 -0
- package/package.json +4 -3
- package/src/adminApp.models.test.ts +894 -1
- package/src/adminApp.test.ts +35 -1
- package/src/adminApp.ts +599 -98
- package/src/adminConfigIdentity.test.ts +56 -0
- package/src/adminConfigIdentity.ts +87 -0
- package/src/adminTextSearch.test.ts +73 -0
- package/src/adminTextSearch.ts +74 -0
- package/src/adminUiV2.test.ts +1 -0
- package/src/adminUiV2.ts +1 -1
- package/src/aggregateAdmin.test.ts +220 -0
- package/src/aggregateAdmin.ts +206 -0
- package/src/documentStorageApp.test.ts +11 -0
- package/src/documentStorageApp.ts +20 -39
- package/src/filterParser.test.ts +68 -0
- package/src/filterParser.ts +229 -0
- package/src/index.ts +35 -0
- package/src/legacy.test.ts +34 -0
- package/src/legacy.ts +63 -0
- package/src/resolvedAdminModel.ts +114 -0
- package/src/routePath.ts +14 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
assignUniqueAdminConfigNames,
|
|
5
|
+
findAdminModelMetaByRoutePath,
|
|
6
|
+
slugFromAdminRoutePath,
|
|
7
|
+
} from "./adminConfigIdentity";
|
|
8
|
+
|
|
9
|
+
describe("slugFromAdminRoutePath", () => {
|
|
10
|
+
it("strips slashes and joins nested segments", () => {
|
|
11
|
+
expect(slugFromAdminRoutePath("/foods")).toBe("foods");
|
|
12
|
+
expect(slugFromAdminRoutePath("/nested/foods/")).toBe("nested-foods");
|
|
13
|
+
expect(slugFromAdminRoutePath("")).toBe("model");
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe("assignUniqueAdminConfigNames", () => {
|
|
18
|
+
it("keeps the Mongoose model name when it is unique", () => {
|
|
19
|
+
expect(
|
|
20
|
+
assignUniqueAdminConfigNames([
|
|
21
|
+
{modelName: "Food", routePath: "/foods"},
|
|
22
|
+
{modelName: "User", routePath: "/users"},
|
|
23
|
+
])
|
|
24
|
+
).toEqual(["Food", "User"]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("keeps the registered entry unsuffixed when the same model is mounted twice", () => {
|
|
28
|
+
expect(
|
|
29
|
+
assignUniqueAdminConfigNames([
|
|
30
|
+
{modelName: "Food", routePath: "/feature-flags", source: "plugin"},
|
|
31
|
+
{modelName: "Food", routePath: "/foods", source: "registered"},
|
|
32
|
+
])
|
|
33
|
+
).toEqual(["Food-feature-flags", "Food"]);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("suffixes later duplicates of the same source", () => {
|
|
37
|
+
expect(
|
|
38
|
+
assignUniqueAdminConfigNames([
|
|
39
|
+
{modelName: "Food", routePath: "/foods", source: "registered"},
|
|
40
|
+
{modelName: "Food", routePath: "/archived-foods", source: "registered"},
|
|
41
|
+
])
|
|
42
|
+
).toEqual(["Food", "Food-archived-foods"]);
|
|
43
|
+
});
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
describe("findAdminModelMetaByRoutePath", () => {
|
|
47
|
+
it("matches the mounted API path rather than the first same-named model", () => {
|
|
48
|
+
const models = [
|
|
49
|
+
{name: "Food", routePath: "/admin/foods", searchFields: ["name"]},
|
|
50
|
+
{name: "Food-archived-foods", routePath: "/admin/archived-foods", searchFields: ["calories"]},
|
|
51
|
+
];
|
|
52
|
+
expect(findAdminModelMetaByRoutePath(models, "/admin/archived-foods")?.searchFields).toEqual([
|
|
53
|
+
"calories",
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type {AdminModelSource} from "./resolvedAdminModel";
|
|
2
|
+
|
|
3
|
+
export interface AdminConfigNameInput {
|
|
4
|
+
modelName: string;
|
|
5
|
+
routePath: string;
|
|
6
|
+
source?: AdminModelSource;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const SOURCE_RANK: Record<AdminModelSource, number> = {
|
|
10
|
+
legacy: 0,
|
|
11
|
+
plugin: 1,
|
|
12
|
+
registered: 2,
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const slugFromAdminRoutePath = (routePath: string): string => {
|
|
16
|
+
const trimmed = routePath.replace(/^\/+|\/+$/g, "").replace(/\//g, "-");
|
|
17
|
+
if (!trimmed) {
|
|
18
|
+
return "model";
|
|
19
|
+
}
|
|
20
|
+
return trimmed;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const sourceRank = (source?: AdminModelSource): number => {
|
|
24
|
+
if (!source) {
|
|
25
|
+
return 0;
|
|
26
|
+
}
|
|
27
|
+
return SOURCE_RANK[source];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* UI route keys must be unique even when the same Mongoose model is mounted at
|
|
32
|
+
* two `routePath`s. Keep `modelName` for a singleton (and for the highest-precedence
|
|
33
|
+
* duplicate). Suffix the rest with the route slug.
|
|
34
|
+
*/
|
|
35
|
+
export const assignUniqueAdminConfigNames = (entries: AdminConfigNameInput[]): string[] => {
|
|
36
|
+
const counts = new Map<string, number>();
|
|
37
|
+
for (const entry of entries) {
|
|
38
|
+
counts.set(entry.modelName, (counts.get(entry.modelName) ?? 0) + 1);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const preferredIndex = new Map<string, number>();
|
|
42
|
+
for (let index = 0; index < entries.length; index += 1) {
|
|
43
|
+
const entry = entries[index];
|
|
44
|
+
if (!entry || (counts.get(entry.modelName) ?? 0) <= 1) {
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
const existing = preferredIndex.get(entry.modelName);
|
|
48
|
+
if (existing === undefined) {
|
|
49
|
+
preferredIndex.set(entry.modelName, index);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
const current = entries[existing];
|
|
53
|
+
if (!current) {
|
|
54
|
+
preferredIndex.set(entry.modelName, index);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (sourceRank(entry.source) > sourceRank(current.source)) {
|
|
58
|
+
preferredIndex.set(entry.modelName, index);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const used = new Set<string>();
|
|
63
|
+
return entries.map((entry, index) => {
|
|
64
|
+
const isDuplicate = (counts.get(entry.modelName) ?? 0) > 1;
|
|
65
|
+
const keepBare = !isDuplicate || preferredIndex.get(entry.modelName) === index;
|
|
66
|
+
let candidate = keepBare
|
|
67
|
+
? entry.modelName
|
|
68
|
+
: `${entry.modelName}-${slugFromAdminRoutePath(entry.routePath)}`;
|
|
69
|
+
if (used.has(candidate)) {
|
|
70
|
+
const base = candidate;
|
|
71
|
+
let suffix = 2;
|
|
72
|
+
while (used.has(`${base}-${suffix}`)) {
|
|
73
|
+
suffix += 1;
|
|
74
|
+
}
|
|
75
|
+
candidate = `${base}-${suffix}`;
|
|
76
|
+
}
|
|
77
|
+
used.add(candidate);
|
|
78
|
+
return candidate;
|
|
79
|
+
});
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export const findAdminModelMetaByRoutePath = <T extends {routePath: string}>(
|
|
83
|
+
models: T[],
|
|
84
|
+
mountedRoutePath: string
|
|
85
|
+
): T | undefined => {
|
|
86
|
+
return models.find((model) => model.routePath === mountedRoutePath);
|
|
87
|
+
};
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import {assert} from "chai";
|
|
3
|
+
import mongoose from "mongoose";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
ADMIN_LIST_SEARCH_PARAM,
|
|
7
|
+
andMongoFilters,
|
|
8
|
+
buildAdminPartialSearchFilter,
|
|
9
|
+
escapeRegexLiteral,
|
|
10
|
+
} from "./adminTextSearch";
|
|
11
|
+
|
|
12
|
+
const foodSchema = new mongoose.Schema({
|
|
13
|
+
calories: {type: Number},
|
|
14
|
+
name: {type: String},
|
|
15
|
+
ownerId: {type: mongoose.Schema.Types.ObjectId},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
describe("escapeRegexLiteral", () => {
|
|
19
|
+
it("escapes regex metacharacters", () => {
|
|
20
|
+
expect(escapeRegexLiteral("Rice.Bowl")).toBe("Rice\\.Bowl");
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
describe("buildAdminPartialSearchFilter", () => {
|
|
25
|
+
it("matches string searchFields with a case-insensitive partial regex", () => {
|
|
26
|
+
const filter = buildAdminPartialSearchFilter({
|
|
27
|
+
model: {schema: foodSchema},
|
|
28
|
+
q: "App",
|
|
29
|
+
searchFields: ["name"],
|
|
30
|
+
});
|
|
31
|
+
assert.deepEqual(filter, {$or: [{name: {$regex: /App/i}}]});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("skips non-string schema paths", () => {
|
|
35
|
+
const filter = buildAdminPartialSearchFilter({
|
|
36
|
+
model: {schema: foodSchema},
|
|
37
|
+
q: "12",
|
|
38
|
+
searchFields: ["calories"],
|
|
39
|
+
});
|
|
40
|
+
assert.isUndefined(filter);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("adds ObjectId equality when q is a valid id", () => {
|
|
44
|
+
const id = new mongoose.Types.ObjectId().toString();
|
|
45
|
+
const filter = buildAdminPartialSearchFilter({
|
|
46
|
+
extraObjectIdFields: ["ownerId"],
|
|
47
|
+
model: {schema: foodSchema},
|
|
48
|
+
q: id,
|
|
49
|
+
searchFields: ["name"],
|
|
50
|
+
});
|
|
51
|
+
assert.deepEqual(filter, {
|
|
52
|
+
$or: [{name: {$regex: new RegExp(id, "i")}}, {ownerId: new mongoose.Types.ObjectId(id)}],
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("andMongoFilters", () => {
|
|
58
|
+
it("returns extra when the base is empty", () => {
|
|
59
|
+
expect(andMongoFilters({}, {name: "x"})).toEqual({name: "x"});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("wraps both sides in $and", () => {
|
|
63
|
+
expect(andMongoFilters({completed: true}, {$or: [{name: /a/i}]})).toEqual({
|
|
64
|
+
$and: [{completed: true}, {$or: [{name: /a/i}]}],
|
|
65
|
+
});
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe("ADMIN_LIST_SEARCH_PARAM", () => {
|
|
70
|
+
it("is q", () => {
|
|
71
|
+
expect(ADMIN_LIST_SEARCH_PARAM).toBe("q");
|
|
72
|
+
});
|
|
73
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import mongoose from "mongoose";
|
|
2
|
+
|
|
3
|
+
/** List-query param for the admin table search box. */
|
|
4
|
+
export const ADMIN_LIST_SEARCH_PARAM = "q";
|
|
5
|
+
|
|
6
|
+
export const escapeRegexLiteral = (value: string): string => {
|
|
7
|
+
return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Case-insensitive partial match across string `searchFields`, plus ObjectId equality
|
|
12
|
+
* when `q` is a valid id.
|
|
13
|
+
*/
|
|
14
|
+
export const buildAdminPartialSearchFilter = ({
|
|
15
|
+
extraObjectIdFields,
|
|
16
|
+
model,
|
|
17
|
+
q,
|
|
18
|
+
searchFields,
|
|
19
|
+
}: {
|
|
20
|
+
extraObjectIdFields?: string[];
|
|
21
|
+
model: {schema: mongoose.Schema};
|
|
22
|
+
q: string;
|
|
23
|
+
searchFields: string[];
|
|
24
|
+
}): Record<string, unknown> | undefined => {
|
|
25
|
+
const trimmed = q.trim();
|
|
26
|
+
if (!trimmed) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const regex = new RegExp(escapeRegexLiteral(trimmed), "i");
|
|
31
|
+
const orConditions: Record<string, unknown>[] = [];
|
|
32
|
+
const seenFields = new Set<string>();
|
|
33
|
+
|
|
34
|
+
for (const field of searchFields) {
|
|
35
|
+
if (seenFields.has(field)) {
|
|
36
|
+
continue;
|
|
37
|
+
}
|
|
38
|
+
seenFields.add(field);
|
|
39
|
+
const schemaPath = model.schema.path(field);
|
|
40
|
+
if (schemaPath && schemaPath.instance !== "String") {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
orConditions.push({[field]: {$regex: regex}});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (mongoose.isValidObjectId(trimmed)) {
|
|
47
|
+
const objectIdFields = extraObjectIdFields
|
|
48
|
+
? [...extraObjectIdFields]
|
|
49
|
+
: Object.keys(model.schema.paths).filter(
|
|
50
|
+
(key) => model.schema.path(key)?.instance === "ObjectID"
|
|
51
|
+
);
|
|
52
|
+
for (const field of objectIdFields) {
|
|
53
|
+
orConditions.push({[field]: new mongoose.Types.ObjectId(trimmed)});
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (orConditions.length === 0) {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
return {$or: orConditions};
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const andMongoFilters = (
|
|
64
|
+
base: Record<string, unknown>,
|
|
65
|
+
extra: Record<string, unknown> | undefined
|
|
66
|
+
): Record<string, unknown> => {
|
|
67
|
+
if (!extra) {
|
|
68
|
+
return base;
|
|
69
|
+
}
|
|
70
|
+
if (Object.keys(base).length === 0) {
|
|
71
|
+
return extra;
|
|
72
|
+
}
|
|
73
|
+
return {$and: [base, extra]};
|
|
74
|
+
};
|
package/src/adminUiV2.test.ts
CHANGED
|
@@ -15,6 +15,7 @@ describe("buildAdminModelQueryFields", () => {
|
|
|
15
15
|
searchFields: ["tags"],
|
|
16
16
|
});
|
|
17
17
|
expect(fields).toContain("_id");
|
|
18
|
+
expect(fields).toContain("q");
|
|
18
19
|
expect(fields).toContain("title");
|
|
19
20
|
expect(fields).toContain("ownerId");
|
|
20
21
|
expect(fields).toContain("created");
|
package/src/adminUiV2.ts
CHANGED
|
@@ -122,7 +122,7 @@ export interface AdminModelQueryFieldSource {
|
|
|
122
122
|
* filters and sort keys match allowed query parameters on `GET` list routes.
|
|
123
123
|
*/
|
|
124
124
|
export const buildAdminModelQueryFields = (config: AdminModelQueryFieldSource): string[] => {
|
|
125
|
-
const fields = new Set<string>(["_id"]);
|
|
125
|
+
const fields = new Set<string>(["_id", "q"]);
|
|
126
126
|
for (const name of config.listFields) {
|
|
127
127
|
fields.add(name);
|
|
128
128
|
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import {describe, expect, it} from "bun:test";
|
|
2
|
+
import type {AdminConfig, AdminContribution, TerrenoPlugin} from "@terreno/api";
|
|
3
|
+
import {modelRouter, Permissions} from "@terreno/api";
|
|
4
|
+
import {FoodModel} from "@terreno/api/testing";
|
|
5
|
+
|
|
6
|
+
import type {AdminModelConfig} from "./adminApp";
|
|
7
|
+
import {
|
|
8
|
+
aggregateAdminContributions,
|
|
9
|
+
aggregateFromTerrenoApp,
|
|
10
|
+
collectRegisteredAdminModels,
|
|
11
|
+
} from "./aggregateAdmin";
|
|
12
|
+
import {resetLegacyDeprecationWarningsForTests} from "./legacy";
|
|
13
|
+
import {normalizeAdminRoutePath} from "./routePath";
|
|
14
|
+
|
|
15
|
+
const foodAdmin: AdminConfig = {
|
|
16
|
+
displayName: "Foods",
|
|
17
|
+
listFields: ["name"],
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const legacyFood: AdminModelConfig = {
|
|
21
|
+
displayName: "Legacy Foods",
|
|
22
|
+
listFields: ["name"],
|
|
23
|
+
model: FoodModel,
|
|
24
|
+
routePath: "/foods",
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
describe("normalizeAdminRoutePath", () => {
|
|
28
|
+
it("normalizes trailing slashes and missing leading slash", () => {
|
|
29
|
+
expect(normalizeAdminRoutePath("users/")).toBe("/users");
|
|
30
|
+
expect(normalizeAdminRoutePath("/users/")).toBe("/users");
|
|
31
|
+
expect(normalizeAdminRoutePath("users")).toBe("/users");
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("aggregateAdminContributions", () => {
|
|
36
|
+
it("merges legacy, plugin, and registered models with precedence", () => {
|
|
37
|
+
resetLegacyDeprecationWarningsForTests();
|
|
38
|
+
const pluginContribution: AdminContribution = {
|
|
39
|
+
homeWidgets: [{displayName: "Overrides", id: "feature-flags-overrides"}],
|
|
40
|
+
models: [
|
|
41
|
+
{
|
|
42
|
+
admin: {displayName: "Plugin Users", listFields: ["email"]},
|
|
43
|
+
model: FoodModel,
|
|
44
|
+
routePath: "/users",
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const registered = collectRegisteredAdminModels({
|
|
50
|
+
getPlugins: () => [],
|
|
51
|
+
getRegistrations: () => [
|
|
52
|
+
modelRouter("/users", FoodModel, {
|
|
53
|
+
admin: {displayName: "Registered Users", listFields: ["email"]},
|
|
54
|
+
permissions: {
|
|
55
|
+
create: [Permissions.IsAny],
|
|
56
|
+
delete: [Permissions.IsAny],
|
|
57
|
+
list: [Permissions.IsAny],
|
|
58
|
+
read: [Permissions.IsAny],
|
|
59
|
+
update: [Permissions.IsAny],
|
|
60
|
+
},
|
|
61
|
+
}),
|
|
62
|
+
],
|
|
63
|
+
} as never);
|
|
64
|
+
|
|
65
|
+
const aggregated = aggregateAdminContributions({
|
|
66
|
+
legacyModels: [legacyFood],
|
|
67
|
+
pluginContributions: [pluginContribution],
|
|
68
|
+
registeredModels: registered,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
expect(aggregated.models).toHaveLength(2);
|
|
72
|
+
const users = aggregated.models.find((m) => m.routePath === "/users");
|
|
73
|
+
expect(users?.source).toBe("registered");
|
|
74
|
+
expect(users?.displayName).toBe("Registered Users");
|
|
75
|
+
expect(aggregated.widgetIds).toEqual(["feature-flags-overrides"]);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("forwards populatePaths from plugin model contributions", () => {
|
|
79
|
+
const aggregated = aggregateAdminContributions({
|
|
80
|
+
pluginContributions: [
|
|
81
|
+
{
|
|
82
|
+
models: [
|
|
83
|
+
{
|
|
84
|
+
admin: {displayName: "Foods", listFields: ["name"]},
|
|
85
|
+
model: FoodModel,
|
|
86
|
+
populatePaths: [{fields: ["email"], path: "ownerId"}],
|
|
87
|
+
routePath: "/foods",
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
},
|
|
91
|
+
],
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
expect(aggregated.models[0]?.populatePaths).toEqual([{fields: ["email"], path: "ownerId"}]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("throws when two registered routers share a routePath", () => {
|
|
98
|
+
const registered = collectRegisteredAdminModels({
|
|
99
|
+
getPlugins: () => [],
|
|
100
|
+
getRegistrations: () => [
|
|
101
|
+
modelRouter("/dup", FoodModel, {
|
|
102
|
+
admin: foodAdmin,
|
|
103
|
+
permissions: {
|
|
104
|
+
create: [Permissions.IsAny],
|
|
105
|
+
delete: [Permissions.IsAny],
|
|
106
|
+
list: [Permissions.IsAny],
|
|
107
|
+
read: [Permissions.IsAny],
|
|
108
|
+
update: [Permissions.IsAny],
|
|
109
|
+
},
|
|
110
|
+
}),
|
|
111
|
+
modelRouter("/dup/", FoodModel, {
|
|
112
|
+
admin: {...foodAdmin, displayName: "Dup 2"},
|
|
113
|
+
permissions: {
|
|
114
|
+
create: [Permissions.IsAny],
|
|
115
|
+
delete: [Permissions.IsAny],
|
|
116
|
+
list: [Permissions.IsAny],
|
|
117
|
+
read: [Permissions.IsAny],
|
|
118
|
+
update: [Permissions.IsAny],
|
|
119
|
+
},
|
|
120
|
+
}),
|
|
121
|
+
],
|
|
122
|
+
} as never);
|
|
123
|
+
|
|
124
|
+
expect(() =>
|
|
125
|
+
aggregateAdminContributions({
|
|
126
|
+
registeredModels: registered,
|
|
127
|
+
})
|
|
128
|
+
).toThrow(/Duplicate admin modelRouter routePath/);
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe("aggregateFromTerrenoApp", () => {
|
|
133
|
+
it("collects plugin adminContribution models", () => {
|
|
134
|
+
const plugin: TerrenoPlugin = {
|
|
135
|
+
adminContribution: () => ({
|
|
136
|
+
models: [
|
|
137
|
+
{
|
|
138
|
+
admin: {displayName: "Flags", listFields: ["key"]},
|
|
139
|
+
model: FoodModel,
|
|
140
|
+
routePath: "/feature-flags",
|
|
141
|
+
},
|
|
142
|
+
],
|
|
143
|
+
}),
|
|
144
|
+
register() {},
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const terrenoApp = {
|
|
148
|
+
getPlugins: () => [plugin],
|
|
149
|
+
getRegistrations: () => [],
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const aggregated = aggregateFromTerrenoApp({terrenoApp: terrenoApp as never});
|
|
153
|
+
expect(aggregated.models).toHaveLength(1);
|
|
154
|
+
expect(aggregated.models[0]?.routePath).toBe("/feature-flags");
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const anyPermissions = {
|
|
159
|
+
create: [Permissions.IsAny],
|
|
160
|
+
delete: [Permissions.IsAny],
|
|
161
|
+
list: [Permissions.IsAny],
|
|
162
|
+
read: [Permissions.IsAny],
|
|
163
|
+
update: [Permissions.IsAny],
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
describe("collectRegisteredAdminModels", () => {
|
|
167
|
+
it("does not copy the public queryFilter onto admin CRUD", () => {
|
|
168
|
+
const registered = collectRegisteredAdminModels({
|
|
169
|
+
getPlugins: () => [],
|
|
170
|
+
getRegistrations: () => [
|
|
171
|
+
modelRouter("/foods", FoodModel, {
|
|
172
|
+
admin: {displayName: "Foods", listFields: ["name"]},
|
|
173
|
+
permissions: anyPermissions,
|
|
174
|
+
queryFilter: () => ({ownerId: "public-owner"}),
|
|
175
|
+
}),
|
|
176
|
+
],
|
|
177
|
+
} as never);
|
|
178
|
+
|
|
179
|
+
expect(registered).toHaveLength(1);
|
|
180
|
+
expect(registered[0]?.queryFilter).toBeUndefined();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it("maps only explicitly set adminPermissions verbs", () => {
|
|
184
|
+
const registered = collectRegisteredAdminModels({
|
|
185
|
+
getPlugins: () => [],
|
|
186
|
+
getRegistrations: () => [
|
|
187
|
+
modelRouter("/foods", FoodModel, {
|
|
188
|
+
admin: {
|
|
189
|
+
adminPermissions: {delete: []},
|
|
190
|
+
displayName: "Foods",
|
|
191
|
+
listFields: ["name"],
|
|
192
|
+
},
|
|
193
|
+
permissions: anyPermissions,
|
|
194
|
+
}),
|
|
195
|
+
],
|
|
196
|
+
} as never);
|
|
197
|
+
|
|
198
|
+
expect(registered[0]?.permissions).toEqual({delete: false});
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
it("uses adminFilter instead of the public queryFilter", async () => {
|
|
202
|
+
const registered = collectRegisteredAdminModels({
|
|
203
|
+
getPlugins: () => [],
|
|
204
|
+
getRegistrations: () => [
|
|
205
|
+
modelRouter("/foods", FoodModel, {
|
|
206
|
+
admin: {
|
|
207
|
+
adminFilter: () => ({$or: [{name: "A"}, {name: "B"}]}),
|
|
208
|
+
displayName: "Foods",
|
|
209
|
+
listFields: ["name"],
|
|
210
|
+
},
|
|
211
|
+
permissions: anyPermissions,
|
|
212
|
+
queryFilter: () => ({ownerId: "public-owner"}),
|
|
213
|
+
}),
|
|
214
|
+
],
|
|
215
|
+
} as never);
|
|
216
|
+
|
|
217
|
+
const scoped = await registered[0]?.queryFilter?.({} as never, {});
|
|
218
|
+
expect(scoped).toEqual({$or: [{name: "A"}, {name: "B"}]});
|
|
219
|
+
});
|
|
220
|
+
});
|