@@ -15,13 +15,11 @@ afterAll(resetEnvironmentUtilsMock)
1515const {
1616 ensureWorkflowAccessMock,
1717 ensureWorkspaceAccessMock,
18- getDefaultWorkspaceIdMock,
1918 listCredentialsMock,
2019 performUpdateCredentialMock,
2120} = vi . hoisted ( ( ) => ( {
2221 ensureWorkflowAccessMock : vi . fn ( ) ,
2322 ensureWorkspaceAccessMock : vi . fn ( ) ,
24- getDefaultWorkspaceIdMock : vi . fn ( ) ,
2523 listCredentialsMock : vi . fn ( ) ,
2624 performUpdateCredentialMock : vi . fn ( ) ,
2725} ) )
@@ -37,7 +35,6 @@ vi.mock('@/lib/credentials/orchestration', () => ({
3735vi . mock ( '@/lib/copilot/tools/handlers/access' , ( ) => ( {
3836 ensureWorkflowAccess : ensureWorkflowAccessMock ,
3937 ensureWorkspaceAccess : ensureWorkspaceAccessMock ,
40- getDefaultWorkspaceId : getDefaultWorkspaceIdMock ,
4138} ) )
4239
4340import { setEnvironmentVariablesServerTool } from './set-environment-variables'
@@ -49,7 +46,6 @@ describe('setEnvironmentVariablesServerTool', () => {
4946 workflow : { id : 'wf-1' , workspaceId : 'ws-from-workflow' } ,
5047 } )
5148 ensureWorkspaceAccessMock . mockResolvedValue ( undefined )
52- getDefaultWorkspaceIdMock . mockResolvedValue ( 'ws-default' )
5349 upsertPersonalEnvVarsMock . mockResolvedValue ( { added : [ 'API_KEY' ] , updated : [ ] } )
5450 upsertWorkspaceEnvVarsMock . mockResolvedValue ( [ 'API_KEY' ] )
5551 listCredentialsMock . mockResolvedValue ( {
@@ -97,22 +93,59 @@ describe('setEnvironmentVariablesServerTool', () => {
9793 expect ( result . scope ) . toBe ( 'personal' )
9894 } )
9995
100- it ( 'falls back to the default workspace when none is in context' , async ( ) => {
101- await setEnvironmentVariablesServerTool . execute (
102- {
103- variables : [ { name : 'API_KEY' , value : 'secret' } ] ,
104- } ,
105- {
106- userId : 'user-1' ,
107- }
96+ it ( 'fails closed when the context carries no workspace' , async ( ) => {
97+ await expect (
98+ setEnvironmentVariablesServerTool . execute (
99+ { variables : [ { name : 'API_KEY' , value : 'secret' } ] } ,
100+ { userId : 'user-1' }
101+ )
102+ ) . rejects . toThrow ( 'Copilot execution workspace is required' )
103+
104+ expect ( upsertWorkspaceEnvVarsMock ) . not . toHaveBeenCalled ( )
105+ } )
106+
107+ it ( 'accepts a workspaceId that re-asserts the execution workspace' , async ( ) => {
108+ const result = await setEnvironmentVariablesServerTool . execute (
109+ { workspaceId : 'ws-1' , variables : [ { name : 'API_KEY' , value : 'secret' } ] } ,
110+ { userId : 'user-1' , workspaceId : 'ws-1' }
108111 )
109112
110- expect ( getDefaultWorkspaceIdMock ) . toHaveBeenCalledWith ( 'user-1' )
111- expect ( upsertWorkspaceEnvVarsMock ) . toHaveBeenCalledWith (
112- 'ws-default' ,
113- { API_KEY : 'secret' } ,
114- 'user-1'
113+ expect ( upsertWorkspaceEnvVarsMock ) . toHaveBeenCalledWith ( 'ws-1' , { API_KEY : 'secret' } , 'user-1' )
114+ expect ( result . workspaceId ) . toBe ( 'ws-1' )
115+ } )
116+
117+ it ( 'rejects a workspaceId that names a different workspace' , async ( ) => {
118+ await expect (
119+ setEnvironmentVariablesServerTool . execute (
120+ { workspaceId : 'ws-other' , variables : [ { name : 'API_KEY' , value : 'secret' } ] } ,
121+ { userId : 'user-1' , workspaceId : 'ws-1' }
122+ )
123+ ) . rejects . toThrow ( 'Workspace ID does not match the Copilot execution workspace' )
124+
125+ expect ( upsertWorkspaceEnvVarsMock ) . not . toHaveBeenCalled ( )
126+ } )
127+
128+ it ( 'resolves the workspace from a workflow in the execution workspace' , async ( ) => {
129+ ensureWorkflowAccessMock . mockResolvedValue ( { workflow : { id : 'wf-1' , workspaceId : 'ws-1' } } )
130+
131+ await setEnvironmentVariablesServerTool . execute (
132+ { workflowId : 'wf-1' , variables : [ { name : 'API_KEY' , value : 'secret' } ] } ,
133+ { userId : 'user-1' , workspaceId : 'ws-1' }
115134 )
135+
136+ expect ( ensureWorkflowAccessMock ) . toHaveBeenCalledWith ( 'wf-1' , 'user-1' , 'write' )
137+ expect ( upsertWorkspaceEnvVarsMock ) . toHaveBeenCalledWith ( 'ws-1' , { API_KEY : 'secret' } , 'user-1' )
138+ } )
139+
140+ it ( 'rejects a workflowId whose workspace differs from the execution workspace' , async ( ) => {
141+ await expect (
142+ setEnvironmentVariablesServerTool . execute (
143+ { workflowId : 'wf-1' , variables : [ { name : 'API_KEY' , value : 'secret' } ] } ,
144+ { userId : 'user-1' , workspaceId : 'ws-1' }
145+ )
146+ ) . rejects . toThrow ( 'Workspace ID does not match the Copilot execution workspace' )
147+
148+ expect ( upsertWorkspaceEnvVarsMock ) . not . toHaveBeenCalled ( )
116149 } )
117150
118151 it ( 'describes a workspace secret through the credential update handler, never rewriting its value' , async ( ) => {
0 commit comments