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 track/click tests

Steven Teyce0cc037924a
diff --git a/apps/web/tests/tracks/track-click.test.ts b/apps/web/tests/tracks/track-click.test.ts
new file mode 100644
index 0000000000..c9db578e70
--- /dev/null
+++ b/apps/web/tests/tracks/track-click.test.ts
@@ -0,0 +1,234 @@
+import { describe, expect, test } from "vitest";
+import { IntegrationHarness } from "../utils/integration";
+import { E2E_LINK } from "../utils/resource";
+
+// Helper function to verify click tracking response
+const expectValidClickResponse = ({
+ response,
+ clickId,
+ hasPartner = false,
+ hasDiscount = false,
+}: {
+ response: { status: number; data: any };
+ clickId: string;
+ hasPartner?: boolean;
+ hasDiscount?: boolean;
+}) => {
+ expect(response.status).toEqual(200);
+ expect(response.data).toStrictEqual({
+ clickId,
+ ...(hasPartner && {
+ partner: expect.objectContaining({
+ id: expect.any(String),
+ name: expect.any(String),
+ image: expect.any(String),
+ }),
+ }),
+ ...(hasDiscount && {
+ discount: expect.objectContaining({
+ id: expect.any(String),
+ amount: expect.any(Number),
+ type: expect.any(String),
+ maxDuration: expect.any(Number),
+ couponId: expect.any(String),
+ couponTestId: expect.any(String),
+ }),
+ }),
+ });
+};
+
+const TRACK_CLICK_HEADERS = {
+ referer: "https://dub.co",
+ "User-Agent":
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
+};
+
+describe("POST /track/click", async () => {
+ const h = new IntegrationHarness();
+ const { http } = await h.init();
+
+ test("track a basic click", async () => {
+ const response = await http.post<{ clickId: string }>({
+ path: "/track/click",
+ headers: TRACK_CLICK_HEADERS,
+ body: {
+ domain: E2E_LINK.domain,
+ key: E2E_LINK.key,
+ },
+ });
+
+ expectValidClickResponse({
+ response,
+ clickId: response.data.clickId,
+ });
+ });
+
+ test("same clickId should be returned on subsequent requests", async () => {
+ const response1 = await http.post<{ clickId: string }>({
+ path: "/track/click",
+ headers: TRACK_CLICK_HEADERS,
+ body: {
+ domain: E2E_LINK.domain,
+ key: E2E_LINK.key,
+ },
+ });
+
+ const response2 = await http.post<{ clickId: string }>({
+ path: "/track/click",
+ headers: TRACK_CLICK_HEADERS,
+ body: {
+ domain: E2E_LINK.domain,
+ key: E2E_LINK.key,
+ },
+ });
+
+ expect(response1.data.clickId).toEqual(response2.data.clickId);
+ });
+
+ test("partner link should return partner data", async () => {
+ const clickResponse = await http.post<{ clickId: string }>({
+ path: "/track/click",
+ headers: TRACK_CLICK_HEADERS,
+ body: {
+ domain: "getacme.link",
+ key: "derek",
+ },
+ });
+
+ expect(clickResponse.status).toEqual(200);
+ expectValidClickResponse({
+ response: clickResponse,
+ clickId: clickResponse.data.clickId,
+ hasPartner: true,
+ hasDiscount: true,
+ });
+ });
+
+ test("missing domain should return validation error", async () => {
+ const response = await http.post({
+ path: "/track/click",
+ headers: TRACK_CLICK_HEADERS,
+ body: {
+ key: E2E_LINK.key,
+ },
+ });
+
+ expect(response.status).toEqual(422);
+ expect(response.data).toStrictEqual({
+ error: {
+ code: "unprocessable_entity",
+ message: "invalid_type: domain: domain is required.",
+ doc_url:
+ "https://dub.co/docs/api-reference/errors#unprocessable-entity",
+ },
+ });
+ });
+
+ test("missing key should return validation error", async () => {
+ const response = await http.post({
+ path: "/track/click",
+ headers: TRACK_CLICK_HEADERS,
+ body: {
+ domain: E2E_LINK.domain,
+ },
+ });
+
+ expect(response.status).toEqual(422);
+ expect(response.data).toStrictEqual({
+ error: {
+ code: "unprocessable_entity",
+ message: "invalid_type: key: key is required.",
+ doc_url:
+ "https://dub.co/docs/api-reference/errors#unprocessable-entity",
+ },
+ });
+ });
+
+ test("non-existent link should return not found error", async () => {
+ const response = await http.post({
+ path: "/track/click",
+ headers: TRACK_CLICK_HEADERS,
+ body: {
+ domain: E2E_LINK.domain,
+ key: "non-existent-key",
+ },
+ });
+
+ expect(response.status).toEqual(404);
+ expect(response.data).toStrictEqual({
+ error: {
+ code: "not_found",
+ message: `Link not found for domain: ${E2E_LINK.domain} and key: non-existent-key.`,
+ doc_url: "https://dub.co/docs/api-reference/errors#not-found",
+ },
+ });
+ });
+
+ // TODO: add this back when we have a way to return error even when clickId is cached
+ // test("Request origin not part of the allowed hostnames list should return forbidden error", async () => {
+ // const response = await http.post({
+ // path: "/track/click",
+ // headers: {
+ // ...TRACK_CLICK_HEADERS,
+ // referer: "https://not-allowed.com",
+ // },
+ // body: {
+ // domain: E2E_LINK.domain,
+ // key: E2E_LINK.key,
+ // },
+ // });
+
+ // expect(response.status).toEqual(403);
+ // expect(response.data).toStrictEqual({
+ // error: {
+ // code: "forbidden",
+ // message: `Request origin 'not-allowed.com' is not included in the allowed hostnames for this workspace (${E2E_LINK.domain}). Update your allowed hostnames here: https://app.dub.co/settings/analytics`,
+ // doc_url: "https://dub.co/docs/api-reference/errors#forbidden",
+ // },
+ // });
+ // });
+
+ test("OPTIONS request should return CORS headers", async () => {
+ const response = await fetch(`${h.baseUrl}/api/track/click`, {
+ method: "OPTIONS",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ });
+
+ expect(response.status).toEqual(204);
+ expect(response.headers.get("access-control-allow-origin")).toBe("*");
+ expect(response.headers.get("access-control-allow-methods")).toBe(
+ "POST, OPTIONS",
+ );
+ expect(response.headers.get("access-control-allow-headers")).toBe(
+ "Content-Type, Authorization",
+ );
+ });
+
+ test("POST request should return CORS headers", async () => {
+ const response = await http.post<{ clickId: string }>({
+ path: "/track/click",
+ headers: TRACK_CLICK_HEADERS,
+ body: {
+ domain: E2E_LINK.domain,
+ key: E2E_LINK.key,
+ },
+ });
+
+ expect(response.status).toEqual(200);
+ });
+
+ test("clickId should be a valid string", async () => {
+ const response = await http.post<{ clickId: string }>({
+ path: "/track/click",
+ headers: TRACK_CLICK_HEADERS,
+ body: {
+ domain: E2E_LINK.domain,
+ key: E2E_LINK.key,
+ },
+ });
+
+ expect(response.data.clickId).toMatch(/^[a-zA-Z0-9]{16}$/);
+ });
+});
diff --git a/apps/web/tests/tracks/track-lead.test.ts b/apps/web/tests/tracks/track-lead.test.ts
index 45ed00006f..9fa2e6df22 100644
--- a/apps/web/tests/tracks/track-lead.test.ts
+++ b/apps/web/tests/tracks/track-lead.test.ts
@@ -1,22 +1,26 @@
import { TrackLeadResponse, TrackSaleResponse } from "@/lib/types";
import { randomCustomer } from "tests/utils/helpers";
-import { E2E_CLICK_ID } from "tests/utils/resource";
import { describe, expect, test } from "vitest";
import { IntegrationHarness } from "../utils/integration";
// Helper function to verify lead tracking response
-const expectValidLeadResponse = (
- response: { status: number; data: TrackLeadResponse },
- customer: any,
-) => {
+const expectValidLeadResponse = ({
+ response,
+ customer,
+ clickId,
+}: {
+ response: { status: number; data: TrackLeadResponse };
+ customer: any;
+ clickId: string;
+}) => {
expect(response.status).toEqual(200);
expect(response.data).toStrictEqual({
- clickId: E2E_CLICK_ID,
+ clickId,
customerName: customer.name,
customerEmail: customer.email,
customerAvatar: customer.avatar,
click: {
- id: E2E_CLICK_ID,
+ id: clickId,
},
customer,
});
@@ -26,12 +30,31 @@ describe("POST /track/lead", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();
+ const clickResponse = await http.post<{ clickId: string }>({
+ path: "/track/click",
+ headers: {
+ referer: "https://dub.co",
+ "User-Agent":
+ "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36",
+ },
+ body: {
+ domain: "getacme.link",
+ key: "derek",
+ },
+ });
+
+ expect(clickResponse.status).toEqual(200);
+ expect(clickResponse.data.clickId).toStrictEqual(expect.any(String));
+
+ const trackedClickId = clickResponse.data.clickId;
+
const customer1 = randomCustomer();
+
test("track a lead", async () => {
const response = await http.post<TrackLeadResponse>({
path: "/track/lead",
body: {
- clickId: E2E_CLICK_ID,
+ clickId: trackedClickId,
eventName: "Signup",
externalId: customer1.externalId,
customerName: customer1.name,
@@ -40,14 +63,18 @@ describe("POST /track/lead", async () => {
},
});
- expectValidLeadResponse(response, customer1);
+ expectValidLeadResponse({
+ response,
+ customer: customer1,
+ clickId: trackedClickId,
+ });
});
test("duplicate request with same externalId", async () => {
const response = await http.post<TrackLeadResponse>({
path: "/track/lead",
body: {
- clickId: E2E_CLICK_ID,
+ clickId: trackedClickId,
eventName: "Signup",
externalId: customer1.externalId,
customerName: customer1.name,
@@ -57,7 +84,11 @@ describe("POST /track/lead", async () => {
});
// should return the same response since it's idempotent
- expectValidLeadResponse(response, customer1);
+ expectValidLeadResponse({
+ response,
+ customer: customer1,
+ clickId: trackedClickId,
+ });
});
test("track a lead with eventQuantity", async () => {
@@ -65,7 +96,7 @@ describe("POST /track/lead", async () => {
const response = await http.post<TrackLeadResponse>({
path: "/track/lead",
body: {
- clickId: E2E_CLICK_ID,
+ clickId: trackedClickId,
eventName: "Start Trial",
externalId: customer2.externalId,
customerName: customer2.name,
@@ -75,7 +106,11 @@ describe("POST /track/lead", async () => {
},
});
- expectValidLeadResponse(response, customer2);
+ expectValidLeadResponse({
+ response,
+ customer: customer2,
+ clickId: trackedClickId,
+ });
});
test("track a lead with mode = 'wait' + track a sale right after", async () => {
@@ -83,7 +118,7 @@ describe("POST /track/lead", async () => {
const response = await http.post<TrackLeadResponse>({
path: "/track/lead",
body: {
- clickId: E2E_CLICK_ID,
+ clickId: trackedClickId,
eventName: "Mode=Wait Signup",
externalId: customer3.externalId,
customerName: customer3.name,
@@ -92,7 +127,11 @@ describe("POST /track/lead", async () => {
mode: "wait",
},
});
- expectValidLeadResponse(response, customer3);
+ expectValidLeadResponse({
+ response,
+ customer: customer3,
+ clickId: trackedClickId,
+ });
const saleResponse = await http.post<TrackSaleResponse>({
path: "/track/sale",
@@ -112,7 +151,7 @@ describe("POST /track/lead", async () => {
const response = await http.post<TrackLeadResponse>({
path: "/track/lead",
body: {
- clickId: E2E_CLICK_ID,
+ clickId: trackedClickId,
customerId: customer4.externalId,
eventName: "Signup",
customerName: customer4.name,
@@ -121,6 +160,10 @@ describe("POST /track/lead", async () => {
},
});
- expectValidLeadResponse(response, customer4);
+ expectValidLeadResponse({
+ response,
+ customer: customer4,
+ clickId: trackedClickId,
+ });
});
});
diff --git a/apps/web/tests/utils/resource.ts b/apps/web/tests/utils/resource.ts
index 670cd45bce..c441bd323c 100644
--- a/apps/web/tests/utils/resource.ts
+++ b/apps/web/tests/utils/resource.ts
@@ -3,6 +3,7 @@ export const E2E_WORKSPACE_ID = "ws_clrei1gld0002vs9mzn93p8ik";
export const E2E_LINK = {
domain: "dub.sh",
+ key: "test-click-tracking",
url: "https://github.com/dubinc",
};
@@ -18,7 +19,6 @@ export const E2E_TAG_2 = {
color: "blue",
};
-export const E2E_CLICK_ID = "GWGrkftJdYlZD2mq";
export const E2E_CUSTOMER_ID = "cm25onzuv0001s1bbxchrc0ae";
export const E2E_CUSTOMER_EXTERNAL_ID = "cus_jTrfVKYN3Buc3F80JoqBiY0g";
export const E2E_WEBHOOK_ID = "wh_MHR7sZXXtZ7keBaNYZ30rQ0v";