mirror of
https://github.com/outline/outline.git
synced 2026-11-28 16:19:46 +03:00
* fix: Match existing users by case-insensitive email on invite * test: Cover case-insensitive User.findByEmail lookup
253 lines
6.5 KiB
TypeScript
253 lines
6.5 KiB
TypeScript
import { faker } from "@faker-js/faker";
|
|
import { Plan, UserRole } from "@shared/types";
|
|
import { buildAdmin, buildTeam, buildUser } from "@server/test/factories";
|
|
import userInviter from "./userInviter";
|
|
import { withContext } from "@server/test/support";
|
|
import { Team, TeamDomain } from "@server/models";
|
|
|
|
describe("userInviter", () => {
|
|
it("should return sent invites", async () => {
|
|
const user = await buildUser();
|
|
const response = await withContext(user, (ctx) =>
|
|
userInviter(ctx, {
|
|
invites: [
|
|
{
|
|
role: UserRole.Member,
|
|
email: faker.internet.email(),
|
|
name: "Test",
|
|
},
|
|
],
|
|
})
|
|
);
|
|
expect(response.sent.length).toEqual(1);
|
|
});
|
|
|
|
it("should filter empty invites", async () => {
|
|
const user = await buildUser();
|
|
const response = await withContext(user, (ctx) =>
|
|
userInviter(ctx, {
|
|
invites: [
|
|
{
|
|
role: UserRole.Member,
|
|
email: " ",
|
|
name: "Test",
|
|
},
|
|
],
|
|
})
|
|
);
|
|
expect(response.sent.length).toEqual(0);
|
|
});
|
|
|
|
it("should error on non allowed domains", async () => {
|
|
const team = await buildTeam();
|
|
const user = await buildUser({ teamId: team.id });
|
|
|
|
await TeamDomain.create({
|
|
teamId: team.id,
|
|
name: faker.internet.domainName(),
|
|
createdById: user.id,
|
|
});
|
|
|
|
await withContext(user, (ctx) =>
|
|
expect(
|
|
userInviter(ctx, {
|
|
invites: [
|
|
{
|
|
role: UserRole.Member,
|
|
email: "test@example.com",
|
|
name: "Test",
|
|
},
|
|
],
|
|
})
|
|
).rejects.toThrow("The domain is not allowed for this workspace")
|
|
);
|
|
});
|
|
|
|
it("should allow invites for allowed domains", async () => {
|
|
const team = await buildTeam();
|
|
const user = await buildUser({ teamId: team.id });
|
|
const allowedDomain = "google.com";
|
|
|
|
await TeamDomain.create({
|
|
teamId: team.id,
|
|
name: allowedDomain,
|
|
createdById: user.id,
|
|
});
|
|
|
|
const response = await withContext(user, (ctx) =>
|
|
userInviter(ctx, {
|
|
invites: [
|
|
{
|
|
role: UserRole.Member,
|
|
email: `test@${allowedDomain}`,
|
|
name: "Test User",
|
|
},
|
|
],
|
|
})
|
|
);
|
|
|
|
expect(response.sent.length).toEqual(1);
|
|
expect(response.sent[0].email).toEqual(`test@${allowedDomain}`);
|
|
});
|
|
|
|
it("should error on guest invites for workspace domains", async () => {
|
|
const planSpy = vi
|
|
.spyOn(Team.prototype, "plan", "get")
|
|
.mockReturnValue(Plan.Business);
|
|
onTestFinished(() => planSpy.mockRestore());
|
|
const team = await buildTeam();
|
|
const user = await buildAdmin({ teamId: team.id });
|
|
|
|
await TeamDomain.create({
|
|
teamId: team.id,
|
|
name: "acme.org",
|
|
createdById: user.id,
|
|
});
|
|
|
|
await withContext(user, (ctx) =>
|
|
expect(
|
|
userInviter(ctx, {
|
|
invites: [
|
|
{
|
|
role: UserRole.Guest,
|
|
email: "Test@Acme.org",
|
|
name: "Test",
|
|
},
|
|
],
|
|
})
|
|
).rejects.toThrow(
|
|
"Users with an email on a workspace domain cannot be added as guests"
|
|
)
|
|
);
|
|
});
|
|
|
|
it("should skip guest invites for existing members on workspace domains", async () => {
|
|
const planSpy = vi
|
|
.spyOn(Team.prototype, "plan", "get")
|
|
.mockReturnValue(Plan.Business);
|
|
onTestFinished(() => planSpy.mockRestore());
|
|
const team = await buildTeam();
|
|
const user = await buildAdmin({ teamId: team.id });
|
|
await buildUser({ teamId: team.id, email: "member@acme.org" });
|
|
|
|
await TeamDomain.create({
|
|
teamId: team.id,
|
|
name: "acme.org",
|
|
createdById: user.id,
|
|
});
|
|
|
|
const response = await withContext(user, (ctx) =>
|
|
userInviter(ctx, {
|
|
invites: [
|
|
{
|
|
role: UserRole.Guest,
|
|
email: "member@acme.org",
|
|
name: "Test",
|
|
},
|
|
],
|
|
})
|
|
);
|
|
expect(response.unsent.length).toEqual(1);
|
|
});
|
|
|
|
it("should allow guest invites for other domains", async () => {
|
|
const planSpy = vi
|
|
.spyOn(Team.prototype, "plan", "get")
|
|
.mockReturnValue(Plan.Business);
|
|
onTestFinished(() => planSpy.mockRestore());
|
|
const team = await buildTeam();
|
|
const user = await buildAdmin({ teamId: team.id });
|
|
|
|
await TeamDomain.create({
|
|
teamId: team.id,
|
|
name: "acme.org",
|
|
createdById: user.id,
|
|
});
|
|
|
|
const response = await withContext(user, (ctx) =>
|
|
userInviter(ctx, {
|
|
invites: [
|
|
{
|
|
role: UserRole.Guest,
|
|
email: "test@other.com",
|
|
name: "Test",
|
|
},
|
|
],
|
|
})
|
|
);
|
|
expect(response.sent.length).toEqual(1);
|
|
});
|
|
|
|
it("should filter obviously bunk emails", async () => {
|
|
const user = await buildUser();
|
|
const response = await withContext(user, (ctx) =>
|
|
userInviter(ctx, {
|
|
invites: [
|
|
{
|
|
role: UserRole.Member,
|
|
email: "notanemail",
|
|
name: "Test",
|
|
},
|
|
],
|
|
})
|
|
);
|
|
expect(response.sent.length).toEqual(0);
|
|
});
|
|
|
|
it("should not send duplicates", async () => {
|
|
const user = await buildUser();
|
|
const response = await withContext(user, (ctx) =>
|
|
userInviter(ctx, {
|
|
invites: [
|
|
{
|
|
role: UserRole.Member,
|
|
email: "the@same.com",
|
|
name: "Test",
|
|
},
|
|
{
|
|
role: UserRole.Member,
|
|
email: "the@SAME.COM",
|
|
name: "Test",
|
|
},
|
|
],
|
|
})
|
|
);
|
|
expect(response.sent.length).toEqual(1);
|
|
});
|
|
|
|
it("should not send invites to existing team members", async () => {
|
|
const email = faker.internet.email().toLowerCase();
|
|
const user = await buildUser({ email });
|
|
const response = await withContext(user, (ctx) =>
|
|
userInviter(ctx, {
|
|
invites: [
|
|
{
|
|
role: UserRole.Member,
|
|
email,
|
|
name: user.name,
|
|
},
|
|
],
|
|
})
|
|
);
|
|
expect(response.sent.length).toEqual(0);
|
|
});
|
|
|
|
it("should not send invites to existing team members with different email case", async () => {
|
|
const email = faker.internet.email().toLowerCase();
|
|
const user = await buildUser({ email: email.toUpperCase() });
|
|
const response = await withContext(user, (ctx) =>
|
|
userInviter(ctx, {
|
|
invites: [
|
|
{
|
|
role: UserRole.Member,
|
|
email,
|
|
name: user.name,
|
|
},
|
|
],
|
|
})
|
|
);
|
|
expect(response.sent.length).toEqual(0);
|
|
expect(response.unsent.length).toEqual(1);
|
|
});
|
|
});
|