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.

fix tests, fix partner/customer redirect bug

Steven Teyf12458bb0c13
diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml
index 1ff532eed2..b2c8907777 100644
--- a/.github/workflows/e2e.yaml
+++ b/.github/workflows/e2e.yaml
@@ -44,4 +44,5 @@ jobs:
QSTASH_CURRENT_SIGNING_KEY: ${{ secrets.QSTASH_CURRENT_SIGNING_KEY }}
NEXT_PUBLIC_NGROK_URL: ${{ github.event.deployment_status.environment_url }}
UNSUBSCRIBE_TOKEN_SECRET: "dummy-secret"
+ VERCEL_ENV: ${{ github.event.deployment_status.environment }}
run: pnpm test
diff --git a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
index 5ca5c7d151..91bdd8d062 100644
--- a/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
+++ b/apps/web/app/(ee)/api/partner-profile/programs/[programId]/customers/[customerId]/route.ts
@@ -67,22 +67,33 @@ export const GET = withPartnerProfile(async ({ partner, params }) => {
});
}
+ const partnerLinkIds = links.map((link) => link.id);
+
const events = await getCustomerEvents({
customerId: customer.id,
- linkIds: links.map((link) => link.id),
+ linkIds: partnerLinkIds,
includeMetadata: false,
});
- if (events.length === 0) {
+ // if there are events between the partner-customer, use the last event's link id
+ // else, check if the customer.linkId actually belongs to the partner
+ // if it does, use it, otherwise, throw an error
+ const attributedPartnerLinkId =
+ events.length > 0
+ ? events[events.length - 1].link_id
+ : customer.linkId && partnerLinkIds.includes(customer.linkId)
+ ? customer.linkId
+ : null;
+
+ if (!attributedPartnerLinkId) {
throw new DubApiError({
code: "not_found",
message: "Customer is not attributed to any links by this partner.",
});
}
- // get the first partner link that this customer interacted with
- const firstLinkId = events[events.length - 1].link_id;
- const link = links.find((link) => link.id === firstLinkId);
+ // get the partner link that this customer interacted with
+ const link = links.find((link) => link.id === attributedPartnerLinkId);
return NextResponse.json(
PartnerProfileCustomerSchema.extend({
diff --git a/apps/web/tests/redirects/index.test.ts b/apps/web/tests/redirects/index.test.ts
index d69d7f1ed3..8d22b62c76 100644
--- a/apps/web/tests/redirects/index.test.ts
+++ b/apps/web/tests/redirects/index.test.ts
@@ -60,276 +60,283 @@ async function assertRedirectWithDubIdCookie(
expect(dubIdCookie).toMatch(/Max-Age=3600\b/i);
}
-describe.runIf(env.CI)("Link Redirects", async () => {
- const h = new IntegrationHarness();
-
- test("root", async () => {
- const response = await fetch(h.baseUrl, fetchOptions);
-
- // the location should start with "https://dub.co"
- expect(response.headers.get("location")).toMatch(/^https:\/\/dub\.co\//);
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(301);
- });
+describe.runIf(env.CI && env.VERCEL_ENV !== "Production")(
+ "Link Redirects",
+ async () => {
+ const h = new IntegrationHarness();
+
+ test("root", async () => {
+ const response = await fetch(h.baseUrl, fetchOptions);
+
+ // the location should start with "https://dub.co"
+ expect(response.headers.get("location")).toMatch(/^https:\/\/dub\.co\//);
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(301);
+ });
- test("regular", async () => {
- const response = await fetch(`${h.baseUrl}/checkly-check`, fetchOptions);
+ test("regular", async () => {
+ const response = await fetch(`${h.baseUrl}/checkly-check`, fetchOptions);
- expect(response.headers.get("location")).toBe("https://www.checklyhq.com/");
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ expect(response.headers.get("location")).toBe(
+ "https://www.checklyhq.com/",
+ );
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("disabled link", async () => {
- const response = await fetch(`${h.baseUrl}/disabled`, fetchOptions);
+ test("disabled link", async () => {
+ const response = await fetch(`${h.baseUrl}/disabled`, fetchOptions);
- // Special case for disabled/notfound links since we're using redirect() from next/navigation:
- // - doesn't support x-powered-by header
- // - uses 307 status code instead of 302
- // This is the same as case-sensitive (incorrect) key test below.
- expect(response.headers.get("location")).toBe("https://dub.co/links");
- expect(response.status).toBe(307);
- });
+ // Special case for disabled/notfound links since we're using redirect() from next/navigation:
+ // - doesn't support x-powered-by header
+ // - uses 307 status code instead of 302
+ // This is the same as case-sensitive (incorrect) key test below.
+ expect(response.headers.get("location")).toBe("https://dub.co/links");
+ expect(response.status).toBe(307);
+ });
- test("with slash", async () => {
- const response = await fetch(`${h.baseUrl}/checkly/check`, fetchOptions);
+ test("with slash", async () => {
+ const response = await fetch(`${h.baseUrl}/checkly/check`, fetchOptions);
- expect(response.headers.get("location")).toBe("https://www.checklyhq.com/");
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ expect(response.headers.get("location")).toBe(
+ "https://www.checklyhq.com/",
+ );
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("korean unicode key", async () => {
- const hangulKey = "한글링크테스트";
- const url = new URL(h.baseUrl);
- url.pathname = `/${hangulKey}`;
+ test("korean unicode key", async () => {
+ const hangulKey = "한글링크테스트";
+ const url = new URL(h.baseUrl);
+ url.pathname = `/${hangulKey}`;
- const response = await fetch(url.href, fetchOptions);
+ const response = await fetch(url.href, fetchOptions);
- expect(response.headers.get("location")).toBe(
- "https://youtu.be/9bZkp7q19f0",
- );
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ expect(response.headers.get("location")).toBe(
+ "https://youtu.be/9bZkp7q19f0",
+ );
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("hebrew unicode key", async () => {
- const hebrewKey = "שלום";
- const url = new URL(h.baseUrl);
- url.pathname = `/${hebrewKey}`;
+ test("hebrew unicode key", async () => {
+ const hebrewKey = "שלום";
+ const url = new URL(h.baseUrl);
+ url.pathname = `/${hebrewKey}`;
- const response = await fetch(url.href, fetchOptions);
+ const response = await fetch(url.href, fetchOptions);
- expect(response.headers.get("location")).toBe(
- "https://youtube.com/shorts/IdP2WdnJK1o",
- );
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ expect(response.headers.get("location")).toBe(
+ "https://youtube.com/shorts/IdP2WdnJK1o",
+ );
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("with dub_id", async () => {
- await assertRedirectWithDubIdCookie(h.baseUrl, "conversion-tracking");
- });
+ test("with dub_id", async () => {
+ await assertRedirectWithDubIdCookie(h.baseUrl, "conversion-tracking");
+ });
- test("with dub_id (and slash in key)", async () => {
- await assertRedirectWithDubIdCookie(h.baseUrl, "conversion/tracking");
- });
+ test("with dub_id (and slash in key)", async () => {
+ await assertRedirectWithDubIdCookie(h.baseUrl, "conversion/tracking");
+ });
- test("with dub_id and via", async () => {
- await assertRedirectWithDubIdCookie(h.baseUrl, "track-test", {
- via: "track-test",
+ test("with dub_id and via", async () => {
+ await assertRedirectWithDubIdCookie(h.baseUrl, "track-test", {
+ via: "track-test",
+ });
});
- });
- test("with dub_client_reference_id", async () => {
- const response = await fetch(
- `${h.baseUrl}/client_reference_id`,
- fetchOptions,
- );
-
- // the location should contain `?client_reference_id=dub_id_` query param
- expect(response.headers.get("location")).toMatch(
- /client_reference_id=dub_id_[a-zA-Z0-9]+/,
- );
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ test("with dub_client_reference_id", async () => {
+ const response = await fetch(
+ `${h.baseUrl}/client_reference_id`,
+ fetchOptions,
+ );
+
+ // the location should contain `?client_reference_id=dub_id_` query param
+ expect(response.headers.get("location")).toMatch(
+ /client_reference_id=dub_id_[a-zA-Z0-9]+/,
+ );
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("with passthrough query", async () => {
- const response = await fetch(
- `${h.baseUrl}/checkly-check-passthrough?utm_source=checkly`,
- fetchOptions,
- );
-
- expect(response.headers.get("location")).toBe(
- "https://www.checklyhq.com/?utm_source=checkly&utm_medium=social&utm_campaign=checks",
- );
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ test("with passthrough query", async () => {
+ const response = await fetch(
+ `${h.baseUrl}/checkly-check-passthrough?utm_source=checkly`,
+ fetchOptions,
+ );
+
+ expect(response.headers.get("location")).toBe(
+ "https://www.checklyhq.com/?utm_source=checkly&utm_medium=social&utm_campaign=checks",
+ );
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("with complex query", async () => {
- const response = await fetch(
- `${h.baseUrl}/checkly-check-query`,
- fetchOptions,
- );
-
- expect(response.headers.get("location")).toBe(
- "https://guides.apple.com/?ug=CglEVUIgR3VpZGUSDgjZMhDEo%2BGA%2BZKqpJUBEg4I2TIQw7y33%2B%2B6ifL%2BARIOCNkyEJC988jqgIrQjQESDgjZMhCB%2B7XSiPTwrfUBEg4I2TIQ5J25xZOynPDxARINCNkyENuVr4POz8aMcBIOCMI7EK36pfjQuerJ0gESDQjCOxDSuurnjM6T7mASDQjCOxD3vr%2F%2Fkq%2FLqUwSDQjCOxCg9cK%2BjeOhnS4%3D",
- );
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ test("with complex query", async () => {
+ const response = await fetch(
+ `${h.baseUrl}/checkly-check-query`,
+ fetchOptions,
+ );
+
+ expect(response.headers.get("location")).toBe(
+ "https://guides.apple.com/?ug=CglEVUIgR3VpZGUSDgjZMhDEo%2BGA%2BZKqpJUBEg4I2TIQw7y33%2B%2B6ifL%2BARIOCNkyEJC988jqgIrQjQESDgjZMhCB%2B7XSiPTwrfUBEg4I2TIQ5J25xZOynPDxARINCNkyENuVr4POz8aMcBIOCMI7EK36pfjQuerJ0gESDQjCOxDSuurnjM6T7mASDQjCOxD3vr%2F%2Fkq%2FLqUwSDQjCOxCg9cK%2BjeOhnS4%3D",
+ );
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("appsflyer tracking url", async () => {
- const response = await fetch(`${h.baseUrl}/appsflyer`, fetchOptions);
+ test("appsflyer tracking url", async () => {
+ const response = await fetch(`${h.baseUrl}/appsflyer`, fetchOptions);
- // location to include clickid, af_siteid query params
- expect(response.headers.get("location")).toMatch(/pid=dubinc_int/);
- expect(response.headers.get("location")).toMatch(/clickid=[a-zA-Z0-9]+/);
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ // location to include clickid, af_siteid query params
+ expect(response.headers.get("location")).toMatch(/pid=dubinc_int/);
+ expect(response.headers.get("location")).toMatch(/clickid=[a-zA-Z0-9]+/);
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("singular tracking url", async () => {
- const response = await fetch(`${h.baseUrl}/singular`, fetchOptions);
+ test("singular tracking url", async () => {
+ const response = await fetch(`${h.baseUrl}/singular`, fetchOptions);
- // location to include cl, ua, ip query params
- expect(response.headers.get("location")).toMatch(/cl=[a-zA-Z0-9]+/);
- expect(response.headers.get("location")).toMatch(/ua=[a-zA-Z0-9]+/);
- expect(response.headers.get("location")).toMatch(/ip=[a-zA-Z0-9]+/);
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ // location to include cl, ua, ip query params
+ expect(response.headers.get("location")).toMatch(/cl=[a-zA-Z0-9]+/);
+ expect(response.headers.get("location")).toMatch(/ua=[a-zA-Z0-9]+/);
+ expect(response.headers.get("location")).toMatch(/ip=[a-zA-Z0-9]+/);
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("singular polyfill wpcn & wpcl params", async () => {
- const response = await fetch(
- `${h.baseUrl}/singular-polyfill`,
- fetchOptions,
- );
+ test("singular polyfill wpcn & wpcl params", async () => {
+ const response = await fetch(
+ `${h.baseUrl}/singular-polyfill`,
+ fetchOptions,
+ );
- const location = response.headers.get("location");
- expect(location).toBeTruthy();
+ const location = response.headers.get("location");
+ expect(location).toBeTruthy();
- const url = new URL(location!);
+ const url = new URL(location!);
- // wpcn should be replaced from {via} template to actual via value
- expect(url.searchParams.get("wpcn")).toBe("singular-polyfill");
+ // wpcn should be replaced from {via} template to actual via value
+ expect(url.searchParams.get("wpcn")).toBe("singular-polyfill");
- // wpcl should be replaced from {dub_id} template to actual dub_id value
- expect(url.searchParams.get("wpcl")).toMatch(/^[a-zA-Z0-9]+$/);
+ // wpcl should be replaced from {dub_id} template to actual dub_id value
+ expect(url.searchParams.get("wpcl")).toMatch(/^[a-zA-Z0-9]+$/);
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("google play store url", async () => {
- const response = await fetch(`${h.baseUrl}/gps`, fetchOptions);
- const location = response.headers.get("location");
+ test("google play store url", async () => {
+ const response = await fetch(`${h.baseUrl}/gps`, fetchOptions);
+ const location = response.headers.get("location");
- expect(response.status).toBe(302);
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(location).toBeTruthy();
+ expect(response.status).toBe(302);
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(location).toBeTruthy();
- const url = new URL(location!);
- const referrerEncoded = url.searchParams.get("referrer");
- expect(referrerEncoded).toBeTruthy();
+ const url = new URL(location!);
+ const referrerEncoded = url.searchParams.get("referrer");
+ expect(referrerEncoded).toBeTruthy();
- const referrer = decodeURIComponent(referrerEncoded!);
- const params = new URLSearchParams(referrer);
+ const referrer = decodeURIComponent(referrerEncoded!);
+ const params = new URLSearchParams(referrer);
- expect(params.get("deepLink")).toBe("https://dub.sh/gps");
- });
+ expect(params.get("deepLink")).toBe("https://dub.sh/gps");
+ });
- test("google play store url with existing referrer", async () => {
- const response = await fetch(
- `${h.baseUrl}/gps-with-referrer`,
- fetchOptions,
- );
- const location = response.headers.get("location");
+ test("google play store url with existing referrer", async () => {
+ const response = await fetch(
+ `${h.baseUrl}/gps-with-referrer`,
+ fetchOptions,
+ );
+ const location = response.headers.get("location");
- expect(response.status).toBe(302);
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(location).toBeTruthy();
+ expect(response.status).toBe(302);
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(location).toBeTruthy();
- const url = new URL(location!);
- const referrerEncoded = url.searchParams.get("referrer");
- expect(referrerEncoded).toBeTruthy();
+ const url = new URL(location!);
+ const referrerEncoded = url.searchParams.get("referrer");
+ expect(referrerEncoded).toBeTruthy();
- const referrer = decodeURIComponent(referrerEncoded!);
- const params = new URLSearchParams(referrer);
+ const referrer = decodeURIComponent(referrerEncoded!);
+ const params = new URLSearchParams(referrer);
- expect(params.get("utm_source")).toBe("google");
- expect(params.get("deepLink")).toBe("https://dub.sh/gps-with-referrer");
- });
+ expect(params.get("utm_source")).toBe("google");
+ expect(params.get("deepLink")).toBe("https://dub.sh/gps-with-referrer");
+ });
- test("query params with no value", async () => {
- const response = await fetch(
- `${h.baseUrl}/query-params-no-value`,
- fetchOptions,
- );
-
- expect(response.headers.get("location")).toBe(
- "https://dub.co/blog?emptyquery",
- );
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ test("query params with no value", async () => {
+ const response = await fetch(
+ `${h.baseUrl}/query-params-no-value`,
+ fetchOptions,
+ );
+
+ expect(response.headers.get("location")).toBe(
+ "https://dub.co/blog?emptyquery",
+ );
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("with case-sensitive (correct) key", async () => {
- const response = await fetch(
- `${h.baseUrl}/cAsE-sensitive-test`,
- fetchOptions,
- );
-
- expect(response.headers.get("location")).toBe(
- "https://dub.co/changelog/case-insensitive-links",
- );
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ test("with case-sensitive (correct) key", async () => {
+ const response = await fetch(
+ `${h.baseUrl}/cAsE-sensitive-test`,
+ fetchOptions,
+ );
+
+ expect(response.headers.get("location")).toBe(
+ "https://dub.co/changelog/case-insensitive-links",
+ );
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("with case-sensitive (incorrect) key", async () => {
- const response = await fetch(
- `${h.baseUrl}/case-sensitive-test`,
- fetchOptions,
- );
+ test("with case-sensitive (incorrect) key", async () => {
+ const response = await fetch(
+ `${h.baseUrl}/case-sensitive-test`,
+ fetchOptions,
+ );
- expect(response.headers.get("location")).toBe("https://dub.co/links");
- expect(response.status).toBe(307);
- });
+ expect(response.headers.get("location")).toBe("https://dub.co/links");
+ expect(response.status).toBe(307);
+ });
- test("with case-sensitive key (and dub_id)", async () => {
- await assertRedirectWithDubIdCookie(h.baseUrl, "cAsE-sensitive-TeSt");
- });
+ test("with case-sensitive key (and dub_id)", async () => {
+ await assertRedirectWithDubIdCookie(h.baseUrl, "cAsE-sensitive-TeSt");
+ });
- test("with password", async () => {
- const response = await fetch(
- `${h.baseUrl}/password/check?pw=dub`,
- fetchOptions,
- );
+ test("with password", async () => {
+ const response = await fetch(
+ `${h.baseUrl}/password/check?pw=dub`,
+ fetchOptions,
+ );
- expect(response.headers.get("location")).toBe("https://dub.co/");
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ expect(response.headers.get("location")).toBe("https://dub.co/");
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("unsupported key", async () => {
- const response = await fetch(`${h.baseUrl}/wp-admin.php`, fetchOptions);
+ test("unsupported key", async () => {
+ const response = await fetch(`${h.baseUrl}/wp-admin.php`, fetchOptions);
- expect(response.headers.get("location")).toMatch(/\/\?dub-no-track=1$/);
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
+ expect(response.headers.get("location")).toMatch(/\/\?dub-no-track=1$/);
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
- test("redirection url", async () => {
- const response = await fetch(
- `${h.baseUrl}/redir-url-test?${REDIRECTION_QUERY_PARAM}=https://dub.co/blog`,
- fetchOptions,
- );
+ test("redirection url", async () => {
+ const response = await fetch(
+ `${h.baseUrl}/redir-url-test?${REDIRECTION_QUERY_PARAM}=https://dub.co/blog`,
+ fetchOptions,
+ );
- expect(response.headers.get("location")).toBe("https://dub.co/blog");
- expect(response.headers.get("x-powered-by")).toBe(poweredBy);
- expect(response.status).toBe(302);
- });
-});
+ expect(response.headers.get("location")).toBe("https://dub.co/blog");
+ expect(response.headers.get("x-powered-by")).toBe(poweredBy);
+ expect(response.status).toBe(302);
+ });
+ },
+);
diff --git a/apps/web/tests/utils/env.ts b/apps/web/tests/utils/env.ts
index 06462a0dff..94845cb5ea 100644
--- a/apps/web/tests/utils/env.ts
+++ b/apps/web/tests/utils/env.ts
@@ -10,6 +10,7 @@ export const integrationTestEnv = z.object({
.string()
.default("false")
.transform((v) => v === "true"),
+ VERCEL_ENV: z.string().optional(),
});
export const env = integrationTestEnv.parse(process.env);