Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jchartrand committed Jan 12, 2025
1 parent 9b3777e commit 9aff772
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 42 deletions.
4 changes: 2 additions & 2 deletions src/Verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<VerificationResponse> {


Expand All @@ -39,6 +37,7 @@ export async function verifyCredential({credential, knownDIDRegistries, reloadIs
checkStatus: getCredentialStatusChecker(credential)
});


verificationResponse.isFatal = false
verificationResponse.credential = credential

Expand All @@ -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})
Expand Down
52 changes: 52 additions & 0 deletions src/test-fixtures/expectedResults.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { VerificationResponse, VerificationStep } from "src/types/result";

Check failure on line 1 in src/test-fixtures/expectedResults.ts

View workflow job for this annotation

GitHub Actions / test-karma (18.x)

Cannot find module 'src/types/result' or its corresponding type declarations.

Check failure on line 1 in src/test-fixtures/expectedResults.ts

View workflow job for this annotation

GitHub Actions / test-node (18.x)

Cannot find module 'src/types/result' or its corresponding type declarations.

Check failure on line 1 in src/test-fixtures/expectedResults.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Cannot find module 'src/types/result' or its corresponding type declarations.

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;
}
6 changes: 6 additions & 0 deletions src/test-fixtures/vc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down Expand Up @@ -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!'
Expand All @@ -71,6 +76,7 @@ export {
getVCv2,

getVCv1Expired,
getVCv1Revoked,
getVCv1Tampered,
getVCv1ValidStatus,
getVCv1,
Expand Down
53 changes: 13 additions & 40 deletions test/Verify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -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);
})
})
Expand All @@ -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
})
Expand All @@ -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)
})
})
})
})
})

0 comments on commit 9aff772

Please sign in to comment.