This commit is contained in:
Tom Moor committed 2025-03-18 22:29:09 -04:00
1 parent cfd7996a6f
commit 13565d27ec
8 files changed
+98 -262

No files matched your search

-13
View File
@@ -52,19 +52,6 @@ describe("#delete", () => {
expect(newDocument?.deletedAt).toBeTruthy();
});
test("should soft delete templates", async () => {
const document = await buildDocument({
template: true,
});
const user = await buildUser();
await document.delete(user);
const newDocument = await Document.findByPk(document.id, {
paranoid: false,
});
expect(newDocument?.lastModifiedById).toBe(user.id);
expect(newDocument?.deletedAt).toBeTruthy();
});
test("should soft delete archived", async () => {
const document = await buildDocument({
archivedAt: new Date(),
+18 -3
View File
@@ -1,6 +1,3 @@
import { ProsemirrorData } from "@shared/types";
import { UrlHelper } from "@shared/utils/UrlHelper";
import { DocumentValidation } from "@shared/validations";
import { isUUID } from "class-validator";
import {
Identifier,
@@ -24,8 +21,13 @@ import {
HasMany,
Unique,
Scopes,
BeforeValidate,
} from "sequelize-typescript";
import slugify from "slugify";
import { ProsemirrorData } from "@shared/types";
import { UrlHelper } from "@shared/utils/UrlHelper";
import { DocumentValidation } from "@shared/validations";
import { generateUrlId } from "@server/utils/url";
import Collection from "./Collection";
import Revision from "./Revision";
import Team from "./Team";
@@ -82,6 +84,14 @@ type AdditionalFindOptions = {
},
],
}),
withCollection: {
include: [
{
model: Collection,
as: "collection",
},
],
},
}))
@Table({ tableName: "documents", modelName: "template" })
@Fix
@@ -216,6 +226,11 @@ class Template extends IdModel<
return !!this.deletedAt;
}
@BeforeValidate
static createUrlId(model: Template) {
return (model.urlId = model.urlId || generateUrlId());
}
/**
* Overrides the standard findByPk behavior to allow also querying by urlId
*
+4 -4
View File
@@ -28,7 +28,7 @@ allow(
DocumentPermission.ReadWrite,
DocumentPermission.Admin,
]),
and(!!document?.isDraft, actor.id === document?.createdById),
!!document?.isDraft && actor.id === document?.createdById,
can(actor, "readDocument", document?.collection)
)
)
@@ -84,7 +84,7 @@ allow(User, "update", Document, (actor, document) =>
]),
or(
can(actor, "updateDocument", document?.collection),
and(!!document?.isDraft && actor.id === document?.createdById)
!!document?.isDraft && actor.id === document?.createdById
)
)
)
@@ -173,7 +173,7 @@ allow(User, ["restore", "permanentDelete"], Document, (actor, document) =>
DocumentPermission.Admin,
]),
can(actor, "updateDocument", document?.collection),
and(!!document?.isDraft && actor.id === document?.createdById),
!!document?.isDraft && actor.id === document?.createdById,
!document?.collection
)
)
@@ -203,7 +203,7 @@ allow(User, "unarchive", Document, (actor, document) =>
DocumentPermission.Admin,
]),
can(actor, "updateDocument", document?.collection),
and(!!document?.isDraft && actor.id === document?.createdById)
!!document?.isDraft && actor.id === document?.createdById
)
)
);
+2 -2
View File
@@ -1,4 +1,4 @@
import { buildUser, buildTeam } from "@server/test/factories";
import { buildUser, buildAdmin, buildTeam } from "@server/test/factories";
import { serialize } from "./index";
it("should serialize policy", async () => {
@@ -10,7 +10,7 @@ it("should serialize policy", async () => {
it("should serialize domain policies on Team", async () => {
const team = await buildTeam();
const user = await buildUser({
const user = await buildAdmin({
teamId: team.id,
});
const response = serialize(user, team);
+2 -23
View File
@@ -16,7 +16,7 @@ describe("policies/team", () => {
expect(abilities.createTeam).toEqual(false);
expect(abilities.createAttachment).toBeTruthy();
expect(abilities.createCollection).toBeTruthy();
expect(abilities.createTemplate).toBeTruthy();
expect(abilities.createTemplate).toEqual(false);
expect(abilities.createGroup).toEqual(false);
expect(abilities.createIntegration).toEqual(false);
});
@@ -53,31 +53,10 @@ describe("policies/team", () => {
expect(abilities.createIntegration).toBeTruthy();
});
describe("read template", () => {
const permissions = new Map<UserRole, boolean>([
[UserRole.Admin, true],
[UserRole.Member, true],
[UserRole.Viewer, false],
[UserRole.Guest, true],
]);
for (const [role, permission] of permissions.entries()) {
it(`check permission for ${role}`, async () => {
const team = await buildTeam();
const user = await buildUser({
teamId: team.id,
role,
});
const abilities = serialize(user, team);
expect(abilities.createDocument).toEqual(permission);
});
}
});
describe("create template", () => {
const permissions = new Map<UserRole, boolean>([
[UserRole.Admin, true],
[UserRole.Member, true],
[UserRole.Member, false],
[UserRole.Viewer, false],
[UserRole.Guest, false],
]);
+11 -208
View File
@@ -31,6 +31,7 @@ import {
buildTeam,
buildGroup,
buildAdmin,
buildTemplate,
} from "@server/test/factories";
import { getTestServer } from "@server/test/support";
@@ -2040,8 +2041,9 @@ describe("#documents.templatize", () => {
expect(body.data.publishedAt).toBeTruthy();
expect(body.data.collectionId).toEqual(collection.id);
});
it("should create a published workspace template", async () => {
const user = await buildUser();
const user = await buildAdmin();
const collection = await buildCollection({
createdById: user.id,
teamId: user.teamId,
@@ -2063,6 +2065,7 @@ describe("#documents.templatize", () => {
expect(body.data.publishedAt).toBeTruthy();
expect(body.data.collectionId).toBeNull();
});
it("should create a draft non-workspace template", async () => {
const user = await buildUser();
const collection = await buildCollection({
@@ -2088,7 +2091,7 @@ describe("#documents.templatize", () => {
expect(body.data.collectionId).toEqual(collection.id);
});
it("should create a draft workspace template", async () => {
const user = await buildUser();
const user = await buildAdmin();
const collection = await buildCollection({
createdById: user.id,
teamId: user.teamId,
@@ -2110,6 +2113,7 @@ describe("#documents.templatize", () => {
expect(body.data.publishedAt).toBeNull();
expect(body.data.collectionId).toBeNull();
});
it("should create a template in a different collection", async () => {
const user = await buildUser();
const collection = await buildCollection({
@@ -2676,56 +2680,6 @@ describe("#documents.move", () => {
expect(res.status).toEqual(403);
});
it("should move a template to workspace", async () => {
const user = await buildAdmin();
const collection = await buildCollection({
teamId: user.teamId,
});
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
collectionId: collection.id,
template: true,
});
const res = await server.post("/api/documents.move", {
body: {
token: user.getJwtToken(),
id: document.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.documents[0].collectionId).toBeNull();
expect(body.policies[0].abilities.move).toBeTruthy();
});
it("should move a workspace template to collection", async () => {
const user = await buildUser();
const collection = await buildCollection({
teamId: user.teamId,
});
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
template: true,
});
const res = await server.post("/api/documents.move", {
body: {
token: user.getJwtToken(),
id: document.id,
collectionId: collection.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.documents[0].collectionId).toEqual(collection.id);
expect(body.policies[0].abilities.move).toBeTruthy();
});
it("should require authentication", async () => {
const res = await server.post("/api/documents.move");
expect(res.status).toEqual(401);
@@ -3043,92 +2997,6 @@ describe("#documents.restore", () => {
expect(body.data.collectionId).toEqual(anotherCollection.id);
});
it("should allow restore of collection templates", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const template = await buildDocument({
template: true,
userId: user.id,
collectionId: collection.id,
teamId: team.id,
});
await template.delete(user);
const res = await server.post("/api/documents.restore", {
body: {
token: user.getJwtToken(),
id: template.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.deletedAt).toEqual(null);
expect(body.data.collectionId).toEqual(collection.id);
});
it("should allow restore of templates from a deleted collection", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const anotherCollection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const template = await buildDocument({
template: true,
userId: user.id,
collectionId: collection.id,
teamId: team.id,
});
await template.delete(user);
await collection.destroy({ hooks: false });
const res = await server.post("/api/documents.restore", {
body: {
token: user.getJwtToken(),
id: template.id,
collectionId: anotherCollection.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.deletedAt).toEqual(null);
expect(body.data.collectionId).toEqual(anotherCollection.id);
});
it("should allow restore of workspace templates", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const template = await buildDocument({
template: true,
userId: user.id,
teamId: team.id,
collectionId: null,
});
await template.delete(user);
const res = await server.post("/api/documents.restore", {
body: {
token: user.getJwtToken(),
id: template.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.deletedAt).toEqual(null);
expect(body.data.collectionId).toEqual(null);
});
it("should not allow restore of documents to a deleted collection", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
@@ -3394,11 +3262,10 @@ describe("#documents.create", () => {
});
it("should retain template variables when a template is created from another template", async () => {
const user = await buildUser();
const template = await buildDocument({
const user = await buildAdmin();
const template = await buildTemplate({
userId: user.id,
teamId: user.teamId,
template: true,
title: "template title",
text: "Created by user {author} on {date}",
});
@@ -3412,7 +3279,6 @@ describe("#documents.create", () => {
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.title).toEqual(template.title);
expect(body.data.text).toEqual(template.text);
});
it("should create a document with empty title if no title is explicitly passed", async () => {
@@ -3430,10 +3296,9 @@ describe("#documents.create", () => {
it("should use template title when doc is supposed to be created using the template and title is not explicitly passed", async () => {
const user = await buildUser();
const template = await buildDocument({
const template = await buildTemplate({
userId: user.id,
teamId: user.teamId,
template: true,
title: "template title",
text: "template text",
});
@@ -3446,15 +3311,13 @@ describe("#documents.create", () => {
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.title).toEqual(template.title);
expect(body.data.text).toEqual(template.text);
});
it("should override template title when doc title is explicitly passed", async () => {
const user = await buildUser();
const template = await buildDocument({
const template = await buildTemplate({
userId: user.id,
teamId: user.teamId,
template: true,
title: "template title",
});
const res = await server.post("/api/documents.create", {
@@ -3611,7 +3474,7 @@ describe("#documents.create", () => {
it("should allow creating a draft template without a collection", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const user = await buildAdmin({ teamId: team.id });
const res = await server.post("/api/documents.create", {
body: {
template: true,
@@ -3847,39 +3710,6 @@ describe("#documents.update", () => {
expect(body.data.text).toBe("Updated text");
});
it("should successfully publish a draft template without collection", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const document = await buildDraftDocument({
title: "title",
text: "text",
teamId: team.id,
userId: user.id,
collectionId: null,
template: true,
});
const res = await server.post("/api/documents.update", {
body: {
token: user.getJwtToken(),
id: document.id,
title: "Updated title",
text: "Updated text",
collectionId: collection.id,
publish: true,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.collectionId).toBe(collection.id);
expect(body.data.title).toBe("Updated title");
expect(body.data.text).toBe("Updated text");
expect(body.data.publishedAt).toBeTruthy();
});
it("should not allow publishing by another collection's user", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
@@ -4497,33 +4327,6 @@ describe("#documents.delete", () => {
expect(body.success).toEqual(true);
});
it("should allow deleting document without collection", async () => {
const team = await buildTeam();
const user = await buildUser({ teamId: team.id });
const collection = await buildCollection({
userId: user.id,
teamId: team.id,
});
const document = await buildDocument({
userId: user.id,
collectionId: collection.id,
teamId: team.id,
});
// delete collection without hooks to trigger document deletion
await collection.destroy({
hooks: false,
});
const res = await server.post("/api/documents.delete", {
body: {
token: user.getJwtToken(),
id: document.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.success).toEqual(true);
});
it("should require authentication", async () => {
const document = await buildDocument();
const res = await server.post("/api/documents.delete", {
+52
View File
@@ -41,6 +41,7 @@ import {
SearchQuery,
Pin,
Comment,
Template,
} from "@server/models";
import AttachmentHelper from "@server/models/helpers/AttachmentHelper";
@@ -413,6 +414,57 @@ export async function buildDocument(
return document;
}
export async function buildTemplate(
// Omission first, addition later?
// This is actually a workaround to allow
// passing collectionId as null. Ideally, it
// should be updated in the Document model itself
// but that'd cascade and require further changes
// beyond the scope of what's required now
overrides: Omit<Partial<Template>, "collectionId"> & {
userId?: string;
text?: string;
collectionId?: string | null;
} = {}
) {
if (!overrides.teamId) {
const team = await buildTeam();
overrides.teamId = team.id;
}
if (!overrides.userId) {
const user = await buildUser();
overrides.userId = user.id;
}
let collection;
if (overrides.collectionId === undefined) {
collection = await buildCollection({
teamId: overrides.teamId,
userId: overrides.userId,
});
overrides.collectionId = collection.id;
}
const text = overrides.text ?? "This is the text in an example template";
const template = await Template.create(
{
title: faker.lorem.words(4),
content: overrides.content ?? parser.parse(text)?.toJSON(),
publishedAt: isNull(overrides.collectionId) ? null : new Date(),
lastModifiedById: overrides.userId,
createdById: overrides.userId,
editorVersion: "12.0.0",
...overrides,
},
{
silent: overrides.createdAt || overrides.updatedAt ? true : false,
}
);
return template;
}
export async function buildComment(overrides: {
userId: string;
documentId: string;
+9 -9
View File
@@ -80,7 +80,6 @@
"Search documents for \"{{searchQuery}}\"": "Search documents for \"{{searchQuery}}\"",
"Move to workspace": "Move to workspace",
"Move": "Move",
"Move to collection": "Move to collection",
"Move {{ documentType }}": "Move {{ documentType }}",
"Are you sure you want to archive this document?": "Are you sure you want to archive this document?",
"Document archived": "Document archived",
@@ -212,7 +211,6 @@
"New": "New",
"Only visible to you": "Only visible to you",
"Draft": "Draft",
"Template": "Template",
"You updated": "You updated",
"{{ userName }} updated": "{{ userName }} updated",
"You deleted": "You deleted",
@@ -555,6 +553,7 @@
"Contents": "Contents",
"Headings you add to the document will appear here": "Headings you add to the document will appear here",
"Table of contents": "Table of contents",
"Template options": "Template options",
"Change name": "Change name",
"Change email": "Change email",
"Suspend user": "Suspend user",
@@ -671,16 +670,12 @@
"Sorry, the last change could not be persisted – please reload the page": "Sorry, the last change could not be persisted – please reload the page",
"{{ count }} days": "{{ count }} day",
"{{ count }} days_plural": "{{ count }} days",
"This template will be permanently deleted in <2></2> unless restored.": "This template will be permanently deleted in <2></2> unless restored.",
"This document will be permanently deleted in <2></2> unless restored.": "This document will be permanently deleted in <2></2> unless restored.",
"Highlight some text and use the <1></1> control to add placeholders that can be filled out when creating new documents": "Highlight some text and use the <1></1> control to add placeholders that can be filled out when creating new documents",
"You’re editing a template": "You’re editing a template",
"Deleted by {{userName}}": "Deleted by {{userName}}",
"Observing {{ userName }}": "Observing {{ userName }}",
"Backlinks": "Backlinks",
"Close": "Close",
"{{ teamName }} is using {{ appName }} to share documents, please login to continue.": "{{ teamName }} is using {{ appName }} to share documents, please login to continue.",
"Are you sure you want to delete the <em>{{ documentTitle }}</em> template?": "Are you sure you want to delete the <em>{{ documentTitle }}</em> template?",
"Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history</em>.": "Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history</em>.",
"Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and <em>{{ any }} nested document</em>.": "Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and <em>{{ any }} nested document</em>.",
"Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and <em>{{ any }} nested document</em>._plural": "Are you sure about that? Deleting the <em>{{ documentTitle }}</em> document will delete all of its history and <em>{{ any }} nested documents</em>.",
@@ -903,6 +898,10 @@
"Last accessed": "Last accessed",
"Domain": "Domain",
"Views": "Views",
"Highlight some text and use the <1></1> control to add placeholders that can be filled out when creating new documents": "Highlight some text and use the <1></1> control to add placeholders that can be filled out when creating new documents",
"You’re editing a template": "You’re editing a template",
"Title": "Title",
"Updated by": "Updated by",
"All roles": "All roles",
"Admins": "Admins",
"Editors": "Editors",
@@ -1042,9 +1041,10 @@
"Sharing is currently disabled.": "Sharing is currently disabled.",
"You can globally enable and disable public document sharing in the <em>security settings</em>.": "You can globally enable and disable public document sharing in the <em>security settings</em>.",
"Documents that have been shared are listed below. Anyone that has the public link can access a read-only version of the document until the link has been revoked.": "Documents that have been shared are listed below. Anyone that has the public link can access a read-only version of the document until the link has been revoked.",
"You can create templates to help your team create consistent and accurate documentation.": "You can create templates to help your team create consistent and accurate documentation.",
"Alphabetical": "Alphabetical",
"There are no templates just yet.": "There are no templates just yet.",
"Create a template": "Create a template",
"Could not load templates": "Could not load templates",
"Templates help your team create consistent and accurate documentation.": "Templates help your team create consistent and accurate documentation.",
"No templates have been created yet": "No templates have been created yet",
"Zapier is a platform that allows {{appName}} to easily integrate with thousands of other business tools. Automate your workflows, sync data, and more.": "Zapier is a platform that allows {{appName}} to easily integrate with thousands of other business tools. Automate your workflows, sync data, and more.",
"A confirmation code has been sent to your email address, please enter the code below to permanently destroy this workspace.": "A confirmation code has been sent to your email address, please enter the code below to permanently destroy this workspace.",
"Confirmation code": "Confirmation code",