diff --git a/packages/core/src/modules/credentials/__tests__/V1CredentialServiceCred.test.ts b/packages/core/src/modules/credentials/__tests__/V1CredentialServiceCred.test.ts index dfb026a23a..84eb9ed333 100644 --- a/packages/core/src/modules/credentials/__tests__/V1CredentialServiceCred.test.ts +++ b/packages/core/src/modules/credentials/__tests__/V1CredentialServiceCred.test.ts @@ -816,8 +816,7 @@ describe('CredentialService', () => { }) it('deleteAssociatedCredential parameter should call deleteCredential in indyHolderService with credentialId', async () => { - const deleteCredentialMock = indyHolderService.deleteCredential as jest.Mock, [string]> - + const deleteCredentialMock = mockFunction(indyHolderService.deleteCredential) const credentialRecord = mockCredentialRecord() mockFunction(credentialRepository.getById).mockReturnValue(Promise.resolve(credentialRecord)) @@ -826,6 +825,17 @@ describe('CredentialService', () => { }) expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId) }) + + it('deleteAssociatedCredentials not set - defaults to true , credential still deleted by default', async () => { + const deleteCredentialMock = mockFunction(indyHolderService.deleteCredential) + + const credentialRecord = mockCredentialRecord() + mockFunction(credentialRepository.getById).mockReturnValue(Promise.resolve(credentialRecord)) + + // deleteAssociatedCredentials not set - defaults to true + await credentialService.delete(credentialRecord) + expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId) + }) }) describe('declineOffer', () => { diff --git a/packages/core/src/modules/credentials/__tests__/V2CredentialServiceCred.test.ts b/packages/core/src/modules/credentials/__tests__/V2CredentialServiceCred.test.ts index ac4a73dd6a..57bba81d93 100644 --- a/packages/core/src/modules/credentials/__tests__/V2CredentialServiceCred.test.ts +++ b/packages/core/src/modules/credentials/__tests__/V2CredentialServiceCred.test.ts @@ -806,7 +806,7 @@ describe('CredentialService', () => { }) it('deleteAssociatedCredential parameter should call deleteCredential in indyHolderService with credentialId', async () => { - const storeCredentialMock = indyHolderService.deleteCredential as jest.Mock, [string]> + const deleteCredentialMock = mockFunction(indyHolderService.deleteCredential) const credentialRecord = mockCredentialRecord() mockFunction(credentialRepository.getById).mockReturnValue(Promise.resolve(credentialRecord)) @@ -814,7 +814,18 @@ describe('CredentialService', () => { await credentialService.delete(credentialRecord, { deleteAssociatedCredentials: true, }) - expect(storeCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId) + expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId) + }) + + it('deleteAssociatedCredentials not set - defaults to true , credential still deleted by default', async () => { + const deleteCredentialMock = mockFunction(indyHolderService.deleteCredential) + + const credentialRecord = mockCredentialRecord() + mockFunction(credentialRepository.getById).mockReturnValue(Promise.resolve(credentialRecord)) + + // deleteAssociatedCredentials not set - defaults to true + await credentialService.delete(credentialRecord) + expect(deleteCredentialMock).toHaveBeenNthCalledWith(1, credentialRecord.credentials[0].credentialRecordId) }) }) diff --git a/packages/core/src/modules/credentials/services/CredentialService.ts b/packages/core/src/modules/credentials/services/CredentialService.ts index 909df658a1..2122deb3ca 100644 --- a/packages/core/src/modules/credentials/services/CredentialService.ts +++ b/packages/core/src/modules/credentials/services/CredentialService.ts @@ -7,6 +7,7 @@ import type { InboundMessageContext } from '../../../agent/models/InboundMessage import type { Logger } from '../../../logger' import type { DidCommMessageRepository } from '../../../storage' import type { MediationRecipientService } from '../../routing' +import type { CredentialFormatService } from '../formats/CredentialFormatService' import type { DidResolverService } from './../../dids/services/DidResolverService' import type { CredentialStateChangedEvent } from './../CredentialEvents' import type { CredentialProtocolVersion } from './../CredentialProtocolVersion' @@ -24,7 +25,6 @@ import type { NegotiateProposalOptions, ProposeCredentialOptions, } from './../CredentialsModuleOptions' -import type { CredentialFormatService } from './../formats/CredentialFormatService' import type { CredentialFormats, HandlerAutoAcceptOptions } from './../formats/models/CredentialFormatServiceOptions' import type { V1CredentialProblemReportMessage, @@ -252,7 +252,9 @@ export abstract class CredentialService { public async delete(credentialRecord: CredentialExchangeRecord, options?: DeleteCredentialOptions): Promise { await this.credentialRepository.delete(credentialRecord) - if (options?.deleteAssociatedCredentials) { + const deleteAssociatedCredentials = options?.deleteAssociatedCredentials ?? true + + if (deleteAssociatedCredentials) { for (const credential of credentialRecord.credentials) { const formatService: CredentialFormatService = this.getFormatService(credential.credentialRecordType) await formatService.deleteCredentialById(credential.credentialRecordId)