1+ import {
2+ credentialsManagedOauthMock ,
3+ credentialsManagedOauthMockFns ,
4+ } from '@sim/testing/mocks/credentials-managed-oauth.mock'
15import { beforeEach , describe , expect , it , vi } from 'vitest'
26
3- const mocks = vi . hoisted ( ( ) => ( {
4- resolve : vi . fn ( ) ,
5- reject : vi . fn ( ) ,
6- recordAccess : vi . fn ( ) ,
7- } ) )
7+ const mocks = vi . hoisted ( ( ) => ( { recordAccess : vi . fn ( ) } ) )
88
9- vi . mock ( '@/lib/credentials/managed-oauth' , ( ) => ( {
10- resolveManagedOAuthToken : mocks . resolve ,
11- rejectManagedOAuthToken : mocks . reject ,
12- } ) )
9+ vi . mock ( '@/lib/credentials/managed-oauth' , ( ) => credentialsManagedOauthMock )
1310vi . mock ( '@/lib/oauth/token-resolution' , ( ) => ( { recordCredentialAccess : mocks . recordAccess } ) )
1411
1512import type { OrganizationMembershipContext } from '@/lib/core/application/organization-authorization'
1613import { createAccountTokenRead } from '@/lib/organizations/home/account-token'
1714import { HomeProviderReadError } from '@/lib/organizations/home/provider-reads'
1815
16+ const mockResolve = credentialsManagedOauthMockFns . mockResolveManagedOAuthToken
17+ const mockReject = credentialsManagedOauthMockFns . mockRejectManagedOAuthToken
1918const input = {
2019 context : { organizationId : 'org' , userId : 'viewer' } as OrganizationMembershipContext ,
2120 credentialId : 'credential' ,
@@ -26,19 +25,19 @@ const refused = () => new HomeProviderReadError('token_rejected', 401)
2625describe ( 'one request reading an account several times' , ( ) => {
2726 beforeEach ( ( ) => {
2827 let issued = 0
29- mocks . resolve . mockReset ( ) . mockImplementation ( async ( ) => {
28+ mockResolve . mockReset ( ) . mockImplementation ( async ( ) => {
3029 issued += 1
3130 return { accessToken : `token-${ issued } ` }
3231 } )
33- mocks . reject . mockReset ( ) . mockResolvedValue ( false )
32+ mockReject . mockReset ( ) . mockResolvedValue ( false )
3433 mocks . recordAccess . mockReset ( )
3534 } )
3635
3736 it ( 'resolves and records the token once across reads' , async ( ) => {
3837 const withToken = createAccountTokenRead ( input )
3938 const used = [ await withToken ( async ( token ) => token ) , await withToken ( async ( token ) => token ) ]
4039 expect ( used ) . toEqual ( [ 'token-1' , 'token-1' ] )
41- expect ( mocks . resolve ) . toHaveBeenCalledOnce ( )
40+ expect ( mockResolve ) . toHaveBeenCalledOnce ( )
4241 expect ( mocks . recordAccess ) . toHaveBeenCalledOnce ( )
4342 } )
4443
@@ -53,11 +52,11 @@ describe('one request reading an account several times', () => {
5352 } )
5453 expect ( seen ) . toEqual ( [ 'token-1' , 'token-2' ] )
5554 expect ( retried ) . toBe ( 'token-2' )
56- expect ( mocks . reject ) . toHaveBeenCalledWith (
55+ expect ( mockReject ) . toHaveBeenCalledWith (
5756 expect . objectContaining ( { rejectedAccessToken : 'token-1' } )
5857 )
5958 await expect ( withToken ( async ( token ) => token ) ) . resolves . toBe ( 'token-2' )
60- expect ( mocks . resolve ) . toHaveBeenCalledTimes ( 2 )
59+ expect ( mockResolve ) . toHaveBeenCalledTimes ( 2 )
6160 } )
6261
6362 it ( 'asks to reconnect when the fresh token is refused too' , async ( ) => {
@@ -77,7 +76,7 @@ describe('one request reading an account several times', () => {
7776 throw failure
7877 } )
7978 ) . rejects . toBe ( failure )
80- expect ( mocks . reject ) . not . toHaveBeenCalled ( )
79+ expect ( mockReject ) . not . toHaveBeenCalled ( )
8180 await expect ( withToken ( async ( token ) => token ) ) . resolves . toBe ( 'token-1' )
8281 } )
8382
0 commit comments