Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: add tests to cron jobs #973

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/web/lib/cron/verify-qstash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ export const verifyQstashSignature = async (
req: Request,
body?: Record<string, unknown>,
) => {
if (process.env.NODE_ENV === "development") {
return;
}

body = body || (await req.json());

const isValid = await receiver.verify({
Expand Down
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
39 changes: 39 additions & 0 deletions apps/web/tests/cron-jobs/delete-public-link.test.ts
Original file line number Diff line number Diff line change
@@ -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("/");
});
8 changes: 1 addition & 7 deletions apps/web/tests/redirects/index.test.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
8 changes: 8 additions & 0 deletions apps/web/tests/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -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",
},
};
Loading