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 integration tests for /track/**/client endpoints

Kiran Kbb49430a5484
diff --git a/apps/web/app/(ee)/api/track/sale/client/route.ts b/apps/web/app/(ee)/api/track/sale/client/route.ts
index 3821996406..0753240d84 100644
--- a/apps/web/app/(ee)/api/track/sale/client/route.ts
+++ b/apps/web/app/(ee)/api/track/sale/client/route.ts
@@ -27,7 +27,7 @@ export const POST = withPublishableKey(
});
}
- let {
+ const {
customerExternalId,
paymentProcessor,
invoiceId,
diff --git a/apps/web/lib/auth/publishable-key.ts b/apps/web/lib/auth/publishable-key.ts
index dae7c6b4cc..fe3d3f9515 100644
--- a/apps/web/lib/auth/publishable-key.ts
+++ b/apps/web/lib/auth/publishable-key.ts
@@ -4,6 +4,7 @@ import { prisma } from "@dub/prisma";
import { getSearchParams } from "@dub/utils";
import { Project } from "@prisma/client";
import { AxiomRequest, withAxiom } from "next-axiom";
+import { COMMON_CORS_HEADERS } from "../api/cors";
interface WithPublishableKeyHandler {
({
@@ -39,9 +40,9 @@ export const withPublishableKey = (
req: AxiomRequest,
{ params = {} }: { params: Record<string, string> | undefined },
) => {
- try {
- let headers = {};
+ let headers = {};
+ try {
const authorizationHeader = req.headers.get("Authorization");
if (authorizationHeader) {
if (!authorizationHeader.includes("Bearer ")) {
@@ -50,6 +51,7 @@ export const withPublishableKey = (
message: "Invalid or missing publishable key.",
});
}
+
const publishableKey = authorizationHeader.replace("Bearer ", "");
if (!publishableKey.startsWith("dub_pk_")) {
throw new DubApiError({
@@ -64,6 +66,7 @@ export const withPublishableKey = (
).limit(publishableKey);
headers = {
+ ...COMMON_CORS_HEADERS,
"Retry-After": reset.toString(),
"X-RateLimit-Limit": limit.toString(),
"X-RateLimit-Remaining": remaining.toString(),
@@ -107,7 +110,7 @@ export const withPublishableKey = (
}
} catch (error) {
req.log.error(error);
- return handleAndReturnErrorResponse(error);
+ return handleAndReturnErrorResponse(error, headers);
}
},
);
diff --git a/apps/web/tests/tracks/track-lead-client.test.ts b/apps/web/tests/tracks/track-lead-client.test.ts
new file mode 100644
index 0000000000..08e95c2ec9
--- /dev/null
+++ b/apps/web/tests/tracks/track-lead-client.test.ts
@@ -0,0 +1,57 @@
+import { randomCustomer } from "tests/utils/helpers";
+import { E2E_TRACK_CLICK_HEADERS } from "tests/utils/resource";
+import { describe, expect, test } from "vitest";
+import { IntegrationHarness } from "../utils/integration";
+
+describe("POST /track/lead/client", async () => {
+ const h = new IntegrationHarness();
+ const { http, env } = await h.init();
+
+ // Track a click
+ const clickResponse = await http.post<{ clickId: string }>({
+ path: "/track/click",
+ headers: E2E_TRACK_CLICK_HEADERS,
+ body: {
+ domain: "getacme.link",
+ key: "derek",
+ },
+ });
+
+ expect(clickResponse.status).toEqual(200);
+ expect(clickResponse.data.clickId).toStrictEqual(expect.any(String));
+
+ const clickId = clickResponse.data.clickId;
+ const customer = randomCustomer();
+
+ test("track a lead (with clickId from a prior /track/lead/client request)", async () => {
+ const response = await fetch(`${env.E2E_BASE_URL}/api/track/lead/client`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${env.E2E_PUBLISHABLE_KEY}`,
+ },
+ body: JSON.stringify({
+ clickId: clickId,
+ eventName: "Signup",
+ customerExternalId: customer.externalId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ }),
+ });
+
+ const leadResponse = await response.json();
+
+ expect(response.status).toEqual(200);
+ expect(leadResponse).toStrictEqual({
+ clickId,
+ customerName: customer.name,
+ customerEmail: customer.email,
+ customerAvatar: customer.avatar,
+ customer: customer,
+ click: {
+ id: clickId,
+ },
+ });
+ });
+});
diff --git a/apps/web/tests/tracks/track-sale-client-test.ts b/apps/web/tests/tracks/track-sale-client-test.ts
new file mode 100644
index 0000000000..6d858feb63
--- /dev/null
+++ b/apps/web/tests/tracks/track-sale-client-test.ts
@@ -0,0 +1,57 @@
+import { randomId, randomSaleAmount } from "tests/utils/helpers";
+import {
+ E2E_CUSTOMER_EXTERNAL_ID,
+ E2E_CUSTOMER_ID,
+} from "tests/utils/resource";
+import { describe, expect, test } from "vitest";
+import { IntegrationHarness } from "../utils/integration";
+
+describe("POST /track/sale/client", async () => {
+ const h = new IntegrationHarness();
+ const { env } = await h.init();
+
+ const sale = {
+ eventName: "Subscription",
+ amount: randomSaleAmount(),
+ currency: "usd",
+ invoiceId: `INV_${randomId()}`,
+ paymentProcessor: "stripe",
+ };
+
+ test("track a sale", async () => {
+ const response = await fetch(`${env.E2E_BASE_URL}/api/track/sale/client`, {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ Authorization: `Bearer ${env.E2E_PUBLISHABLE_KEY}`,
+ },
+ body: JSON.stringify({}),
+ });
+
+ const leadResponse = await response.json();
+
+ expect(response.status).toEqual(200);
+ expect(leadResponse).toStrictEqual({
+ eventName: "Subscription",
+ customer: {
+ id: E2E_CUSTOMER_ID,
+ name: expect.any(String),
+ email: expect.any(String),
+ avatar: expect.any(String),
+ externalId: E2E_CUSTOMER_EXTERNAL_ID,
+ },
+ sale: {
+ amount: sale.amount,
+ currency: sale.currency,
+ paymentProcessor: sale.paymentProcessor,
+ invoiceId: sale.invoiceId,
+ metadata: null,
+ },
+ amount: sale.amount,
+ currency: sale.currency,
+ paymentProcessor: sale.paymentProcessor,
+ metadata: null,
+ invoiceId: sale.invoiceId,
+ });
+ });
+});
diff --git a/apps/web/tests/tracks/track-sale.test.ts b/apps/web/tests/tracks/track-sale.test.ts
index a91458181a..e330a3dc60 100644
--- a/apps/web/tests/tracks/track-sale.test.ts
+++ b/apps/web/tests/tracks/track-sale.test.ts
@@ -1,6 +1,5 @@
import { TrackSaleResponse } from "@/lib/types";
-import { randomValue } from "@dub/utils";
-import { randomId } from "tests/utils/helpers";
+import { randomId, randomSaleAmount } from "tests/utils/helpers";
import {
E2E_CUSTOMER_EXTERNAL_ID,
E2E_CUSTOMER_EXTERNAL_ID_2,
@@ -39,10 +38,6 @@ const expectValidSaleResponse = (
});
};
-const randomSaleAmount = () => {
- return randomValue([400, 900, 1900]);
-};
-
// Helper function to verify commission details
const verifyCommission = async (
http: any,
diff --git a/apps/web/tests/utils/env.ts b/apps/web/tests/utils/env.ts
index 75c0c685ce..5431b3dcbe 100644
--- a/apps/web/tests/utils/env.ts
+++ b/apps/web/tests/utils/env.ts
@@ -4,6 +4,7 @@ export const integrationTestEnv = z.object({
E2E_BASE_URL: z.string().url().min(1),
E2E_TOKEN: z.string().min(1),
E2E_TOKEN_OLD: z.string().min(1),
+ E2E_PUBLISHABLE_KEY: z.string().min(1),
CI: z.coerce
.string()
.default("false")
diff --git a/apps/web/tests/utils/helpers.ts b/apps/web/tests/utils/helpers.ts
index d544b07d40..8bb81bea19 100644
--- a/apps/web/tests/utils/helpers.ts
+++ b/apps/web/tests/utils/helpers.ts
@@ -1,5 +1,5 @@
import { generateRandomName } from "@/lib/names";
-import { OG_AVATAR_URL, nanoid } from "@dub/utils";
+import { OG_AVATAR_URL, nanoid, randomValue } from "@dub/utils";
export const randomId = (length = 24) => nanoid(length);
@@ -27,3 +27,7 @@ export const randomEmail = ({
} = {}) => {
return `${generateRandomName().split(" ").join(".").toLowerCase()}@${domain}`;
};
+
+export const randomSaleAmount = () => {
+ return randomValue([400, 900, 1900]);
+};
diff --git a/apps/web/tests/utils/integration.ts b/apps/web/tests/utils/integration.ts
index 8e719ac26e..8bfc25b78e 100644
--- a/apps/web/tests/utils/integration.ts
+++ b/apps/web/tests/utils/integration.ts
@@ -52,7 +52,11 @@ export class IntegrationHarness {
workspace,
};
- return { ...this.resources, http: this.http };
+ return {
+ ...this.resources,
+ http: this.http,
+ env: this.env,
+ };
}
// Delete link