@@ -14,6 +14,7 @@ const mocks = vi.hoisted(() => ({
1414 getBaseUrl : vi . fn ( ) ,
1515 requireClient : vi . fn ( ) ,
1616 createConnection : vi . fn ( ) ,
17+ getPerRequestScopes : vi . fn ( ) ,
1718 launchConnection : vi . fn ( ) ,
1819} ) )
1920
@@ -46,6 +47,9 @@ vi.mock('@/lib/credentials/application/launch-credential-connection', () => ({
4647 execute : mocks . launchConnection ,
4748 } ,
4849} ) )
50+ vi . mock ( '@/lib/oauth/utils' , ( ) => ( {
51+ getPerRequestOAuthLinkScopes : mocks . getPerRequestScopes ,
52+ } ) )
4953
5054import { GET } from '@/app/api/auth/oauth2/authorize/route'
5155
@@ -89,6 +93,7 @@ describe('OAuth2 authorize route', () => {
8993 } ,
9094 } )
9195 mocks . linkAccount . mockResolvedValue ( linkResponse ( ) )
96+ mocks . getPerRequestScopes . mockReturnValue ( undefined )
9297 } )
9398
9499 it ( 'creates a canonical application draft for a legacy connect URL' , async ( ) => {
@@ -123,6 +128,29 @@ describe('OAuth2 authorize route', () => {
123128 expect ( mocks . createConnection ) . not . toHaveBeenCalled ( )
124129 } )
125130
131+ it ( 'passes per-request scopes to providers that cannot inherit static connector scopes' , async ( ) => {
132+ const scopes = [ 'openid' , 'https://dynamics.microsoft.com/user_impersonation' ]
133+ mocks . getPerRequestScopes . mockReturnValue ( scopes )
134+ mocks . createConnection . mockResolvedValue ( {
135+ providerId : 'microsoft-dataverse' ,
136+ workspaceId : WORKSPACE_ID ,
137+ draftId : 'draft-1' ,
138+ expiresAt : new Date ( ) ,
139+ authorizationUrl : '' ,
140+ } )
141+
142+ await GET ( request ( { providerId : 'microsoft-dataverse' , workspaceId : WORKSPACE_ID } ) )
143+
144+ expect ( mocks . linkAccount ) . toHaveBeenCalledWith (
145+ expect . objectContaining ( {
146+ body : expect . objectContaining ( {
147+ providerId : 'microsoft-dataverse' ,
148+ scopes,
149+ } ) ,
150+ } )
151+ )
152+ } )
153+
126154 it ( 'launches an exact draft without creating another one' , async ( ) => {
127155 const response = await GET ( request ( { draftId : 'draft-1' } ) )
128156
0 commit comments