Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat(ci): add api task integration tests

Tin831effdcc9f6
diff --git a/apps/api/vitest.integration.config.ts b/apps/api/vitest.integration.config.ts
index 13457a673..bdaf9f4ce 100644
--- a/apps/api/vitest.integration.config.ts
+++ b/apps/api/vitest.integration.config.ts
@@ -5,6 +5,9 @@ export default defineConfig({
environment: "node",
include: ["../../tests/api-integration/**/*.test.ts"],
setupFiles: ["../../tests/api-integration/setup.ts"],
+ fileParallelism: false,
+ maxWorkers: 1,
+ minWorkers: 1,
coverage: {
enabled: false,
},
diff --git a/tests/api-integration/helpers/fixtures.ts b/tests/api-integration/helpers/fixtures.ts
new file mode 100644
index 000000000..08c2de54c
--- /dev/null
+++ b/tests/api-integration/helpers/fixtures.ts
@@ -0,0 +1,99 @@
+import { randomUUID } from "node:crypto";
+import db, { schema } from "../../../apps/api/src/database";
+
+export type SeededMemberContext = {
+ user: typeof schema.userTable.$inferSelect;
+ workspace: typeof schema.workspaceTable.$inferSelect;
+};
+
+export async function createWorkspaceMember(
+ overrides?: Partial<{
+ userName: string;
+ workspaceName: string;
+ role: string;
+ }>,
+): Promise<SeededMemberContext> {
+ const userId = `user-${randomUUID()}`;
+ const workspaceId = `workspace-${randomUUID()}`;
+
+ const [user] = await db
+ .insert(schema.userTable)
+ .values({
+ id: userId,
+ email: `${userId}@example.com`,
+ emailVerified: true,
+ name: overrides?.userName || "Integration Test User",
+ })
+ .returning();
+
+ const [workspace] = await db
+ .insert(schema.workspaceTable)
+ .values({
+ id: workspaceId,
+ createdAt: new Date(),
+ name: overrides?.workspaceName || "Integration Test Workspace",
+ slug: `workspace-${randomUUID()}`,
+ })
+ .returning();
+
+ await db.insert(schema.workspaceUserTable).values({
+ workspaceId: workspace.id,
+ userId: user.id,
+ role: overrides?.role || "owner",
+ joinedAt: new Date(),
+ });
+
+ return { user, workspace };
+}
+
+export async function createProjectFixture({
+ workspaceId,
+ name = "Integration Project",
+ icon = "Folder",
+ slug = `project-${randomUUID()}`,
+}: {
+ workspaceId: string;
+ name?: string;
+ icon?: string;
+ slug?: string;
+}) {
+ const [project] = await db
+ .insert(schema.projectTable)
+ .values({
+ workspaceId,
+ name,
+ icon,
+ slug,
+ })
+ .returning();
+
+ const [todoColumn] = await db
+ .insert(schema.columnTable)
+ .values({
+ projectId: project.id,
+ name: "To Do",
+ slug: "to-do",
+ position: 0,
+ isFinal: false,
+ })
+ .returning();
+
+ const [doneColumn] = await db
+ .insert(schema.columnTable)
+ .values({
+ projectId: project.id,
+ name: "Done",
+ slug: "done",
+ position: 1,
+ isFinal: true,
+ })
+ .returning();
+
+ return {
+ project,
+ columns: {
+ todo: todoColumn,
+ done: doneColumn,
+ },
+ };
+}
diff --git a/tests/api-integration/project.test.ts b/tests/api-integration/project.test.ts
index 48f78ca21..7e43f0811 100644
--- a/tests/api-integration/project.test.ts
+++ b/tests/api-integration/project.test.ts
@@ -1,49 +1,10 @@
-import { randomUUID } from "node:crypto";
import { eq } from "drizzle-orm";
import { beforeEach, describe, expect, it } from "vitest";
import db, { schema } from "../../apps/api/src/database";
import { createApp } from "../../apps/api/src/index";
import { mockAnonymousSession, mockAuthenticatedSession } from "./helpers/auth";
import { resetTestDatabase } from "./helpers/database";
-
-type SeededMemberContext = {
- user: typeof schema.userTable.$inferSelect;
- workspace: typeof schema.workspaceTable.$inferSelect;
-};
-
-async function createWorkspaceMember(): Promise<SeededMemberContext> {
- const userId = `user-${randomUUID()}`;
- const workspaceId = `workspace-${randomUUID()}`;
-
- const [user] = await db
- .insert(schema.userTable)
- .values({
- id: userId,
- email: `${userId}@example.com`,
- emailVerified: true,
- name: "Integration Test User",
- })
- .returning();
-
- const [workspace] = await db
- .insert(schema.workspaceTable)
- .values({
- id: workspaceId,
- createdAt: new Date(),
- name: "Integration Test Workspace",
- slug: `workspace-${randomUUID()}`,
- })
- .returning();
-
- await db.insert(schema.workspaceUserTable).values({
- workspaceId: workspace.id,
- userId: user.id,
- role: "owner",
- joinedAt: new Date(),
- });
-
- return { user, workspace };
-}
+import { createWorkspaceMember } from "./helpers/fixtures";
describe("API integration: project creation", () => {
beforeEach(async () => {
@@ -133,7 +94,7 @@ describe("API integration: project creation", () => {
it("rejects project creation for users outside the workspace", async () => {
const member = await createWorkspaceMember();
- const outsiderId = `user-${randomUUID()}`;
+ const outsiderId = "user-outsider";
const [outsider] = await db
.insert(schema.userTable)
diff --git a/tests/api-integration/task.test.ts b/tests/api-integration/task.test.ts
new file mode 100644
index 000000000..d7534285f
--- /dev/null
+++ b/tests/api-integration/task.test.ts
@@ -0,0 +1,171 @@
+import { randomUUID } from "node:crypto";
+import { and, eq } from "drizzle-orm";
+import { beforeEach, describe, expect, it } from "vitest";
+import db, { schema } from "../../apps/api/src/database";
+import { createApp } from "../../apps/api/src/index";
+import { mockAnonymousSession, mockAuthenticatedSession } from "./helpers/auth";
+import { resetTestDatabase } from "./helpers/database";
+import {
+ createProjectFixture,
+ createWorkspaceMember,
+} from "./helpers/fixtures";
+
+describe("API integration: task creation", () => {
+ beforeEach(async () => {
+ await resetTestDatabase();
+ });
+
+ it("rejects unauthenticated task creation requests", async () => {
+ const member = await createWorkspaceMember();
+ const { project } = await createProjectFixture({
+ workspaceId: member.workspace.id,
+ });
+
+ mockAnonymousSession();
+ const { app } = createApp();
+
+ const response = await app.request(`/api/task/${project.id}`, {
+ method: "POST",
+ headers: {
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({
+ title: "Unauthorized task",
+ description: "Should not be created",
+ priority: "low",
+ status: "to-do",
+ }),
+ });
+
+ expect(response.status).toBe(401);
+ await expect(response.text()).resolves.toBe("Unauthorized");
+ });
+
+ it("creates a task with the matching column, assignee, and next number", async () => {
+ const member = await createWorkspaceMember();
+ const { project, columns } = await createProjectFixture({
+ workspaceId: member.workspace.id,
+ name: "Delivery",
+ slug: "delivery",
+ });
+
+ await db.insert(schema.taskTable).values({
+ projectId: project.id,
+ userId: member.user.id,
+ title: "Existing task",
+ description: "Already there",
+ status: "to-do",
+ columnId: columns.todo.id,
+ priority: "medium",
+ number: 1,
+ position: 1,
+ });
+
+ mockAuthenticatedSession(member.user);
+ const { app } = createApp();
+
+ const response = await app.request(`/api/task/${project.id}`, {
+ method: "POST",
+ headers: {
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({
+ title: "Ship integration flow",
+ description: "Cover the first create-task path",
+ priority: "high",
+ status: "to-do",
+ userId: member.user.id,
+ }),
+ });
+
+ expect(response.status).toBe(200);
+ const payload = (await response.json()) as {
+ id: string;
+ projectId: string;
+ title: string;
+ description: string;
+ priority: string;
+ status: string;
+ userId: string | null;
+ number: number | null;
+ position: number | null;
+ assigneeName?: string;
+ };
+
+ expect(payload).toMatchObject({
+ projectId: project.id,
+ title: "Ship integration flow",
+ description: "Cover the first create-task path",
+ priority: "high",
+ status: "to-do",
+ userId: member.user.id,
+ number: 2,
+ position: 2,
+ assigneeName: member.user.name,
+ });
+
+ const persistedTask = await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, payload.id),
+ });
+
+ expect(persistedTask).toMatchObject({
+ id: payload.id,
+ projectId: project.id,
+ columnId: columns.todo.id,
+ userId: member.user.id,
+ title: "Ship integration flow",
+ priority: "high",
+ status: "to-do",
+ number: 2,
+ position: 2,
+ });
+ });
+
+ it("rejects task creation for users outside the project workspace", async () => {
+ const member = await createWorkspaceMember();
+ const outsiderId = `user-${randomUUID()}`;
+ const { project } = await createProjectFixture({
+ workspaceId: member.workspace.id,
+ });
+
+ const [outsider] = await db
+ .insert(schema.userTable)
+ .values({
+ id: outsiderId,
+ email: `${outsiderId}@example.com`,
+ emailVerified: true,
+ name: "Task Outsider",
+ })
+ .returning();
+
+ mockAuthenticatedSession(outsider);
+ const { app } = createApp();
+
+ const response = await app.request(`/api/task/${project.id}`, {
+ method: "POST",
+ headers: {
+ "content-type": "application/json",
+ },
+ body: JSON.stringify({
+ title: "Forbidden task",
+ description: "Should not be created",
+ priority: "low",
+ status: "to-do",
+ }),
+ });
+
+ expect(response.status).toBe(403);
+ await expect(response.text()).resolves.toBe(
+ "You don't have access to this workspace",
+ );
+
+ const persistedTask = await db.query.taskTable.findFirst({
+ where: and(
+ eq(schema.taskTable.projectId, project.id),
+ eq(schema.taskTable.title, "Forbidden task"),
+ ),
+ });
+
+ expect(persistedTask).toBeUndefined();
+ });
+});