44import { databaseMock , drizzleOrmMock , loggerMock } from '@sim/testing'
55import { beforeEach , describe , expect , it , vi } from 'vitest'
66
7- const DEFAULT_PERMISSION_GROUP_CONFIG = {
8- allowedIntegrations : null ,
9- allowedModelProviders : null ,
10- hideTraceSpans : false ,
11- hideKnowledgeBaseTab : false ,
12- hideCopilot : false ,
13- hideApiKeysTab : false ,
14- hideEnvironmentTab : false ,
15- hideFilesTab : false ,
16- disableMcpTools : false ,
17- disableCustomTools : false ,
18- disableSkills : false ,
19- hideTemplates : false ,
20- disableInvitations : false ,
21- hideDeployApi : false ,
22- hideDeployMcp : false ,
23- hideDeployA2a : false ,
24- hideDeployChatbot : false ,
25- hideDeployTemplate : false ,
26- }
27-
28- const mockGetAllowedIntegrationsFromEnv = vi . fn < ( ) => string [ ] | null > ( )
29- const mockIsOrganizationOnEnterprisePlan = vi . fn < ( ) => Promise < boolean > > ( )
30- const mockGetProviderFromModel = vi . fn < ( model : string ) => string > ( )
7+ const {
8+ DEFAULT_PERMISSION_GROUP_CONFIG ,
9+ mockGetAllowedIntegrationsFromEnv,
10+ mockIsOrganizationOnEnterprisePlan,
11+ mockGetProviderFromModel,
12+ } = vi . hoisted ( ( ) => ( {
13+ DEFAULT_PERMISSION_GROUP_CONFIG : {
14+ allowedIntegrations : null ,
15+ allowedModelProviders : null ,
16+ hideTraceSpans : false ,
17+ hideKnowledgeBaseTab : false ,
18+ hideCopilot : false ,
19+ hideApiKeysTab : false ,
20+ hideEnvironmentTab : false ,
21+ hideFilesTab : false ,
22+ disableMcpTools : false ,
23+ disableCustomTools : false ,
24+ disableSkills : false ,
25+ hideTemplates : false ,
26+ disableInvitations : false ,
27+ hideDeployApi : false ,
28+ hideDeployMcp : false ,
29+ hideDeployA2a : false ,
30+ hideDeployChatbot : false ,
31+ hideDeployTemplate : false ,
32+ } ,
33+ mockGetAllowedIntegrationsFromEnv : vi . fn < ( ) => string [ ] | null > ( ) ,
34+ mockIsOrganizationOnEnterprisePlan : vi . fn < ( ) => Promise < boolean > > ( ) ,
35+ mockGetProviderFromModel : vi . fn < ( model : string ) => string > ( ) ,
36+ } ) )
3137
3238vi . mock ( '@sim/db' , ( ) => databaseMock )
3339vi . mock ( '@sim/db/schema' , ( ) => ( { } ) )
@@ -52,7 +58,6 @@ vi.mock('@/providers/utils', () => ({
5258 getProviderFromModel : mockGetProviderFromModel ,
5359} ) )
5460
55- import { getAllowedIntegrationsFromEnv } from '@/lib/core/config/feature-flags'
5661import {
5762 getUserPermissionConfig ,
5863 IntegrationNotAllowedError ,
@@ -112,13 +117,13 @@ describe('env allowlist fallback when userId is absent', () => {
112117 vi . clearAllMocks ( )
113118 } )
114119
115- it ( 'returns null config when no userId and no env allowlist' , async ( ) => {
120+ it ( 'returns null allowlist when no userId and no env allowlist' , async ( ) => {
116121 mockGetAllowedIntegrationsFromEnv . mockReturnValue ( null )
117122
118123 const userId : string | undefined = undefined
119124 const permissionConfig = userId ? await getUserPermissionConfig ( userId ) : null
120125 const allowedIntegrations =
121- permissionConfig ?. allowedIntegrations ?? getAllowedIntegrationsFromEnv ( )
126+ permissionConfig ?. allowedIntegrations ?? mockGetAllowedIntegrationsFromEnv ( )
122127
123128 expect ( allowedIntegrations ) . toBeNull ( )
124129 } )
@@ -129,7 +134,7 @@ describe('env allowlist fallback when userId is absent', () => {
129134 const userId : string | undefined = undefined
130135 const permissionConfig = userId ? await getUserPermissionConfig ( userId ) : null
131136 const allowedIntegrations =
132- permissionConfig ?. allowedIntegrations ?? getAllowedIntegrationsFromEnv ( )
137+ permissionConfig ?. allowedIntegrations ?? mockGetAllowedIntegrationsFromEnv ( )
133138
134139 expect ( allowedIntegrations ) . toEqual ( [ 'slack' , 'gmail' ] )
135140 } )
@@ -140,7 +145,7 @@ describe('env allowlist fallback when userId is absent', () => {
140145 const userId : string | undefined = undefined
141146 const permissionConfig = userId ? await getUserPermissionConfig ( userId ) : null
142147 const allowedIntegrations =
143- permissionConfig ?. allowedIntegrations ?? getAllowedIntegrationsFromEnv ( )
148+ permissionConfig ?. allowedIntegrations ?? mockGetAllowedIntegrationsFromEnv ( )
144149
145150 expect ( allowedIntegrations ) . not . toBeNull ( )
146151 expect ( allowedIntegrations ! . includes ( 'slack' ) ) . toBe ( true )
@@ -210,14 +215,12 @@ describe('validateBlockType', () => {
210215 await validateBlockType ( undefined , 'GOOGLE_DRIVE' )
211216 } )
212217
213- it ( 'includes reason in error for env-only enforcement ' , async ( ) => {
218+ it ( 'includes env reason in error when env allowlist is the source ' , async ( ) => {
214219 await expect ( validateBlockType ( undefined , 'discord' ) ) . rejects . toThrow ( / A L L O W E D _ I N T E G R A T I O N S / )
215220 } )
216221
217- it ( 'does not include env reason when userId is provided' , async ( ) => {
218- await expect ( validateBlockType ( 'user-123' , 'discord' ) ) . rejects . toThrow (
219- / p e r m i s s i o n g r o u p s e t t i n g s /
220- )
222+ it ( 'includes env reason even when userId is present if env is the source' , async ( ) => {
223+ await expect ( validateBlockType ( 'user-123' , 'discord' ) ) . rejects . toThrow ( / A L L O W E D _ I N T E G R A T I O N S / )
221224 } )
222225 } )
223226} )
0 commit comments