From 2f71fe218bfbd8022fe2d4571e8842e92ef5ccd6 Mon Sep 17 00:00:00 2001 From: Kiran K Date: Fri, 7 Jun 2024 23:08:19 +0530 Subject: [PATCH] wip: add tests to cron jobs --- apps/web/lib/cron/verify-qstash.ts | 4 ++ apps/web/package.json | 2 +- .../cron-jobs/delete-public-link.test.ts | 39 +++++++++++++++++++ apps/web/tests/redirects/index.test.ts | 8 +--- apps/web/tests/utils/helpers.ts | 8 ++++ 5 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 apps/web/tests/cron-jobs/delete-public-link.test.ts diff --git a/apps/web/lib/cron/verify-qstash.ts b/apps/web/lib/cron/verify-qstash.ts index 729a8fa499..68be86fdaf 100644 --- a/apps/web/lib/cron/verify-qstash.ts +++ b/apps/web/lib/cron/verify-qstash.ts @@ -11,6 +11,10 @@ export const verifyQstashSignature = async ( req: Request, body?: Record, ) => { + if (process.env.NODE_ENV === "development") { + return; + } + body = body || (await req.json()); const isValid = await receiver.verify({ diff --git a/apps/web/package.json b/apps/web/package.json index 41a089001f..6527d178bf 100644 --- a/apps/web/package.json +++ b/apps/web/package.json @@ -8,7 +8,7 @@ "lint": "next lint", "start": "next start", "script": "tsx ./scripts/run.ts", - "test": "vitest -no-file-parallelism", + "test": "vitest -no-file-parallelism cron-jobs/", "generate-openapi": "tsx ./scripts/generate-openapi.ts" }, "dependencies": { diff --git a/apps/web/tests/cron-jobs/delete-public-link.test.ts b/apps/web/tests/cron-jobs/delete-public-link.test.ts new file mode 100644 index 0000000000..314368ae51 --- /dev/null +++ b/apps/web/tests/cron-jobs/delete-public-link.test.ts @@ -0,0 +1,39 @@ +import { expect, test } from "vitest"; +import { fetchOptions } from "../../tests/utils/helpers"; +import { env } from "../utils/env"; +import { IntegrationHarness } from "../utils/integration"; +import { link } from "../utils/resource"; + +const { domain, url } = link; + +test.runIf(env.CI)("delete public link", async () => { + const h = new IntegrationHarness(); + + let response = await fetch(`${h.baseUrl}/api/links`, { + method: "POST", + body: JSON.stringify({ + domain, + url, + publicStats: true, + }), + }); + + const link = await response.json(); + + // Run the cron job to delete the public link + response = await fetch(`${h.baseUrl}/api/cron/links/delete`, { + method: "POST", + body: JSON.stringify({ + linkId: link.id, + }), + }); + + expect(response.status).toBe(200); + expect(await response.text()).toBe("Link deleted."); + + // Verify the shortLink was deleted + response = await fetch(link.shortLink, fetchOptions); + + expect(response.status).toBe(302); + expect(response.headers.get("location")).toBe("/"); +}); diff --git a/apps/web/tests/redirects/index.test.ts b/apps/web/tests/redirects/index.test.ts index e883a18583..da558767e5 100644 --- a/apps/web/tests/redirects/index.test.ts +++ b/apps/web/tests/redirects/index.test.ts @@ -1,15 +1,9 @@ import { describe, expect, test } from "vitest"; +import { fetchOptions } from "../../tests/utils/helpers"; import { env } from "../utils/env"; import { IntegrationHarness } from "../utils/integration"; const poweredBy = "Dub.co - Link management for modern marketing teams"; -const fetchOptions: RequestInit = { - cache: "no-store", - redirect: "manual", - headers: { - "dub-no-track": "true", - }, -}; describe.runIf(env.CI)("Link Redirects", async () => { const h = new IntegrationHarness(); diff --git a/apps/web/tests/utils/helpers.ts b/apps/web/tests/utils/helpers.ts index 6e782f940f..d694c822fd 100644 --- a/apps/web/tests/utils/helpers.ts +++ b/apps/web/tests/utils/helpers.ts @@ -1 +1,9 @@ export const randomId = () => Math.random().toString(36).substr(2, 9); + +export const fetchOptions: RequestInit = { + cache: "no-store", + redirect: "manual", + headers: { + "dub-no-track": "true", + }, +};