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 tests

Steven Tey3f6ecbce4fa2
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index b4c8a811a5..c61af5365b 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -105,9 +105,9 @@ export const POST = withWorkspace(
// remove link from validLinks and add error to errorLinks
validLinks = validLinks.filter((_, i) => i !== index);
errorLinks.push({
- link,
error: `Invalid tagIds detected: ${invalidTagIds.join(", ")}`,
code: "unprocessable_entity",
+ link,
});
}
@@ -117,9 +117,9 @@ export const POST = withWorkspace(
if (invalidTagNames?.length) {
validLinks = validLinks.filter((_, i) => i !== index);
errorLinks.push({
- link,
error: `Invalid tagNames detected: ${invalidTagNames.join(", ")}`,
code: "unprocessable_entity",
+ link,
});
}
});
@@ -155,9 +155,9 @@ export const PATCH = withWorkspace(
let errorLinks = linkIds
.filter((id) => links.find((link) => link.id === id) === undefined)
.map((id) => ({
- link: { id },
error: "Link not found",
code: "not_found",
+ link: { id },
}));
let { tagNames, expiresAt } = data;
@@ -224,21 +224,24 @@ export const PATCH = withWorkspace(
processedLinks
.filter(({ error }) => error != null)
.map(({ link, error, code }) => ({
- link,
error: error as string,
code: code as string,
+ link,
})),
);
- const response = await bulkUpdateLinks({
- linkIds: validLinkIds,
- data: {
- ...data,
- tagIds,
- expiresAt,
- },
- workspaceId: workspace.id,
- });
+ const response =
+ validLinkIds.length > 0
+ ? await bulkUpdateLinks({
+ linkIds: validLinkIds,
+ data: {
+ ...data,
+ tagIds,
+ expiresAt,
+ },
+ workspaceId: workspace.id,
+ })
+ : [];
return NextResponse.json([...response, ...errorLinks], { headers });
},
diff --git a/apps/web/tests/links/bulk-update-link.test.ts b/apps/web/tests/links/bulk-update-link.test.ts
new file mode 100644
index 0000000000..4ddc68586d
--- /dev/null
+++ b/apps/web/tests/links/bulk-update-link.test.ts
@@ -0,0 +1,88 @@
+import { DUB_DOMAINS } from "@dub/utils";
+import { Link } from "@prisma/client";
+import { expect, test } from "vitest";
+import { randomId } from "../utils/helpers";
+import { IntegrationHarness } from "../utils/integration";
+import { link } from "../utils/resource";
+import { expectedLink } from "../utils/schema";
+
+const { domain, url } = link;
+
+test("PATCH /links/bulk", async (ctx) => {
+ const h = new IntegrationHarness(ctx);
+ const { workspace, http, user } = await h.init();
+ const { workspaceId } = workspace;
+ const projectId = workspaceId.replace("ws_", "");
+
+ const { data: createdLinks } = await http.post<Link[]>({
+ path: "/links/bulk",
+ query: { workspaceId },
+ body: [
+ {
+ url: `https://example.com/${randomId()}`,
+ domain,
+ },
+ {
+ url,
+ domain: "git.new",
+ },
+ ],
+ });
+
+ // add a link that will not be found
+ const linkIds = createdLinks.map(({ id }) => id).concat(["xxx"]);
+ const newData = {
+ url: `https://example.com/${randomId()}`,
+ tagIds: ["clvkopm8b0009nf98azsp9epk"],
+ };
+
+ const { status, data: links } = await http.patch<Link[]>({
+ path: "/links/bulk",
+ query: { workspaceId },
+ body: {
+ linkIds,
+ data: newData,
+ },
+ });
+
+ expect(status).toEqual(200);
+ expect(links).toHaveLength(3);
+
+ // first link should be updated
+ expect(links[0]).toStrictEqual({
+ ...expectedLink,
+ url: newData.url,
+ userId: user.id,
+ projectId,
+ workspaceId,
+ tags: [
+ {
+ id: "clvkopm8b0009nf98azsp9epk",
+ name: "E2E Tests (DO NOT DELETE)",
+ color: "red",
+ },
+ ],
+ shortLink: `https://${domain}/${createdLinks[0].key}`,
+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${createdLinks[0].key}?qr=1`,
+ });
+
+ const { allowedHostnames } = DUB_DOMAINS.find((d) => d.slug === "git.new")!;
+
+ // second link will throw an error because git.new only allows certain destination URLs
+ expect(links[1]).toStrictEqual({
+ error: `Invalid url. You can only use git.new short links for URLs starting with ${allowedHostnames!
+ .map((d) => `\`${d}\``)
+ .join(", ")}.`,
+ code: "unprocessable_entity",
+ link: createdLinks[1],
+ });
+
+ // third link should throw an error because it does not exist
+ expect(links[2]).toStrictEqual({
+ error: "Link not found",
+ code: "not_found",
+ link: { id: "xxx" },
+ });
+
+ await Promise.all([h.deleteLink(links[0].id), h.deleteLink(links[1].id)]);
+});