chore: Test performance (#5786)
This commit is contained in:
@@ -5,15 +5,9 @@ import { TeamDomain } from "@server/models";
|
||||
import Collection from "@server/models/Collection";
|
||||
import UserAuthentication from "@server/models/UserAuthentication";
|
||||
import { buildUser, buildTeam, buildAdmin } from "@server/test/factories";
|
||||
import {
|
||||
setupTestDatabase,
|
||||
setCloudHosted,
|
||||
setSelfHosted,
|
||||
} from "@server/test/support";
|
||||
import { setCloudHosted, setSelfHosted } from "@server/test/support";
|
||||
import accountProvisioner from "./accountProvisioner";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("accountProvisioner", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
@@ -371,7 +365,7 @@ describe("accountProvisioner", () => {
|
||||
},
|
||||
authenticationProvider: {
|
||||
name: "google",
|
||||
providerId: "example-company.com",
|
||||
providerId: faker.internet.domainName(),
|
||||
},
|
||||
authentication: {
|
||||
providerId: uuidv4(),
|
||||
@@ -390,6 +384,7 @@ describe("accountProvisioner", () => {
|
||||
|
||||
it("should always use existing team if self-hosted", async () => {
|
||||
const team = await buildTeam();
|
||||
const domain = faker.internet.domainName();
|
||||
const { user, isNewUser } = await accountProvisioner({
|
||||
ip,
|
||||
user: {
|
||||
@@ -402,11 +397,11 @@ describe("accountProvisioner", () => {
|
||||
name: team.name,
|
||||
avatarUrl: team.avatarUrl,
|
||||
subdomain: faker.internet.domainWord(),
|
||||
domain: "allowed-domain.com",
|
||||
domain,
|
||||
},
|
||||
authenticationProvider: {
|
||||
name: "google",
|
||||
providerId: "allowed-domain.com",
|
||||
providerId: domain,
|
||||
},
|
||||
authentication: {
|
||||
providerId: uuidv4(),
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { findLatestEvent, setupTestDatabase } from "@server/test/support";
|
||||
import { findLatestEvent } from "@server/test/support";
|
||||
import commentCreator from "./commentCreator";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("commentCreator", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import { Comment } from "@server/models";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { findLatestEvent, setupTestDatabase } from "@server/test/support";
|
||||
import { findLatestEvent } from "@server/test/support";
|
||||
import commentDestroyer from "./commentDestroyer";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("commentDestroyer", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -2,13 +2,10 @@ import path from "path";
|
||||
import fs from "fs-extra";
|
||||
import Attachment from "@server/models/Attachment";
|
||||
import { buildUser } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import documentImporter from "./documentImporter";
|
||||
|
||||
jest.mock("@server/storage/files");
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("documentImporter", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -6,11 +6,8 @@ import {
|
||||
buildTeam,
|
||||
buildUser,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import documentMover from "./documentMover";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("documentMover", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -2,13 +2,10 @@ import { subDays } from "date-fns";
|
||||
import { Attachment, Document } from "@server/models";
|
||||
import DeleteAttachmentTask from "@server/queues/tasks/DeleteAttachmentTask";
|
||||
import { buildAttachment, buildDocument } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import documentPermanentDeleter from "./documentPermanentDeleter";
|
||||
|
||||
jest.mock("@server/queues/tasks/DeleteAttachmentTask");
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("documentPermanentDeleter", () => {
|
||||
it("should destroy documents", async () => {
|
||||
const document = await buildDocument({
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { findLatestEvent, setupTestDatabase } from "@server/test/support";
|
||||
import { findLatestEvent } from "@server/test/support";
|
||||
import documentUpdater from "./documentUpdater";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("documentUpdater", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import { FileOperation } from "@server/models";
|
||||
import { buildAdmin, buildFileOperation } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import fileOperationDeleter from "./fileOperationDeleter";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("fileOperationDeleter", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -6,11 +6,9 @@ import {
|
||||
buildDocument,
|
||||
buildCollection,
|
||||
} from "@server/test/factories";
|
||||
import { findLatestEvent, setupTestDatabase } from "@server/test/support";
|
||||
import { findLatestEvent } from "@server/test/support";
|
||||
import notificationUpdater from "./notificationUpdater";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("notificationUpdater", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
@@ -48,7 +46,7 @@ describe("notificationUpdater", () => {
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
const event = await findLatestEvent({});
|
||||
const event = await findLatestEvent();
|
||||
|
||||
expect(notification.viewedAt).not.toBe(null);
|
||||
expect(notification.archivedAt).toBe(null);
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { findLatestEvent, setupTestDatabase } from "@server/test/support";
|
||||
import { findLatestEvent } from "@server/test/support";
|
||||
import pinCreator from "./pinCreator";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("pinCreator", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import { Pin } from "@server/models";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { findLatestEvent, setupTestDatabase } from "@server/test/support";
|
||||
import { findLatestEvent } from "@server/test/support";
|
||||
import pinDestroyer from "./pinDestroyer";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("pinCreator", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { findLatestEvent, setupTestDatabase } from "@server/test/support";
|
||||
import { findLatestEvent } from "@server/test/support";
|
||||
import revisionCreator from "./revisionCreator";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("revisionCreator", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
@@ -18,7 +16,9 @@ describe("revisionCreator", () => {
|
||||
user,
|
||||
ip,
|
||||
});
|
||||
const event = await findLatestEvent();
|
||||
const event = await findLatestEvent({
|
||||
teamId: user.teamId,
|
||||
});
|
||||
expect(revision.documentId).toEqual(document.id);
|
||||
expect(revision.userId).toEqual(user.id);
|
||||
expect(revision.createdAt).toEqual(document.updatedAt);
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
import { Star, Event } from "@server/models";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { findLatestEvent, setupTestDatabase } from "@server/test/support";
|
||||
import { findLatestEvent } from "@server/test/support";
|
||||
import starCreator from "./starCreator";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("starCreator", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import { Star } from "@server/models";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { findLatestEvent, setupTestDatabase } from "@server/test/support";
|
||||
import { findLatestEvent } from "@server/test/support";
|
||||
import starDestroyer from "./starDestroyer";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("starDestroyer", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import { Star } from "@server/models";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { findLatestEvent, setupTestDatabase } from "@server/test/support";
|
||||
import { findLatestEvent } from "@server/test/support";
|
||||
import starUpdater from "./starUpdater";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("starUpdater", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
import { Subscription, Event } from "@server/models";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import subscriptionCreator from "./subscriptionCreator";
|
||||
import subscriptionDestroyer from "./subscriptionDestroyer";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("subscriptionCreator", () => {
|
||||
const ip = "127.0.0.1";
|
||||
const subscribedEvent = "documents.update";
|
||||
|
||||
@@ -5,11 +5,8 @@ import {
|
||||
buildSubscription,
|
||||
buildUser,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import subscriptionDestroyer from "./subscriptionDestroyer";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("subscriptionDestroyer", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -6,11 +6,8 @@ import {
|
||||
buildTeam,
|
||||
buildDocument,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import teamPermanentDeleter from "./teamPermanentDeleter";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("teamPermanentDeleter", () => {
|
||||
it("should destroy related data", async () => {
|
||||
const team = await buildTeam({
|
||||
|
||||
@@ -1,15 +1,9 @@
|
||||
import { faker } from "@faker-js/faker";
|
||||
import TeamDomain from "@server/models/TeamDomain";
|
||||
import { buildTeam, buildUser } from "@server/test/factories";
|
||||
import {
|
||||
setCloudHosted,
|
||||
setSelfHosted,
|
||||
setupTestDatabase,
|
||||
} from "@server/test/support";
|
||||
import { setCloudHosted, setSelfHosted } from "@server/test/support";
|
||||
import teamProvisioner from "./teamProvisioner";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("teamProvisioner", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
@@ -104,12 +98,14 @@ describe("teamProvisioner", () => {
|
||||
});
|
||||
|
||||
it("should error on mismatched team and authentication provider", async () => {
|
||||
const subdomain = faker.internet.domainWord();
|
||||
|
||||
const exampleTeam = await buildTeam({
|
||||
subdomain: faker.internet.domainWord(),
|
||||
subdomain,
|
||||
authenticationProviders: [
|
||||
{
|
||||
name: "google",
|
||||
providerId: "teamProvisioner3.com",
|
||||
providerId: `${subdomain}.com`,
|
||||
},
|
||||
],
|
||||
});
|
||||
@@ -215,9 +211,11 @@ describe("teamProvisioner", () => {
|
||||
const user = await buildUser({
|
||||
teamId: existing.id,
|
||||
});
|
||||
const allowedDomain = faker.internet.domainName();
|
||||
const otherDomain = faker.internet.domainName();
|
||||
await TeamDomain.create({
|
||||
teamId: existing.id,
|
||||
name: "allowed-domain.com",
|
||||
name: allowedDomain,
|
||||
createdById: user.id,
|
||||
});
|
||||
|
||||
@@ -226,11 +224,11 @@ describe("teamProvisioner", () => {
|
||||
await teamProvisioner({
|
||||
name: "Updated name",
|
||||
subdomain: faker.internet.domainWord(),
|
||||
domain: "other-domain.com",
|
||||
domain: otherDomain,
|
||||
teamId: existing.id,
|
||||
authenticationProvider: {
|
||||
name: "google",
|
||||
providerId: "other-domain.com",
|
||||
providerId: otherDomain,
|
||||
},
|
||||
ip,
|
||||
});
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { CollectionPermission, UserRole } from "@shared/types";
|
||||
import { CollectionUser } from "@server/models";
|
||||
import { buildUser, buildAdmin, buildCollection } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import userDemoter from "./userDemoter";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("userDemoter", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { buildUser, buildAdmin } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import userDestroyer from "./userDestroyer";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("userDestroyer", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { faker } from "@faker-js/faker";
|
||||
import { UserRole } from "@shared/types";
|
||||
import { buildUser } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import userInviter from "./userInviter";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("userInviter", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { faker } from "@faker-js/faker";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { TeamDomain } from "@server/models";
|
||||
import {
|
||||
@@ -6,11 +7,8 @@ import {
|
||||
buildInvite,
|
||||
buildAdmin,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import userProvisioner from "./userProvisioner";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("userProvisioner", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
@@ -338,17 +336,19 @@ describe("userProvisioner", () => {
|
||||
it("should create a user from allowed domain", async () => {
|
||||
const team = await buildTeam();
|
||||
const admin = await buildAdmin({ teamId: team.id });
|
||||
const domain = faker.internet.domainName();
|
||||
await TeamDomain.create({
|
||||
teamId: team.id,
|
||||
name: "example-company.com",
|
||||
name: domain,
|
||||
createdById: admin.id,
|
||||
});
|
||||
|
||||
const authenticationProviders = await team.$get("authenticationProviders");
|
||||
const authenticationProvider = authenticationProviders[0];
|
||||
const email = faker.internet.email({ provider: domain });
|
||||
const result = await userProvisioner({
|
||||
name: "Test Name",
|
||||
email: "user@example-company.com",
|
||||
name: faker.person.fullName(),
|
||||
email,
|
||||
teamId: team.id,
|
||||
ip,
|
||||
authentication: {
|
||||
@@ -363,28 +363,31 @@ describe("userProvisioner", () => {
|
||||
expect(authentication?.accessToken).toEqual("123");
|
||||
expect(authentication?.scopes.length).toEqual(1);
|
||||
expect(authentication?.scopes[0]).toEqual("read");
|
||||
expect(user.email).toEqual("user@example-company.com");
|
||||
expect(user.email).toEqual(email);
|
||||
expect(isNewUser).toEqual(true);
|
||||
});
|
||||
|
||||
it("should create a user from allowed domain with emailMatchOnly", async () => {
|
||||
const team = await buildTeam();
|
||||
const admin = await buildAdmin({ teamId: team.id });
|
||||
const domain = faker.internet.domainName();
|
||||
const email = faker.internet.email({ provider: domain });
|
||||
|
||||
await TeamDomain.create({
|
||||
teamId: team.id,
|
||||
name: "example-company.com",
|
||||
name: domain,
|
||||
createdById: admin.id,
|
||||
});
|
||||
|
||||
const result = await userProvisioner({
|
||||
name: "Test Name",
|
||||
email: "user@example-company.com",
|
||||
email,
|
||||
teamId: team.id,
|
||||
ip,
|
||||
});
|
||||
const { user, authentication, isNewUser } = result;
|
||||
expect(authentication).toBeUndefined();
|
||||
expect(user.email).toEqual("user@example-company.com");
|
||||
expect(user.email).toEqual(email);
|
||||
expect(isNewUser).toEqual(true);
|
||||
});
|
||||
|
||||
@@ -395,7 +398,7 @@ describe("userProvisioner", () => {
|
||||
try {
|
||||
await userProvisioner({
|
||||
name: "Test Name",
|
||||
email: "user@example-company.com",
|
||||
email: faker.internet.email(),
|
||||
teamId: team.id,
|
||||
ip,
|
||||
});
|
||||
@@ -411,7 +414,7 @@ describe("userProvisioner", () => {
|
||||
const admin = await buildAdmin({ teamId: team.id });
|
||||
await TeamDomain.create({
|
||||
teamId: team.id,
|
||||
name: "other.com",
|
||||
name: faker.internet.domainName(),
|
||||
createdById: admin.id,
|
||||
});
|
||||
|
||||
@@ -422,7 +425,7 @@ describe("userProvisioner", () => {
|
||||
try {
|
||||
await userProvisioner({
|
||||
name: "Bad Domain User",
|
||||
email: "user@example.com",
|
||||
email: faker.internet.domainName(),
|
||||
teamId: team.id,
|
||||
ip,
|
||||
authentication: {
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import GroupUser from "@server/models/GroupUser";
|
||||
import { buildGroup, buildAdmin, buildUser } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import userSuspender from "./userSuspender";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("userSuspender", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { buildAdmin, buildUser } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import userUnsuspender from "./userUnsuspender";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("userUnsuspender", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
|
||||
@@ -2,11 +2,8 @@ import { DefaultState } from "koa";
|
||||
import randomstring from "randomstring";
|
||||
import ApiKey from "@server/models/ApiKey";
|
||||
import { buildUser, buildTeam } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import auth from "./authentication";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("Authentication middleware", () => {
|
||||
describe("with JWT", () => {
|
||||
it("should authenticate with correct token", async () => {
|
||||
|
||||
@@ -7,13 +7,10 @@ import {
|
||||
buildTeam,
|
||||
buildDocument,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import slugify from "@server/utils/slugify";
|
||||
import Collection from "./Collection";
|
||||
import Document from "./Document";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
@@ -6,11 +6,8 @@ import {
|
||||
buildTeam,
|
||||
buildUser,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import slugify from "@server/utils/slugify";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { buildDocument } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import Revision from "./Revision";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("#findLatest", () => {
|
||||
test("should return latest revision", async () => {
|
||||
const document = await buildDocument({
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
import { buildTeam, buildCollection } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("collectionIds", () => {
|
||||
it("should return non-private collection ids", async () => {
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { buildAdmin, buildTeam } from "@server/test/factories";
|
||||
import { setCloudHosted, setupTestDatabase } from "@server/test/support";
|
||||
import { setCloudHosted } from "@server/test/support";
|
||||
import TeamDomain from "./TeamDomain";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("team domain model", () => {
|
||||
beforeEach(setCloudHosted);
|
||||
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { CollectionPermission } from "@shared/types";
|
||||
import { buildUser, buildTeam, buildCollection } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import CollectionUser from "./CollectionUser";
|
||||
import UserAuthentication from "./UserAuthentication";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
beforeAll(() => {
|
||||
jest.useFakeTimers().setSystemTime(new Date("2018-01-02T00:00:00.000Z"));
|
||||
});
|
||||
|
||||
@@ -7,9 +7,6 @@ import {
|
||||
buildUser,
|
||||
buildShare,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
beforeEach(() => {
|
||||
jest.resetAllMocks();
|
||||
|
||||
@@ -6,11 +6,8 @@ import {
|
||||
buildCollection,
|
||||
buildAdmin,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import { serialize } from "./index";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("admin", () => {
|
||||
it("should allow updating collection but not reading documents", async () => {
|
||||
const team = await buildTeam();
|
||||
|
||||
@@ -6,11 +6,8 @@ import {
|
||||
buildDraftDocument,
|
||||
buildCollection,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import { serialize } from "./index";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("read_write collection", () => {
|
||||
it("should allow read write permissions for member", async () => {
|
||||
const team = await buildTeam();
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
import { buildUser, buildTeam } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import { serialize } from "./index";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
it("should serialize policy", async () => {
|
||||
const user = await buildUser();
|
||||
const response = serialize(user, user);
|
||||
|
||||
@@ -1,13 +1,7 @@
|
||||
import { buildUser, buildTeam, buildAdmin } from "@server/test/factories";
|
||||
import {
|
||||
setCloudHosted,
|
||||
setSelfHosted,
|
||||
setupTestDatabase,
|
||||
} from "@server/test/support";
|
||||
import { setCloudHosted, setSelfHosted } from "@server/test/support";
|
||||
import { serialize } from "./index";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
it("should allow reading only", async () => {
|
||||
await setSelfHosted();
|
||||
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
import { Backlink } from "@server/models";
|
||||
import { buildDocument } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import BacklinksProcessor from "./BacklinksProcessor";
|
||||
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("documents.publish", () => {
|
||||
test("should create new backlink records", async () => {
|
||||
const otherDocument = await buildDocument();
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
import { Revision } from "@server/models";
|
||||
import { buildDocument } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import RevisionsProcessor from "./RevisionsProcessor";
|
||||
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("documents.update.debounced", () => {
|
||||
test("should create a revision", async () => {
|
||||
const document = await buildDocument();
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import path from "path";
|
||||
import { glob } from "glob";
|
||||
import env from "@server/env";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import { requireDirectory } from "@server/utils/fs";
|
||||
import BaseProcessor from "./BaseProcessor";
|
||||
|
||||
const processors = {};
|
||||
const rootDir = env.ENVIRONMENT === "test" ? "" : "build";
|
||||
|
||||
requireDirectory<{ default: BaseProcessor }>(__dirname).forEach(
|
||||
([module, id]) => {
|
||||
@@ -16,7 +18,7 @@ requireDirectory<{ default: BaseProcessor }>(__dirname).forEach(
|
||||
);
|
||||
|
||||
glob
|
||||
.sync("build/plugins/*/server/processors/!(*.test).js")
|
||||
.sync(path.join(rootDir, "plugins/*/server/processors/!(*.test).[jt]s"))
|
||||
.forEach((filePath: string) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const processor = require(path.join(process.cwd(), filePath)).default;
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { subDays } from "date-fns";
|
||||
import { Document } from "@server/models";
|
||||
import { buildDocument, buildTeam } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import CleanupDeletedDocumentsTask from "./CleanupDeletedDocumentsTask";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("CleanupDeletedDocumentsTask", () => {
|
||||
it("should not destroy documents not deleted", async () => {
|
||||
const team = await buildTeam();
|
||||
|
||||
@@ -6,11 +6,8 @@ import {
|
||||
buildWebhookSubscription,
|
||||
buildViewer,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import CleanupDemotedUserTask from "./CleanupDemotedUserTask";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("CleanupDemotedUserTask", () => {
|
||||
it("should delete api keys for suspended user", async () => {
|
||||
const admin = await buildAdmin();
|
||||
|
||||
@@ -2,11 +2,8 @@ import { subDays } from "date-fns";
|
||||
import { FileOperationState, FileOperationType } from "@shared/types";
|
||||
import { FileOperation } from "@server/models";
|
||||
import { buildFileOperation, buildTeam } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import CleanupExpiredFileOperationsTask from "./CleanupExpiredFileOperationsTask";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("CleanupExpiredFileOperationsTask", () => {
|
||||
it("should expire exports older than 15 days ago", async () => {
|
||||
const team = await buildTeam();
|
||||
|
||||
@@ -1,10 +1,7 @@
|
||||
import { Document } from "@server/models";
|
||||
import { buildCollection, buildDocument } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import DetachDraftsFromCollectionTask from "./DetachDraftsFromCollectionTask";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("DetachDraftsFromCollectionTask", () => {
|
||||
const ip = "127.0.0.1";
|
||||
it("should detach drafts from deleted collection", async () => {
|
||||
|
||||
@@ -5,13 +5,10 @@ import {
|
||||
buildCollection,
|
||||
buildUser,
|
||||
} from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import DocumentPublishedNotificationsTask from "./DocumentPublishedNotificationsTask";
|
||||
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
beforeEach(async () => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
@@ -2,11 +2,8 @@ import { subDays } from "date-fns";
|
||||
import { FileOperationState, FileOperationType } from "@shared/types";
|
||||
import { FileOperation } from "@server/models";
|
||||
import { buildFileOperation, buildTeam } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import ErrorTimedOutFileOperationsTask from "./ErrorTimedOutFileOperationsTask";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("ErrorTimedOutFileOperationsTask", () => {
|
||||
it("should error exports older than 12 hours", async () => {
|
||||
const team = await buildTeam();
|
||||
|
||||
@@ -2,11 +2,8 @@ import fs from "fs";
|
||||
import path from "path";
|
||||
import { FileOperation } from "@server/models";
|
||||
import { buildFileOperation } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import ImportMarkdownZipTask from "./ImportMarkdownZipTask";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("ImportMarkdownZipTask", () => {
|
||||
it("should import the documents, attachments", async () => {
|
||||
const fileOperation = await buildFileOperation();
|
||||
|
||||
@@ -2,11 +2,8 @@ import fs from "fs";
|
||||
import path from "path";
|
||||
import { FileOperation } from "@server/models";
|
||||
import { buildFileOperation } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import ImportNotionTask from "./ImportNotionTask";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("ImportNotionTask", () => {
|
||||
it("should import successfully from a Markdown export", async () => {
|
||||
const fileOperation = await buildFileOperation();
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
import { subDays } from "date-fns";
|
||||
import InviteReminderEmail from "@server/emails/templates/InviteReminderEmail";
|
||||
import { buildInvite } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import InviteReminderTask from "./InviteReminderTask";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
describe("InviteReminderTask", () => {
|
||||
it("should not destroy documents not deleted", async () => {
|
||||
const spy = jest.spyOn(InviteReminderEmail.prototype, "schedule");
|
||||
|
||||
@@ -6,13 +6,10 @@ import {
|
||||
Revision,
|
||||
} from "@server/models";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import RevisionCreatedNotificationsTask from "./RevisionCreatedNotificationsTask";
|
||||
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
beforeEach(async () => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import path from "path";
|
||||
import { glob } from "glob";
|
||||
import env from "@server/env";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import { requireDirectory } from "@server/utils/fs";
|
||||
import BaseTask from "./BaseTask";
|
||||
|
||||
const tasks = {};
|
||||
const rootDir = env.ENVIRONMENT === "test" ? "" : "build";
|
||||
|
||||
requireDirectory<{ default: BaseTask<any> }>(__dirname).forEach(
|
||||
([module, id]) => {
|
||||
@@ -16,7 +18,7 @@ requireDirectory<{ default: BaseTask<any> }>(__dirname).forEach(
|
||||
);
|
||||
|
||||
glob
|
||||
.sync("build/plugins/*/server/tasks/!(*.test).js")
|
||||
.sync(path.join(rootDir, "plugins/*/server/tasks/!(*.test).[jt]s"))
|
||||
.forEach((filePath: string) => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
||||
const task = require(path.join(process.cwd(), filePath)).default;
|
||||
|
||||
@@ -39,9 +39,10 @@ describe("#auth.info", () => {
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
|
||||
const availableTeamIds = body.data.availableTeams.map((t: any) => t.id);
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(availableTeamIds.length).toEqual(3);
|
||||
expect(availableTeamIds).toContain(team.id);
|
||||
expect(availableTeamIds).toContain(team2.id);
|
||||
|
||||
@@ -1726,10 +1726,6 @@ describe("#documents.search", () => {
|
||||
},
|
||||
});
|
||||
|
||||
// setTimeout is needed here because SearchQuery is saved asynchronously
|
||||
// in order to not slow down the response time.
|
||||
await new Promise((resolve) => setTimeout(resolve, 100));
|
||||
|
||||
const searchQuery = await SearchQuery.findAll({
|
||||
where: {
|
||||
teamId: user.teamId,
|
||||
|
||||
@@ -20,7 +20,6 @@ import {
|
||||
ValidationError,
|
||||
IncorrectEditionError,
|
||||
} from "@server/errors";
|
||||
import Logger from "@server/logging/Logger";
|
||||
import auth from "@server/middlewares/authentication";
|
||||
import { rateLimiter } from "@server/middlewares/rateLimiter";
|
||||
import { transaction } from "@server/middlewares/transaction";
|
||||
@@ -818,15 +817,13 @@ router.post(
|
||||
// When requesting subsequent pages of search results we don't want to record
|
||||
// duplicate search query records
|
||||
if (offset === 0) {
|
||||
void SearchQuery.create({
|
||||
await SearchQuery.create({
|
||||
userId: user?.id,
|
||||
teamId,
|
||||
shareId,
|
||||
source: ctx.state.auth.type || "app", // we'll consider anything that isn't "api" to be "app"
|
||||
query,
|
||||
results: totalCount,
|
||||
}).catch((err) => {
|
||||
Logger.error("Failed to create search query", err);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -76,32 +76,24 @@ describe("#team.update", () => {
|
||||
it("should add new allowed Domains, removing empty string values", async () => {
|
||||
const team = await buildTeam();
|
||||
const admin = await buildAdmin({ teamId: team.id });
|
||||
const domain1 = faker.internet.domainName();
|
||||
const domain2 = faker.internet.domainName();
|
||||
const res = await server.post("/api/team.update", {
|
||||
body: {
|
||||
token: admin.getJwtToken(),
|
||||
allowedDomains: [
|
||||
"example-company.com",
|
||||
"",
|
||||
"example-company.org",
|
||||
"",
|
||||
"",
|
||||
],
|
||||
allowedDomains: [domain1, "", domain2, "", ""],
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.allowedDomains.sort()).toEqual([
|
||||
"example-company.com",
|
||||
"example-company.org",
|
||||
]);
|
||||
expect(body.data.allowedDomains.includes(domain1)).toBe(true);
|
||||
expect(body.data.allowedDomains.includes(domain2)).toBe(true);
|
||||
|
||||
const teamDomains: TeamDomain[] = await TeamDomain.findAll({
|
||||
where: { teamId: team.id },
|
||||
});
|
||||
expect(teamDomains.map((d) => d.name).sort()).toEqual([
|
||||
"example-company.com",
|
||||
"example-company.org",
|
||||
]);
|
||||
expect(teamDomains.map((d) => d.name).includes(domain1)).toBe(true);
|
||||
expect(teamDomains.map((d) => d.name).includes(domain2)).toBe(true);
|
||||
});
|
||||
|
||||
it("should remove old allowed Domains", async () => {
|
||||
@@ -139,27 +131,25 @@ describe("#team.update", () => {
|
||||
name: faker.internet.domainName(),
|
||||
createdById: admin.id,
|
||||
});
|
||||
const domain1 = faker.internet.domainName();
|
||||
const domain2 = faker.internet.domainName();
|
||||
|
||||
const res = await server.post("/api/team.update", {
|
||||
body: {
|
||||
token: admin.getJwtToken(),
|
||||
allowedDomains: ["example-company.org", "example-company.net"],
|
||||
allowedDomains: [domain1, domain2],
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.allowedDomains.sort()).toEqual([
|
||||
"example-company.net",
|
||||
"example-company.org",
|
||||
]);
|
||||
expect(body.data.allowedDomains.includes(domain1)).toBe(true);
|
||||
expect(body.data.allowedDomains.includes(domain2)).toBe(true);
|
||||
|
||||
const teamDomains: TeamDomain[] = await TeamDomain.findAll({
|
||||
where: { teamId: team.id },
|
||||
});
|
||||
expect(teamDomains.map((d) => d.name).sort()).toEqual(
|
||||
["example-company.org", "example-company.net"].sort()
|
||||
);
|
||||
|
||||
expect(teamDomains.map((d) => d.name).includes(domain1)).toBe(true);
|
||||
expect(teamDomains.map((d) => d.name).includes(domain2)).toBe(true);
|
||||
expect(await TeamDomain.findByPk(existingTeamDomain.id)).toBeNull();
|
||||
});
|
||||
|
||||
|
||||
@@ -129,7 +129,7 @@ export function buildTeam(overrides: Record<string, any> = {}) {
|
||||
authenticationProviders: [
|
||||
{
|
||||
name: "slack",
|
||||
providerId: uuidv4(),
|
||||
providerId: uuidv4().replace(/-/g, ""),
|
||||
},
|
||||
],
|
||||
...overrides,
|
||||
@@ -188,7 +188,7 @@ export async function buildUser(overrides: Partial<User> = {}) {
|
||||
authentications: [
|
||||
{
|
||||
authenticationProviderId: authenticationProvider!.id,
|
||||
providerId: uuidv4(),
|
||||
providerId: uuidv4().replace(/-/g, ""),
|
||||
},
|
||||
],
|
||||
...overrides,
|
||||
|
||||
15
server/test/globalSetup.ts
Normal file
15
server/test/globalSetup.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import "./env";
|
||||
import { sequelize } from "@server/storage/database";
|
||||
|
||||
module.exports = async function () {
|
||||
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`;
|
||||
|
||||
await sequelize.query(flushQuery);
|
||||
};
|
||||
5
server/test/globalTeardown.ts
Normal file
5
server/test/globalTeardown.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { sequelize } from "@server/storage/database";
|
||||
|
||||
module.exports = async function () {
|
||||
await sequelize.close();
|
||||
};
|
||||
@@ -1,7 +1,5 @@
|
||||
import Redis from "@server/storage/redis";
|
||||
|
||||
// NOTE: this require must come after the ENV var override
|
||||
// so that sequelize uses the test config variables
|
||||
require("@server/storage/database");
|
||||
|
||||
jest.mock("bull");
|
||||
|
||||
@@ -17,35 +17,10 @@ export function getTestServer() {
|
||||
server.close();
|
||||
};
|
||||
|
||||
setupTestDatabase();
|
||||
afterAll(server.disconnect);
|
||||
|
||||
return server;
|
||||
}
|
||||
|
||||
export function setupTestDatabase() {
|
||||
const flush = async () => {
|
||||
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`;
|
||||
|
||||
await sequelize.query(flushQuery);
|
||||
};
|
||||
|
||||
const disconnect = async () => {
|
||||
await sequelize.close();
|
||||
};
|
||||
|
||||
beforeAll(flush);
|
||||
|
||||
afterAll(disconnect);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the environment to be cloud hosted
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user