@@ -357,6 +357,9 @@ describe('POST /api/webhooks polling configuration', () => {
357357 } )
358358} )
359359
360+ const CREDENTIAL_ALLOWED = { ok : true , workspaceId : 'workspace-1' }
361+ const CREDENTIAL_DENIED = { ok : false , error : 'You do not have access to this credential.' }
362+
360363/** Mocks an actor with write access to `workflow-1` and no provider side effects. */
361364function setupUpsertMocks ( ) : void {
362365 vi . clearAllMocks ( )
@@ -539,7 +542,7 @@ describe('POST /api/webhooks credential references', () => {
539542 } )
540543
541544 it ( 'saves a credential the actor can use in the workflow workspace' , async ( ) => {
542- mocks . authorizeCredentialUseForAuth . mockResolvedValue ( { ok : true , workspaceId : 'workspace-1' } )
545+ mocks . authorizeCredentialUseForAuth . mockResolvedValue ( CREDENTIAL_ALLOWED )
543546 queueCreatePathRows ( )
544547
545548 const response = await POST ( upsertRequest ( { credentialId : 'own-credential' } ) )
@@ -578,7 +581,7 @@ describe('POST /api/webhooks credential references', () => {
578581 * authorized and kept, while a stored `userId` is never carried forward.
579582 */
580583 it ( 'authorizes and keeps the stored credential on a re-save that omits it' , async ( ) => {
581- mocks . authorizeCredentialUseForAuth . mockResolvedValue ( { ok : true , workspaceId : 'workspace-1' } )
584+ mocks . authorizeCredentialUseForAuth . mockResolvedValue ( CREDENTIAL_ALLOWED )
582585 queueUpdatePathRows ( true , { credentialId : 'stored-credential' , userId : 'stored-user' } )
583586
584587 const response = await POST ( upsertRequest ( { eventType : 'record.created' } ) )
@@ -596,10 +599,7 @@ describe('POST /api/webhooks credential references', () => {
596599 } )
597600
598601 it ( 'refuses a re-save whose stored credential the actor cannot use' , async ( ) => {
599- mocks . authorizeCredentialUseForAuth . mockResolvedValue ( {
600- ok : false ,
601- error : 'You do not have access to this credential.' ,
602- } )
602+ mocks . authorizeCredentialUseForAuth . mockResolvedValue ( CREDENTIAL_DENIED )
603603 queueUpdatePathRows ( true , { credentialId : 'stored-credential' } )
604604
605605 const response = await POST ( upsertRequest ( { eventType : 'record.created' } ) )
@@ -616,10 +616,7 @@ describe('POST /api/webhooks credential references', () => {
616616 it . each ( [ null , '' ] ) (
617617 'still authorizes the stored credential when a re-save sends credentialId %j' ,
618618 async ( credentialId ) => {
619- mocks . authorizeCredentialUseForAuth . mockResolvedValue ( {
620- ok : false ,
621- error : 'You do not have access to this credential.' ,
622- } )
619+ mocks . authorizeCredentialUseForAuth . mockResolvedValue ( CREDENTIAL_DENIED )
623620 mocks . shouldRecreateExternalWebhookSubscription . mockReturnValue ( true )
624621 queueUpdatePathRows ( true , { credentialId : 'stored-credential' } )
625622
@@ -638,9 +635,7 @@ describe('POST /api/webhooks credential references', () => {
638635 /** Rotation without recreation never touches the old credential, so it needs no access to it. */
639636 it ( 'rotates the credential without access to the stored one when nothing is recreated' , async ( ) => {
640637 mocks . authorizeCredentialUseForAuth . mockImplementation ( async ( _auth , { credentialId } ) =>
641- credentialId === 'new-credential'
642- ? { ok : true , workspaceId : 'workspace-1' }
643- : { ok : false , error : 'You do not have access to this credential.' }
638+ credentialId === 'new-credential' ? CREDENTIAL_ALLOWED : CREDENTIAL_DENIED
644639 )
645640 queueUpdatePathRows ( true , { credentialId : 'stored-credential' } )
646641
@@ -657,7 +652,7 @@ describe('POST /api/webhooks credential references', () => {
657652
658653 /** Recreation cleans up the previous subscription with the stored credential. */
659654 it ( 'authorizes both credentials when a rotation recreates the subscription' , async ( ) => {
660- mocks . authorizeCredentialUseForAuth . mockResolvedValue ( { ok : true , workspaceId : 'workspace-1' } )
655+ mocks . authorizeCredentialUseForAuth . mockResolvedValue ( CREDENTIAL_ALLOWED )
661656 mocks . shouldRecreateExternalWebhookSubscription . mockReturnValue ( true )
662657 queueUpdatePathRows ( true , { credentialId : 'stored-credential' } )
663658
@@ -675,7 +670,7 @@ describe('POST /api/webhooks credential references', () => {
675670 * when the request omits it, and a `userId` echoed back by the provider is not saved.
676671 */
677672 it ( 'authorizes the stored credential and drops userId when an omitting re-save recreates' , async ( ) => {
678- mocks . authorizeCredentialUseForAuth . mockResolvedValue ( { ok : true , workspaceId : 'workspace-1' } )
673+ mocks . authorizeCredentialUseForAuth . mockResolvedValue ( CREDENTIAL_ALLOWED )
679674 mocks . shouldRecreateExternalWebhookSubscription . mockReturnValue ( true )
680675 mocks . createExternalWebhookSubscription . mockResolvedValue ( {
681676 updatedProviderConfig : { externalId : 'subscription-2' , userId : 'stored-user' } ,
@@ -691,7 +686,6 @@ describe('POST /api/webhooks credential references', () => {
691686 workflowId : 'workflow-1' ,
692687 } )
693688 const savedConfig = dbChainMockFns . set . mock . calls . at ( - 1 ) ?. [ 0 ] . providerConfig
694- expect ( savedConfig . userId ) . toBeUndefined ( )
695689 expect ( savedConfig ) . toEqual ( { eventType : 'record.created' , externalId : 'subscription-2' } )
696690 } )
697691
0 commit comments