The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add tests for creating links with error cases and user-defined key
Kiran K4b3f68b705b2
diff --git a/apps/web/tests/links/create-link-error.test.ts b/apps/web/tests/links/create-link-error.test.tsnew file mode 100644index 0000000000..1ee4827faf--- /dev/null+++ b/apps/web/tests/links/create-link-error.test.ts@@ -0,0 +1,86 @@+import { expect, test } from "vitest";+import { HttpClient } from "../utils/http";+import { Link } from "@prisma/client";+import { IntegrationHarness } from "../utils/integration";++const domain = "dub.sh";+const url = "https://github.com/dubinc";++const cases = [+ {+ name: "create link with domain not belonging to workspace",+ body: {+ domain: "google.com",+ url,+ },+ expected: {+ status: 403,+ data: {+ error: {+ code: "forbidden",+ message: "Domain does not belong to workspace.",+ doc_url: "https://dub.co/docs/api-reference/errors#forbidden",+ },+ },+ },+ },+ {+ name: "create link with invalid destination url",+ body: {+ domain,+ url: "invalid",+ },+ expected: {+ status: 422,+ data: {+ error: {+ code: "unprocessable_entity",+ message: "Invalid destination url.",+ doc_url:+ "https://dub.co/docs/api-reference/errors#unprocessable_entity",+ },+ },+ },+ },+ {+ name: "create link with invalid tag id",+ body: {+ domain,+ url,+ tagIds: ["invalid"],+ },+ expected: {+ status: 422,+ data: {+ error: {+ code: "unprocessable_entity",+ message: "Invalid tagIds detected: invalid",+ doc_url:+ "https://dub.co/docs/api-reference/errors#unprocessable_entity",+ },+ },+ },+ },+];++cases.forEach(({ name, body, expected }) => {+ test(name, async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, apiKey } = await h.init();++ const http = new HttpClient({+ baseUrl: h.baseUrl,+ headers: {+ Authorization: `Bearer ${apiKey.token}`,+ },+ });++ const response = await http.post<Link>({+ path: "/links",+ query: { workspaceId: workspace.workspaceId },+ body,+ });++ expect(response).toEqual(expected);+ });+});diff --git a/apps/web/tests/links/create-link.test.ts b/apps/web/tests/links/create-link.test.tsindex e5910d837c..f5a9227e69 100644--- a/apps/web/tests/links/create-link.test.ts+++ b/apps/web/tests/links/create-link.test.ts@@ -3,6 +3,7 @@ import { HttpClient } from "../utils/http";import { Link } from "@prisma/client";import { IntegrationHarness } from "../utils/integration";import { expectedLink } from "../utils/schema";+import { nanoid } from "@dub/utils";test("creates new link", async (ctx) => {const h = new IntegrationHarness(ctx);@@ -40,3 +41,43 @@ test("creates new link", async (ctx) => {tags: [],});});++test("creates new link with user defined key", async (ctx) => {+ const h = new IntegrationHarness(ctx);+ const { workspace, apiKey, user } = await h.init();++ const http = new HttpClient({+ baseUrl: h.baseUrl,+ headers: {+ Authorization: `Bearer ${apiKey.token}`,+ },+ });++ const url = "https://github.com/dubinc";+ const domain = "dub.sh";+ const key = nanoid(6);++ const { status, data: link } = await http.post<Link>({+ path: "/links",+ query: { workspaceId: workspace.workspaceId },+ body: {+ url,+ domain,+ key,+ },+ });++ expect(status).toEqual(200);+ expect(link).toEqual({+ ...expectedLink,+ key,+ domain,+ url,+ userId: user.id,+ projectId: workspace.id,+ workspaceId: workspace.workspaceId,+ shortLink: `https://${domain}/${key}`,+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${key}`,+ tags: [],+ });+});diff --git a/apps/web/tests/utils/http.ts b/apps/web/tests/utils/http.tsindex d6b4df023d..b4c858a8f0 100644--- a/apps/web/tests/utils/http.ts+++ b/apps/web/tests/utils/http.ts@@ -48,6 +48,10 @@ export class HttpClient {const { status } = response;const data = (await response.json()) as TResponse;+ if (!response.ok) {+ console.error(`Request to ${url} failed with status ${status}`);+ }+return { data, status };}