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 tests for creating tags and workspaces

Kiran K122b95ddbdac
diff --git a/apps/web/tests/tags/create-tag-error.test.ts b/apps/web/tests/tags/create-tag-error.test.ts
new file mode 100644
index 0000000000..6eb895d488
--- /dev/null
+++ b/apps/web/tests/tags/create-tag-error.test.ts
@@ -0,0 +1,105 @@
+import { expect, test } from "vitest";
+import { HttpClient } from "../utils/http";
+import { Tag } from "@prisma/client";
+import { IntegrationHarness } from "../utils/integration";
+
+const cases = [
+ {
+ name: "create tag with invalid color",
+ body: {
+ tag: "news",
+ color: "invalid",
+ },
+ expected: {
+ status: 422,
+ data: {
+ error: {
+ code: "unprocessable_entity",
+ message:
+ "invalid_enum_value: color: Invalid color. Must be one of: red, yellow, green, blue, purple, pink, brown",
+ doc_url:
+ "https://dub.co/docs/api-reference/errors#unprocessable_entity",
+ },
+ },
+ },
+ },
+ {
+ name: "create tag without name",
+ body: {
+ color: "red",
+ },
+ expected: {
+ status: 422,
+ data: {
+ error: {
+ code: "unprocessable_entity",
+ message: "invalid_type: tag: Required",
+ doc_url:
+ "https://dub.co/docs/api-reference/errors#unprocessable_entity",
+ },
+ },
+ },
+ },
+];
+
+cases.forEach(({ name, body, expected }) => {
+ test(name, 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 response = await http.post<Tag>({
+ path: "/tags",
+ query: { workspaceId: workspace.workspaceId },
+ body,
+ });
+
+ expect(response).toEqual(expected);
+ });
+});
+
+test("create tag with existing name", 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}`,
+ },
+ });
+
+ await http.post({
+ path: "/tags",
+ query: { workspaceId: workspace.workspaceId },
+ body: {
+ tag: "news",
+ color: "red",
+ },
+ });
+
+ // Create the same tag again
+ const { status, data: error } = await http.post<Tag>({
+ path: "/tags",
+ query: { workspaceId: workspace.workspaceId },
+ body: {
+ tag: "news",
+ color: "red",
+ },
+ });
+
+ expect(status).toBe(409);
+ expect(error).toEqual({
+ error: {
+ code: "conflict",
+ message: "A tag with that name already exists.",
+ doc_url: "https://dub.co/docs/api-reference/errors#conflict",
+ },
+ });
+});
diff --git a/apps/web/tests/utils/integration.ts b/apps/web/tests/utils/integration.ts
index 018603e451..4b66af0fb0 100644
--- a/apps/web/tests/utils/integration.ts
+++ b/apps/web/tests/utils/integration.ts
@@ -54,6 +54,7 @@ export class IntegrationHarness {
name: "Dub",
slug: `dub-${nanoid()}`,
plan: "pro",
+ inviteCode: nanoid(6),
billingCycleStart: new Date().getDate(),
users: {
create: {
diff --git a/apps/web/tests/workspaces/create-workspace-error.test.ts b/apps/web/tests/workspaces/create-workspace-error.test.ts
new file mode 100644
index 0000000000..0dfe6e79d4
--- /dev/null
+++ b/apps/web/tests/workspaces/create-workspace-error.test.ts
@@ -0,0 +1,52 @@
+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("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}`,
+ });
+});
diff --git a/apps/web/tests/workspaces/create-workspace.test.ts b/apps/web/tests/workspaces/create-workspace.test.ts
index f34a95b393..b05b78cfea 100644
--- a/apps/web/tests/workspaces/create-workspace.test.ts
+++ b/apps/web/tests/workspaces/create-workspace.test.ts
@@ -40,51 +40,3 @@ test("creates new workspace", async (ctx) => {
path: `/workspaces/ws_${workspace.id}`,
});
});
-
-// TODO:
-// Move this to error handling tests
-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}`,
- });
-});
diff --git a/apps/web/tests/workspaces/list-workspaces.test.ts b/apps/web/tests/workspaces/list-workspaces.test.ts
new file mode 100644
index 0000000000..a2c9088804
--- /dev/null
+++ b/apps/web/tests/workspaces/list-workspaces.test.ts
@@ -0,0 +1,70 @@
+import { expect, test } from "vitest";
+import { HttpClient } from "../utils/http";
+import { Project } from "@prisma/client";
+import { IntegrationHarness } from "../utils/integration";
+import { expectedWorkspace } from "../utils/schema";
+import slugify from "@sindresorhus/slugify";
+import { nanoid } from "@dub/utils";
+
+test("retrieve list of workspaces", 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}`,
+ },
+ });
+
+ const workspacesToCreate = [
+ { name: "Personal", slug: slugify(nanoid(6)) },
+ { name: "Work", slug: slugify(nanoid(6)) },
+ ];
+
+ await Promise.allSettled(
+ workspacesToCreate.map(({ name, slug }) =>
+ http.post({
+ path: "/workspaces",
+ body: { name, slug },
+ }),
+ ),
+ );
+
+ // Fetch the list of workspaces
+ const { status, data: workspaces } = await http.get<Project[]>({
+ path: "/workspaces",
+ });
+
+ expect(status).toEqual(200);
+ expect(workspaces.length).equal(3);
+
+ expect(workspaces).toContainEqual({
+ ...expectedWorkspace,
+ name: workspace.name,
+ slug: workspace.slug,
+ plan: "pro",
+ users: [{ role: "owner" }],
+ });
+
+ expect(workspaces).toContainEqual({
+ ...expectedWorkspace,
+ ...workspacesToCreate[0],
+ users: [{ role: "owner" }],
+ });
+
+ expect(workspaces).toContainEqual({
+ ...expectedWorkspace,
+ ...workspacesToCreate[1],
+ users: [{ role: "owner" }],
+ });
+
+ // Delete the workspaces
+ await Promise.allSettled(
+ workspaces.map((w) =>
+ http.delete({
+ path: `/workspaces/ws_${w.id}`,
+ }),
+ ),
+ );
+});