mirror of
https://github.com/outline/outline.git
synced 2026-11-22 14:01:41 +03:00
* fix: Strip top-level domain from generated team subdomain * Move subdomain generation into Team.findAvailableSubdomain and reuse in installation
177 lines
6.0 KiB
TypeScript
177 lines
6.0 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { Team } from "@server/models";
|
|
import {
|
|
buildTeam,
|
|
buildCollection,
|
|
buildAttachment,
|
|
buildSubdomain,
|
|
} from "@server/test/factories";
|
|
|
|
describe("Team", () => {
|
|
describe("findByDomain", () => {
|
|
it("should find a team by its domain", async () => {
|
|
const domain = `${randomUUID()}.example.com`;
|
|
const team = await buildTeam({ domain });
|
|
const result = await Team.findByDomain(domain);
|
|
expect(result?.id).toEqual(team.id);
|
|
});
|
|
|
|
it("should normalize domain to lowercase", async () => {
|
|
const id = randomUUID().split("-")[0];
|
|
const team = await buildTeam({ domain: `${id}.example.com` });
|
|
const result = await Team.findByDomain(`${id}.Example.COM`);
|
|
expect(result?.id).toEqual(team.id);
|
|
});
|
|
|
|
it("should strip protocol from input", async () => {
|
|
const domain = `${randomUUID()}.example.com`;
|
|
const team = await buildTeam({ domain });
|
|
const result = await Team.findByDomain(`https://${domain}`);
|
|
expect(result?.id).toEqual(team.id);
|
|
});
|
|
|
|
it("should strip port from input", async () => {
|
|
const domain = `${randomUUID()}.example.com`;
|
|
const team = await buildTeam({ domain });
|
|
const result = await Team.findByDomain(`${domain}:3000`);
|
|
expect(result?.id).toEqual(team.id);
|
|
});
|
|
|
|
it("should strip path from input", async () => {
|
|
const domain = `${randomUUID()}.example.com`;
|
|
const team = await buildTeam({ domain });
|
|
const result = await Team.findByDomain(`${domain}/some/path`);
|
|
expect(result?.id).toEqual(team.id);
|
|
});
|
|
|
|
it("should return null for unregistered domain", async () => {
|
|
const result = await Team.findByDomain("unknown.example.com");
|
|
expect(result).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe("findAvailableSubdomain", () => {
|
|
it("should slugify the requested value", async () => {
|
|
const slug = buildSubdomain();
|
|
const subdomain = await Team.findAvailableSubdomain(
|
|
`${slug.toUpperCase()} Inc`
|
|
);
|
|
expect(subdomain).toEqual(`${slug}-inc`);
|
|
});
|
|
|
|
it("should strip a trailing top-level domain", async () => {
|
|
const slug = buildSubdomain();
|
|
expect(await Team.findAvailableSubdomain(`${slug}.com`)).toEqual(slug);
|
|
expect(await Team.findAvailableSubdomain(`${slug} .com`)).toEqual(slug);
|
|
expect(await Team.findAvailableSubdomain(`${slug}.io`)).toEqual(slug);
|
|
});
|
|
|
|
it("should fall back to a default when too short or reserved", async () => {
|
|
expect(await Team.findAvailableSubdomain("ab")).toMatch(/^team\d*$/);
|
|
expect(await Team.findAvailableSubdomain("www")).toMatch(/^team\d*$/);
|
|
});
|
|
|
|
it("should append a number when the subdomain is taken", async () => {
|
|
const slug = buildSubdomain();
|
|
await buildTeam({ subdomain: slug });
|
|
expect(await Team.findAvailableSubdomain(slug)).toEqual(`${slug}1`);
|
|
});
|
|
});
|
|
|
|
describe("collectionIds", () => {
|
|
it("should return non-private collection ids", async () => {
|
|
const team = await buildTeam();
|
|
const collection = await buildCollection({
|
|
teamId: team.id,
|
|
});
|
|
// build a collection in another team
|
|
await buildCollection();
|
|
// build a private collection
|
|
await buildCollection({
|
|
teamId: team.id,
|
|
permission: null,
|
|
});
|
|
const response = await team.collectionIds();
|
|
expect(response.length).toEqual(1);
|
|
expect(response[0]).toEqual(collection.id);
|
|
});
|
|
});
|
|
|
|
describe("previousSubdomains", () => {
|
|
it("should list the previous subdomains", async () => {
|
|
const id = randomUUID();
|
|
const originalSubdomain = `example-${id}`;
|
|
const subdomain = `updated-${id}`;
|
|
const subdomain2 = `another-${id}`;
|
|
const team = await buildTeam({
|
|
subdomain: originalSubdomain,
|
|
});
|
|
|
|
await team.update({ subdomain });
|
|
expect(team.subdomain).toEqual(subdomain);
|
|
expect(team.previousSubdomains?.length).toEqual(1);
|
|
expect(team.previousSubdomains?.[0]).toEqual(originalSubdomain);
|
|
|
|
await team.update({ subdomain: subdomain2 });
|
|
expect(team.subdomain).toEqual(subdomain2);
|
|
expect(team.previousSubdomains?.length).toEqual(2);
|
|
expect(team.previousSubdomains?.[0]).toEqual(originalSubdomain);
|
|
expect(team.previousSubdomains?.[1]).toEqual(subdomain);
|
|
});
|
|
});
|
|
|
|
describe("publicAvatarUrl", () => {
|
|
it("should return null when no avatarUrl is set", async () => {
|
|
const team = await buildTeam({ avatarUrl: null });
|
|
const result = await team.publicAvatarUrl();
|
|
expect(result).toBeNull();
|
|
});
|
|
|
|
it("should return external URL unchanged", async () => {
|
|
const url = "https://example.com/logo.png";
|
|
const team = await buildTeam({ avatarUrl: url });
|
|
const result = await team.publicAvatarUrl();
|
|
expect(result).toEqual(url);
|
|
});
|
|
|
|
it("should return signed URL for private-bucket attachment redirect", async () => {
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(new Date("2026-04-16T00:00:00.000Z"));
|
|
try {
|
|
const team = await buildTeam();
|
|
const attachment = await buildAttachment({
|
|
teamId: team.id,
|
|
acl: "private",
|
|
});
|
|
|
|
await team.update({
|
|
avatarUrl: `/api/attachments.redirect?id=${attachment.id}`,
|
|
});
|
|
|
|
const result = await team.publicAvatarUrl();
|
|
expect(result).toEqual(await attachment.signedUrl);
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|
|
|
|
it("should return canonical URL for public-bucket attachment redirect", async () => {
|
|
const team = await buildTeam();
|
|
const id = randomUUID();
|
|
const attachment = await buildAttachment({
|
|
id,
|
|
teamId: team.id,
|
|
key: `avatars/${team.id}/${id}/logo.png`,
|
|
acl: "public-read",
|
|
});
|
|
|
|
await team.update({
|
|
avatarUrl: `/api/attachments.redirect?id=${attachment.id}`,
|
|
});
|
|
|
|
const result = await team.publicAvatarUrl();
|
|
expect(result).toEqual(attachment.canonicalUrl);
|
|
});
|
|
});
|
|
});
|