Path: blob/master/src/packages/next/pages/api/v2/projects/delete.test.ts
2575 views
/** @jest-environment node */12import { createMocks } from "lib/api/test-framework";3import handler from "./delete";45// Mock the dependencies6jest.mock("@cocalc/server/projects/delete", () => jest.fn());7jest.mock("lib/account/get-account", () => jest.fn());89describe("/api/v2/projects/delete", () => {10beforeEach(() => {11jest.clearAllMocks();12});1314test("unauthenticated request should return JSON error, not throw", async () => {15// Mock getAccountId to return undefined (not authenticated)16const getAccountId = require("lib/account/get-account");17getAccountId.mockResolvedValue(undefined);1819const { req, res } = createMocks({20method: "POST",21url: "/api/v2/projects/delete",22body: {23project_id: "00000000-0000-0000-0000-000000000000",24},25});2627// This should NOT throw - it should handle the error gracefully28await expect(handler(req, res)).resolves.not.toThrow();2930// Should return JSON error response31const data = res._getJSONData();32expect(data).toHaveProperty("error");33expect(data.error).toContain("must be signed in");34});3536test("authenticated request calls deleteProject", async () => {37const mockAccountId = "11111111-1111-1111-1111-111111111111";38const mockProjectId = "00000000-0000-0000-0000-000000000000";3940// Mock getAccountId to return a valid account_id41const getAccountId = require("lib/account/get-account");42getAccountId.mockResolvedValue(mockAccountId);4344// Mock deleteProject45const deleteProject = require("@cocalc/server/projects/delete");46deleteProject.mockResolvedValue(undefined);4748const { req, res } = createMocks({49method: "POST",50url: "/api/v2/projects/delete",51body: {52project_id: mockProjectId,53},54});5556await handler(req, res);5758// Should call deleteProject with correct params59expect(deleteProject).toHaveBeenCalledWith({60account_id: mockAccountId,61project_id: mockProjectId,62});6364// Should return OK status65const data = res._getJSONData();66expect(data).toHaveProperty("status");67expect(data.status).toBe("ok");68});69});707172