Files
calendar/packages/trpc/server/routers/viewer/workflows/util.test.ts
T
Benny JooGitHubbenny@cal.com <sldisek783@gmail.com>Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
f98acb7301 refactor: migrate workflows utilities from trpc to features layer (#25534)
* refactor: migrate workflows utilities from trpc to features layer

Move workflow utility functions from packages/trpc/server/routers/viewer/workflows/util.ts
to packages/features/ee/workflows/lib/workflowUtils.ts to break circular dependencies.

Functions migrated:
- isAuthorized
- getAllWorkflowsFromEventType
- scheduleWorkflowNotifications
- scheduleBookingReminders

Changes:
- Created new workflowUtils.ts in features layer with migrated functions
- Added getBookingsForWorkflowReminders to WorkflowRepository (no prisma outside repositories)
- Updated all imports in features layer to use new location
- Updated trpc util.ts to re-export from features for backward compatibility
- Fixed pre-existing lint warning (any -> unknown) in handleMarkNoShow.ts

This is part of the effort to remove circular dependencies between
packages/features and packages/trpc.

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor: remove re-exports from trpc util.ts, update imports to use features layer

Per user request, removed the backward compatibility re-exports from
packages/trpc/server/routers/viewer/workflows/util.ts and updated all
imports in the trpc package to import directly from the features layer.

Files updated to import from @calcom/features/ee/workflows/lib/workflowUtils:
- confirm.handler.ts (getAllWorkflowsFromEventType)
- delete.handler.ts (isAuthorized)
- update.handler.ts (isAuthorized, scheduleWorkflowNotifications)
- getAllActiveWorkflows.handler.ts (getAllWorkflowsFromEventType)
- get.handler.ts (isAuthorized)
- util.test.ts (isAuthorized)
- delete.handler.test.ts (isAuthorized)

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix: update test imports to use features layer for workflow utilities

Updated test files to import scheduleBookingReminders and
scheduleWorkflowNotifications from @calcom/features/ee/workflows/lib/workflowUtils
instead of @calcom/trpc/server/routers/viewer/workflows/util.

Files updated:
- packages/features/ee/workflows/lib/test/workflows.test.ts
- packages/features/tasker/tasks/scanWorkflowBody.test.ts

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* refactor: split workflowUtils.ts into individual files

Split the monolithic workflowUtils.ts into separate files for each function:
- isAuthorized.ts - Authorization check for workflow access
- getAllWorkflowsFromEventType.ts - Get workflows for an event type
- scheduleWorkflowNotifications.ts - Schedule workflow notifications
- scheduleBookingReminders.ts - Schedule booking reminders

The workflowUtils.ts now re-exports from these individual files for
backward compatibility.

Co-Authored-By: benny@cal.com <sldisek783@gmail.com>

* fix import

* fix more

* wip

* wip

* remove workflowUtils

* wip

* refactor deleteRemindersOfActiveOnIds

* fix

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2025-12-16 10:17:46 +00:00

401 lines
11 KiB
TypeScript

import { prisma } from "@calcom/prisma/__mocks__/prisma";
import { describe, it, expect, vi, beforeEach } from "vitest";
import { isAuthorized } from "@calcom/features/ee/workflows/lib/isAuthorized";
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
vi.mock("@calcom/features/pbac/services/permission-check.service");
vi.mock("@calcom/prisma", () => ({
prisma,
}));
describe("isAuthorized", () => {
const mockPermissionCheckService = vi.mocked(PermissionCheckService);
let mockCheckPermission: ReturnType<typeof vi.fn>;
beforeEach(() => {
vi.clearAllMocks();
mockCheckPermission = vi.fn();
mockPermissionCheckService.mockImplementation(
() =>
({
checkPermission: mockCheckPermission,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any)
);
});
describe("null workflow", () => {
it("should return false when workflow is null", async () => {
const result = await isAuthorized(null, 123);
expect(result).toBe(false);
});
});
describe("personal workflows (no teamId)", () => {
it("should return true when user owns the personal workflow", async () => {
const workflow = {
id: 1,
teamId: null,
userId: 123,
};
const result = await isAuthorized(workflow, 123);
expect(result).toBe(true);
expect(mockPermissionCheckService).not.toHaveBeenCalled();
});
it("should return false when user does not own the personal workflow", async () => {
const workflow = {
id: 1,
teamId: null,
userId: 456,
};
const result = await isAuthorized(workflow, 123);
expect(result).toBe(false);
expect(mockPermissionCheckService).not.toHaveBeenCalled();
});
it("should ignore permission parameter for personal workflows", async () => {
const workflow = {
id: 1,
teamId: null,
userId: 123,
};
const readResult = await isAuthorized(workflow, 123, "workflow.read");
const updateResult = await isAuthorized(workflow, 123, "workflow.update");
const deleteResult = await isAuthorized(workflow, 123, "workflow.delete");
expect(readResult).toBe(true);
expect(updateResult).toBe(true);
expect(deleteResult).toBe(true);
expect(mockPermissionCheckService).not.toHaveBeenCalled();
});
});
describe("team workflows with PBAC", () => {
describe("read operations", () => {
it("should use workflow.read permission by default with all roles as fallback", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
};
mockCheckPermission.mockResolvedValue(true);
const result = await isAuthorized(workflow, 123);
expect(result).toBe(true);
expect(mockPermissionCheckService).toHaveBeenCalledTimes(1);
expect(mockCheckPermission).toHaveBeenCalledWith({
userId: 123,
teamId: 456,
permission: "workflow.read",
fallbackRoles: ["ADMIN", "OWNER", "MEMBER"],
});
});
it("should use workflow.read permission when explicitly passed", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
};
mockCheckPermission.mockResolvedValue(true);
const result = await isAuthorized(workflow, 123, "workflow.read");
expect(result).toBe(true);
expect(mockCheckPermission).toHaveBeenCalledWith({
userId: 123,
teamId: 456,
permission: "workflow.read",
fallbackRoles: ["ADMIN", "OWNER", "MEMBER"],
});
});
it("should return false when PBAC denies read permission", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
};
mockCheckPermission.mockResolvedValue(false);
const result = await isAuthorized(workflow, 123, "workflow.read");
expect(result).toBe(false);
expect(mockCheckPermission).toHaveBeenCalledWith({
userId: 123,
teamId: 456,
permission: "workflow.read",
fallbackRoles: ["ADMIN", "OWNER", "MEMBER"],
});
});
});
describe("update operations", () => {
it("should use workflow.update permission with admin/owner roles as fallback", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
};
mockCheckPermission.mockResolvedValue(true);
const result = await isAuthorized(workflow, 123, "workflow.update");
expect(result).toBe(true);
expect(mockPermissionCheckService).toHaveBeenCalledTimes(1);
expect(mockCheckPermission).toHaveBeenCalledWith({
userId: 123,
teamId: 456,
permission: "workflow.update",
fallbackRoles: ["ADMIN", "OWNER"],
});
});
it("should return false when PBAC denies update permission", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
};
mockCheckPermission.mockResolvedValue(false);
const result = await isAuthorized(workflow, 123, "workflow.update");
expect(result).toBe(false);
expect(mockCheckPermission).toHaveBeenCalledWith({
userId: 123,
teamId: 456,
permission: "workflow.update",
fallbackRoles: ["ADMIN", "OWNER"],
});
});
});
describe("delete operations", () => {
it("should use workflow.delete permission with admin/owner roles as fallback", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
};
mockCheckPermission.mockResolvedValue(true);
const result = await isAuthorized(workflow, 123, "workflow.delete");
expect(result).toBe(true);
expect(mockCheckPermission).toHaveBeenCalledWith({
userId: 123,
teamId: 456,
permission: "workflow.delete",
fallbackRoles: ["ADMIN", "OWNER"],
});
});
it("should return false when PBAC denies delete permission", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
};
mockCheckPermission.mockResolvedValue(false);
const result = await isAuthorized(workflow, 123, "workflow.delete");
expect(result).toBe(false);
expect(mockCheckPermission).toHaveBeenCalledWith({
userId: 123,
teamId: 456,
permission: "workflow.delete",
fallbackRoles: ["ADMIN", "OWNER"],
});
});
});
describe("other permissions", () => {
it("should use workflow.create permission with admin/owner roles as fallback", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
};
mockCheckPermission.mockResolvedValue(true);
const result = await isAuthorized(workflow, 123, "workflow.create");
expect(result).toBe(true);
expect(mockCheckPermission).toHaveBeenCalledWith({
userId: 123,
teamId: 456,
permission: "workflow.create",
fallbackRoles: ["ADMIN", "OWNER"],
});
});
it("should use workflow.manage permission with admin/owner roles as fallback", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
};
mockCheckPermission.mockResolvedValue(true);
const result = await isAuthorized(workflow, 123, "workflow.manage");
expect(result).toBe(true);
expect(mockCheckPermission).toHaveBeenCalledWith({
userId: 123,
teamId: 456,
permission: "workflow.manage",
fallbackRoles: ["ADMIN", "OWNER"],
});
});
});
describe("permission service integration", () => {
it("should create a new PermissionCheckService instance for each call", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
};
mockCheckPermission.mockResolvedValue(true);
await isAuthorized(workflow, 123, "workflow.read");
await isAuthorized(workflow, 123, "workflow.update");
expect(mockPermissionCheckService).toHaveBeenCalledTimes(2);
});
it("should handle permission service errors gracefully", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
};
mockCheckPermission.mockRejectedValue(new Error("Permission service error"));
await expect(isAuthorized(workflow, 123, "workflow.read")).rejects.toThrow(
"Permission service error"
);
});
});
});
describe("edge cases", () => {
it("should handle workflow with teamId 0 as personal workflow", async () => {
const workflow = {
id: 1,
teamId: 0,
userId: 123,
};
const result = await isAuthorized(workflow, 123, "workflow.delete");
expect(result).toBe(true);
expect(mockPermissionCheckService).not.toHaveBeenCalled();
});
it("should handle workflow with positive teamId as team workflow", async () => {
const workflow = {
id: 1,
teamId: 1,
userId: 123,
};
mockCheckPermission.mockResolvedValue(true);
const result = await isAuthorized(workflow, 123, "workflow.read");
expect(result).toBe(true);
expect(mockCheckPermission).toHaveBeenCalledWith({
userId: 123,
teamId: 1,
permission: "workflow.read",
fallbackRoles: ["ADMIN", "OWNER", "MEMBER"],
});
});
it("should handle different user IDs correctly", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
};
mockCheckPermission.mockResolvedValue(true);
const result = await isAuthorized(workflow, 789, "workflow.read");
expect(result).toBe(true);
expect(mockCheckPermission).toHaveBeenCalledWith({
userId: 789,
teamId: 456,
permission: "workflow.read",
fallbackRoles: ["ADMIN", "OWNER", "MEMBER"],
});
});
it("should handle workflow with undefined teamId as personal workflow", async () => {
const workflow = {
id: 1,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
teamId: undefined as any,
userId: 123,
};
const result = await isAuthorized(workflow, 123, "workflow.delete");
expect(result).toBe(true);
expect(mockPermissionCheckService).not.toHaveBeenCalled();
});
});
describe("type safety", () => {
it("should work with minimal workflow object", async () => {
const workflow = {
id: 1,
teamId: null,
userId: 123,
};
const result = await isAuthorized(workflow, 123, "workflow.read");
expect(result).toBe(true);
});
it("should work with workflow object containing extra properties", async () => {
const workflow = {
id: 1,
teamId: 456,
userId: 123,
name: "Test Workflow",
createdAt: new Date(),
updatedAt: new Date(),
};
mockCheckPermission.mockResolvedValue(true);
const result = await isAuthorized(workflow, 123, "workflow.update");
expect(result).toBe(true);
});
});
});