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 Playwright API tests for UTM templates.

Kiran K98e260cbc0af
diff --git a/apps/web/playwright/api/utm/utm.spec.ts b/apps/web/playwright/api/utm/utm.spec.ts
new file mode 100644
index 0000000000..cce166dbcf
--- /dev/null
+++ b/apps/web/playwright/api/utm/utm.spec.ts
@@ -0,0 +1,337 @@
+import { expect } from "@playwright/test";
+import type { UtmTemplate } from "@prisma/client";
+import { randomName } from "../../utils";
+import { test, type ApiClient } from "../fixtures";
+
+type UtmTemplateResponse = Pick<
+ UtmTemplate,
+ | "id"
+ | "name"
+ | "utm_source"
+ | "utm_medium"
+ | "utm_campaign"
+ | "utm_term"
+ | "utm_content"
+ | "ref"
+ | "userId"
+ | "projectId"
+> & {
+ createdAt: string;
+ updatedAt: string;
+};
+
+const expectedUtmTemplate = {
+ id: expect.any(String),
+ utm_source: null,
+ utm_medium: null,
+ utm_campaign: null,
+ utm_term: null,
+ utm_content: null,
+ ref: null,
+ userId: expect.any(String),
+ projectId: expect.any(String),
+ createdAt: expect.any(String),
+ updatedAt: expect.any(String),
+};
+
+async function createUtmTemplate(
+ api: ApiClient,
+ overrides: Record<string, unknown> = {},
+) {
+ return api.post<UtmTemplateResponse>("/api/utm", {
+ name: randomName("utm"),
+ ...overrides,
+ });
+}
+
+async function deleteUtmTemplate(api: ApiClient, id: string | undefined) {
+ if (!id) return;
+ await api.delete(`/api/utm/${id}`);
+}
+
+test.describe.configure({
+ mode: "parallel",
+});
+
+test("POST /utm", async ({ api, workspace }) => {
+ let id: string | undefined;
+ const body = {
+ name: randomName("utm"),
+ utm_source: "facebook",
+ utm_medium: "social",
+ utm_campaign: "summer",
+ utm_term: "shoes",
+ utm_content: "cta",
+ ref: "partner",
+ };
+
+ try {
+ const { status, data } = await api.post<UtmTemplateResponse>(
+ "/api/utm",
+ body,
+ );
+ id = data.id;
+
+ expect(status).toEqual(201);
+ expect(data).toStrictEqual({
+ ...expectedUtmTemplate,
+ ...body,
+ projectId: workspace.id,
+ });
+ } finally {
+ await deleteUtmTemplate(api, id);
+ }
+});
+
+test("POST /utm – utm tag at max length", async ({ api }) => {
+ let id: string | undefined;
+ const utmSource = "a".repeat(255);
+
+ try {
+ const { status, data } = await createUtmTemplate(api, {
+ utm_source: utmSource,
+ });
+ id = data.id;
+
+ expect(status).toEqual(201);
+ expect(data.utm_source).toBe(utmSource);
+ } finally {
+ await deleteUtmTemplate(api, id);
+ }
+});
+
+test("POST /utm – empty utm tags become null", async ({ api }) => {
+ let id: string | undefined;
+
+ try {
+ const { status, data } = await createUtmTemplate(api, {
+ utm_source: "",
+ utm_medium: "",
+ });
+ id = data.id;
+
+ expect(status).toEqual(201);
+ expect(data.utm_source).toBeNull();
+ expect(data.utm_medium).toBeNull();
+ } finally {
+ await deleteUtmTemplate(api, id);
+ }
+});
+
+const errorCases = [
+ {
+ name: "POST /utm – missing name",
+ body: {},
+ expected: {
+ status: 422,
+ data: {
+ error: {
+ code: "unprocessable_entity",
+ message:
+ "invalid_type: name: Invalid input: expected string, received undefined",
+ doc_url:
+ "https://dub.co/docs/api-reference/errors#unprocessable-entity",
+ },
+ },
+ },
+ },
+ {
+ name: "POST /utm – empty name",
+ body: { name: "" },
+ expected: {
+ status: 422,
+ data: {
+ error: {
+ code: "unprocessable_entity",
+ message: "too_small: name: UTM name is required",
+ doc_url:
+ "https://dub.co/docs/api-reference/errors#unprocessable-entity",
+ },
+ },
+ },
+ },
+ {
+ name: "POST /utm – name too long",
+ body: { name: "a".repeat(51) },
+ expected: {
+ status: 422,
+ data: {
+ error: {
+ code: "unprocessable_entity",
+ message:
+ "too_big: name: Too big: expected string to have <=50 characters",
+ doc_url:
+ "https://dub.co/docs/api-reference/errors#unprocessable-entity",
+ },
+ },
+ },
+ },
+ {
+ name: "POST /utm – utm_source too long",
+ body: { name: randomName("utm"), utm_source: "a".repeat(256) },
+ expected: {
+ status: 422,
+ data: {
+ error: {
+ code: "unprocessable_entity",
+ message:
+ "too_big: utm_source: Too big: expected string to have <=255 characters",
+ doc_url:
+ "https://dub.co/docs/api-reference/errors#unprocessable-entity",
+ },
+ },
+ },
+ },
+];
+
+for (const { name, body, expected } of errorCases) {
+ test(name, async ({ api }) => {
+ expect(await api.post("/api/utm", body)).toEqual(expected);
+ });
+}
+
+test("POST /utm – existing name", async ({ api }) => {
+ let id: string | undefined;
+ const templateName = randomName("utm");
+
+ try {
+ const { data: created } = await createUtmTemplate(api, {
+ name: templateName,
+ });
+ id = created.id;
+
+ const { status, data: error } = await api.post("/api/utm", {
+ name: templateName,
+ });
+
+ expect(status).toBe(409);
+ expect(error).toEqual({
+ error: {
+ code: "conflict",
+ message: "A template with that name already exists.",
+ doc_url: "https://dub.co/docs/api-reference/errors#conflict",
+ },
+ });
+ } finally {
+ await deleteUtmTemplate(api, id);
+ }
+});
+
+test("GET /utm", async ({ api }) => {
+ let id: string | undefined;
+
+ try {
+ const { data: created } = await createUtmTemplate(api, {
+ utm_source: "google",
+ utm_medium: "cpc",
+ });
+ id = created.id;
+
+ const { status, data: templates } =
+ await api.get<(UtmTemplateResponse & { user: { id: string } })[]>(
+ "/api/utm",
+ );
+
+ expect(status).toEqual(200);
+ expect(templates).toEqual(
+ expect.arrayContaining([
+ expect.objectContaining({
+ id: created.id,
+ name: created.name,
+ utm_source: "google",
+ utm_medium: "cpc",
+ user: expect.objectContaining({ id: created.userId }),
+ }),
+ ]),
+ );
+ } finally {
+ await deleteUtmTemplate(api, id);
+ }
+});
+
+test("PATCH /utm/{id}", async ({ api }) => {
+ let id: string | undefined;
+ const name = randomName("utm");
+
+ try {
+ const { data: created } = await createUtmTemplate(api, {
+ name,
+ utm_source: "facebook",
+ utm_medium: "social",
+ });
+ id = created.id;
+
+ const update = {
+ name: `${name}-updated`,
+ utm_source: "twitter",
+ utm_medium: null,
+ utm_campaign: "launch",
+ utm_term: null,
+ utm_content: null,
+ ref: "blog",
+ };
+
+ const { status, data } = await api.patch<
+ UtmTemplateResponse & { partnerGroup: null }
+ >(`/api/utm/${id}`, update);
+
+ expect(status).toEqual(200);
+ expect(data).toStrictEqual({
+ ...expectedUtmTemplate,
+ ...update,
+ id: created.id,
+ userId: created.userId,
+ projectId: created.projectId,
+ createdAt: created.createdAt,
+ updatedAt: expect.any(String),
+ partnerGroup: null,
+ });
+ } finally {
+ await deleteUtmTemplate(api, id);
+ }
+});
+
+test("PATCH /utm/{id} – not found", async ({ api }) => {
+ expect(
+ await api.patch("/api/utm/utm_missing", { name: randomName("utm") }),
+ ).toEqual({
+ status: 404,
+ data: {
+ error: {
+ code: "not_found",
+ message: "Template not found.",
+ doc_url: "https://dub.co/docs/api-reference/errors#not-found",
+ },
+ },
+ });
+});
+
+test("DELETE /utm/{id}", async ({ api }) => {
+ const { data: created } = await createUtmTemplate(api);
+
+ const { status, data } = await api.delete<{ id: string }>(
+ `/api/utm/${created.id}`,
+ );
+
+ expect(status).toEqual(200);
+ expect(data).toStrictEqual({ id: created.id });
+
+ expect(
+ await api.patch(`/api/utm/${created.id}`, { name: randomName("utm") }),
+ ).toMatchObject({
+ status: 404,
+ });
+});
+
+test("DELETE /utm/{id} – not found", async ({ api }) => {
+ expect(await api.delete("/api/utm/utm_missing")).toEqual({
+ status: 404,
+ data: {
+ error: {
+ code: "not_found",
+ message: "UTM template not found.",
+ doc_url: "https://dub.co/docs/api-reference/errors#not-found",
+ },
+ },
+ });
+});