Skip to content

Commit

Permalink
fix: Tests to allow multiple subjects for credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
nklomp committed Sep 29, 2022
1 parent 52b1662 commit 5e407ac
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions packages/ssi-types/__tests__/uniform-claims.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from 'fs'
import { CredentialMapper, ICredential, IVerifiableCredential } from '../src'
import { ICredentialSubject } from '../dist'

function getFile(path: string) {
return fs.readFileSync(path, 'utf-8')
Expand All @@ -14,27 +15,27 @@ describe('Uniform VC claims', () => {
const jwtVc: IVerifiableCredential = getFileAsJson('packages/ssi-types/__tests__/vc_vp_examples/vp/vp_general.json').verifiableCredential[0]
jwtVc['exp' as keyof IVerifiableCredential] = (+new Date()).toString()
const vc = CredentialMapper.toUniformCredential(jwtVc)
expect(vc.credentialSubject.expirationDate).toEqual(new Date(parseInt(jwtVc['exp' as keyof IVerifiableCredential] as string)).toISOString())
expect(vc.expirationDate).toEqual(new Date(parseInt(jwtVc['exp' as keyof IVerifiableCredential] as string)).toISOString())
})

it('should set expiration date if exp is present in JWT vc as number', () => {
const jwtVc: IVerifiableCredential = getFileAsJson('packages/ssi-types/__tests__/vc_vp_examples/vp/vp_general.json').verifiableCredential[0]

jwtVc['exp' as keyof IVerifiableCredential] = new Date().valueOf()
const vc = CredentialMapper.toUniformCredential(jwtVc)
expect(vc.credentialSubject.expirationDate).toEqual(new Date(jwtVc['exp' as keyof IVerifiableCredential] as string).toISOString())
expect(vc.expirationDate).toEqual(new Date(jwtVc['exp' as keyof IVerifiableCredential] as string).toISOString())
})

it('should throw an error if expiration date and exp are different in JWT vc', () => {
const jwtVc: IVerifiableCredential = getFileAsJson('packages/ssi-types/__tests__/vc_vp_examples/vp/vp_general.json').verifiableCredential[0]
jwtVc['exp' as keyof IVerifiableCredential] = (+new Date()).toString()
;(<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject.expirationDate = (+new Date(
;(<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).expirationDate = (+new Date(
(jwtVc['exp' as keyof IVerifiableCredential] as string) + 2
)).toString()
expect(() => CredentialMapper.toUniformCredential(jwtVc)).toThrowError(
`Inconsistent expiration dates between JWT claim (${new Date(
parseInt(jwtVc['exp' as keyof IVerifiableCredential] as string)
).toISOString()}) and VC value (${(<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject.expirationDate})`
).toISOString()}) and VC value (${(<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).expirationDate})`
)
})

Expand Down Expand Up @@ -79,17 +80,17 @@ describe('Uniform VC claims', () => {

it('should set credentialSubject.id if sub is present in JWT vc', () => {
const jwtVc: IVerifiableCredential = getFileAsJson('packages/ssi-types/__tests__/vc_vp_examples/vp/vp_general.json').verifiableCredential[0]
jwtVc['sub' as keyof IVerifiableCredential] = (<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject.id
jwtVc['sub' as keyof IVerifiableCredential] = (<ICredentialSubject>(<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject).id
const vc = CredentialMapper.toUniformCredential(jwtVc)
expect(vc.credentialSubject.id).toEqual(jwtVc['sub' as keyof IVerifiableCredential])
expect(!Array.isArray(vc.credentialSubject) && vc.credentialSubject.id).toEqual(jwtVc['sub' as keyof IVerifiableCredential])
})

it('should throw an error if credentialSubject.id and sub are different in JWT vc', () => {
const jwtVc: IVerifiableCredential = getFileAsJson('packages/ssi-types/__tests__/vc_vp_examples/vp/vp_general.json').verifiableCredential[0]
jwtVc['sub' as keyof IVerifiableCredential] = 'did:test:123'
expect(() => CredentialMapper.toUniformCredential(jwtVc)).toThrowError(
`Inconsistent credential subject ids between JWT claim (${jwtVc['sub' as keyof IVerifiableCredential]}) and VC value (${
(<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject.id
(<ICredentialSubject>(<ICredential>jwtVc['vc' as keyof IVerifiableCredential]).credentialSubject).id
})`
)
})
Expand Down Expand Up @@ -119,7 +120,8 @@ describe('Uniform VP claims', () => {
// vp should be decoded
expect(vp.holder).toEqual('did:example:ebfeb1f712ebc6f1c276e12ec21')
// vc should be decoded for a uniform vp
expect((vp.verifiableCredential[0] as IVerifiableCredential).credentialSubject.degree.type).toEqual('BachelorDegree')
const vc = vp.verifiableCredential[0] as IVerifiableCredential
expect(!Array.isArray(vc.credentialSubject) && vc.credentialSubject.degree.type).toEqual('BachelorDegree')
})

it('JWT Decoded VP should populate response', () => {
Expand All @@ -129,7 +131,8 @@ describe('Uniform VP claims', () => {
// vp should be decoded
expect(vp.holder).toEqual('did:example:ebfeb1f712ebc6f1c276e12ec21')
// vc should be decoded for a uniform vp
expect((vp.verifiableCredential[0] as IVerifiableCredential).credentialSubject.degree.type).toEqual('BachelorDegree')
const vc = vp.verifiableCredential[0] as IVerifiableCredential
expect(!Array.isArray(vc.credentialSubject) && vc.credentialSubject.degree.type).toEqual('BachelorDegree')
})

it('JSON-LD VP String should populate response', () => {
Expand Down

0 comments on commit 5e407ac

Please sign in to comment.