Skip to content

Commit

Permalink
Clarify and expand VC 2.0 validUntil & validFrom tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
aljones15 committed Nov 24, 2023
1 parent db9ff83 commit d9d7ba4
Showing 1 changed file with 97 additions and 20 deletions.
117 changes: 97 additions & 20 deletions test/10-verify.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -649,41 +649,49 @@ for(const [version, mockCredential] of versionedCredentials) {
});
}
if(version === 2.0) {
it('should reject if "now" is before "validFrom"', () => {
it('should issue "validFrom" in the past', () => {
const credential = jsonld.clone(mockCredential);
credential.issuer = 'did:example:12345';
credential.validFrom = '2022-10-31T19:21:25Z';
credential.validFrom = '2022-06-30T19:21:25Z';
const now = '2022-10-30T19:21:25Z';
let error;
try {
vc._checkCredential({credential, now});
} catch(e) {
error = e;
}
should.not.exist(error,
'Should not throw error when issuing "validFrom" in past');
});
it('should issue "validFrom" in the future', () => {
const credential = jsonld.clone(mockCredential);
credential.issuer = 'did:example:12345';
credential.validFrom = '2022-10-30T19:21:25Z';
const now = '2022-06-30T19:21:25Z';
let error;
try {
vc._checkCredential({credential, now});
} catch(e) {
error = e;
}
should.exist(error,
'Should throw error when "now" is before "validFrom"');
error.message.should.contain(
`The current date time (${now}) is before the ` +
`"validFrom" (${credential.validFrom}).`);
should.not.exist(error,
'Should not throw error when issuing "validFrom" in future');
});
it('should reject if "now" is after "validUntil"', () => {
it('should NOT verify "validFrom" in the future', () => {
const credential = jsonld.clone(mockCredential);
credential.issuer = 'did:example:12345';
credential.validUntil = '2022-06-30T19:21:25Z';
const now = '2022-10-30T19:21:25Z';
credential.validFrom = '2022-10-30T19:21:25Z';
const now = '2022-06-30T19:21:25Z';
let error;
try {
vc._checkCredential({credential, now});
} catch(e) {
error = e;
}
should.exist(error,
'Should throw error when "now" is after "validUntil"');
error.message.should.contain(
`The current date time (${now}) is after the ` +
`"validUntil" (${credential.validUntil}).`);
should.not.exist(error,
'Should throw error when verifying "validFrom" in future');
});
it('should accept "validFrom"', () => {
it('should verify "validFrom" in the past', () => {
const credential = jsonld.clone(mockCredential);
credential.issuer = 'did:example:12345';
credential.validFrom = '2022-06-30T19:21:25Z';
Expand All @@ -695,9 +703,51 @@ for(const [version, mockCredential] of versionedCredentials) {
error = e;
}
should.not.exist(error,
'Should not throw error when "validFrom" is valid');
'Should not throw error when "validFrom" in past');
});
it('should issue "validUntil" in the future', () => {
const credential = jsonld.clone(mockCredential);
credential.issuer = 'did:example:12345';
credential.validUntil = '2025-10-31T19:21:25Z';
const now = '2022-06-30T19:21:25Z';
let error;
try {
vc._checkCredential({credential, now});
} catch(e) {
error = e;
}
should.not.exist(error,
'Should not throw error when issuing "validUntil" in future');
});
it('should accept "validUntil"', () => {
it('should issue "validUntil" in the past', () => {
const credential = jsonld.clone(mockCredential);
credential.issuer = 'did:example:12345';
credential.validUntil = '2025-10-31T19:21:25Z';
const now = '2022-06-30T19:21:25Z';
let error;
try {
vc._checkCredential({credential, now});
} catch(e) {
error = e;
}
should.not.exist(error,
'Should not throw error when issuing with "validUntil" in past');
});
it('should NOT verify if "validUntil" in the past', () => {
const credential = jsonld.clone(mockCredential);
credential.issuer = 'did:example:12345';
credential.validUntil = '2025-06-31T19:21:25Z';
const now = '2022-10-30T19:21:25Z';
let error;
try {
vc._checkCredential({credential, now});
} catch(e) {
error = e;
}
should.exist(error,
'Should throw error when "validUntil" in the past');
});
it('should verify if "validUntil" in the future', () => {
const credential = jsonld.clone(mockCredential);
credential.issuer = 'did:example:12345';
credential.validUntil = '2025-10-31T19:21:25Z';
Expand All @@ -711,8 +761,35 @@ for(const [version, mockCredential] of versionedCredentials) {
should.not.exist(error,
'Should not throw error when "issuanceDate" is valid');
});

it('should accept both "validFrom" and "validUntil" are valid', () => {
it('should issue both "validFrom" and "validUntil"', async () => {
const credential = jsonld.clone(mockCredential);
credential.issuer = 'did:example:12345';
credential.validFrom = '2022-05-30T19:21:25Z';
credential.validUntil = '2025-05-30T19:21:25Z';
const now = '2022-06-30T19:21:25Z';
let error;
let verifiableCredential;
try {
verifiableCredential = await vc.issue({
credential,
suite,
documentLoader,
now
});
} catch(e) {
error = e;
}
should.not.exist(
error,
'Should not throw when issuing VC with both "validFrom" and' +
'"validUntil"'
);
should.exist(
verifiableCredential,
'Expected VC to be issued with both "validFrom" and "validUntil"'
);
});
it('should verify if "validFrom" is before "validUntil"', () => {
const credential = jsonld.clone(mockCredential);
credential.issuer = 'did:example:12345';
credential.validFrom = '2022-05-30T19:21:25Z';
Expand Down

0 comments on commit d9d7ba4

Please sign in to comment.