Skip to content

Commit

Permalink
Use klona for test data & move klona to mock file.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Dec 7, 2023
1 parent 611d267 commit b6fa6d3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@
"dependencies": {
"@digitalbazaar/credentials-v2-context": "github:digitalbazaar/credentials-v2-context#initial",
"credentials-context": "^2.0.0",
"ed25519-signature-2018-context": "^1.1.0",
"jsonld": "^8.3.1",
"jsonld-signatures": "^11.2.1",
"ed25519-signature-2018-context": "^1.1.0"
"jsonld-signatures": "^11.2.1"
},
"devDependencies": {
"@digitalbazaar/credentials-examples-context": "^1.0.0",
Expand Down Expand Up @@ -60,6 +60,7 @@
"karma-mocha-reporter": "^2.2.5",
"karma-sourcemap-loader": "^0.4.0",
"karma-webpack": "^5.0.0",
"klona": "^2.0.6",
"mocha": "^10.2.0",
"mocha-lcov-reporter": "^1.3.0",
"uuid": "^9.0.0",
Expand Down
34 changes: 17 additions & 17 deletions test/10-verify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ for(const [version, mockCredential] of versionedCredentials) {
describe(`Verifiable Credentials Data Model ${version}`, async function() {
describe('vc.issue()', () => {
it('should issue a verifiable credential with proof', async () => {
const credential = jsonld.clone(mockCredential);
const credential = mockCredential();
const verifiableCredential = await vc.issue({
credential,
suite,
Expand All @@ -108,7 +108,7 @@ for(const [version, mockCredential] of versionedCredentials) {
publicKeyBase58: keyPair.publicKeyBase58
});
keyPair.id = `did:key:${fp}#${fp}`;
const credential = jsonld.clone(mockCredential);
const credential = mockCredential();
credential.id = `urn:uuid:${uuid()}`;
credential.issuer = `did:key:${fp}`;
credential.expirationDate = '2020-05-31T19:21:25Z';
Expand All @@ -129,7 +129,7 @@ for(const [version, mockCredential] of versionedCredentials) {
verifiableCredential.proof.should.be.an('object');
});
it('should add "issuanceDate" to verifiable credentials', async () => {
const credential = jsonld.clone(mockCredential);
const credential = mockCredential();
delete credential.issuanceDate;
const now = new Date();
const expectedIssuanceDate = `${now.toISOString().slice(0, -5)}Z`;
Expand Down Expand Up @@ -157,7 +157,7 @@ for(const [version, mockCredential] of versionedCredentials) {
let error;
try {
await vc.issue({
credential: jsonld.clone(mockCredential),
credential: mockCredential(),
suite
});
} catch(e) {
Expand All @@ -174,7 +174,7 @@ for(const [version, mockCredential] of versionedCredentials) {
describe('vc.createPresentation()', () => {
it('should create an unsigned presentation', () => {
const presentation = vc.createPresentation({
verifiableCredential: jsonld.clone(mockCredential),
verifiableCredential: mockCredential(),
id: 'test:ebc6f1c2',
holder: 'did:ex:holder123'
});
Expand All @@ -189,7 +189,7 @@ for(const [version, mockCredential] of versionedCredentials) {
describe('vc.signPresentation()', () => {
it('should create a signed VP', async () => {
const presentation = vc.createPresentation({
verifiableCredential: jsonld.clone(mockCredential),
verifiableCredential: mockCredential(),
id: 'test:ebc6f1c2',
holder: 'did:ex:holder123',
version
Expand All @@ -214,7 +214,7 @@ for(const [version, mockCredential] of versionedCredentials) {
describe('verify API (credentials)', () => {
it('should verify a vc', async () => {
const verifiableCredential = await vc.issue({
credential: jsonld.clone(mockCredential),
credential: mockCredential(),
suite,
documentLoader
});
Expand Down Expand Up @@ -257,7 +257,7 @@ for(const [version, mockCredential] of versionedCredentials) {
});

const verifiableCredential = await vc.issue({
credential: jsonld.clone(mockCredential),
credential: mockCredential(),
suite: ecdsaSdSignSuite,
documentLoader
});
Expand All @@ -280,7 +280,7 @@ for(const [version, mockCredential] of versionedCredentials) {
});

it('should verify a vc with a positive status check', async () => {
const credential = jsonld.clone(mockCredential);
const credential = mockCredential();
credential['@context'].push({
'@context': {
id: '@id',
Expand Down Expand Up @@ -394,7 +394,7 @@ for(const [version, mockCredential] of versionedCredentials) {
});
it('should fail to verify a vc with a negative status check',
async () => {
const credential = jsonld.clone(mockCredential);
const credential = mockCredential();
credential['@context'].push({
'@context': {
id: '@id',
Expand Down Expand Up @@ -429,7 +429,7 @@ for(const [version, mockCredential] of versionedCredentials) {
});
it('should not run "checkStatus" on a vc without a ' +
'"credentialStatus" property', async () => {
const credential = jsonld.clone(mockCredential);
const credential = mockCredential();
const verifiableCredential = await vc.issue({
credential,
suite,
Expand Down Expand Up @@ -474,7 +474,7 @@ for(const [version, mockCredential] of versionedCredentials) {
});

const verifiableCredential = await vc.issue({
credential: jsonld.clone(mockCredential),
credential: mockCredential(),
suite: ecdsaSdSignSuite,
documentLoader
});
Expand Down Expand Up @@ -696,7 +696,7 @@ for(const [version, mockCredential] of versionedCredentials) {
});
if(version === 1.0) {
it('should reject if "now" is before "issuanceDate"', () => {
const credential = jsonld.clone(mockCredential);
const credential = mockCredential();
credential.issuer = 'did:example:12345';
credential.issuanceDate = '2022-10-31T19:21:25Z';
const now = '2022-06-30T19:21:25Z';
Expand All @@ -715,7 +715,7 @@ for(const [version, mockCredential] of versionedCredentials) {

}
it('should reject if "credentialSubject" is empty', () => {
const credential = jsonld.clone(mockCredential);
const credential = mockCredential();
credential.credentialSubject = {};
credential.issuer = 'did:example:12345';
if(version === 1.0) {
Expand All @@ -733,7 +733,7 @@ for(const [version, mockCredential] of versionedCredentials) {
'"credentialSubject" must make a claim.');
});
it('should reject if a "credentialSubject" is empty', () => {
const credential = jsonld.clone(mockCredential);
const credential = mockCredential();
credential.credentialSubject = [{}, {id: 'did:key:zFoo'}];
credential.issuer = 'did:example:12345';
if(version === 1.0) {
Expand All @@ -752,7 +752,7 @@ for(const [version, mockCredential] of versionedCredentials) {
});

it('should accept multiple credentialSubjects', () => {
const credential = jsonld.clone(mockCredential);
const credential = mockCredential();
credential.credentialSubject = [
{id: 'did:key:zFoo'},
{name: 'did key'}
Expand All @@ -776,7 +776,7 @@ for(const [version, mockCredential] of versionedCredentials) {
}

async function _generateCredential(_mockCredential) {
const mockCredential = jsonld.clone(_mockCredential);
const mockCredential = _mockCredential();
const {didDocument, documentLoader} = await _loadDid();
mockCredential.issuer = didDocument.didDocument.id;
mockCredential.id = `http://example.edu/credentials/${uuid()}`;
Expand Down
13 changes: 9 additions & 4 deletions test/mocks/credential.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/*!
* Copyright (c) 2019-2023 Digital Bazaar, Inc. All rights reserved.
*/
import {klona} from 'klona';

export const versionedCredentials = new Map([
[1.0, {
[1.0, () => klona({
'@context': [
'https://www.w3.org/2018/credentials/v1',
'https://www.w3.org/2018/credentials/examples/v1'
Expand All @@ -12,8 +17,8 @@ export const versionedCredentials = new Map([
id: 'did:example:ebfeb1f712ebc6f1c276e12ec21',
alumniOf: '<span lang="en">Example University</span>'
}
}],
[2.0, {
})],
[2.0, () => klona({
'@context': [
'https://www.w3.org/ns/credentials/v2',
'https://www.w3.org/2018/credentials/examples/v1'
Expand All @@ -25,5 +30,5 @@ export const versionedCredentials = new Map([
id: 'did:example:ebfeb1f712ebc6f1c276e12ec21',
alumniOf: '<span lang="en">Example University</span>'
}
}]
})]
]);

0 comments on commit b6fa6d3

Please sign in to comment.