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.

Setup env variables

Kiran Kc475bb9ca92c
diff --git a/apps/web/tests/links/create-link.test.ts b/apps/web/tests/links/create-link.test.ts
index 78a6e1a692..bd6b0f13ea 100644
--- a/apps/web/tests/links/create-link.test.ts
+++ b/apps/web/tests/links/create-link.test.ts
@@ -5,28 +5,58 @@ import { IntegrationHarness } from "../utils/integration";
test("creates new link", async (ctx) => {
const h = new IntegrationHarness(ctx);
- const { workspace, apiKey } = await h.init();
+ const { workspace, apiKey, user, defaultDomains } = await h.init();
const http = new HttpClient({
- baseUrl: "http://localhost:8888/api",
+ baseUrl: h.baseUrl,
headers: {
Authorization: `Bearer ${apiKey.token}`,
},
});
+
+ const domain = defaultDomains[0];
- const res = await http.post<Link>({
+ const { status, data: link } = await http.post<Link>({
path: "/links",
query: { workspaceId: workspace.workspaceId },
- body: { url: "https://google.com" },
+ body: { url: "https://google.com", domain },
});
- expect(res.status).toEqual(200);
- expect(res.data).toMatchObject({
+ expect(status).toEqual(200);
+ expect(link).toMatchObject({
+ id: expect.any(String),
+ domain,
+ key: expect.any(String),
url: "https://google.com",
- domain: "kiran.localhost",
- shortLink: `https://kiran.localhost/${res.data.key}`,
- archived: false,
- workspaceId: workspace.workspaceId,
+ userId: user.id,
projectId: workspace.id,
+ workspaceId: workspace.workspaceId,
+ archived: false,
+ expiresAt: null,
+ password: null,
+ proxy: false,
+ title: null,
+ description: null,
+ image: null,
+ utm_source: null,
+ utm_medium: null,
+ utm_campaign: null,
+ utm_term: null,
+ utm_content: null,
+ rewrite: false,
+ ios: null,
+ android: null,
+ geo: null,
+ publicStats: false,
+ clicks: 0,
+ lastClicked: null,
+ checkDisabled: false,
+ tagId: null,
+ comments: null,
+ tags: [],
+ createdAt: expect.any(String),
+ updatedAt: expect.any(String),
+ shortLink: `${domain}/${link.key}`,
+ qrCode: `https://api.dub.co/qr?url=${domain}/${link.key}`,
});
});
diff --git a/apps/web/tests/links/list-links.test.ts b/apps/web/tests/links/list-links.test.ts
index 342fa0f622..18758ad2dd 100644
--- a/apps/web/tests/links/list-links.test.ts
+++ b/apps/web/tests/links/list-links.test.ts
@@ -3,60 +3,56 @@ import { HttpClient } from "../utils/http";
import { Link } from "@prisma/client";
import { IntegrationHarness } from "../utils/integration";
-test(
- "list links",
- async (ctx) => {
- const h = new IntegrationHarness(ctx);
- const { workspace, apiKey } = await h.init();
+test("list links", async (ctx) => {
+ const h = new IntegrationHarness(ctx);
+ const { workspace, apiKey } = await h.init();
- const http = new HttpClient({
- baseUrl: "http://localhost:8888/api",
- headers: {
- Authorization: `Bearer ${apiKey.token}`,
- },
- });
+ const http = new HttpClient({
+ baseUrl: h.baseUrl,
+ headers: {
+ Authorization: `Bearer ${apiKey.token}`,
+ },
+ });
- // Create a link
- const { data: firstLink } = await http.post<Link>({
- path: "/links",
- query: { workspaceId: workspace.workspaceId },
- body: { url: "https://google.com" },
- });
+ // Create a link
+ const { data: firstLink } = await http.post<Link>({
+ path: "/links",
+ query: { workspaceId: workspace.workspaceId },
+ body: { url: "https://google.com" },
+ });
- // Fake delay
- await new Promise((resolve) => setTimeout(resolve, 2000));
+ // Fake delay
+ await new Promise((resolve) => setTimeout(resolve, 1000));
- // Create another link
- const { data: secondLink } = await http.post<Link>({
- path: "/links",
- query: { workspaceId: workspace.workspaceId },
- body: { url: "https://google.uk" },
- });
+ // Create another link
+ const { data: secondLink } = await http.post<Link>({
+ path: "/links",
+ query: { workspaceId: workspace.workspaceId },
+ body: { url: "https://google.uk" },
+ });
- // List links
- const {data: links, status} = await http.get<Link[]>({
- path: "/links",
- query: { workspaceId: workspace.workspaceId },
- });
+ // List links
+ const { data: links, status } = await http.get<Link[]>({
+ path: "/links",
+ query: { workspaceId: workspace.workspaceId },
+ });
- expect(status).toEqual(200);
- expect(links.length).toEqual(2);
- // expect(links[0]).toMatchObject({
- // url: "https://google.uk",
- // domain: "kiran.localhost",
- // shortLink: `https://kiran.localhost/${firstLink.key}`,
- // archived: false,
- // // workspaceId: workspace.workspaceId,
- // projectId: workspace.id,
- // });
- // expect(links[1]).toMatchObject({
- // url: "https://google.com",
- // domain: "kiran.localhost",
- // shortLink: `https://kiran.localhost/${secondLink.key}`,
- // archived: false,
- // // workspaceId: workspace.workspaceId,
- // projectId: workspace.id,
- // });
- },
- { timeout: 10000 },
-);
+ expect(status).toEqual(200);
+ expect(links.length).toEqual(2);
+ // expect(links[0]).toMatchObject({
+ // url: "https://google.uk",
+ // domain: "kiran.localhost",
+ // shortLink: `https://kiran.localhost/${firstLink.key}`,
+ // archived: false,
+ // // workspaceId: workspace.workspaceId,
+ // projectId: workspace.id,
+ // });
+ // expect(links[1]).toMatchObject({
+ // url: "https://google.com",
+ // domain: "kiran.localhost",
+ // shortLink: `https://kiran.localhost/${secondLink.key}`,
+ // archived: false,
+ // // workspaceId: workspace.workspaceId,
+ // projectId: workspace.id,
+ // });
+});
diff --git a/apps/web/tests/utils/env.ts b/apps/web/tests/utils/env.ts
new file mode 100644
index 0000000000..800ae24d35
--- /dev/null
+++ b/apps/web/tests/utils/env.ts
@@ -0,0 +1,5 @@
+import { z } from "zod";
+
+export const integrationTestEnv = z.object({
+ API_BASE_URL: z.string().url().default("http://api.localhost:8888"),
+});
diff --git a/apps/web/tests/utils/integration.ts b/apps/web/tests/utils/integration.ts
index d0cdd7f611..3d70f7c45a 100644
--- a/apps/web/tests/utils/integration.ts
+++ b/apps/web/tests/utils/integration.ts
@@ -2,27 +2,32 @@ import { hashToken } from "@/lib/auth";
import prisma from "@/lib/prisma";
import { nanoid } from "@dub/utils";
import { Project, Token, User } from "@prisma/client";
-import { onTestFinished, test } from "vitest";
import type { TaskContext } from "vitest";
+import { integrationTestEnv } from "./env";
+import { DUB_DOMAINS_ARRAY } from "@dub/utils";
interface Resources {
user: User;
workspace: Project & { workspaceId: string };
apiKey: Token & { token: string };
+ defaultDomains: string[];
}
export class IntegrationHarness {
private readonly ctx: TaskContext;
public resources: Resources;
+ public baseUrl: string;
constructor(ctx: TaskContext) {
- this.ctx = ctx
+ const env = integrationTestEnv.parse(process.env);
+
+ this.ctx = ctx;
+ this.baseUrl = env.API_BASE_URL;
}
async init() {
- const resources = await this.seed();
- this.resources = resources;
- return resources;
+ this.resources = await this.seed();
+ return this.resources;
}
async seed() {
@@ -76,10 +81,34 @@ export class IntegrationHarness {
},
});
+ // Default domains for the workspace
+ const defaultDomains = await prisma.defaultDomains.findUniqueOrThrow({
+ where: {
+ projectId: workspace.id,
+ },
+ select: {
+ dubsh: true,
+ chatgpt: true,
+ sptifi: true,
+ gitnew: true,
+ amznid: true,
+ loooooooong: true,
+ },
+ });
+
+ const defaultDomainsArray = Object.keys(defaultDomains)
+ .filter((key) => defaultDomains[key])
+ .map((domain) =>
+ DUB_DOMAINS_ARRAY.find((d) => d.replace(".", "") === domain),
+ ) as string[];
+
+ console.log(DUB_DOMAINS_ARRAY);
+
const resource = {
user,
workspace: { ...workspace, workspaceId: `ws_${workspace.id}` },
apiKey: { ...apiKey, token },
+ defaultDomains: defaultDomainsArray,
} as const;
return resource;
diff --git a/apps/web/vitest.config.ts b/apps/web/vitest.config.ts
index 1296293160..0d82a4a8d8 100644
--- a/apps/web/vitest.config.ts
+++ b/apps/web/vitest.config.ts
@@ -6,8 +6,11 @@ export default defineConfig({
test: {
dir: "./tests",
reporters: ["verbose"],
- // outputFile: "./.vitest/html",
globals: true,
setupFiles: ["./tests/setup.ts"],
+ testTimeout: 10000,
+ env: {
+ NEXT_PUBLIC_IS_DUB: "true",
+ },
},
});