perf: reduce memory usage upon running server tests (#3949)

* 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
This commit is contained in:
Apoorv Mishra
2022-08-11 21:39:17 +05:30
committed by GitHub
parent 8e1f42a9cb
commit 0c51bfb899
68 changed files with 463 additions and 178 deletions

View File

@@ -15,7 +15,8 @@
"<rootDir>/server/test/setup.ts"
],
"testEnvironment": "node",
"runner": "@getoutline/jest-runner-serial"
"runner": "@getoutline/jest-runner-serial",
"detectLeaks": true
},
{
"displayName": "app",
@@ -56,6 +57,9 @@
"setupFiles": [
"<rootDir>/__mocks__/console.js"
],
"setupFilesAfterEnv": [
"<rootDir>/shared/test/setup.ts"
],
"testEnvironment": "node"
},
{
@@ -79,4 +83,4 @@
}
}
]
}
}

View File

@@ -4,12 +4,14 @@ import { TeamDomain } from "@server/models";
import Collection from "@server/models/Collection";
import UserAuthentication from "@server/models/UserAuthentication";
import { buildUser, buildTeam } from "@server/test/factories";
import { flushdb, seed } from "@server/test/support";
import { getTestDatabase, seed } from "@server/test/support";
import accountProvisioner from "./accountProvisioner";
beforeEach(() => {
return flushdb();
});
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("accountProvisioner", () => {
const ip = "127.0.0.1";

View File

@@ -2,11 +2,16 @@ import path from "path";
import fs from "fs-extra";
import Attachment from "@server/models/Attachment";
import { buildUser } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import documentImporter from "./documentImporter";
jest.mock("../utils/s3");
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("documentImporter", () => {
const ip = "127.0.0.1";

View File

@@ -5,10 +5,14 @@ import {
buildCollection,
buildUser,
} from "@server/test/factories";
import { flushdb, seed } from "@server/test/support";
import { getTestDatabase, seed } from "@server/test/support";
import documentMover from "./documentMover";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("documentMover", () => {
const ip = "127.0.0.1";

View File

@@ -1,10 +1,14 @@
import { subDays } from "date-fns";
import { Attachment, Document } from "@server/models";
import { buildAttachment, buildDocument } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import documentPermanentDeleter from "./documentPermanentDeleter";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("documentPermanentDeleter", () => {
it("should destroy documents", async () => {

View File

@@ -1,10 +1,14 @@
import { sequelize } from "@server/database/sequelize";
import { Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import documentUpdater from "./documentUpdater";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("documentUpdater", () => {
const ip = "127.0.0.1";

View File

@@ -1,9 +1,13 @@
import { FileOperation } from "@server/models";
import { buildAdmin, buildFileOperation } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import fileOperationDeleter from "./fileOperationDeleter";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("fileOperationDeleter", () => {
const ip = "127.0.0.1";

View File

@@ -1,9 +1,14 @@
import { Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import pinCreator from "./pinCreator";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("pinCreator", () => {
const ip = "127.0.0.1";

View File

@@ -1,9 +1,14 @@
import { Pin, Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import pinDestroyer from "./pinDestroyer";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("pinCreator", () => {
const ip = "127.0.0.1";

View File

@@ -1,9 +1,13 @@
import { Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import revisionCreator from "./revisionCreator";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("revisionCreator", () => {
const ip = "127.0.0.1";

View File

@@ -1,10 +1,15 @@
import { sequelize } from "@server/database/sequelize";
import { Star, Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import starCreator from "./starCreator";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("starCreator", () => {
const ip = "127.0.0.1";

View File

@@ -1,9 +1,13 @@
import { Star, Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import starDestroyer from "./starDestroyer";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("starDestroyer", () => {
const ip = "127.0.0.1";

View File

@@ -1,9 +1,13 @@
import { Star, Event } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import starUpdater from "./starUpdater";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("starUpdater", () => {
const ip = "127.0.0.1";

View File

@@ -6,10 +6,14 @@ import {
buildTeam,
buildDocument,
} from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import teamPermanentDeleter from "./teamPermanentDeleter";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("teamPermanentDeleter", () => {
it("should destroy related data", async () => {

View File

@@ -1,10 +1,14 @@
import env from "@server/env";
import TeamDomain from "@server/models/TeamDomain";
import { buildTeam, buildUser } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import teamProvisioner from "./teamProvisioner";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("teamProvisioner", () => {
const ip = "127.0.0.1";

View File

@@ -1,8 +1,12 @@
import { buildUser, buildAdmin } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import userDestroyer from "./userDestroyer";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("userDestroyer", () => {
const ip = "127.0.0.1";

View File

@@ -1,8 +1,12 @@
import { buildUser } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import userInviter from "./userInviter";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("userInviter", () => {
const ip = "127.0.0.1";

View File

@@ -1,10 +1,14 @@
import { v4 as uuidv4 } from "uuid";
import { TeamDomain } from "@server/models";
import { buildUser, buildTeam, buildInvite } from "@server/test/factories";
import { flushdb, seed } from "@server/test/support";
import { getTestDatabase, seed } from "@server/test/support";
import userProvisioner from "./userProvisioner";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("userProvisioner", () => {
const ip = "127.0.0.1";

View File

@@ -1,9 +1,13 @@
import GroupUser from "@server/models/GroupUser";
import { buildGroup, buildAdmin, buildUser } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import userSuspender from "./userSuspender";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("userSuspender", () => {
const ip = "127.0.0.1";

View File

@@ -1,10 +1,14 @@
import randomstring from "randomstring";
import ApiKey from "@server/models/ApiKey";
import { buildUser, buildTeam } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import auth from "./authentication";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("Authentication middleware", () => {
describe("with JWT", () => {

View File

@@ -7,13 +7,19 @@ import {
buildTeam,
buildDocument,
} from "@server/test/factories";
import { flushdb, seed } from "@server/test/support";
import { getTestDatabase, seed } from "@server/test/support";
import slugify from "@server/utils/slugify";
import Collection from "./Collection";
import Document from "./Document";
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("#url", () => {
test("should return correct url for the collection", () => {

View File

@@ -6,11 +6,17 @@ import {
buildUser,
buildShare,
} from "@server/test/factories";
import { flushdb, seed } from "@server/test/support";
import { getTestDatabase, seed } from "@server/test/support";
import slugify from "@server/utils/slugify";
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("#getSummary", () => {
test("should strip markdown", async () => {

View File

@@ -1,10 +1,16 @@
import { buildUser, buildGroup, buildCollection } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import CollectionGroup from "./CollectionGroup";
import GroupUser from "./GroupUser";
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("afterDestroy hook", () => {
test("should destroy associated group and collection join relations", async () => {

View File

@@ -1,9 +1,15 @@
import { buildDocument } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import Revision from "./Revision";
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("#findLatest", () => {
test("should return latest revision", async () => {

View File

@@ -1,7 +1,14 @@
import { buildTeam, buildCollection } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("collectionIds", () => {
it("should return non-private collection ids", async () => {

View File

@@ -1,8 +1,15 @@
import { buildAdmin, buildTeam } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import TeamDomain from "./TeamDomain";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("team domain model", () => {
describe("create", () => {

View File

@@ -1,16 +1,21 @@
import { buildUser, buildTeam, buildCollection } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import CollectionUser from "./CollectionUser";
import UserAuthentication from "./UserAuthentication";
beforeEach(() => flushdb());
const db = getTestDatabase();
beforeAll(() => {
jest.useFakeTimers().setSystemTime(new Date("2018-01-02T00:00:00.000Z"));
});
afterAll(() => {
jest.useRealTimers();
db.disconnect();
});
beforeEach(db.flush);
describe("user model", () => {
describe("destroy", () => {
it("should delete user authentications", async () => {

View File

@@ -1,9 +1,13 @@
import { CollectionUser, Collection } from "@server/models";
import { buildUser, buildTeam, buildCollection } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import { serialize } from "./index";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("read_write permission", () => {
it("should allow read write permissions for team member", async () => {

View File

@@ -4,10 +4,14 @@ import {
buildDocument,
buildCollection,
} from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import { serialize } from "./index";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("read_write collection", () => {
it("should allow read write permissions for team member", async () => {

View File

@@ -1,8 +1,12 @@
import { buildUser, buildTeam } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import { serialize } from "./index";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
it("should serialize policy", async () => {
const user = await buildUser();

View File

@@ -1,8 +1,13 @@
import { buildUser, buildTeam, buildAdmin } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import { serialize } from "./index";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
it("should allow reading only", async () => {
const team = await buildTeam();
const user = await buildUser({

View File

@@ -1,12 +1,18 @@
import { Backlink } from "@server/models";
import { buildDocument } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import BacklinksProcessor from "./BacklinksProcessor";
const ip = "127.0.0.1";
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("documents.publish", () => {
test("should create new backlink records", async () => {
@@ -161,7 +167,7 @@ describe("documents.update", () => {
ip,
});
document.text = `First link is gone
[this is a another link](${yetAnotherDocument.url})`;
await document.save();

View File

@@ -5,14 +5,20 @@ import {
buildCollection,
buildUser,
} from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import NotificationsProcessor from "./NotificationsProcessor";
jest.mock("@server/emails/templates/DocumentNotificationEmail");
const ip = "127.0.0.1";
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
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 () => {

View File

@@ -1,12 +1,18 @@
import { Revision } from "@server/models";
import { buildDocument } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import RevisionsProcessor from "./RevisionsProcessor";
const ip = "127.0.0.1";
beforeEach(() => flushdb());
beforeEach(jest.resetAllMocks);
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
describe("documents.update.debounced", () => {
test("should create a revision", async () => {

View File

@@ -1,18 +1,21 @@
import { buildUser, buildWebhookSubscription } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import { UserEvent } from "@server/types";
import DeliverWebhookTask from "../tasks/DeliverWebhookTask";
import WebhookProcessor from "./WebhookProcessor";
jest.mock("@server/queues/tasks/DeliverWebhookTask");
const ip = "127.0.0.1";
beforeEach(() => flushdb());
beforeEach(() => {
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
});
const ip = "127.0.0.1";
describe("WebhookProcessor", () => {
test("it schedules a delivery for the event", async () => {
const subscription = await buildWebhookSubscription({

View File

@@ -1,10 +1,14 @@
import { subDays } from "date-fns";
import { Document } from "@server/models";
import { buildDocument } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import CleanupDeletedDocumentsTask from "./CleanupDeletedDocumentsTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("CleanupDeletedDocumentsTask", () => {
it("should not destroy documents not deleted", async () => {

View File

@@ -6,10 +6,14 @@ import {
buildWebhookSubscription,
buildViewer,
} from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import CleanupDemotedUserTask from "./CleanupDemotedUserTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("CleanupDemotedUserTask", () => {
it("should delete api keys for suspended user", async () => {

View File

@@ -5,10 +5,14 @@ import {
FileOperationType,
} from "@server/models/FileOperation";
import { buildFileOperation } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import CleanupExpiredFileOperationsTask from "./CleanupExpiredFileOperationsTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("CleanupExpiredFileOperationsTask", () => {
it("should expire exports older than 15 days ago", async () => {

View File

@@ -1,10 +1,14 @@
import { subDays } from "date-fns";
import { WebhookDelivery } from "@server/models";
import { buildWebhookDelivery } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import CleanupWebhookDeliveriesTask from "./CleanupWebhookDeliveriesTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
const deliveryExists = async (delivery: WebhookDelivery) => {
const results = await WebhookDelivery.findOne({ where: { id: delivery.id } });

View File

@@ -6,12 +6,16 @@ import {
buildWebhookDelivery,
buildWebhookSubscription,
} from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import { UserEvent } from "@server/types";
import DeliverWebhookTask from "./DeliverWebhookTask";
beforeEach(() => flushdb());
beforeEach(() => {
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(async () => {
await db.flush();
jest.resetAllMocks();
fetchMock.resetMocks();
fetchMock.doMock();

View File

@@ -2,10 +2,14 @@ import fs from "fs";
import path from "path";
import { FileOperation } from "@server/models";
import { buildFileOperation } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import ImportMarkdownZipTask from "./ImportMarkdownZipTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("ImportMarkdownZipTask", () => {
it("should import the documents, attachments", async () => {

View File

@@ -2,10 +2,14 @@ import fs from "fs";
import path from "path";
import { FileOperation } from "@server/models";
import { buildFileOperation } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import ImportNotionTask from "./ImportNotionTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("ImportNotionTask", () => {
it("should import successfully from a Markdown export", async () => {

View File

@@ -1,10 +1,14 @@
import { subDays } from "date-fns";
import InviteReminderEmail from "@server/emails/templates/InviteReminderEmail";
import { buildInvite } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import InviteReminderTask from "./InviteReminderTask";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("InviteReminderTask", () => {
it("should not destroy documents not deleted", async () => {

View File

@@ -6,13 +6,16 @@ import {
buildAttachment,
buildDocument,
} from "@server/test/factories";
import { flushdb, getTestServer } from "@server/test/support";
const server = getTestServer();
import { getTestDatabase, getTestServer } from "@server/test/support";
jest.mock("@server/utils/s3");
beforeEach(() => flushdb());
const db = getTestDatabase();
const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#attachments.create", () => {
it("should require authentication", async () => {

View File

@@ -1,10 +1,14 @@
import sharedEnv from "@shared/env";
import env from "@server/env";
import { buildUser, buildTeam } from "@server/test/factories";
import { flushdb, getTestServer } from "@server/test/support";
import { getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#auth.info", () => {
it("should return current authentication", async () => {

View File

@@ -1,10 +1,13 @@
import { v4 as uuidv4 } from "uuid";
import { buildUser, buildAdmin, buildTeam } from "@server/test/factories";
import { flushdb, getTestServer } from "@server/test/support";
import { getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#authenticationProviders.info", () => {
it("should return auth provider", async () => {

View File

@@ -7,10 +7,14 @@ import {
buildCollection,
buildDocument,
} from "@server/test/factories";
import { flushdb, seed, getTestServer } from "@server/test/support";
import { seed, getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#collections.list", () => {
it("should require authentication", async () => {

View File

@@ -1,8 +1,11 @@
import { flushdb, getTestServer } from "@server/test/support";
import { getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#cron.daily", () => {
it("should require authentication", async () => {

View File

@@ -15,10 +15,14 @@ import {
buildDocument,
buildViewer,
} from "@server/test/factories";
import { flushdb, seed, getTestServer } from "@server/test/support";
import { seed, getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#documents.info", () => {
it("should return published document", async () => {

View File

@@ -1,8 +1,12 @@
import { buildEvent, buildUser } from "@server/test/factories";
import { flushdb, seed, getTestServer } from "@server/test/support";
import { seed, getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#events.list", () => {
it("should only return activity events", async () => {

View File

@@ -10,13 +10,17 @@ import {
buildTeam,
buildUser,
} from "@server/test/factories";
import { flushdb, getTestServer } from "@server/test/support";
import { getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
jest.mock("@server/utils/s3");
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#fileOperations.info", () => {
it("should return fileOperation", async () => {

View File

@@ -1,9 +1,13 @@
import { Event } from "@server/models";
import { buildUser, buildAdmin, buildGroup } from "@server/test/factories";
import { flushdb, getTestServer } from "@server/test/support";
import { getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#groups.create", () => {
it("should create a group", async () => {

View File

@@ -1,16 +1,20 @@
import env from "@server/env";
import { IntegrationAuthentication, SearchQuery } from "@server/models";
import { buildDocument, buildIntegration } from "@server/test/factories";
import { flushdb, seed, getTestServer } from "@server/test/support";
import { seed, getTestDatabase, getTestServer } from "@server/test/support";
import * as Slack from "@server/utils/slack";
const server = getTestServer();
beforeEach(() => flushdb());
jest.mock("../../utils/slack", () => ({
post: jest.fn(),
}));
const db = getTestDatabase();
const server = getTestServer();
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#hooks.unfurl", () => {
it("should return documents", async () => {
const { user, document } = await seed();

View File

@@ -1,8 +1,8 @@
import { flushdb, getTestServer } from "@server/test/support";
import { getTestServer } from "@server/test/support";
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
describe("POST unknown endpoint", () => {
it("should be not found", async () => {

View File

@@ -4,11 +4,15 @@ import {
buildUser,
buildIntegration,
} from "@server/test/factories";
import { flushdb, getTestServer } from "@server/test/support";
import { getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#integrations.update", () => {
it("should allow updating integration events", async () => {

View File

@@ -1,7 +1,11 @@
import { flushdb, seed, getTestServer } from "@server/test/support";
import { seed, getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#pagination", () => {
it("should allow offset and limit", async () => {

View File

@@ -1,9 +1,13 @@
import { Revision } from "@server/models";
import { buildDocument, buildUser } from "@server/test/factories";
import { flushdb, seed, getTestServer } from "@server/test/support";
import { seed, getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#revisions.info", () => {
it("should return a document revision", async () => {

View File

@@ -6,10 +6,15 @@ import {
buildAdmin,
buildCollection,
} from "@server/test/factories";
import { flushdb, seed, getTestServer } from "@server/test/support";
import { seed, getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#shares.list", () => {
it("should only return shares created by user", async () => {

View File

@@ -1,8 +1,12 @@
import { buildUser, buildStar, buildDocument } from "@server/test/factories";
import { flushdb, getTestServer } from "@server/test/support";
import { getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#stars.create", () => {
it("should create a star", async () => {

View File

@@ -1,9 +1,13 @@
import { TeamDomain } from "@server/models";
import { buildAdmin, buildCollection, buildTeam } from "@server/test/factories";
import { flushdb, seed, getTestServer } from "@server/test/support";
import { seed, getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#team.update", () => {
it("should update team details", async () => {

View File

@@ -1,20 +1,19 @@
import TestServer from "fetch-test-server";
import webService from "@server/services/web";
import { buildTeam, buildAdmin, buildUser } from "@server/test/factories";
import { flushdb, seed } from "@server/test/support";
import { seed, getTestDatabase, getTestServer } from "@server/test/support";
const app = webService();
const server = new TestServer(app.callback());
beforeEach(() => flushdb());
const db = getTestDatabase();
const server = getTestServer();
beforeAll(() => {
jest.useFakeTimers().setSystemTime(new Date("2018-01-02T00:00:00.000Z"));
});
afterAll(() => {
jest.useRealTimers();
return server.close();
server.disconnect();
});
beforeEach(db.flush);
describe("#users.list", () => {
it("should allow filtering by user name", async () => {
const user = await buildUser({

View File

@@ -1,9 +1,13 @@
import { View, CollectionUser } from "@server/models";
import { buildUser } from "@server/test/factories";
import { flushdb, seed, getTestServer } from "@server/test/support";
import { seed, getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("#views.list", () => {
it("should return views for a document", async () => {

View File

@@ -1,9 +1,12 @@
import { buildUser, buildCollection } from "@server/test/factories";
import { flushdb, getTestServer } from "@server/test/support";
import { getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("auth/redirect", () => {
it("should redirect to home", async () => {

View File

@@ -3,11 +3,14 @@ import SigninEmail from "@server/emails/templates/SigninEmail";
import WelcomeEmail from "@server/emails/templates/WelcomeEmail";
import env from "@server/env";
import { buildUser, buildGuestUser, buildTeam } from "@server/test/factories";
import { flushdb, getTestServer } from "@server/test/support";
import { getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("email", () => {
it("should require email param", async () => {

View File

@@ -1,8 +1,12 @@
import { buildShare, buildDocument } from "@server/test/factories";
import { flushdb, getTestServer } from "@server/test/support";
import { getTestDatabase, getTestServer } from "@server/test/support";
const db = getTestDatabase();
const server = getTestServer();
beforeEach(() => flushdb());
afterAll(server.disconnect);
beforeEach(db.flush);
describe("/share/:id", () => {
it("should return standard title in html when loading unpublished share", async () => {

View File

@@ -1,9 +1,13 @@
import { Revision, Event } from "@server/models";
import { buildDocument } from "@server/test/factories";
import { flushdb } from "@server/test/support";
import { getTestDatabase } from "@server/test/support";
import script from "./20210716000000-backfill-revisions";
beforeEach(() => flushdb());
const db = getTestDatabase();
afterAll(db.disconnect);
beforeEach(db.flush);
describe("#work", () => {
it("should create events for revisions", async () => {

View File

@@ -1,24 +1,11 @@
import TestServer from "fetch-test-server";
import { v4 as uuidv4 } from "uuid";
import { sequelize } from "@server/database/sequelize";
import { sequelize as db } from "@server/database/sequelize";
import { User, Document, Collection, Team } from "@server/models";
import webService from "@server/services/web";
const sql = sequelize.getQueryInterface();
const tables = Object.keys(sequelize.models).map((model) => {
const n = sequelize.models[model].getTableName();
return (sql.queryGenerator as any).quoteTable(
typeof n === "string" ? n : n.tableName
);
});
const flushQuery = `TRUNCATE ${tables.join(", ")} CASCADE`;
export function flushdb() {
return sequelize.query(flushQuery);
}
export const seed = async () => {
return sequelize.transaction(async (transaction) => {
return db.transaction(async (transaction) => {
const team = await Team.create(
{
name: "Team",
@@ -112,14 +99,35 @@ export const seed = async () => {
});
};
let testServer: typeof TestServer | undefined;
export function getTestServer() {
if (testServer) {
return testServer;
}
const app = webService();
testServer = new TestServer(app.callback());
return testServer;
const server = new TestServer(app.callback());
server.disconnect = async () => {
await db.close();
server.close();
};
return server;
}
export function getTestDatabase() {
const flush = async () => {
const sql = db.getQueryInterface();
const tables = Object.keys(db.models).map((model) => {
const n = db.models[model].getTableName();
return (sql.queryGenerator as any).quoteTable(
typeof n === "string" ? n : n.tableName
);
});
const flushQuery = `TRUNCATE ${tables.join(", ")} CASCADE`;
await db.query(flushQuery);
};
const disconnect = async () => {
await db.close();
};
return { flush, disconnect };
}

3
shared/test/setup.ts Normal file
View File

@@ -0,0 +1,3 @@
jest.mock("i18next-http-backend");
export {};