@@ -447,6 +447,32 @@ describe('SCIM group push → mapped permission set', () => {
447447 expect ( rows . map ( ( r ) => r . organization_id ) ) . toEqual ( [ ORG_B ] ) ;
448448 } ) ;
449449
450+ it ( '(S5) a re-save after the connection moved organization is refused, never answered with an organization the row does not carry' , async ( ) => {
451+ const h = await boot ( ) ;
452+ const first = await saveMapping ( h , { connectionId : CONNECTION , groupExternalId : 'okta-grp-sales' , permissionSet : SALES } ) ;
453+ expect ( first . status , JSON . stringify ( first . body ) ) . toBe ( 200 ) ;
454+ const mappingId = mappingIdOf ( first ) ;
455+
456+ // CONTROL: a re-save in the same organization is idempotent.
457+ const again = await saveMapping ( h , { connectionId : CONNECTION , groupExternalId : 'okta-grp-sales' , permissionSet : SALES } ) ;
458+ expect ( again . status ) . toBe ( 200 ) ;
459+ expect ( ( again . body as unknown as { data : { mappingId : string ; created : boolean ; organizationId : string } } ) . data )
460+ . toMatchObject ( { mappingId, created : false , organizationId : ORG } ) ;
461+
462+ const ORG_B = 'org_scim_30959_b' ;
463+ await revokeScimConnectionCredential ( h . engine as never , h . credentialId ) ;
464+ await mintScimConnectionCredential ( h . engine as never , SECRET , { connectionId : CONNECTION , organizationId : ORG_B } ) ;
465+
466+ const refused = await saveMapping ( h , { connectionId : CONNECTION , groupExternalId : 'okta-grp-sales' , permissionSet : SALES } ) ;
467+ expect ( refused . status , JSON . stringify ( refused . body ) ) . toBe ( 409 ) ;
468+ const error = ( refused . body as { error : { code : string ; message : string } } ) . error ;
469+ expect ( error . code ) . toBe ( 'RESOURCE_CONFLICT' ) ;
470+ expect ( error . message ) . toContain ( mappingId ) ;
471+ expect ( error . message ) . toContain ( ORG ) ;
472+ const rows = ( await h . engine . find ( 'sys_idp_group_mapping' , { } , SYSTEM ) ) as Array < Record < string , unknown > > ;
473+ expect ( rows . map ( ( r ) => [ r . id , r . organization_id ] ) ) . toEqual ( [ [ mappingId , ORG ] ] ) ;
474+ } ) ;
475+
450476 it ( '(S4) a connection whose every credential is revoked is refused at save' , async ( ) => {
451477 const h = await boot ( ) ;
452478 await revokeScimConnectionCredential ( h . engine as never , h . credentialId ) ;
0 commit comments