From a562302f1efa276e7690a9a93f29d7d4eef8438f Mon Sep 17 00:00:00 2001 From: ashwin275 <110539449+ashwin275@users.noreply.github.com> Date: Mon, 16 Dec 2024 17:56:38 +0530 Subject: [PATCH] Identiy Service : added test cases --- .../src/did/anchor-cord.service.spec.ts | 170 ++++++++++++++++++ .../src/did/did.service.spec.ts | 6 +- .../src/vc/vc.service.spec.ts | 6 +- 3 files changed, 176 insertions(+), 6 deletions(-) create mode 100644 services/identity-service/src/did/anchor-cord.service.spec.ts diff --git a/services/identity-service/src/did/anchor-cord.service.spec.ts b/services/identity-service/src/did/anchor-cord.service.spec.ts new file mode 100644 index 000000000..817677da4 --- /dev/null +++ b/services/identity-service/src/did/anchor-cord.service.spec.ts @@ -0,0 +1,170 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { DidService } from './did.service'; +import { BlockchainAnchorFactory } from './factories/blockchain-anchor.factory'; +import { VaultService } from '../utils/vault.service'; +import { PrismaService } from '../utils/prisma.service'; +import { GenerateDidDTO } from './dtos/GenerateDidRequest.dto'; +import { InternalServerErrorException } from '@nestjs/common'; + + +describe('DidService - generateDID', () => { + let service: DidService; + let blockchainFactory: BlockchainAnchorFactory; + let vaultService: VaultService; + let prismaService: PrismaService; + + const mockBlockchainService = { + anchorDid: jest.fn(), + }; + + const mockVaultService = { + writePvtKey: jest.fn(), + }; + + const mockPrismaService = { + identity: { + create: jest.fn(), + }, + }; + + const mockGenerateDidDTO: GenerateDidDTO = { + "services": [ + { + "id": "IdentityHub", + "type": "IdentityHub", + "serviceEndpoint": { + "instance": [ + "https://cord.network.in" + ] + } + } + ] + , "method": "cord" + } + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + providers: [ + DidService, + { + provide: BlockchainAnchorFactory, + useValue: { + getAnchorService: jest.fn(() => mockBlockchainService), + }, + }, + { + provide: VaultService, + useValue: mockVaultService, + }, + { + provide: PrismaService, + useValue: mockPrismaService, + }, + ], + }).compile(); + + service = module.get(DidService); + blockchainFactory = module.get(BlockchainAnchorFactory); + vaultService = module.get(VaultService); + prismaService = module.get(PrismaService); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + afterAll(async () => { + await prismaService?.$disconnect?.(); + }); + + it('should verify ANCHOR_TO_CORD is true', () => { + const anchorToCord = process.env.ANCHOR_TO_CORD; + const isTrue = anchorToCord?.toLowerCase().trim() === 'true'; + expect(isTrue).toBe(true); + }); + + + it('should verify the environment variable is a valid URL', () => { + const ISSUER_AGENT_BASE_URL = process.env.ISSUER_AGENT_BASE_URL; + function isValidUrl(value: string): boolean { + try { + new URL(value); + return true; + } catch (err) { + return false; + } + } + expect(isValidUrl(ISSUER_AGENT_BASE_URL)).toBe(true); + }); + + it('should anchor a DID to the blockchain and validate JSON keys', async () => { + const mockResponse = { + document: { + uri: 'did:cord:test123', + authentication: ['did:cord:test123#key-1'], + service: mockGenerateDidDTO.services, + keyAgreement: ['did:cord:test123#key-2'], + capabilityDelegation: ['did:cord:test123#key-3'], + assertionMethod: ['did:cord:test123#key-4'], + }, + mnemonic: 'mock-mnemonic', + delegateKeys: ['key1', 'key2'], + }; + + mockBlockchainService.anchorDid.mockResolvedValueOnce(mockResponse); + + const result = await service.generateDID(mockGenerateDidDTO); + console.log("result", result); + expect(blockchainFactory.getAnchorService).toHaveBeenCalledWith('cord'); + expect(mockBlockchainService.anchorDid).toHaveBeenCalledWith(mockGenerateDidDTO); + + + expect(result).toBeDefined(); + + + const expectedKeys = [ + 'uri', + 'authentication', + 'service', + 'keyAgreement', + 'capabilityDelegation', + 'assertionMethod', + ]; + + expectedKeys.forEach((key) => { + expect(result).toHaveProperty(key); + }); + }); + + + + + it('should throw an error if blockchain anchoring fails', async () => { + mockBlockchainService.anchorDid.mockRejectedValueOnce(new Error('Blockchain Error')); + + await expect(service.generateDID(mockGenerateDidDTO)).rejects.toThrow(InternalServerErrorException); + }); + + it('should throw an error if writing to the vault fails', async () => { + const mockResponse = { + document: { uri: 'did:cord:test123' }, + mnemonic: 'mock-mnemonic', + delegateKeys: ['key1', 'key2'], + }; + mockBlockchainService.anchorDid.mockResolvedValueOnce(mockResponse); + mockVaultService.writePvtKey.mockRejectedValueOnce(new Error('Vault Error')); + + await expect(service.generateDID(mockGenerateDidDTO)).rejects.toThrow(InternalServerErrorException); + }); + + it('should throw an error if writing to the database fails', async () => { + const mockResponse = { + document: { uri: 'did:cord:test123' }, + mnemonic: 'mock-mnemonic', + delegateKeys: ['key1', 'key2'], + }; + mockBlockchainService.anchorDid.mockResolvedValueOnce(mockResponse); + mockPrismaService.identity.create.mockRejectedValueOnce(new Error('Database Error')); + + await expect(service.generateDID(mockGenerateDidDTO)).rejects.toThrow(InternalServerErrorException); + }); +}); diff --git a/services/identity-service/src/did/did.service.spec.ts b/services/identity-service/src/did/did.service.spec.ts index db8ef10c9..ebec83cd2 100644 --- a/services/identity-service/src/did/did.service.spec.ts +++ b/services/identity-service/src/did/did.service.spec.ts @@ -2,7 +2,8 @@ import { Test, TestingModule } from '@nestjs/testing'; import { DidService } from './did.service'; import { PrismaService } from '../utils/prisma.service'; import { VaultService } from '../utils/vault.service'; -// import { AnchorCordService } from 'src/utils/cord.service'; +import { AnchorCordService } from './implementations/anchor-cord.service'; +import { BlockchainAnchorFactory } from './factories/blockchain-anchor.factory'; import { GenerateDidDTO, VerificationKeyType } from './dtos/GenerateDidRequest.dto'; import { ConfigService } from '@nestjs/config'; @@ -33,8 +34,7 @@ describe('DidService', () => { beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ - // providers: [DidService, PrismaService, VaultService, ConfigService,AnchorCordService], - providers: [DidService, PrismaService, VaultService, ConfigService], + providers: [DidService, PrismaService, VaultService, ConfigService,BlockchainAnchorFactory,AnchorCordService], }).compile(); service = module.get(DidService); diff --git a/services/identity-service/src/vc/vc.service.spec.ts b/services/identity-service/src/vc/vc.service.spec.ts index 22e10f545..ce243c672 100644 --- a/services/identity-service/src/vc/vc.service.spec.ts +++ b/services/identity-service/src/vc/vc.service.spec.ts @@ -3,7 +3,8 @@ import VcService from './vc.service'; import { PrismaService } from '../utils/prisma.service'; import { DidService } from '../did/did.service'; import { VaultService } from '../utils/vault.service'; -// import { AnchorCordService } from 'src/utils/cord.service'; +import { BlockchainAnchorFactory } from 'src/did/factories/blockchain-anchor.factory'; +import { AnchorCordService } from 'src/did/implementations/anchor-cord.service'; describe('DidService', () => { let service: VcService; let didService: DidService; @@ -17,8 +18,7 @@ describe('DidService', () => { beforeAll(async () => { const module: TestingModule = await Test.createTestingModule({ - providers: [VcService, PrismaService, DidService, VaultService], - // providers: [VcService, PrismaService, DidService, VaultService,AnchorCordService], + providers: [VcService, PrismaService, DidService, VaultService,BlockchainAnchorFactory,AnchorCordService], }).compile(); service = module.get(VcService);