|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { auditMock, authMockFns, permissionsMock, permissionsMockFns } from '@sim/testing' |
| 5 | +import { NextRequest } from 'next/server' |
| 6 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 7 | + |
| 8 | +const { mockGetWorkspaceFile, mockGetShareForResource, mockUpsertFileShare, mockValidateSharing } = |
| 9 | + vi.hoisted(() => ({ |
| 10 | + mockGetWorkspaceFile: vi.fn(), |
| 11 | + mockGetShareForResource: vi.fn(), |
| 12 | + mockUpsertFileShare: vi.fn(), |
| 13 | + mockValidateSharing: vi.fn(), |
| 14 | + })) |
| 15 | + |
| 16 | +vi.mock('@/lib/uploads/contexts/workspace', () => ({ |
| 17 | + getWorkspaceFile: mockGetWorkspaceFile, |
| 18 | +})) |
| 19 | + |
| 20 | +vi.mock('@/lib/public-shares/share-manager', () => ({ |
| 21 | + getShareForResource: mockGetShareForResource, |
| 22 | + upsertFileShare: mockUpsertFileShare, |
| 23 | +})) |
| 24 | + |
| 25 | +vi.mock('@/ee/access-control/utils/permission-check', () => { |
| 26 | + class PublicFileSharingNotAllowedError extends Error { |
| 27 | + constructor() { |
| 28 | + super('Public file sharing is not allowed based on your permission group settings') |
| 29 | + this.name = 'PublicFileSharingNotAllowedError' |
| 30 | + } |
| 31 | + } |
| 32 | + return { validatePublicFileSharing: mockValidateSharing, PublicFileSharingNotAllowedError } |
| 33 | +}) |
| 34 | + |
| 35 | +vi.mock('@/lib/workspaces/permissions/utils', () => permissionsMock) |
| 36 | +vi.mock('@sim/audit', () => auditMock) |
| 37 | + |
| 38 | +const WS = '7727ef3f-8cf6-4686-b063-2bb006a10785' |
| 39 | +const FILE_ID = 'wf_abc' |
| 40 | + |
| 41 | +import { GET, PUT } from '@/app/api/workspaces/[id]/files/[fileId]/share/route' |
| 42 | + |
| 43 | +const params = (id = WS, fileId = FILE_ID) => ({ params: Promise.resolve({ id, fileId }) }) |
| 44 | + |
| 45 | +const putRequest = (body: unknown) => |
| 46 | + new NextRequest(`http://localhost/api/workspaces/${WS}/files/${FILE_ID}/share`, { |
| 47 | + method: 'PUT', |
| 48 | + headers: { 'Content-Type': 'application/json' }, |
| 49 | + body: JSON.stringify(body), |
| 50 | + }) |
| 51 | + |
| 52 | +const getRequest = () => |
| 53 | + new NextRequest(`http://localhost/api/workspaces/${WS}/files/${FILE_ID}/share`) |
| 54 | + |
| 55 | +const SHARE = { |
| 56 | + id: 'sh_1', |
| 57 | + token: 'tok_1', |
| 58 | + url: 'https://sim.ai/f/tok_1', |
| 59 | + isActive: true, |
| 60 | + resourceType: 'file' as const, |
| 61 | + resourceId: FILE_ID, |
| 62 | +} |
| 63 | + |
| 64 | +describe('share route', () => { |
| 65 | + beforeEach(() => { |
| 66 | + vi.clearAllMocks() |
| 67 | + authMockFns.mockGetSession.mockResolvedValue({ |
| 68 | + user: { id: 'user-1', name: 'User One', email: 'u@example.com' }, |
| 69 | + }) |
| 70 | + permissionsMockFns.mockGetUserEntityPermissions.mockResolvedValue('write') |
| 71 | + mockGetWorkspaceFile.mockResolvedValue({ id: FILE_ID, name: 'report.pdf' }) |
| 72 | + mockGetShareForResource.mockResolvedValue(SHARE) |
| 73 | + mockUpsertFileShare.mockResolvedValue(SHARE) |
| 74 | + mockValidateSharing.mockResolvedValue(undefined) // policy allows by default |
| 75 | + }) |
| 76 | + |
| 77 | + describe('GET', () => { |
| 78 | + it('returns 401 when unauthenticated', async () => { |
| 79 | + authMockFns.mockGetSession.mockResolvedValueOnce(null) |
| 80 | + const res = await GET(getRequest(), params()) |
| 81 | + expect(res.status).toBe(401) |
| 82 | + }) |
| 83 | + |
| 84 | + it('returns 403 when the caller has no workspace access', async () => { |
| 85 | + permissionsMockFns.mockGetUserEntityPermissions.mockResolvedValueOnce(null) |
| 86 | + const res = await GET(getRequest(), params()) |
| 87 | + expect(res.status).toBe(403) |
| 88 | + }) |
| 89 | + |
| 90 | + it('returns the share for a member', async () => { |
| 91 | + const res = await GET(getRequest(), params()) |
| 92 | + expect(res.status).toBe(200) |
| 93 | + expect(await res.json()).toEqual({ share: SHARE }) |
| 94 | + }) |
| 95 | + }) |
| 96 | + |
| 97 | + describe('PUT', () => { |
| 98 | + it('returns 403 for a read-only member', async () => { |
| 99 | + permissionsMockFns.mockGetUserEntityPermissions.mockResolvedValueOnce('read') |
| 100 | + const res = await PUT(putRequest({ isActive: true }), params()) |
| 101 | + expect(res.status).toBe(403) |
| 102 | + expect(mockUpsertFileShare).not.toHaveBeenCalled() |
| 103 | + }) |
| 104 | + |
| 105 | + it('returns 404 when the file is not in the workspace', async () => { |
| 106 | + mockGetWorkspaceFile.mockResolvedValueOnce(null) |
| 107 | + const res = await PUT(putRequest({ isActive: true }), params()) |
| 108 | + expect(res.status).toBe(404) |
| 109 | + expect(mockUpsertFileShare).not.toHaveBeenCalled() |
| 110 | + }) |
| 111 | + |
| 112 | + it('enables the share for a writer', async () => { |
| 113 | + const res = await PUT(putRequest({ isActive: true }), params()) |
| 114 | + expect(res.status).toBe(200) |
| 115 | + expect(mockUpsertFileShare).toHaveBeenCalledWith({ |
| 116 | + workspaceId: WS, |
| 117 | + fileId: FILE_ID, |
| 118 | + userId: 'user-1', |
| 119 | + isActive: true, |
| 120 | + }) |
| 121 | + expect(await res.json()).toEqual({ share: SHARE }) |
| 122 | + }) |
| 123 | + |
| 124 | + it('returns 403 when org access-control disables public sharing (enable)', async () => { |
| 125 | + const { PublicFileSharingNotAllowedError } = await import( |
| 126 | + '@/ee/access-control/utils/permission-check' |
| 127 | + ) |
| 128 | + mockValidateSharing.mockRejectedValueOnce(new PublicFileSharingNotAllowedError()) |
| 129 | + const res = await PUT(putRequest({ isActive: true }), params()) |
| 130 | + expect(res.status).toBe(403) |
| 131 | + expect(mockUpsertFileShare).not.toHaveBeenCalled() |
| 132 | + }) |
| 133 | + |
| 134 | + it('allows disabling a share even when policy disallows enabling', async () => { |
| 135 | + mockValidateSharing.mockRejectedValue(new Error('should not be called for disable')) |
| 136 | + const res = await PUT(putRequest({ isActive: false }), params()) |
| 137 | + expect(res.status).toBe(200) |
| 138 | + expect(mockValidateSharing).not.toHaveBeenCalled() |
| 139 | + expect(mockUpsertFileShare).toHaveBeenCalledWith({ |
| 140 | + workspaceId: WS, |
| 141 | + fileId: FILE_ID, |
| 142 | + userId: 'user-1', |
| 143 | + isActive: false, |
| 144 | + }) |
| 145 | + }) |
| 146 | + |
| 147 | + it('rejects a missing isActive body', async () => { |
| 148 | + const res = await PUT(putRequest({}), params()) |
| 149 | + expect(res.status).toBe(400) |
| 150 | + }) |
| 151 | + }) |
| 152 | +}) |
0 commit comments