11/** @vitest -environment node */
2- import { createMockRequest } from '@sim/testing'
3- import { beforeEach , describe , expect , it , vi } from 'vitest'
2+ import { createMockRequest , resetEnvFlagsMock , setEnvFlags } from '@sim/testing'
3+ import { afterEach , beforeEach , describe , expect , it , vi } from 'vitest'
44
55const mocks = vi . hoisted ( ( ) => ( {
66 session : vi . fn ( ) ,
@@ -22,7 +22,10 @@ vi.mock('@/lib/workspaces/permissions/utils', () => ({ isOrganizationAdminOrOwne
2222vi . mock ( '@/lib/billing/core/subscription' , ( ) => ( {
2323 isOrganizationOnEnterprisePlan : mocks . enterprise ,
2424} ) )
25- vi . mock ( '@/lib/permission-groups/resolve.server' , ( ) => ( { resolveWorkspaceGroup : mocks . group } ) )
25+ vi . mock ( '@/lib/permission-groups/resolve.server' , async ( importOriginal ) => ( {
26+ ...( await importOriginal < typeof import ( '@/lib/permission-groups/resolve.server' ) > ( ) ) ,
27+ resolveWorkspaceGroup : mocks . group ,
28+ } ) )
2629
2730import { userPermissionConfigSchema } from '@/lib/api/contracts/permission-groups'
2831import { OrchestrationError } from '@/lib/core/orchestration/types'
@@ -53,6 +56,7 @@ function get(query = '?workspaceId=workspace') {
5356
5457beforeEach ( ( ) => {
5558 vi . clearAllMocks ( )
59+ setEnvFlags ( { isHosted : true , isAccessControlEnabled : true } )
5660 mocks . session . mockResolvedValue ( {
5761 user : { id : 'viewer' } ,
5862 session : { id : 'session' , activeOrganizationId : 'unrelated-org' } ,
@@ -64,7 +68,40 @@ beforeEach(() => {
6468 mocks . group . mockResolvedValue ( null )
6569} )
6670
71+ afterEach ( resetEnvFlagsMock )
72+
6773describe ( 'user permission policy shared read' , ( ) => {
74+ it . each ( [
75+ { hosted : false , accessControl : false , entitled : false } ,
76+ { hosted : false , accessControl : true , entitled : true } ,
77+ { hosted : true , accessControl : false , entitled : true } ,
78+ ] ) (
79+ 'matches the active permission regime ($hosted, $accessControl)' ,
80+ async ( { hosted, accessControl, entitled } ) => {
81+ setEnvFlags ( {
82+ isHosted : hosted ,
83+ isAccessControlEnabled : accessControl ,
84+ isBillingEnabled : false ,
85+ } )
86+ mocks . admin . mockResolvedValue ( true )
87+ const group = {
88+ permissionGroupId : 'group' ,
89+ groupName : 'Restricted' ,
90+ config : { ...DEFAULT_PERMISSION_GROUP_CONFIG , hideCopilot : true } ,
91+ }
92+ mocks . group . mockResolvedValue ( group )
93+ const expected = { ...unrestricted , ...( entitled ? group : { } ) , entitled, isOrgAdmin : true }
94+ expect ( await ( await get ( ) ) . json ( ) ) . toEqual ( expected )
95+ expect (
96+ await readUserPermissionConfig . execute ( { principal, input : { workspaceId : 'workspace' } } )
97+ ) . toEqual ( expected )
98+ if ( ! entitled ) {
99+ expect ( mocks . group ) . not . toHaveBeenCalled ( )
100+ expect ( mocks . enterprise ) . not . toHaveBeenCalled ( )
101+ }
102+ }
103+ )
104+
68105 it ( 'authenticates before parsing or protected lookups' , async ( ) => {
69106 mocks . session . mockResolvedValue ( null )
70107 expect ( ( await get ( '' ) ) . status ) . toBe ( 401 )
0 commit comments