* perf: reduce memory usage upon running server tests * perf: plug leaks in server/routes * perf: plug leaks in server/scripts * perf: plug leaks in server/policies * perf: plug leaks in server/models * perf: plug leaks in server/middlewares * perf: plug leaks in server/commands * fix: missing await on db.flush * perf: plug leaks in server/queues * chore: remove unused legacy funcs * fix: await on db.flush * perf: await on GC to run in between tests * fix: remove db refs * fix: revert embeds * perf: plug leaks in shared/i18n
185 lines
5.3 KiB
TypeScript
185 lines
5.3 KiB
TypeScript
import DocumentNotificationEmail from "@server/emails/templates/DocumentNotificationEmail";
|
|
import { View, NotificationSetting } from "@server/models";
|
|
import {
|
|
buildDocument,
|
|
buildCollection,
|
|
buildUser,
|
|
} from "@server/test/factories";
|
|
import { getTestDatabase } from "@server/test/support";
|
|
import NotificationsProcessor from "./NotificationsProcessor";
|
|
|
|
jest.mock("@server/emails/templates/DocumentNotificationEmail");
|
|
const ip = "127.0.0.1";
|
|
|
|
const db = getTestDatabase();
|
|
|
|
afterAll(db.disconnect);
|
|
|
|
beforeEach(async () => {
|
|
await db.flush();
|
|
jest.resetAllMocks();
|
|
});
|
|
|
|
describe("documents.publish", () => {
|
|
test("should not send a notification to author", async () => {
|
|
const user = await buildUser();
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
lastModifiedById: user.id,
|
|
});
|
|
await NotificationSetting.create({
|
|
userId: user.id,
|
|
teamId: user.teamId,
|
|
event: "documents.publish",
|
|
});
|
|
|
|
const processor = new NotificationsProcessor();
|
|
await processor.perform({
|
|
name: "documents.publish",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId,
|
|
teamId: document.teamId,
|
|
actorId: document.createdById,
|
|
data: {
|
|
title: document.title,
|
|
},
|
|
ip,
|
|
});
|
|
expect(DocumentNotificationEmail.schedule).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("should send a notification to other users in team", async () => {
|
|
const user = await buildUser();
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
});
|
|
await NotificationSetting.create({
|
|
userId: user.id,
|
|
teamId: user.teamId,
|
|
event: "documents.publish",
|
|
});
|
|
|
|
const processor = new NotificationsProcessor();
|
|
await processor.perform({
|
|
name: "documents.publish",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId,
|
|
teamId: document.teamId,
|
|
actorId: document.createdById,
|
|
data: {
|
|
title: document.title,
|
|
},
|
|
ip,
|
|
});
|
|
expect(DocumentNotificationEmail.schedule).toHaveBeenCalled();
|
|
});
|
|
|
|
test("should not send a notification to users without collection access", async () => {
|
|
const user = await buildUser();
|
|
const collection = await buildCollection({
|
|
teamId: user.teamId,
|
|
permission: null,
|
|
});
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
collectionId: collection.id,
|
|
});
|
|
await NotificationSetting.create({
|
|
userId: user.id,
|
|
teamId: user.teamId,
|
|
event: "documents.publish",
|
|
});
|
|
const processor = new NotificationsProcessor();
|
|
await processor.perform({
|
|
name: "documents.publish",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId,
|
|
teamId: document.teamId,
|
|
actorId: document.createdById,
|
|
data: {
|
|
title: document.title,
|
|
},
|
|
ip,
|
|
});
|
|
expect(DocumentNotificationEmail.schedule).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("revisions.create", () => {
|
|
test("should send a notification to other collaborators", async () => {
|
|
const document = await buildDocument();
|
|
const collaborator = await buildUser({
|
|
teamId: document.teamId,
|
|
});
|
|
document.collaboratorIds = [collaborator.id];
|
|
await document.save();
|
|
await NotificationSetting.create({
|
|
userId: collaborator.id,
|
|
teamId: collaborator.teamId,
|
|
event: "documents.update",
|
|
});
|
|
const processor = new NotificationsProcessor();
|
|
await processor.perform({
|
|
name: "revisions.create",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId,
|
|
teamId: document.teamId,
|
|
actorId: collaborator.id,
|
|
modelId: document.id,
|
|
ip,
|
|
});
|
|
expect(DocumentNotificationEmail.schedule).toHaveBeenCalled();
|
|
});
|
|
|
|
test("should not send a notification if viewed since update", async () => {
|
|
const document = await buildDocument();
|
|
const collaborator = await buildUser({
|
|
teamId: document.teamId,
|
|
});
|
|
document.collaboratorIds = [collaborator.id];
|
|
await document.save();
|
|
await NotificationSetting.create({
|
|
userId: collaborator.id,
|
|
teamId: collaborator.teamId,
|
|
event: "documents.update",
|
|
});
|
|
await View.touch(document.id, collaborator.id, true);
|
|
|
|
const processor = new NotificationsProcessor();
|
|
await processor.perform({
|
|
name: "revisions.create",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId,
|
|
teamId: document.teamId,
|
|
actorId: collaborator.id,
|
|
modelId: document.id,
|
|
ip,
|
|
});
|
|
expect(DocumentNotificationEmail.schedule).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("should not send a notification to last editor", async () => {
|
|
const user = await buildUser();
|
|
const document = await buildDocument({
|
|
teamId: user.teamId,
|
|
lastModifiedById: user.id,
|
|
});
|
|
await NotificationSetting.create({
|
|
userId: user.id,
|
|
teamId: user.teamId,
|
|
event: "documents.update",
|
|
});
|
|
const processor = new NotificationsProcessor();
|
|
await processor.perform({
|
|
name: "revisions.create",
|
|
documentId: document.id,
|
|
collectionId: document.collectionId,
|
|
teamId: document.teamId,
|
|
actorId: user.id,
|
|
modelId: document.id,
|
|
ip,
|
|
});
|
|
expect(DocumentNotificationEmail.schedule).not.toHaveBeenCalled();
|
|
});
|
|
});
|