The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Fix tests and add new test for creating a workspace
Kiran K1c9eb64bd930
diff --git a/apps/web/tests/links/list-links.test.ts b/apps/web/tests/links/list-links.test.tsindex 1ffb46f5d9..f2d91761da 100644--- a/apps/web/tests/links/list-links.test.ts+++ b/apps/web/tests/links/list-links.test.ts@@ -42,7 +42,6 @@ test("list links", async (ctx) => {expect(status).toEqual(200);expect(links.length).toEqual(2);-expect(links[0]).toMatchObject({id: expect.any(String),domain,diff --git a/apps/web/tests/utils/http.ts b/apps/web/tests/utils/http.tsindex 69dfeb4c4d..d6b4df023d 100644--- a/apps/web/tests/utils/http.ts+++ b/apps/web/tests/utils/http.ts@@ -58,4 +58,12 @@ export class HttpClient {public async post<TResponse>(req: Request) {return await this.request<TResponse>("POST", req);}++ public async put<TResponse>(req: Request) {+ return await this.request<TResponse>("PUT", req);+ }++ public async delete<TResponse>(req: Request) {+ return await this.request<TResponse>("DELETE", req);+ }}diff --git a/apps/web/tests/utils/integration.ts b/apps/web/tests/utils/integration.tsindex 11056c429c..018603e451 100644--- a/apps/web/tests/utils/integration.ts+++ b/apps/web/tests/utils/integration.ts@@ -31,8 +31,8 @@ export class IntegrationHarness {async seed() {this.ctx.onTestFinished(async () => {await prisma.$transaction([- prisma.project.delete({ where: { id: this.resources.workspace.id } }),prisma.token.delete({ where: { id: this.resources.apiKey.id } }),+ prisma.project.delete({ where: { id: this.resources.workspace.id } }),prisma.user.delete({ where: { id: this.resources.user.id } }),]);@@ -53,13 +53,14 @@ export class IntegrationHarness {data: {name: "Dub",slug: `dub-${nanoid()}`,+ plan: "pro",+ billingCycleStart: new Date().getDate(),users: {create: {userId: user.id,role: "owner",},},- billingCycleStart: new Date().getDate(),defaultDomains: {create: {},},diff --git a/apps/web/tests/workspaces/create-workspace.test.ts b/apps/web/tests/workspaces/create-workspace.test.tsnew file mode 100644index 0000000000..044b2f810f--- /dev/null+++ b/apps/web/tests/workspaces/create-workspace.test.ts@@ -0,0 +1,102 @@+import { expect, test } from "vitest";+import { HttpClient } from "../utils/http";+import { Project } from "@prisma/client";+import { IntegrationHarness } from "../utils/integration";+import { nanoid } from "@dub/utils";+import slugify from "@sindresorhus/slugify";++test("creates new workspace", 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 name = "Dub Workspace";+ const slug = slugify(`dub-${nanoid()}`);++ const { status, data: workspace } = await http.post<Project>({+ path: "/workspaces",+ body: {+ name,+ slug,+ },+ });++ expect(status).toEqual(200);+ expect(workspace).toEqual({+ id: expect.any(String),+ name,+ slug,+ logo: null,+ plan: "free",+ stripeId: null,+ billingCycleStart: 4,+ usage: 0,+ usageLimit: 1000,+ linksUsage: 0,+ linksLimit: 25,+ domainsLimit: 3,+ tagsLimit: 5,+ usersLimit: 1,+ monitoringId: null,+ domains: [],+ inviteCode: expect.any(String),+ createdAt: expect.any(String),+ updatedAt: expect.any(String),+ });++ await http.delete({+ path: `/workspaces/ws_${workspace.id}`,+ });+});++test("should not create workspace with slug in use", 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 name = "Dub Workspace";+ const slug = slugify(`dub-${nanoid()}`);++ // Create a workspace with the same slug+ const { data: workspace } = await http.post<Project>({+ path: "/workspaces",+ body: {+ name,+ slug,+ },+ });++ // Create another workspace with the same slug+ const { status, data: error } = await http.post<Project>({+ path: "/workspaces",+ body: {+ name,+ slug,+ },+ });++ expect(status).toEqual(409);+ expect(error).toEqual({+ error: {+ code: "conflict",+ message: "Slug is already in use.",+ doc_url: "https://dub.co/docs/api-reference/errors#conflict",+ },+ });++ await http.delete({+ path: `/workspaces/ws_${workspace.id}`,+ });+});