fix(api,web): separate workspace context from auth session (#551)
Co-authored-by: Jason Woltje <jason@diversecanvas.com> Co-committed-by: Jason Woltje <jason@diversecanvas.com>
This commit was merged in pull request #551.
This commit is contained in:
75
apps/api/src/workspaces/workspaces.controller.spec.ts
Normal file
75
apps/api/src/workspaces/workspaces.controller.spec.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import { describe, it, expect, beforeEach, vi } from "vitest";
|
||||
import { Test, TestingModule } from "@nestjs/testing";
|
||||
import { WorkspacesController } from "./workspaces.controller";
|
||||
import { WorkspacesService } from "./workspaces.service";
|
||||
import { AuthGuard } from "../auth/guards/auth.guard";
|
||||
import { WorkspaceMemberRole } from "@prisma/client";
|
||||
import type { AuthUser } from "@mosaic/shared";
|
||||
|
||||
describe("WorkspacesController", () => {
|
||||
let controller: WorkspacesController;
|
||||
let service: WorkspacesService;
|
||||
|
||||
const mockWorkspacesService = {
|
||||
getUserWorkspaces: vi.fn(),
|
||||
};
|
||||
|
||||
const mockUser: AuthUser = {
|
||||
id: "user-1",
|
||||
email: "test@example.com",
|
||||
name: "Test User",
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
controllers: [WorkspacesController],
|
||||
providers: [
|
||||
{
|
||||
provide: WorkspacesService,
|
||||
useValue: mockWorkspacesService,
|
||||
},
|
||||
],
|
||||
})
|
||||
.overrideGuard(AuthGuard)
|
||||
.useValue({ canActivate: () => true })
|
||||
.compile();
|
||||
|
||||
controller = module.get<WorkspacesController>(WorkspacesController);
|
||||
service = module.get<WorkspacesService>(WorkspacesService);
|
||||
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe("GET /api/workspaces", () => {
|
||||
it("should call service with authenticated user id", async () => {
|
||||
mockWorkspacesService.getUserWorkspaces.mockResolvedValueOnce([]);
|
||||
|
||||
await controller.getUserWorkspaces(mockUser);
|
||||
|
||||
expect(service.getUserWorkspaces).toHaveBeenCalledWith("user-1");
|
||||
});
|
||||
|
||||
it("should return workspace list from service", async () => {
|
||||
const mockWorkspaces = [
|
||||
{
|
||||
id: "ws-1",
|
||||
name: "My Workspace",
|
||||
ownerId: "user-1",
|
||||
role: WorkspaceMemberRole.OWNER,
|
||||
createdAt: new Date("2026-01-01"),
|
||||
},
|
||||
];
|
||||
mockWorkspacesService.getUserWorkspaces.mockResolvedValueOnce(mockWorkspaces);
|
||||
|
||||
const result = await controller.getUserWorkspaces(mockUser);
|
||||
|
||||
expect(result).toEqual(mockWorkspaces);
|
||||
});
|
||||
|
||||
it("should propagate service errors", async () => {
|
||||
mockWorkspacesService.getUserWorkspaces.mockRejectedValueOnce(new Error("Database error"));
|
||||
|
||||
await expect(controller.getUserWorkspaces(mockUser)).rejects.toThrow("Database error");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user