@@ -11,7 +11,7 @@ import { callWithBorrowedCredential, BORROWED_CREDENTIAL_SKEW_MS } from '@object
1111import { ConnectorUpstreamUnavailableError } from '@objectstack/spec/integration' ;
1212import type { ILock } from '@objectstack/spec/contracts' ;
1313import type { ConnectorConnectionRecord , ConnectorConnectionStore , ConnectorGrantWrite } from '../connector-connections.js' ;
14- import { createOAuth2GrantHandle , type OAuth2GrantConfig } from './grant-handle.js' ;
14+ import { createOAuth2GrantHandle , reconnectRequiredReason , type OAuth2GrantConfig } from './grant-handle.js' ;
1515
1616const TOKEN_URL = 'https://idp.example.com/oauth/token' ;
1717
@@ -308,7 +308,7 @@ describe('ADR-0143 D4 — authorization_code: refresh', () => {
308308 expect ( refused . message ) . not . toContain ( 'dummy-rt-a' ) ;
309309 expect ( calls . markReconnectRequired ) . toEqual ( [ 'org_a' ] ) ;
310310 expect ( held . get ( 'org_a' ) ) . toMatchObject ( { status : 'reconnect_required' , refreshToken : undefined } ) ;
311- expect ( onGrantRefused ) . toHaveBeenCalledWith ( 'org_a' ) ;
311+ expect ( onGrantRefused ) . toHaveBeenCalledWith ( 'org_a' , refused . message ) ;
312312 // The next dispatch reads the row: refused before any token request.
313313 await expect ( handle . get ( { organizationId : 'org_a' } ) ) . rejects . toMatchObject ( { status : 503 } ) ;
314314 expect ( idp . state . requests ) . toHaveLength ( 1 ) ;
@@ -410,7 +410,7 @@ describe('ADR-0143 D2.1 — an MCP instance whose connection keeps the client it
410410 await expect ( handle . get ( { organizationId : 'org_a' } ) ) . rejects . toMatchObject ( { code : 'SERVICE_UNAVAILABLE' , status : 503 } ) ;
411411 expect ( idp . seen ) . toHaveLength ( 0 ) ;
412412 expect ( calls . markReconnectRequired ) . toEqual ( [ 'org_a' ] ) ;
413- expect ( onGrantRefused ) . toHaveBeenCalledWith ( 'org_a' ) ;
413+ expect ( onGrantRefused ) . toHaveBeenCalledWith ( 'org_a' , expect . stringContaining ( 'keeps no client to refresh with' ) ) ;
414414 } ) ;
415415} ) ;
416416
@@ -458,3 +458,94 @@ describe('[#28863] the stored reason of a connection marked reconnect required n
458458 expect ( idp . state . requests ) . toHaveLength ( 1 ) ;
459459 } ) ;
460460} ) ;
461+
462+ describe ( '[#28963] a connection already marked reconnect required answers the cause it stores, and the log names it' , ( ) => {
463+ const NO_REFRESH_TOKEN = 'issued no refresh token, so an access token the connection has to renew cannot be renewed' ;
464+ const NO_CLIENT = 'keeps no client to refresh with, so an access token it has to renew cannot be renewed' ;
465+ const REFUSED = 'its authorization server refused the grant (revoked, or expired with no usable refresh)' ;
466+ const refusalOf = ( p : Promise < unknown > ) => p . then ( ( ) => { throw new Error ( 'the dispatch was not refused' ) ; } , ( e : Error & { status ?: number ; code ?: string } ) => e ) ;
467+
468+ it ( 'a row marked because no refresh token was issued answers that sentence on the first dispatch and on every later one' , async ( ) => {
469+ const idp = tokenEndpoint ( ) ;
470+ const { store, held } = fakeConnections ( [ { org : 'org_a' } ] ) ;
471+ const handle = createOAuth2GrantHandle ( { connector : 'crm' , config : AUTH_CODE , connections : store , fetch : idp . fetch } ) ;
472+ const first = await refusalOf ( handle . get ( { organizationId : 'org_a' } ) ) ;
473+ const stored = String ( held . get ( 'org_a' ) ?. statusReason ) ;
474+ expect ( stored ) . toContain ( NO_REFRESH_TOKEN ) ;
475+ expect ( first . message ) . toBe ( stored ) ;
476+ // Every later dispatch reads the marked row and answers what it stores, not the refusal sentence.
477+ for ( const later of [
478+ ( ) => handle . get ( { organizationId : 'org_a' } ) ,
479+ ( ) => handle . get ( { organizationId : 'org_a' } ) ,
480+ ( ) => handle . refresh ( { organizationId : 'org_a' } ) ,
481+ ] ) {
482+ const refused = await refusalOf ( later ( ) ) ;
483+ expect ( refused ) . toMatchObject ( { status : 503 , code : 'SERVICE_UNAVAILABLE' } ) ;
484+ expect ( refused . message ) . toBe ( stored ) ;
485+ expect ( refused . message ) . not . toMatch ( / r e f u s e d / ) ;
486+ }
487+ // So does a handle that never saw the marking (another replica, a restart).
488+ const other = createOAuth2GrantHandle ( { connector : 'crm' , config : AUTH_CODE , connections : store , fetch : idp . fetch } ) ;
489+ expect ( ( await refusalOf ( other . get ( { organizationId : 'org_a' } ) ) ) . message ) . toBe ( stored ) ;
490+ expect ( idp . state . requests ) . toHaveLength ( 0 ) ;
491+ } ) ;
492+
493+ it ( 'a row another replica marked while this one waited for the lock answers the cause that replica stored' , async ( ) => {
494+ const idp = tokenEndpoint ( ) ;
495+ const { store, held } = fakeConnections ( [ { org : 'org_a' , refreshToken : 'dummy-rt-a' } ] ) ;
496+ const markedThere = reconnectRequiredReason ( 'crm' , 'org_a' , 'no_client' ) ;
497+ const lock = {
498+ async withLock ( _key : string , fn : ( ) => Promise < unknown > ) {
499+ Object . assign ( held . get ( 'org_a' ) ! , { status : 'reconnect_required' , statusReason : markedThere , refreshToken : undefined } ) ;
500+ return fn ( ) ;
501+ } ,
502+ } as unknown as ILock ;
503+ const handle = createOAuth2GrantHandle ( { connector : 'crm' , config : AUTH_CODE , connections : store , lock : ( ) => lock , fetch : idp . fetch } ) ;
504+ const refused = await refusalOf ( handle . get ( { organizationId : 'org_a' } ) ) ;
505+ expect ( refused ) . toMatchObject ( { status : 503 , code : 'SERVICE_UNAVAILABLE' } ) ;
506+ expect ( refused . message ) . toBe ( markedThere ) ;
507+ expect ( idp . state . requests ) . toHaveLength ( 0 ) ;
508+ } ) ;
509+
510+ it ( 'a marked row that stores no cause still answers the refusal sentence, on both reads' , async ( ) => {
511+ const idp = tokenEndpoint ( ) ;
512+ const { store, held } = fakeConnections ( [ { org : 'org_a' , status : 'reconnect_required' } ] ) ;
513+ const handle = createOAuth2GrantHandle ( { connector : 'crm' , config : AUTH_CODE , connections : store , fetch : idp . fetch } ) ;
514+ const atSelect = await refusalOf ( handle . get ( { organizationId : 'org_a' } ) ) ;
515+ expect ( atSelect . message ) . toBe ( reconnectRequiredReason ( 'crm' , 'org_a' ) ) ;
516+ expect ( atSelect . message ) . toContain ( REFUSED ) ;
517+
518+ held . get ( 'org_a' ) ! . status = 'connected' ;
519+ held . get ( 'org_a' ) ! . refreshToken = 'dummy-rt-a' ;
520+ const lock = {
521+ async withLock ( _key : string , fn : ( ) => Promise < unknown > ) {
522+ held . get ( 'org_a' ) ! . status = 'reconnect_required' ;
523+ return fn ( ) ;
524+ } ,
525+ } as unknown as ILock ;
526+ const locked = createOAuth2GrantHandle ( { connector : 'crm' , config : AUTH_CODE , connections : store , lock : ( ) => lock , fetch : idp . fetch } ) ;
527+ const underLock = await refusalOf ( locked . get ( { organizationId : 'org_a' } ) ) ;
528+ expect ( underLock . message ) . toBe ( reconnectRequiredReason ( 'crm' , 'org_a' ) ) ;
529+ expect ( idp . state . requests ) . toHaveLength ( 0 ) ;
530+ } ) ;
531+
532+ it ( 'the plugin is told the cause of each refresh path, in the words it stored' , async ( ) => {
533+ const cases = [
534+ { cause : NO_REFRESH_TOKEN , config : AUTH_CODE , row : { org : 'org_a' } , answer : undefined } ,
535+ { cause : NO_CLIENT , config : { grant : 'authorization_code' } as OAuth2GrantConfig , row : { org : 'org_a' , refreshToken : 'dummy-rt-a' } , answer : undefined } ,
536+ { cause : REFUSED , config : AUTH_CODE , row : { org : 'org_a' , refreshToken : 'dummy-rt-a' } , answer : { status : 400 , body : { error : 'invalid_grant' } } } ,
537+ ] ;
538+ for ( const { cause, config, row, answer } of cases ) {
539+ const idp = tokenEndpoint ( ) ;
540+ if ( answer ) idp . state . answer = ( ) => answer ;
541+ const { store, held } = fakeConnections ( [ row ] ) ;
542+ const onGrantRefused = vi . fn ( ) ;
543+ const handle = createOAuth2GrantHandle ( { connector : 'crm' , config, connections : store , fetch : idp . fetch , onGrantRefused } ) ;
544+ await refusalOf ( handle . get ( { organizationId : 'org_a' } ) ) ;
545+ const stored = String ( held . get ( 'org_a' ) ?. statusReason ) ;
546+ expect ( stored ) . toContain ( cause ) ;
547+ expect ( onGrantRefused ) . toHaveBeenCalledTimes ( 1 ) ;
548+ expect ( onGrantRefused ) . toHaveBeenCalledWith ( 'org_a' , stored ) ;
549+ }
550+ } ) ;
551+ } ) ;
0 commit comments