From 9aff7723fae6dfa37d0ef06f6997c948b7e86ac7 Mon Sep 17 00:00:00 2001 From: James Chartrand Date: Sun, 12 Jan 2025 17:38:48 -0500 Subject: [PATCH] fix tests --- src/Verify.ts | 4 +-- src/test-fixtures/expectedResults.ts | 52 +++++++++++++++++++++++++++ src/test-fixtures/vc.ts | 6 ++++ test/Verify.spec.ts | 53 +++++++--------------------- 4 files changed, 73 insertions(+), 42 deletions(-) create mode 100644 src/test-fixtures/expectedResults.ts diff --git a/src/Verify.ts b/src/Verify.ts index b713557..10f3464 100644 --- a/src/Verify.ts +++ b/src/Verify.ts @@ -21,8 +21,6 @@ const documentLoader = securityLoader({ fetchRemoteContexts: true }).build(); const suite = new Ed25519Signature2020(); - - export async function verifyCredential({credential, knownDIDRegistries, reloadIssuerRegistry = true}:{credential: Credential, knownDIDRegistries: object, reloadIssuerRegistry: boolean}): Promise { @@ -39,6 +37,7 @@ export async function verifyCredential({credential, knownDIDRegistries, reloadIs checkStatus: getCredentialStatusChecker(credential) }); + verificationResponse.isFatal = false verificationResponse.credential = credential @@ -57,6 +56,7 @@ export async function verifyCredential({credential, knownDIDRegistries, reloadIs delete verificationResponse.results delete verificationResponse.statusResult + delete verificationResponse.verified const { issuer } = credential await addTrustedIssuersToVerificationResponse({verificationResponse, knownDIDRegistries,reloadIssuerRegistry, issuer}) diff --git a/src/test-fixtures/expectedResults.ts b/src/test-fixtures/expectedResults.ts new file mode 100644 index 0000000..6d84e9d --- /dev/null +++ b/src/test-fixtures/expectedResults.ts @@ -0,0 +1,52 @@ +import { VerificationResponse, VerificationStep } from "src/types/result"; + +const expectedResult = { + "credential": {}, + "isFatal": false, + "log": [ + { + "id": "valid_signature", + "valid": true + }, + { + "id": "issuer_did_resolves", + "valid": true + }, + { + "id": "expiration", + "valid": true + }, + { + "id": "registered_issuer", + "valid": true, + "foundInRegistries": [ + "DCC Sandbox Registry" + ] + } + ] + } + + const getCopyOfExpectedResult = (credential:object, withStatus: boolean) : VerificationResponse => { + const expectedResultCopy = JSON.parse(JSON.stringify(expectedResult)) + if (withStatus) { + expectedResultCopy.log?.push( + { + "id": "revocation_status", + "valid": true + } + ) + } + expectedResultCopy.credential = credential; + return expectedResultCopy; + } + + export const getExpectedVerifiedResult = ({credential, withStatus }: {credential:object, withStatus:boolean}) : VerificationResponse => { + return getCopyOfExpectedResult(credential, withStatus); + } + + export const getExpectedUnverifiedResult = ( {credential, unVerifiedStep, withStatus }: {credential:object, unVerifiedStep:string, withStatus:boolean}) : VerificationResponse => { + const expectedResult = getCopyOfExpectedResult(credential, withStatus); + const step = expectedResult.log?.find((entry:VerificationStep)=>entry.id === unVerifiedStep) + if (step) step.valid = false; + return expectedResult; + } \ No newline at end of file diff --git a/src/test-fixtures/vc.ts b/src/test-fixtures/vc.ts index 72b5ca2..3afab68 100644 --- a/src/test-fixtures/vc.ts +++ b/src/test-fixtures/vc.ts @@ -6,6 +6,7 @@ import { v2ExpiredWithValidStatus } from "./verifiableCredentials/v2/v2ExpiredWi import { v1WithValidStatus } from "./verifiableCredentials/v1/v1WithValidStatus.js" import { v1NoStatus } from "./verifiableCredentials/v1/v1NoStatus.js" +import { v1Revoked } from "./verifiableCredentials/v1/v1Revoked.js" import { v1Expired } from "./verifiableCredentials/v1/v1Expired.js" const getVCv1 = (): any => { @@ -49,6 +50,10 @@ const getVCv2Expired = (): any => { const getVCv2Revoked = (): any => { return JSON.parse(JSON.stringify(v2Revoked)) } +const getVCv1Revoked = (): any => { + return JSON.parse(JSON.stringify(v1Revoked)) +} + const getVCv2ExpiredAndTampered = (): any => { const cred = getVCv2Expired() cred.name = 'tampered!' @@ -71,6 +76,7 @@ export { getVCv2, getVCv1Expired, + getVCv1Revoked, getVCv1Tampered, getVCv1ValidStatus, getVCv1, diff --git a/test/Verify.spec.ts b/test/Verify.spec.ts index 393790b..637b260 100644 --- a/test/Verify.spec.ts +++ b/test/Verify.spec.ts @@ -2,62 +2,31 @@ import chai from 'chai' import deepEqualInAnyOrder from 'deep-equal-in-any-order' import { strict as assert } from 'assert'; import { verifyCredential } from '../src/Verify.js' -import { getVCv2Expired, getVCv1Tampered, getVCv1Expired, getVCv1ValidStatus, getVCv2ValidStatus, getVCv2Tampered } from '../src/test-fixtures/vc.js' +import { getVCv2Expired, getVCv1Tampered, getVCv1Expired, getVCv1Revoked, getVCv1ValidStatus, getVCv2ValidStatus, getVCv2Tampered } from '../src/test-fixtures/vc.js' import { knownDIDRegistries } from '../.knownDidRegistries.js'; +import { getExpectedVerifiedResult, getExpectedUnverifiedResult } from '../src/test-fixtures/expectedResults.js'; chai.use(deepEqualInAnyOrder); const {expect} = chai; -const expectedResult = { - "verified": true, - "credential": null, - "isFatal": false, - "log": [ - { - "id": "valid_signature", - "valid": true - }, - { - "id": "issuer_did_resolves", - "valid": true - }, - { - "id": "expiration", - "valid": true - }, - { - "id": "revocation_status", - "valid": true - }, - { - "id": "registered_issuer", - "valid": true, - "foundInRegistries": [ - "DCC Sandbox Registry" - ] - } - ] -} - describe('Verify', () => { + describe('.verifyCredential', () => { describe('with VC version 1', () => { describe('returns fatal error', () => { - it('when tampered with', async () => { const tamperedVC1 : any = getVCv1Tampered() const result = await verifyCredential({credential: tamperedVC1, reloadIssuerRegistry: true, knownDIDRegistries}) assert.ok(result.verified === false); }) - }) describe('returns as verified', () => { it('when status is valid', async () => { const credential : any = getVCv1ValidStatus() - expectedResult.credential = credential; + const expectedResult = getExpectedVerifiedResult({credential, withStatus: true}) const result = await verifyCredential({credential, reloadIssuerRegistry: true, knownDIDRegistries}) expect(result).to.deep.equalInAnyOrder(expectedResult) // eslint-disable-line no-use-before-define }) @@ -67,7 +36,11 @@ describe('Verify', () => { it('when expired', async () => { const credential : any = getVCv1Expired() const result = await verifyCredential({credential, reloadIssuerRegistry: true, knownDIDRegistries}) - assert.ok(result.verified === false); + assert.ok(result.log); + }) + it('when revoked', async () => { + const credential : any = getVCv1Revoked() + const result = await verifyCredential({credential, reloadIssuerRegistry: true, knownDIDRegistries}) assert.ok(result.log); }) }) @@ -81,14 +54,13 @@ describe('Verify', () => { it('when tampered with', async () => { const tamperedVC2 : any = getVCv2Tampered() const result = await verifyCredential({credential: tamperedVC2, reloadIssuerRegistry: true, knownDIDRegistries}) - assert.ok(result.verified === false); }) }) describe('returns as verified', () => { it('when status is valid', async () => { const credential : any = getVCv2ValidStatus() - expectedResult.credential = credential; + const expectedResult = getExpectedVerifiedResult({credential, withStatus: true}) const result = await verifyCredential({credential, reloadIssuerRegistry: true, knownDIDRegistries}) expect(result).to.deep.equalInAnyOrder(expectedResult) // eslint-disable-line no-use-before-define }) @@ -97,11 +69,12 @@ describe('Verify', () => { describe('returns as unverified', () => { it('when expired', async () => { const credential : any = getVCv2Expired() + const expectedResult = getExpectedUnverifiedResult({credential, unVerifiedStep: 'expiration', withStatus:false}) const result = await verifyCredential({credential, reloadIssuerRegistry: true, knownDIDRegistries}) - assert.ok(result.verified === false); - assert.ok(result.log); + expect(result).to.deep.equalInAnyOrder(expectedResult) }) }) }) }) }) +