@@ -5,11 +5,13 @@ import { auditMock, authMockFns, permissionsMock, permissionsMockFns } from '@si
55import { NextRequest } from 'next/server'
66import { beforeEach , describe , expect , it , vi } from 'vitest'
77
8- const { mockGetWorkspaceFile, mockGetShareForResource, mockUpsertFileShare } = vi . hoisted ( ( ) => ( {
9- mockGetWorkspaceFile : vi . fn ( ) ,
10- mockGetShareForResource : vi . fn ( ) ,
11- mockUpsertFileShare : vi . fn ( ) ,
12- } ) )
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+ } ) )
1315
1416vi . mock ( '@/lib/uploads/contexts/workspace' , ( ) => ( {
1517 getWorkspaceFile : mockGetWorkspaceFile ,
@@ -20,6 +22,16 @@ vi.mock('@/lib/public-shares/share-manager', () => ({
2022 upsertFileShare : mockUpsertFileShare ,
2123} ) )
2224
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+
2335vi . mock ( '@/lib/workspaces/permissions/utils' , ( ) => permissionsMock )
2436vi . mock ( '@sim/audit' , ( ) => auditMock )
2537
@@ -59,6 +71,7 @@ describe('share route', () => {
5971 mockGetWorkspaceFile . mockResolvedValue ( { id : FILE_ID , name : 'report.pdf' } )
6072 mockGetShareForResource . mockResolvedValue ( SHARE )
6173 mockUpsertFileShare . mockResolvedValue ( SHARE )
74+ mockValidateSharing . mockResolvedValue ( undefined ) // policy allows by default
6275 } )
6376
6477 describe ( 'GET' , ( ) => {
@@ -108,6 +121,29 @@ describe('share route', () => {
108121 expect ( await res . json ( ) ) . toEqual ( { share : SHARE } )
109122 } )
110123
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+
111147 it ( 'rejects a missing isActive body' , async ( ) => {
112148 const res = await PUT ( putRequest ( { } ) , params ( ) )
113149 expect ( res . status ) . toBe ( 400 )
0 commit comments