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.

Update tag creation and deletion tests

Kiran Kfdc6986826f5
diff --git a/apps/web/lib/auth/workspace.ts b/apps/web/lib/auth/workspace.ts
index ee6be14a87..4371d41088 100644
--- a/apps/web/lib/auth/workspace.ts
+++ b/apps/web/lib/auth/workspace.ts
@@ -207,6 +207,8 @@ export const withWorkspace = (
slug: true,
logo: true,
usage: true,
+ aiUsage: true,
+ aiLimit: true,
usageLimit: true,
linksUsage: true,
linksLimit: true,
@@ -217,6 +219,8 @@ export const withWorkspace = (
stripeId: true,
billingCycleStart: true,
createdAt: true,
+ updatedAt: true,
+ monitoringId: true,
users: {
where: {
userId: session.user.id,
diff --git a/apps/web/tests/links/create-link.test.ts b/apps/web/tests/links/create-link.test.ts
index 2092d44a0a..a8b8531127 100644
--- a/apps/web/tests/links/create-link.test.ts
+++ b/apps/web/tests/links/create-link.test.ts
@@ -10,7 +10,8 @@ const { domain, url } = link;
describe("POST /links", async () => {
const h = new IntegrationHarness();
const { workspace, user, http } = await h.init();
- const { workspaceId, id: projectId } = workspace;
+ const { workspaceId } = workspace;
+ const projectId = workspaceId.replace("ws_", "");
test("default domain", async () => {
const { status, data: link } = await http.post<Link>({
@@ -25,7 +26,7 @@ describe("POST /links", async () => {
});
expect(status).toEqual(200);
- expect(link).toMatchObject({
+ expect(link).toStrictEqual({
...expectedLink,
url,
publicStats: true,
@@ -55,7 +56,7 @@ describe("POST /links", async () => {
});
expect(status).toEqual(200);
- expect(link).toMatchObject({
+ expect(link).toStrictEqual({
...expectedLink,
key,
url,
@@ -87,7 +88,7 @@ describe("POST /links", async () => {
expect(status).toEqual(200);
expect(link.key.startsWith(prefix)).toBeTruthy();
- expect(link).toMatchObject({
+ expect(link).toStrictEqual({
...expectedLink,
domain,
url,
@@ -125,7 +126,7 @@ describe("POST /links", async () => {
});
expect(status).toEqual(200);
- expect(link).toMatchObject({
+ expect(link).toStrictEqual({
...expectedLink,
...utm,
url: longUrl.href,
@@ -153,7 +154,7 @@ describe("POST /links", async () => {
});
expect(status).toEqual(200);
- expect(link).toMatchObject({
+ expect(link).toStrictEqual({
...expectedLink,
url,
password,
@@ -183,7 +184,7 @@ describe("POST /links", async () => {
});
expect(status).toEqual(200);
- expect(link).toMatchObject({
+ expect(link).toStrictEqual({
...expectedLink,
url,
expiresAt: "2030-04-16T11:30:00.000Z",
@@ -215,7 +216,7 @@ describe("POST /links", async () => {
});
expect(status).toEqual(200);
- expect(link).toMatchObject({
+ expect(link).toStrictEqual({
...expectedLink,
url,
ios,
@@ -248,7 +249,7 @@ describe("POST /links", async () => {
});
expect(status).toEqual(200);
- expect(link).toMatchObject({
+ expect(link).toStrictEqual({
...expectedLink,
url,
geo,
@@ -265,8 +266,8 @@ describe("POST /links", async () => {
test("tags", async () => {
const tagsToCreate = [
- { tag: "news", color: "red" },
- { tag: "work", color: "green" },
+ { tag: nanoid(6), color: "red" },
+ { tag: nanoid(6), color: "green" },
];
const response = await Promise.all(
@@ -299,7 +300,7 @@ describe("POST /links", async () => {
expect(status).toEqual(200);
expect(link.tags).toHaveLength(2);
- expect(link).toMatchObject({
+ expect(link).toStrictEqual({
...expectedLink,
url,
tagId: expect.any(String), // TODO: Fix this
@@ -311,7 +312,10 @@ describe("POST /links", async () => {
tags: expect.arrayContaining(tags),
});
- await h.deleteLink(link.id);
+ await Promise.all([
+ ...tagIds.map((id) => h.deleteTag(id)),
+ h.deleteLink(link.id),
+ ]);
});
test("custom social media cards", async () => {
@@ -329,7 +333,7 @@ describe("POST /links", async () => {
});
expect(status).toEqual(200);
- expect(link).toMatchObject({
+ expect(link).toStrictEqual({
...expectedLink,
url,
title,
diff --git a/apps/web/tests/links/delete-link.test.ts b/apps/web/tests/links/delete-link.test.ts
index 7551fc3b25..8e7d47290e 100644
--- a/apps/web/tests/links/delete-link.test.ts
+++ b/apps/web/tests/links/delete-link.test.ts
@@ -25,7 +25,7 @@ test("DELETE /links/{linkId}", async (ctx) => {
});
expect(status).toBe(200);
- expect(data).toMatchObject({
+ expect(data).toStrictEqual({
id: link.id,
});
diff --git a/apps/web/tests/links/list-links.test.ts b/apps/web/tests/links/list-links.test.ts
index acdacb8637..443fcb1c17 100644
--- a/apps/web/tests/links/list-links.test.ts
+++ b/apps/web/tests/links/list-links.test.ts
@@ -8,77 +8,45 @@ const { domain, url } = link;
test("GET /links", async (ctx) => {
const h = new IntegrationHarness(ctx);
const { workspace, http, user } = await h.init();
- const { workspaceId, id: projectId } = workspace;
+ const { workspaceId } = workspace;
+ const projectId = workspaceId.replace("ws_", "");
- // Create a link
const { data: firstLink } = await http.post<Link>({
path: "/links",
query: { workspaceId },
body: { url, domain },
});
- // Fake delay
- await new Promise((resolve) => setTimeout(resolve, 2000));
-
- // Create another link
- const { data: secondLink } = await http.post<Link>({
- path: "/links",
- query: { workspaceId },
- body: { url, domain },
- });
-
- // List links
const { data: links, status } = await http.get<Link[]>({
path: "/links",
query: { workspaceId },
});
+ const linkFound = links.find((l) => l.id === firstLink.id);
+
expect(status).toEqual(200);
- expect(links.length).toBeGreaterThanOrEqual(2);
- // expect(links).toContain([
- // {
- // ...expectedLink,
- // domain,
- // url,
- // userId: user.id,
- // projectId,
- // workspaceId,
- // tags: [],
- // shortLink: `https://${domain}/${secondLink.key}`,
- // qrCode: `https://api.dub.co/qr?url=https://${domain}/${secondLink.key}?qr=1`,
- // user: {
- // id: user.id,
- // createdAt: expect.any(String),
- // email: expect.any(String),
- // emailVerified: null,
- // image: expect.any(String),
- // name: expect.any(String),
- // source: null,
- // subscribed: true,
- // },
- // },
- // {
- // ...expectedLink,
- // domain,
- // url,
- // userId: user.id,
- // projectId,
- // workspaceId,
- // tags: [],
- // shortLink: `https://${domain}/${firstLink.key}`,
- // qrCode: `https://api.dub.co/qr?url=https://${domain}/${firstLink.key}?qr=1`,
- // user: {
- // id: user.id,
- // createdAt: expect.any(String),
- // email: expect.any(String),
- // emailVerified: null,
- // image: expect.any(String),
- // name: expect.any(String),
- // source: null,
- // subscribed: true,
- // },
- // },
- // ]);
+ expect(links.length).toBeGreaterThanOrEqual(1);
+ expect(linkFound).toStrictEqual({
+ ...firstLink,
+ domain,
+ url,
+ userId: user.id,
+ projectId,
+ workspaceId,
+ tags: [],
+ shortLink: `https://${domain}/${firstLink.key}`,
+ qrCode: `https://api.dub.co/qr?url=https://${domain}/${firstLink.key}?qr=1`,
+ user: {
+ id: user.id,
+ emailVerified: null,
+ source: null,
+ subscribed: true,
+ image: expect.any(String),
+ name: expect.any(String),
+ createdAt: expect.any(String),
+ email: expect.any(String),
+ },
+ });
- await Promise.all([h.deleteLink(firstLink.id), h.deleteLink(secondLink.id)]);
+ await h.deleteLink(firstLink.id);
});
diff --git a/apps/web/tests/links/retrieve-link.test.ts b/apps/web/tests/links/retrieve-link.test.ts
index 1fc7a374aa..00f46816c0 100644
--- a/apps/web/tests/links/retrieve-link.test.ts
+++ b/apps/web/tests/links/retrieve-link.test.ts
@@ -10,7 +10,8 @@ const { domain, url } = link;
test("GET /links/info", async (ctx) => {
const h = new IntegrationHarness(ctx);
const { workspace, http, user } = await h.init();
- const { workspaceId, id: projectId } = workspace;
+ const { workspaceId } = workspace;
+ const projectId = workspaceId.replace("ws_", "");
const newLink: Partial<Link> = {
url,
@@ -39,13 +40,13 @@ test("GET /links/info", async (ctx) => {
});
expect(status).toEqual(200);
- expect(link).toMatchObject({
+ expect(link).toStrictEqual({
...expectedLink,
...newLink,
- expiresAt: "2030-04-16T18:29:59.000Z",
- userId: user.id,
projectId,
workspaceId,
+ userId: user.id,
+ expiresAt: "2030-04-16T18:29:59.000Z",
shortLink: `https://${domain}/${newLink.key}`,
qrCode: `https://api.dub.co/qr?url=https://${domain}/${newLink.key}?qr=1`,
tags: [],
diff --git a/apps/web/tests/links/update-link.test.ts b/apps/web/tests/links/update-link.test.ts
index 7b270ea6c3..2895e58c06 100644
--- a/apps/web/tests/links/update-link.test.ts
+++ b/apps/web/tests/links/update-link.test.ts
@@ -7,17 +7,11 @@ import { expectedLink } from "../utils/schema";
const { domain, url } = link;
-// TODO:
-// Add/update/delete tags
-
describe("PUT /links/{linkId}", async () => {
const h = new IntegrationHarness();
const { workspace, http, user } = await h.init();
- const { workspaceId, id: projectId } = workspace;
-
- // afterAll(async () => {
- // await h.teardown();
- // });
+ const { workspaceId } = workspace;
+ const projectId = workspaceId.replace("ws_", "");
test("update link", async () => {
// Update the link with new data
@@ -54,7 +48,7 @@ describe("PUT /links/{linkId}", async () => {
body: newLink,
});
- expect(updatedLink).toMatchObject({
+ expect(updatedLink).toStrictEqual({
...expectedLink,
...newLink,
expiresAt: "2030-04-16T18:29:59.000Z",
@@ -78,7 +72,7 @@ describe("PUT /links/{linkId}", async () => {
workspaceId,
shortLink: `https://${domain}/${newLink.key}`,
qrCode: `https://api.dub.co/qr?url=https://${domain}/${newLink.key}?qr=1`,
- }).toMatchObject(updatedLink);
+ }).toStrictEqual(updatedLink);
await h.deleteLink(link.id);
});
@@ -103,7 +97,7 @@ describe("PUT /links/{linkId}", async () => {
});
expect(status).toEqual(200);
- expect(updatedLink).toMatchObject({
+ expect(updatedLink).toStrictEqual({
...expectedLink,
archived: true,
domain,
@@ -147,7 +141,7 @@ describe("PUT /links/{linkId}", async () => {
});
expect(status).toEqual(200);
- expect(updatedLink).toMatchObject({
+ expect(updatedLink).toStrictEqual({
...expectedLink,
archived: false,
domain,
diff --git a/apps/web/tests/metatags/retrieve-metatags.test.ts b/apps/web/tests/metatags/retrieve-metatags.test.ts
index d917d9d4b8..f1d6ea082e 100644
--- a/apps/web/tests/metatags/retrieve-metatags.test.ts
+++ b/apps/web/tests/metatags/retrieve-metatags.test.ts
@@ -18,7 +18,7 @@ test("GET /metatags", async (ctx) => {
});
expect(status).toEqual(200);
- expect(metatags).toMatchObject({
+ expect(metatags).toStrictEqual({
title: "Dub.co - Link Management for Modern Marketing Teams",
description:
"Dub.co is the open-source link management infrastructure for modern marketing teams to create, share, and track short links.",
diff --git a/apps/web/tests/tags/create-tag.test.ts b/apps/web/tests/tags/create-tag.test.ts
index 5ea5bafc0d..b0c6850195 100644
--- a/apps/web/tests/tags/create-tag.test.ts
+++ b/apps/web/tests/tags/create-tag.test.ts
@@ -18,11 +18,11 @@ test("POST /tags", async (ctx) => {
});
expect(status).toEqual(201);
- expect(tag).toEqual({
+ expect(tag).toStrictEqual({
...expectedTag,
name: "social",
color: "red",
- projectId: workspace.id,
+ projectId: workspaceId.replace("ws_", ""),
});
await h.deleteTag(tag.id);
diff --git a/apps/web/tests/tags/list-tags.test.ts b/apps/web/tests/tags/list-tags.test.ts
index 5e6d2f453a..56038f3286 100644
--- a/apps/web/tests/tags/list-tags.test.ts
+++ b/apps/web/tests/tags/list-tags.test.ts
@@ -5,6 +5,7 @@ import { IntegrationHarness } from "../utils/integration";
test("GET /tags", async (ctx) => {
const h = new IntegrationHarness(ctx);
const { workspace, http } = await h.init();
+ const { workspaceId } = workspace;
const tagsToCreate = [
{ tag: "news", color: "red" },
@@ -14,7 +15,7 @@ test("GET /tags", async (ctx) => {
for (const { tag, color } of tagsToCreate) {
await http.post<Tag>({
path: "/tags",
- query: { workspaceId: workspace.workspaceId },
+ query: { workspaceId },
body: { tag, color },
});
}
@@ -22,11 +23,11 @@ test("GET /tags", async (ctx) => {
// Fetch the list of tags
const { status, data: tags } = await http.get<Tag[]>({
path: "/tags",
- query: { workspaceId: workspace.workspaceId },
+ query: { workspaceId },
});
expect(status).toEqual(200);
- expect(tags).toEqual([
+ expect(tags).toStrictEqual([
{
id: expect.any(String),
name: "news",
diff --git a/apps/web/tests/utils/schema.ts b/apps/web/tests/utils/schema.ts
index 7e67664040..c543574592 100644
--- a/apps/web/tests/utils/schema.ts
+++ b/apps/web/tests/utils/schema.ts
@@ -42,9 +42,9 @@ export const expectedWorkspace: Partial<Project> = {
logo: null,
plan: "free",
stripeId: null,
- usage: 0,
+ usage: expect.any(Number),
usageLimit: 1000,
- linksUsage: 0,
+ linksUsage: expect.any(Number),
linksLimit: 25,
domainsLimit: 3,
tagsLimit: 5,
diff --git a/apps/web/tests/workspaces/create-workspace-error.test.ts b/apps/web/tests/workspaces/create-workspace-error.test.ts
index 276b7b83b2..be46d4d3c6 100644
--- a/apps/web/tests/workspaces/create-workspace-error.test.ts
+++ b/apps/web/tests/workspaces/create-workspace-error.test.ts
@@ -1,20 +1,10 @@
import { Project } from "@prisma/client";
import { expect, test } from "vitest";
-import { HttpClient } from "../utils/http";
import { IntegrationHarness } from "../utils/integration";
test("should not create workspace with slug in use", async (ctx) => {
const h = new IntegrationHarness(ctx);
- const { apiKey, workspace } = await h.init();
-
- const http = new HttpClient({
- baseUrl: h.baseUrl,
- headers: {
- Authorization: `Bearer ${apiKey.token}`,
- },
- });
-
- console.log("existing workspace", workspace);
+ const { workspace, http } = await h.init();
// Create another workspace with the same slug
const { status, data: error } = await http.post<Project>({
@@ -25,11 +15,8 @@ test("should not create workspace with slug in use", async (ctx) => {
},
});
- console.log("error", error);
- console.log("status", status);
-
expect(status).toEqual(409);
- expect(error).toEqual({
+ expect(error).toStrictEqual({
error: {
code: "conflict",
message: "Slug is already in use.",
diff --git a/apps/web/tests/workspaces/create-workspace.test.ts b/apps/web/tests/workspaces/create-workspace.test.ts
index 161eada709..c3876ee263 100644
--- a/apps/web/tests/workspaces/create-workspace.test.ts
+++ b/apps/web/tests/workspaces/create-workspace.test.ts
@@ -1,21 +1,14 @@
import { nanoid } from "@dub/utils";
import { Project } from "@prisma/client";
import slugify from "@sindresorhus/slugify";
-import { afterAll, expect, test } from "vitest";
-import { HttpClient } from "../utils/http";
+import { expect, test } from "vitest";
import { IntegrationHarness } from "../utils/integration";
import { expectedWorkspace } from "../utils/schema";
-test("POST /workspaces", async (ctx) => {
+// Not running this test now
+test.skip("POST /workspaces", async (ctx) => {
const h = new IntegrationHarness(ctx);
- const { apiKey } = await h.init();
-
- const http = new HttpClient({
- baseUrl: h.baseUrl,
- headers: {
- Authorization: `Bearer ${apiKey.token}`,
- },
- });
+ const { http } = await h.init();
const name = "Dub Workspace";
const slug = slugify(`dub-${nanoid()}`);
@@ -29,15 +22,11 @@ test("POST /workspaces", async (ctx) => {
});
expect(status).toEqual(200);
- expect(workspace).toEqual({
+ expect(workspace).toStrictEqual({
...expectedWorkspace,
name,
slug,
domains: [],
users: [{ role: "owner" }],
});
-
- afterAll(async () => {
- await h.cleanup(workspace.id);
- });
});
diff --git a/apps/web/tests/workspaces/list-workspaces.test.ts b/apps/web/tests/workspaces/list-workspaces.test.ts
index dbbe71892b..e965b355cc 100644
--- a/apps/web/tests/workspaces/list-workspaces.test.ts
+++ b/apps/web/tests/workspaces/list-workspaces.test.ts
@@ -1,32 +1,29 @@
import { Project } from "@prisma/client";
+import { expectedWorkspace } from "tests/utils/schema";
import { expect, test } from "vitest";
-import { HttpClient } from "../utils/http";
import { IntegrationHarness } from "../utils/integration";
test("GET /workspaces", async (ctx) => {
const h = new IntegrationHarness(ctx);
- const { apiKey } = await h.init();
-
- const http = new HttpClient({
- baseUrl: h.baseUrl,
- headers: {
- Authorization: `Bearer ${apiKey.token}`,
- },
- });
+ const { workspace, http } = await h.init();
const { status, data: workspaces } = await http.get<Project[]>({
path: "/workspaces",
});
+ const workspaceFound = workspaces.find(
+ (w) => w.slug === h.resources.workspace.slug,
+ );
+
expect(status).toEqual(200);
- expect(workspaces.length).greaterThanOrEqual(1);
- // expect(workspaces).toMatchObject([
- // {
- // ...expectedWorkspace,
- // name: workspace.name,
- // slug: workspace.slug,
- // plan: "pro",
- // domains: [],
- // },
- // ]);
+ expect(workspaces.length).toBeGreaterThanOrEqual(1);
+ expect(workspaceFound).toStrictEqual({
+ ...expectedWorkspace,
+ name: workspace.name,
+ slug: workspace.slug,
+ id: workspace.id,
+ plan: "pro",
+ domains: [],
+ users: [{ role: "owner" }],
+ });
});
diff --git a/apps/web/tests/workspaces/retrieve-workspace.error.test.ts b/apps/web/tests/workspaces/retrieve-workspace.error.test.ts
index b3d64f1ab9..501fdae136 100644
--- a/apps/web/tests/workspaces/retrieve-workspace.error.test.ts
+++ b/apps/web/tests/workspaces/retrieve-workspace.error.test.ts
@@ -1,26 +1,21 @@
import { Project } from "@prisma/client";
import { expect, test } from "vitest";
-import { HttpClient } from "../utils/http";
import { IntegrationHarness } from "../utils/integration";
-// TODO:
-// Retrieve workspace not owned by current user
-
test("retrieve a workspace by invalid slug or id", 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 { http } = await h.init();
const { status, data: error } = await http.get<Project>({
path: `/workspaces/xxxx`,
});
expect(status).toEqual(404);
- // Add error
+ expect(error).toStrictEqual({
+ error: {
+ code: "not_found",
+ message: "Workspace not found.",
+ doc_url: "https://dub.co/docs/api-reference/errors#not_found",
+ },
+ });
});
diff --git a/apps/web/tests/workspaces/retrieve-workspace.test.ts b/apps/web/tests/workspaces/retrieve-workspace.test.ts
index 111c0775f8..09c007bec0 100644
--- a/apps/web/tests/workspaces/retrieve-workspace.test.ts
+++ b/apps/web/tests/workspaces/retrieve-workspace.test.ts
@@ -1,19 +1,11 @@
import { Project } from "@prisma/client";
import { expect, test } from "vitest";
-import { HttpClient } from "../utils/http";
import { IntegrationHarness } from "../utils/integration";
import { expectedWorkspace } from "../utils/schema";
test("GET /workspaces/{id}", 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 { workspace, http } = await h.init();
const { status, data: workspaceFetched } = await http.get<Project>({
path: `/workspaces/${workspace.workspaceId}`,
@@ -24,7 +16,7 @@ test("GET /workspaces/{id}", async (ctx) => {
...expectedWorkspace,
name: workspace.name,
slug: workspace.slug,
- id: `ws_${workspace.id}`,
+ id: workspace.id,
plan: "pro",
domains: [],
users: [{ role: "owner" }],
@@ -33,25 +25,18 @@ test("GET /workspaces/{id}", async (ctx) => {
test("GET /workspaces/{slug}", 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 { workspace, http } = await h.init();
const { status, data: workspaceFetched } = await http.get<Project>({
path: `/workspaces/${workspace.slug}`,
});
expect(status).toEqual(200);
- expect(workspaceFetched).toMatchObject({
+ expect(workspaceFetched).toStrictEqual({
...expectedWorkspace,
name: workspace.name,
slug: workspace.slug,
- id: `ws_${workspace.id}`,
+ id: workspace.id,
plan: "pro",
domains: [],
users: [{ role: "owner" }],