Skip to content

Commit

Permalink
Move expirationDate test to v1 tests & use mockCredential in all tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Dec 8, 2023
1 parent dbaf8c8 commit a037534
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 34 deletions.
24 changes: 13 additions & 11 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,21 @@ export function _checkCredential({
if(jsonld.getValues(credential, 'issuanceDate').length > 1) {
throw new Error('"issuanceDate" property can only have one value.');
}
// check issuanceDate format on issue
assertDateString({credential, prop: 'issuanceDate'});
// optionally check expirationDate
if('expirationDate' in credential) {
// check if `expirationDate` property is a date
assertDateString({credential, prop: 'expirationDate'});
if(mode === 'verify') {
// check if `now` is after `expirationDate`
if(now > new Date(credential.expirationDate)) {
throw new Error('Credential has expired.');
}
}
}
// check if `now` is before `issuanceDate` on verification
if(mode === 'verify') {
assertDateString({credential, prop: 'issuanceDate'});
// check if `now` is before `issuanceDate`
const issuanceDate = new Date(credential.issuanceDate);
if(now < issuanceDate) {
throw new Error(
Expand Down Expand Up @@ -681,15 +692,6 @@ export function _checkCredential({
}
});

if('expirationDate' in credential) {
const {expirationDate} = credential;
// check if `expirationDate` property is a date
assertDateString({credential, prop: 'expirationDate'});
// check if `now` is after `expirationDate`
if(now > new Date(expirationDate)) {
throw new Error('Credential has expired.');
}
}
// check if properties that require a type are
// defined, objects, and objects with types
for(const prop of mustHaveType) {
Expand Down
42 changes: 19 additions & 23 deletions test/10-verify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import {
} from '@digitalbazaar/ed25519-verification-key-2018';
import {invalidContexts} from './contexts/index.js';
import jsigs from 'jsonld-signatures';
import {klona} from 'klona';
import {mock as mockData} from './mocks/mock.data.js';
import {v4 as uuid} from 'uuid';
import {VeresOneDriver} from 'did-veres-one';
import {versionedCredentials} from './mocks/credential.js';
Expand Down Expand Up @@ -779,7 +777,7 @@ for(const [version, mockCredential] of versionedCredentials) {

describe('_checkCredential', () => {
it('should reject a credentialSubject.id that is not a URI', () => {
const credential = klona(mockData.credentials.alpha);
const credential = mockCredential();
credential.issuer = 'http://example.edu/credentials/58473';
credential.credentialSubject.id = '12345';
let error;
Expand All @@ -796,7 +794,7 @@ for(const [version, mockCredential] of versionedCredentials) {
});

it('should reject an issuer that is not a URI', () => {
const credential = klona(mockData.credentials.alpha);
const credential = mockCredential();
credential.issuer = '12345';
let error;
try {
Expand All @@ -812,7 +810,7 @@ for(const [version, mockCredential] of versionedCredentials) {
});

it('should reject an evidence id that is not a URI', () => {
const credential = klona(mockData.credentials.alpha);
const credential = mockCredential();
credential.issuer = 'did:example:12345';
credential.evidence = '12345';
let error;
Expand All @@ -828,25 +826,23 @@ for(const [version, mockCredential] of versionedCredentials) {
.contain('"evidence" must be a URI');
});

it('should reject if "expirationDate" has passed', () => {
const credential = klona(mockData.credentials.alpha);
credential.issuer = 'did:example:12345';
// set expirationDate to an expired date.
credential.expirationDate = '2020-05-31T19:21:25Z';
let error;
try {
vc._checkCredential({credential});
} catch(e) {
error = e;
}
should.exist(error,
'Should throw error when "expirationDate" has passed');
error.message.should
.contain('Credential has expired.');
});
if(version === 1.0) {
// we submit with second precission,
// but throw with millisecond precision
it('should reject if "expirationDate" has passed', () => {
const credential = mockCredential();
credential.issuer = 'did:example:12345';
// set expirationDate to an expired date.
credential.expirationDate = '2020-05-31T19:21:25Z';
let error;
try {
vc._checkCredential({credential, mode: 'verify'});
} catch(e) {
error = e;
}
should.exist(error,
'Should throw error when "expirationDate" has passed');
error.message.should
.contain('Credential has expired.');
});
it('should reject if "now" is before "issuanceDate"', () => {
const credential = mockCredential();
credential.issuer = 'did:example:12345';
Expand Down

0 comments on commit a037534

Please sign in to comment.