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.

scope queries, extract shared helpers, fix group cleanup safety

Pedro Ladeiraf1750d6aabb7
diff --git a/apps/web/scripts/dev/data.json b/apps/web/scripts/dev/data.json
index 80895aa01d..ec2e217563 100644
--- a/apps/web/scripts/dev/data.json
+++ b/apps/web/scripts/dev/data.json
@@ -1,6 +1,6 @@
{
"workspace": {
- "id": "ws_clrei1gld0002vs9mzn93p8ik",
+ "id": "ws_1KETZ919F83ZJH6A80HWEHW6E",
"name": "Acme, Inc.",
"slug": "acme",
"logo": "https://assets.dub.co/logo.png",
@@ -15,7 +15,7 @@
"usersLimit": 100,
"aiLimit": 100,
"groupsLimit": 100,
- "defaultProgramId": "prog_CYCu7IMAapjkRpTnr8F1azjN",
+ "defaultProgramId": "prog_1K2J9DRWPPJ2F1RX53N92TSGA",
"invoicePrefix": "ACME",
"conversionEnabled": true,
"webhookEnabled": true,
@@ -24,7 +24,7 @@
},
"users": [
{
- "id": "clxz1q7c7000hbqx5ckv4r82h",
+ "id": "user_cludszk1h0000wmd2e0ea2b0p",
"name": "Owner",
"email": "[email protected]",
"emailVerified": "2026-01-21T00:00:00.000Z",
@@ -89,7 +89,7 @@
],
"groups": [
{
- "id": "grp_1K2E25381GVMG7HHM057TB92F",
+ "id": "grp_1K2J9DRWPPJ2F1RX53N92TSGC",
"name": "Default Group",
"slug": "default",
"color": null,
@@ -103,19 +103,22 @@
],
"defaultLinks": [
{
- "url": "https://github.com/dubinc"
+ "url": "https://acme.com"
+ },
+ {
+ "url": "https://example.com"
}
]
}
],
"program": {
- "id": "prog_CYCu7IMAapjkRpTnr8F1azjN",
+ "id": "prog_1K2J9DRWPPJ2F1RX53N92TSGA",
"name": "Acme",
"slug": "acme",
"defaultFolderId": "fold_1K2J9DRWPPJ2F1RX53N92TSGB",
- "defaultGroupId": "grp_1K2E25381GVMG7HHM057TB92F",
+ "defaultGroupId": "grp_1K2J9DRWPPJ2F1RX53N92TSGC",
"domain": "dub.sh",
- "url": "https://acme.dub.sh/",
+ "url": "https://acme.com",
"termsUrl": "https://acme.com/terms",
"helpUrl": "https://acme.com/help",
"supportEmail": "[email protected]",
@@ -123,44 +126,72 @@
},
"partners": [
{
- "id": "pn_H4TB2V5hDIjpqB7PwrxESoY3",
- "name": "Steven",
- "email": "[email protected]",
- "description": "E2E Test Partner",
+ "id": "pn_1K2J9DRWPPJ2F1RX53N92TSGD",
+ "name": "Partner 1",
+ "email": "[email protected]",
+ "description": "Partner 1 description",
"country": "US",
"createdAt": "2026-01-21T00:00:00.000Z",
"user": {
"id": "user_cl8dea8g1073109m7wkwlldqj",
- "name": "Steven",
- "email": "[email protected]",
+ "name": "Partner 1",
+ "email": "[email protected]",
"emailVerified": "2026-01-21T00:00:00.000Z"
}
},
{
- "id": "pn_NNG3YjwhLhA7nCZSaXeLIsWu",
- "name": "Marvin",
- "email": "[email protected]",
- "description": "E2E Test Partner - US",
+ "id": "pn_1K2J9DRWPPJ2F1RX53N92TSGE",
+ "name": "Partner 2",
+ "email": "[email protected]",
+ "description": "Partner 2 description",
"country": "US",
"createdAt": "2026-01-21T00:00:00.000Z",
"user": {
"id": "user_cludszk1h0000wmd2e0ea2b0t",
- "name": "Marvin",
- "email": "[email protected]",
+ "name": "Partner 2",
+ "email": "[email protected]",
"emailVerified": "2026-01-21T00:00:00.000Z"
}
},
{
- "id": "pn_1K8ND11BZ4XPEX39QX3YMBGY0",
- "name": "Kiran E2E",
- "email": "[email protected]",
- "description": "E2E Test Partner - SG",
- "country": "SG",
+ "id": "pn_1K2J9DRWPPJ2F1RX53N92TSGF",
+ "name": "Partner 3",
+ "email": "[email protected]",
+ "description": "Partner 3 description",
+ "country": "US",
"createdAt": "2026-01-21T00:00:00.000Z",
"user": {
"id": "user_cludszk1h0000wmd2e0ea2b0u",
- "name": "Kiran E2E",
- "email": "[email protected]",
+ "name": "Partner 3",
+ "email": "[email protected]",
+ "emailVerified": "2026-01-21T00:00:00.000Z"
+ }
+ },
+ {
+ "id": "pn_1K2J9DRWPPJ2F1RX53N92TSGG",
+ "name": "Partner 4",
+ "email": "[email protected]",
+ "description": "Partner 4 description",
+ "country": "US",
+ "createdAt": "2026-01-21T00:00:00.000Z",
+ "user": {
+ "id": "user_cludszk1h0000wmd2e0ea2b0v",
+ "name": "Partner 4",
+ "email": "[email protected]",
+ "emailVerified": "2026-01-21T00:00:00.000Z"
+ }
+ },
+ {
+ "id": "pn_1K2J9DRWPPJ2F1RX53N92TSGH",
+ "name": "Partner 5",
+ "email": "[email protected]",
+ "description": "Partner 5 description",
+ "country": "US",
+ "createdAt": "2026-01-21T00:00:00.000Z",
+ "user": {
+ "id": "user_cludszk1h0000wmd2e0ea2b0w",
+ "name": "Partner 5",
+ "email": "[email protected]",
"emailVerified": "2026-01-21T00:00:00.000Z"
}
}
diff --git a/apps/web/tests/utils/verify-commission.ts b/apps/web/tests/utils/verify-commission.ts
index 89a2b1af01..dc1198e000 100644
--- a/apps/web/tests/utils/verify-commission.ts
+++ b/apps/web/tests/utils/verify-commission.ts
@@ -1,5 +1,6 @@
import { expect } from "vitest";
import { prisma } from "./prisma";
+import { E2E_PROGRAM, E2E_WORKSPACE_ID } from "./resource";
interface VerifyCommissionProps {
customerExternalId?: string;
@@ -19,10 +20,15 @@ export const verifyCommission = async ({
}: VerifyCommissionProps) => {
let customerId: string | undefined;
- // Resolve customer ID first if customerExternalId is given
+ // Resolve customer ID (scoped by projectId — externalId is unique per project)
if (customerExternalId) {
- const customer = await prisma.customer.findFirst({
- where: { externalId: customerExternalId },
+ const customer = await prisma.customer.findUnique({
+ where: {
+ projectId_externalId: {
+ projectId: E2E_WORKSPACE_ID,
+ externalId: customerExternalId,
+ },
+ },
select: { id: true },
});
@@ -36,6 +42,7 @@ export const verifyCommission = async ({
while (Date.now() - startTime < TIMEOUT_MS) {
const commission = await prisma.commission.findFirst({
where: {
+ programId: E2E_PROGRAM.id,
...(customerId && { customerId }),
...(invoiceId && { invoiceId }),
},
@@ -66,6 +73,6 @@ export const verifyCommission = async ({
// Timeout reached - fail the test
throw new Error(
`Commission not found within ${TIMEOUT_MS / 1000} seconds. ` +
- `customerId: ${customerId}, invoiceId: ${invoiceId}`,
+ `programId: ${E2E_PROGRAM.id}, customerId: ${customerId}, invoiceId: ${invoiceId}`,
);
};
diff --git a/apps/web/tests/workflows/award-bounty-workflow.test.ts b/apps/web/tests/workflows/award-bounty-workflow.test.ts
index 699681039a..4f39f94cba 100644
--- a/apps/web/tests/workflows/award-bounty-workflow.test.ts
+++ b/apps/web/tests/workflows/award-bounty-workflow.test.ts
@@ -4,43 +4,9 @@ import { prisma } from "../utils/prisma";
import { describe, expect, test, onTestFinished } from "vitest";
import { randomEmail } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
-import { E2E_TRACK_CLICK_HEADERS } from "../utils/resource";
+import { trackLeads } from "./utils/track-leads";
import { verifyBountySubmission } from "./utils/verify-bounty-submission";
-async function trackLeads(
- http: any,
- partnerLink: { domain: string; key: string },
- count: number,
-) {
- for (let i = 0; i < count; i++) {
- const { status: clickStatus, data: clickData } = await http.post({
- path: "/track/click",
- headers: E2E_TRACK_CLICK_HEADERS,
- body: {
- domain: partnerLink.domain,
- key: partnerLink.key,
- },
- });
-
- expect(clickStatus).toEqual(200);
- expect(clickData.clickId).toBeDefined();
-
- const { status: leadStatus } = await http.post({
- path: "/track/lead",
- body: {
- clickId: clickData.clickId,
- eventName: `Signup-${i}-${Date.now()}`,
- customerExternalId: `e2e-customer-${i}-${Date.now()}`,
- customerEmail: `customer${i}@example.com`,
- },
- });
-
- expect(leadStatus).toEqual(200);
-
- await new Promise((resolve) => setTimeout(resolve, 200));
- }
-}
-
describe.sequential("Workflow - AwardBounty", async () => {
const h = new IntegrationHarness();
const { http } = await h.init();
diff --git a/apps/web/tests/workflows/move-group-workflow.test.ts b/apps/web/tests/workflows/move-group-workflow.test.ts
index 6e32c1b5c7..580458d9e1 100644
--- a/apps/web/tests/workflows/move-group-workflow.test.ts
+++ b/apps/web/tests/workflows/move-group-workflow.test.ts
@@ -6,41 +6,17 @@ import { PartnerGroup } from "@dub/prisma/client";
import { describe, expect, test, onTestFinished } from "vitest";
import { randomEmail } from "../utils/helpers";
import { IntegrationHarness } from "../utils/integration";
-import { E2E_PROGRAM, E2E_TRACK_CLICK_HEADERS } from "../utils/resource";
+import { E2E_PROGRAM } from "../utils/resource";
+import { trackLeads } from "./utils/track-leads";
import { verifyPartnerGroupMove } from "./utils/verify-partner-group-move";
-async function trackLeads(
+async function cleanupOrphanedGroup(
http: any,
- partnerLink: { domain: string; key: string },
- count: number,
+ slug: string,
+ allGroups: PartnerGroup[],
) {
- for (let i = 0; i < count; i++) {
- const { status: clickStatus, data: clickData } = await http.post({
- path: "/track/click",
- headers: E2E_TRACK_CLICK_HEADERS,
- body: {
- domain: partnerLink.domain,
- key: partnerLink.key,
- },
- });
-
- expect(clickStatus).toEqual(200);
- expect(clickData.clickId).toBeDefined();
-
- const { status: leadStatus } = await http.post({
- path: "/track/lead",
- body: {
- clickId: clickData.clickId,
- eventName: `Signup-${i}-${Date.now()}`,
- customerExternalId: `e2e-customer-${i}-${Date.now()}`,
- customerEmail: `customer${i}@example.com`,
- },
- });
-
- expect(leadStatus).toEqual(200);
-
- await new Promise((resolve) => setTimeout(resolve, 200));
- }
+ const orphan = allGroups.find((g) => g.slug === slug);
+ if (orphan) await http.delete({ path: `/groups/${orphan.id}` });
}
describe.sequential("Workflow - MoveGroup", async () => {
@@ -48,24 +24,21 @@ describe.sequential("Workflow - MoveGroup", async () => {
const { http } = await h.init();
const programId = E2E_PROGRAM.id;
- const { data: allGroups } = await http.get<PartnerGroup[]>({
+ const { data: allGroupsForCleanup } = await http.get<PartnerGroup[]>({
path: "/groups",
});
- for (const group of allGroups) {
- if (group.slug?.startsWith("e2e-")) {
- await http.delete({ path: `/groups/${group.id}` });
- }
- }
-
test("Workflow is created when move rules are configured", async () => {
+ const slug = "e2e-target-config";
+ await cleanupOrphanedGroup(http, slug, allGroupsForCleanup);
+
const { status: targetStatus, data: targetGroup } = await http.post<
PartnerGroup
>({
path: "/groups",
body: {
name: "E2E Target Group - Config Test",
- slug: `e2e-target-config-${Date.now()}`,
+ slug,
color: randomValue(RESOURCE_COLORS),
},
});
@@ -114,11 +87,14 @@ describe.sequential("Workflow - MoveGroup", async () => {
});
test("Workflow is deleted when move rules are removed", async () => {
+ const slug = "e2e-remove-rules";
+ await cleanupOrphanedGroup(http, slug, allGroupsForCleanup);
+
const { status: groupStatus, data: group } = await http.post<PartnerGroup>({
path: "/groups",
body: {
name: "E2E Group - Remove Rules",
- slug: `e2e-remove-rules-${Date.now()}`,
+ slug,
color: randomValue(RESOURCE_COLORS),
},
});
@@ -168,6 +144,9 @@ describe.sequential("Workflow - MoveGroup", async () => {
});
test("Disabled workflow doesn't execute partner move", async () => {
+ const slug = "e2e-target-disabled";
+ await cleanupOrphanedGroup(http, slug, allGroupsForCleanup);
+
const { data: existingGroups } = await http.get<PartnerGroup[]>({
path: "/groups",
});
@@ -181,7 +160,7 @@ describe.sequential("Workflow - MoveGroup", async () => {
path: "/groups",
body: {
name: "E2E Target Group - Disabled Move",
- slug: `e2e-target-disabled-${Date.now()}`,
+ slug,
color: randomValue(RESOURCE_COLORS),
},
});
@@ -254,6 +233,9 @@ describe.sequential("Workflow - MoveGroup", async () => {
});
test("Workflow doesn't execute when conditions are not met", async () => {
+ const slug = "e2e-target-not-met";
+ await cleanupOrphanedGroup(http, slug, allGroupsForCleanup);
+
const { data: existingGroups } = await http.get<PartnerGroup[]>({
path: "/groups",
});
@@ -267,7 +249,7 @@ describe.sequential("Workflow - MoveGroup", async () => {
path: "/groups",
body: {
name: "E2E Target Group - Not Met",
- slug: `e2e-target-not-met-${Date.now()}`,
+ slug,
color: randomValue(RESOURCE_COLORS),
},
});
@@ -328,6 +310,9 @@ describe.sequential("Workflow - MoveGroup", async () => {
});
test("Workflow executes when conditions are met - partner moves to target group", { timeout: 90000 }, async () => {
+ const slug = "e2e-target-exec";
+ await cleanupOrphanedGroup(http, slug, allGroupsForCleanup);
+
const { data: existingGroups } = await http.get<PartnerGroup[]>({
path: "/groups",
});
@@ -341,7 +326,7 @@ describe.sequential("Workflow - MoveGroup", async () => {
path: "/groups",
body: {
name: "E2E Target Group - Move Execution",
- slug: `e2e-target-exec-${Date.now()}`,
+ slug,
color: randomValue(RESOURCE_COLORS),
},
});
@@ -412,6 +397,9 @@ describe.sequential("Workflow - MoveGroup", async () => {
});
test("No duplicate group moves on multiple triggers", { timeout: 90000 }, async () => {
+ const slug = "e2e-target-no-dup";
+ await cleanupOrphanedGroup(http, slug, allGroupsForCleanup);
+
const { data: existingGroups } = await http.get<PartnerGroup[]>({
path: "/groups",
});
@@ -425,7 +413,7 @@ describe.sequential("Workflow - MoveGroup", async () => {
path: "/groups",
body: {
name: "E2E Target Group - No Dup Move",
- slug: `e2e-target-no-dup-${Date.now()}`,
+ slug: "e2e-target-no-dup",
color: randomValue(RESOURCE_COLORS),
},
});
@@ -490,11 +478,14 @@ describe.sequential("Workflow - MoveGroup", async () => {
});
test("Multiple move rules can be configured (AND operator)", async () => {
+ const slug = "e2e-multi-rules";
+ await cleanupOrphanedGroup(http, slug, allGroupsForCleanup);
+
const { status: groupStatus, data: group } = await http.post<PartnerGroup>({
path: "/groups",
body: {
name: "E2E Group - Multiple Rules",
- slug: `e2e-multi-rules-${Date.now()}`,
+ slug,
color: randomValue(RESOURCE_COLORS),
},
});
diff --git a/apps/web/tests/workflows/utils/track-leads.ts b/apps/web/tests/workflows/utils/track-leads.ts
new file mode 100644
index 0000000000..353665ed8a
--- /dev/null
+++ b/apps/web/tests/workflows/utils/track-leads.ts
@@ -0,0 +1,36 @@
+import { expect } from "vitest";
+import { E2E_TRACK_CLICK_HEADERS } from "../../utils/resource";
+
+export async function trackLeads(
+ http: any,
+ partnerLink: { domain: string; key: string },
+ count: number,
+) {
+ for (let i = 0; i < count; i++) {
+ const { status: clickStatus, data: clickData } = await http.post({
+ path: "/track/click",
+ headers: E2E_TRACK_CLICK_HEADERS,
+ body: {
+ domain: partnerLink.domain,
+ key: partnerLink.key,
+ },
+ });
+
+ expect(clickStatus).toEqual(200);
+ expect(clickData.clickId).toBeDefined();
+
+ const { status: leadStatus } = await http.post({
+ path: "/track/lead",
+ body: {
+ clickId: clickData.clickId,
+ eventName: `Signup-${i}-${Date.now()}`,
+ customerExternalId: `e2e-customer-${i}-${Date.now()}`,
+ customerEmail: `customer${i}@example.com`,
+ },
+ });
+
+ expect(leadStatus).toEqual(200);
+
+ await new Promise((resolve) => setTimeout(resolve, 200));
+ }
+}