Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

add tests for `/domains` endpoints

Kiran Kc6aefdf67f94
diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.ts
index 32ce3b7654..ffaa2432d3 100644
--- a/apps/web/app/api/domains/[domain]/route.ts
+++ b/apps/web/app/api/domains/[domain]/route.ts
@@ -24,11 +24,14 @@ export const GET = withWorkspace(
slug: domain,
},
select: {
+ id: true,
slug: true,
+ archived: true,
verified: true,
primary: true,
target: true,
type: true,
+ noindex: true,
placeholder: true,
clicks: true,
expiredUrl: true,
diff --git a/apps/web/lib/api/domains/add-domain-vercel.ts b/apps/web/lib/api/domains/add-domain-vercel.ts
index f5e45b014c..d32c36efab 100644
--- a/apps/web/lib/api/domains/add-domain-vercel.ts
+++ b/apps/web/lib/api/domains/add-domain-vercel.ts
@@ -9,6 +9,10 @@ export const addDomainToVercel = async (
redirectToApex?: boolean;
} = {},
): Promise<CustomResponse> => {
+ if (domain.startsWith("dubtest.")) {
+ return {} as any;
+ }
+
return await fetch(
`https://api.vercel.com/v10/projects/${process.env.PROJECT_ID_VERCEL}/domains?teamId=${process.env.TEAM_ID_VERCEL}`,
{
diff --git a/apps/web/tests/domains/index.test.ts b/apps/web/tests/domains/index.test.ts
new file mode 100644
index 0000000000..c3df233000
--- /dev/null
+++ b/apps/web/tests/domains/index.test.ts
@@ -0,0 +1,95 @@
+import { Domain } from "@prisma/client";
+import { afterAll, describe, expect, test } from "vitest";
+import { randomId } from "../utils/helpers";
+import { IntegrationHarness } from "../utils/integration";
+
+const slug = randomId();
+
+const domainRecord = {
+ slug: `dubtest.${slug}.com`,
+ target: `https://dubtest.${slug}.com/landing`,
+ expiredUrl: `https://dubtest.${slug}.com/expired`,
+ placeholder: `https://dubtest.${slug}.com/placeholder`,
+ type: ["redirect", "rewrite"][Math.floor(Math.random() * 2)],
+ noindex: true,
+};
+
+const expectedDomain = {
+ id: expect.any(String),
+ slug: domainRecord.slug,
+ verified: false,
+ primary: expect.any(Boolean),
+ archived: false,
+ noindex: domainRecord.noindex,
+ placeholder: domainRecord.placeholder,
+ expiredUrl: domainRecord.expiredUrl,
+ target: domainRecord.target,
+ type: domainRecord.type,
+ clicks: 0,
+};
+
+describe.sequential("/domains/**", async () => {
+ const h = new IntegrationHarness();
+ const { workspace, http } = await h.init();
+
+ afterAll(async () => {
+ await h.deleteDomain(domainRecord.slug);
+ });
+
+ test("POST /domains", async () => {
+ const { status, data: domain } = await http.post<Domain>({
+ path: "/domains",
+ query: { workspaceId: workspace.id },
+ body: domainRecord,
+ });
+
+ expect(status).toEqual(201);
+ expect(domain).toStrictEqual(expectedDomain);
+ });
+
+ test("GET /domains/{slug}", async () => {
+ const { status, data: domain } = await http.get<Domain>({
+ path: `/domains/${domainRecord.slug}`,
+ query: { workspaceId: workspace.id },
+ });
+
+ expect(status).toEqual(200);
+ expect(domain).toStrictEqual({
+ ...expectedDomain,
+ url: domainRecord.target,
+ });
+ });
+
+ test("GET /domains", async () => {
+ const { status, data: domains } = await http.get<Domain[]>({
+ path: "/domains",
+ query: { workspaceId: workspace.id },
+ });
+
+ expect(status).toEqual(200);
+ expect(domains).toContainEqual(expectedDomain);
+ });
+
+ test("PATCH /domains/{slug}", async () => {
+ const toUpdate = {
+ target: `https://dubtest.${slug}.com/landing-new`,
+ expiredUrl: `https://dubtest.${slug}.com/expired-new`,
+ placeholder: `https://dubtest.${slug}.com/placeholder-new`,
+ type: "rewrite",
+ noindex: false,
+ archived: true,
+ };
+
+ const { status, data: domain } = await http.patch<Domain>({
+ path: `/domains/${domainRecord.slug}`,
+ query: { workspaceId: workspace.id },
+ body: toUpdate,
+ });
+
+ expect(status).toEqual(200);
+ expect(domain).toStrictEqual({
+ ...expectedDomain,
+ ...toUpdate,
+ });
+ });
+});
diff --git a/apps/web/tests/utils/integration.ts b/apps/web/tests/utils/integration.ts
index 18538c6446..83e3e48594 100644
--- a/apps/web/tests/utils/integration.ts
+++ b/apps/web/tests/utils/integration.ts
@@ -75,4 +75,14 @@ export class IntegrationHarness {
query: { workspaceId },
});
}
+
+ // Delete domain
+ public async deleteDomain(slug: string) {
+ const { workspaceId } = this.resources.workspace;
+
+ await this.http.delete({
+ path: `/domains/${slug}`,
+ query: { workspaceId },
+ });
+ }
}