chore: Improve perf of server tests (#5785)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { faker } from "@faker-js/faker";
|
||||
import SigninEmail from "@server/emails/templates/SigninEmail";
|
||||
import WelcomeEmail from "@server/emails/templates/WelcomeEmail";
|
||||
import { AuthenticationProvider } from "@server/models";
|
||||
@@ -21,18 +22,15 @@ describe("email", () => {
|
||||
|
||||
it("should respond with redirect location when user is SSO enabled", async () => {
|
||||
const spy = jest.spyOn(WelcomeEmail.prototype, "schedule");
|
||||
const team = await buildTeam({
|
||||
subdomain: "example",
|
||||
});
|
||||
const user = await buildUser({
|
||||
teamId: team.id,
|
||||
});
|
||||
const subdomain = faker.internet.domainWord();
|
||||
const team = await buildTeam({ subdomain });
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const res = await server.post("/auth/email", {
|
||||
body: {
|
||||
email: user.email,
|
||||
},
|
||||
headers: {
|
||||
host: "example.outline.dev",
|
||||
host: `${subdomain}.outline.dev`,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
@@ -44,16 +42,14 @@ describe("email", () => {
|
||||
|
||||
it("should respond with success and email to be sent when user has SSO but disabled", async () => {
|
||||
const spy = jest.spyOn(SigninEmail.prototype, "schedule");
|
||||
const team = await buildTeam({
|
||||
subdomain: "example",
|
||||
});
|
||||
const user = await buildUser({
|
||||
teamId: team.id,
|
||||
});
|
||||
const subdomain = faker.internet.domainWord();
|
||||
const team = await buildTeam({ subdomain });
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
|
||||
// Disable all the auth providers
|
||||
await AuthenticationProvider.update(
|
||||
{
|
||||
teamId: team.id,
|
||||
enabled: false,
|
||||
},
|
||||
{
|
||||
@@ -68,7 +64,7 @@ describe("email", () => {
|
||||
email: user.email,
|
||||
},
|
||||
headers: {
|
||||
host: "example.outline.dev",
|
||||
host: `${subdomain}.outline.dev`,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
@@ -81,15 +77,14 @@ describe("email", () => {
|
||||
it("should not send email when user is on another subdomain but respond with success", async () => {
|
||||
const user = await buildUser();
|
||||
const spy = jest.spyOn(WelcomeEmail.prototype, "schedule");
|
||||
await buildTeam({
|
||||
subdomain: "example",
|
||||
});
|
||||
const subdomain = faker.internet.domainWord();
|
||||
await buildTeam({ subdomain });
|
||||
const res = await server.post("/auth/email", {
|
||||
body: {
|
||||
email: user.email,
|
||||
},
|
||||
headers: {
|
||||
host: "example.outline.dev",
|
||||
host: `${subdomain}.outline.dev`,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -102,9 +97,8 @@ describe("email", () => {
|
||||
|
||||
it("should respond with success and email to be sent when user is not SSO enabled", async () => {
|
||||
const spy = jest.spyOn(SigninEmail.prototype, "schedule");
|
||||
const team = await buildTeam({
|
||||
subdomain: "example",
|
||||
});
|
||||
const subdomain = faker.internet.domainWord();
|
||||
const team = await buildTeam({ subdomain });
|
||||
const user = await buildGuestUser({
|
||||
teamId: team.id,
|
||||
});
|
||||
@@ -113,7 +107,7 @@ describe("email", () => {
|
||||
email: user.email,
|
||||
},
|
||||
headers: {
|
||||
host: "example.outline.dev",
|
||||
host: `${subdomain}.outline.dev`,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
@@ -125,15 +119,14 @@ describe("email", () => {
|
||||
|
||||
it("should respond with success regardless of whether successful to prevent crawling email logins", async () => {
|
||||
const spy = jest.spyOn(WelcomeEmail.prototype, "schedule");
|
||||
await buildTeam({
|
||||
subdomain: "example",
|
||||
});
|
||||
const subdomain = faker.internet.domainWord();
|
||||
await buildTeam({ subdomain });
|
||||
const res = await server.post("/auth/email", {
|
||||
body: {
|
||||
email: "user@example.com",
|
||||
},
|
||||
headers: {
|
||||
host: "example.outline.dev",
|
||||
host: `${subdomain}.outline.dev`,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
@@ -147,8 +140,9 @@ describe("email", () => {
|
||||
it("should default to current subdomain with SSO", async () => {
|
||||
const spy = jest.spyOn(SigninEmail.prototype, "schedule");
|
||||
const email = "sso-user@example.org";
|
||||
const subdomain = faker.internet.domainWord();
|
||||
const team = await buildTeam({
|
||||
subdomain: "example",
|
||||
subdomain,
|
||||
});
|
||||
await buildGuestUser({
|
||||
email,
|
||||
@@ -162,7 +156,7 @@ describe("email", () => {
|
||||
email,
|
||||
},
|
||||
headers: {
|
||||
host: "example.outline.dev",
|
||||
host: `${subdomain}.outline.dev`,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
@@ -175,8 +169,9 @@ describe("email", () => {
|
||||
it("should default to current subdomain with guest email", async () => {
|
||||
const spy = jest.spyOn(SigninEmail.prototype, "schedule");
|
||||
const email = "guest-user@example.org";
|
||||
const subdomain = faker.internet.domainWord();
|
||||
const team = await buildTeam({
|
||||
subdomain: "example",
|
||||
subdomain,
|
||||
});
|
||||
await buildUser({
|
||||
email,
|
||||
@@ -190,7 +185,7 @@ describe("email", () => {
|
||||
email,
|
||||
},
|
||||
headers: {
|
||||
host: "example.outline.dev",
|
||||
host: `${subdomain}.outline.dev`,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
@@ -203,8 +198,9 @@ describe("email", () => {
|
||||
it("should default to custom domain with SSO", async () => {
|
||||
const spy = jest.spyOn(WelcomeEmail.prototype, "schedule");
|
||||
const email = "sso-user-2@example.org";
|
||||
const domain = faker.internet.domainName();
|
||||
const team = await buildTeam({
|
||||
domain: "docs.mycompany.com",
|
||||
domain,
|
||||
});
|
||||
await buildGuestUser({
|
||||
email,
|
||||
@@ -218,7 +214,7 @@ describe("email", () => {
|
||||
email,
|
||||
},
|
||||
headers: {
|
||||
host: "docs.mycompany.com",
|
||||
host: domain,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
@@ -231,8 +227,9 @@ describe("email", () => {
|
||||
it("should default to custom domain with guest email", async () => {
|
||||
const spy = jest.spyOn(SigninEmail.prototype, "schedule");
|
||||
const email = "guest-user-2@example.org";
|
||||
const domain = faker.internet.domainName();
|
||||
const team = await buildTeam({
|
||||
domain: "docs.mycompany.com",
|
||||
domain,
|
||||
});
|
||||
await buildUser({
|
||||
email,
|
||||
@@ -246,7 +243,7 @@ describe("email", () => {
|
||||
email,
|
||||
},
|
||||
headers: {
|
||||
host: "docs.mycompany.com",
|
||||
host: domain,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import { IntegrationService } from "@shared/types";
|
||||
import env from "@server/env";
|
||||
import { IntegrationAuthentication, SearchQuery } from "@server/models";
|
||||
import { buildDocument, buildIntegration } from "@server/test/factories";
|
||||
import { seed, getTestServer } from "@server/test/support";
|
||||
import {
|
||||
buildDocument,
|
||||
buildIntegration,
|
||||
buildTeam,
|
||||
buildUser,
|
||||
} from "@server/test/factories";
|
||||
import { getTestServer } from "@server/test/support";
|
||||
import * as Slack from "../slack";
|
||||
|
||||
jest.mock("../slack", () => ({
|
||||
@@ -13,7 +18,12 @@ const server = getTestServer();
|
||||
|
||||
describe("#hooks.unfurl", () => {
|
||||
it("should return documents", async () => {
|
||||
const { user, document } = await seed();
|
||||
const user = await buildUser();
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
await IntegrationAuthentication.create({
|
||||
service: IntegrationService.Slack,
|
||||
userId: user.id,
|
||||
@@ -46,7 +56,8 @@ describe("#hooks.unfurl", () => {
|
||||
|
||||
describe("#hooks.slack", () => {
|
||||
it("should return no matches", async () => {
|
||||
const { user, team } = await seed();
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const res = await server.post("/api/hooks.slack", {
|
||||
body: {
|
||||
token: env.SLACK_VERIFICATION_TOKEN,
|
||||
@@ -61,7 +72,8 @@ describe("#hooks.slack", () => {
|
||||
});
|
||||
|
||||
it("should return search results with summary if query is in title", async () => {
|
||||
const { user, team } = await seed();
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const document = await buildDocument({
|
||||
title: "This title contains a search term",
|
||||
userId: user.id,
|
||||
@@ -83,7 +95,8 @@ describe("#hooks.slack", () => {
|
||||
});
|
||||
|
||||
it("should return search results if query is regex-like", async () => {
|
||||
const { user, team } = await seed();
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
await buildDocument({
|
||||
title: "This title contains a search term",
|
||||
userId: user.id,
|
||||
@@ -103,7 +116,8 @@ describe("#hooks.slack", () => {
|
||||
});
|
||||
|
||||
it("should return search results with snippet if query is in text", async () => {
|
||||
const { user, team } = await seed();
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const document = await buildDocument({
|
||||
text: "This title contains a search term",
|
||||
userId: user.id,
|
||||
@@ -127,7 +141,8 @@ describe("#hooks.slack", () => {
|
||||
});
|
||||
|
||||
it("should save search term, hits and source", async () => {
|
||||
const { user, team } = await seed();
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
await server.post("/api/hooks.slack", {
|
||||
body: {
|
||||
token: env.SLACK_VERIFICATION_TOKEN,
|
||||
@@ -143,6 +158,7 @@ describe("#hooks.slack", () => {
|
||||
setTimeout(async () => {
|
||||
const searchQuery = await SearchQuery.findAll({
|
||||
where: {
|
||||
teamId: team.id,
|
||||
query: "contains",
|
||||
},
|
||||
});
|
||||
@@ -150,12 +166,13 @@ describe("#hooks.slack", () => {
|
||||
expect(searchQuery[0].results).toBe(0);
|
||||
expect(searchQuery[0].source).toBe("slack");
|
||||
resolve(undefined);
|
||||
}, 100);
|
||||
}, 250);
|
||||
});
|
||||
});
|
||||
|
||||
it("should respond with help content for help keyword", async () => {
|
||||
const { user, team } = await seed();
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const res = await server.post("/api/hooks.slack", {
|
||||
body: {
|
||||
token: env.SLACK_VERIFICATION_TOKEN,
|
||||
@@ -170,7 +187,8 @@ describe("#hooks.slack", () => {
|
||||
});
|
||||
|
||||
it("should respond with help content for no keyword", async () => {
|
||||
const { user, team } = await seed();
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const res = await server.post("/api/hooks.slack", {
|
||||
body: {
|
||||
token: env.SLACK_VERIFICATION_TOKEN,
|
||||
@@ -185,7 +203,8 @@ describe("#hooks.slack", () => {
|
||||
});
|
||||
|
||||
it("should return search results with snippet for unknown user", async () => {
|
||||
const { user, team } = await seed();
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
// unpublished document will not be returned
|
||||
await buildDocument({
|
||||
text: "This title contains a search term",
|
||||
@@ -217,13 +236,9 @@ describe("#hooks.slack", () => {
|
||||
});
|
||||
|
||||
it("should return search results with snippet for user through integration mapping", async () => {
|
||||
const { user } = await seed();
|
||||
const serviceTeamId = "slack_team_id";
|
||||
await buildIntegration({
|
||||
const user = await buildUser();
|
||||
const integration = await buildIntegration({
|
||||
teamId: user.teamId,
|
||||
settings: {
|
||||
serviceTeamId,
|
||||
},
|
||||
});
|
||||
const document = await buildDocument({
|
||||
text: "This title contains a search term",
|
||||
@@ -234,7 +249,7 @@ describe("#hooks.slack", () => {
|
||||
body: {
|
||||
token: env.SLACK_VERIFICATION_TOKEN,
|
||||
user_id: "unknown-slack-user-id",
|
||||
team_id: serviceTeamId,
|
||||
team_id: (integration.settings as any)?.serviceTeamId,
|
||||
text: "contains",
|
||||
},
|
||||
});
|
||||
@@ -249,7 +264,8 @@ describe("#hooks.slack", () => {
|
||||
});
|
||||
|
||||
it("should error if incorrect verification token", async () => {
|
||||
const { user, team } = await seed();
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const res = await server.post("/api/hooks.slack", {
|
||||
body: {
|
||||
token: "wrong-verification-token",
|
||||
@@ -264,7 +280,8 @@ describe("#hooks.slack", () => {
|
||||
|
||||
describe("#hooks.interactive", () => {
|
||||
it("should respond with replacement message", async () => {
|
||||
const { user, team } = await seed();
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const document = await buildDocument({
|
||||
title: "This title contains a search term",
|
||||
userId: user.id,
|
||||
@@ -294,7 +311,8 @@ describe("#hooks.interactive", () => {
|
||||
});
|
||||
|
||||
it("should respond with replacement message if unknown user", async () => {
|
||||
const { user, team } = await seed();
|
||||
const team = await buildTeam();
|
||||
const user = await buildUser({ teamId: team.id });
|
||||
const document = await buildDocument({
|
||||
title: "This title contains a search term",
|
||||
userId: user.id,
|
||||
@@ -324,7 +342,7 @@ describe("#hooks.interactive", () => {
|
||||
});
|
||||
|
||||
it("should error if incorrect verification token", async () => {
|
||||
const { user } = await seed();
|
||||
const user = await buildUser();
|
||||
const payload = JSON.stringify({
|
||||
type: "message_action",
|
||||
token: "wrong-verification-token",
|
||||
|
||||
Reference in New Issue
Block a user