|
| 1 | +/* |
| 2 | + * Copyright 2022 - 2024 Digital Bazaar, Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: LicenseRef-w3c-3-clause-bsd-license-2008 OR LicenseRef-w3c-test-suite-license-2023 |
| 5 | + */ |
| 6 | + |
| 7 | +import {addPerTestMetadata, setupMatrix} from '../tests/helpers.js'; |
| 8 | +import {createInvalidVp, createLocalVp} from '../tests/data-generator.js'; |
| 9 | +import assert from 'node:assert/strict'; |
| 10 | +import chai from 'chai'; |
| 11 | +import {createRequire} from 'module'; |
| 12 | +import {filterByTag} from 'vc-test-suite-implementations'; |
| 13 | +import {injectOrReject} from '../tests/assertions.js'; |
| 14 | +import {klona} from 'klona'; |
| 15 | +import {TestEndpoints} from '../tests/TestEndpoints.js'; |
| 16 | + |
| 17 | +// eslint-disable-next-line no-unused-vars |
| 18 | +const should = chai.should(); |
| 19 | + |
| 20 | +const require = createRequire(import.meta.url); |
| 21 | +const baseContextUrl = 'https://www.w3.org/ns/credentials/v2'; |
| 22 | + |
| 23 | +const tag = 'vc2.0'; |
| 24 | +const {match} = filterByTag({tags: [tag]}); |
| 25 | + |
| 26 | +// 4.2 Verifiable Credentials https://w3c.github.io/vc-data-model/#verifiable-credentials |
| 27 | +// There are no actual MUSTs here, just references to other sections. |
| 28 | + |
| 29 | +// 4.3 Contexts https://w3c.github.io/vc-data-model/#contexts |
| 30 | +describe('Contexts', function() { |
| 31 | + setupMatrix.call(this, match); |
| 32 | + for(const [name, implementation] of match) { |
| 33 | + const endpoints = new TestEndpoints({implementation, tag}); |
| 34 | + |
| 35 | + describe(name, function() { |
| 36 | + beforeEach(addPerTestMetadata); |
| 37 | + |
| 38 | + it('Verifiable credentials MUST include a @context property.', |
| 39 | + async function() { |
| 40 | + this.test.link = `https://w3c.github.io/vc-data-model/#types:~:text=Verifiable%20credentials%20and%20verifiable%20presentations%20MUST%20include%20a%20%40context%20property.`; |
| 41 | + // positive @context test |
| 42 | + const vc = await endpoints.issue(require( |
| 43 | + '../tests/input/credential-ok.json')); |
| 44 | + vc.should.have.property('@context').to.be.an('array', |
| 45 | + 'Failed to respond with a VC with intact `@context`.'); |
| 46 | + // negative @context test |
| 47 | + await injectOrReject(endpoints, |
| 48 | + './input/credential-no-context-fail-or-inject.json'); |
| 49 | + |
| 50 | + }); |
| 51 | + it('Verifiable presentations MUST include a @context property.', |
| 52 | + async function() { |
| 53 | + this.test.link = `https://w3c.github.io/vc-data-model/#types:~:text=Verifiable%20credentials%20and%20verifiable%20presentations%20MUST%20include%20a%20%40context%20property.`; |
| 54 | + const validVp = await createLocalVp({ |
| 55 | + presentation: require('../tests/input/presentation-ok.json') |
| 56 | + }); |
| 57 | + await assert.doesNotReject( |
| 58 | + endpoints.verifyVp(validVp), |
| 59 | + `verifier ${name} rejected VP with valid @context.` |
| 60 | + ); |
| 61 | + const invalidVp = klona(validVp); |
| 62 | + delete invalidVp['@context']; |
| 63 | + await assert.rejects(endpoints.verifyVp(invalidVp), |
| 64 | + 'Failed to reject a VP with a missing @context.'); |
| 65 | + }); |
| 66 | + it('Verifiable credentials: The value of the @context property ' + |
| 67 | + 'MUST be an ordered set where the first item is a URL with the value ' + |
| 68 | + 'https://www.w3.org/ns/credentials/v2.', async function() { |
| 69 | + this.test.link = `https://w3c.github.io/vc-data-model/#types:~:text=The%20value%20of%20the%20%40context%20property%20MUST%20be%20an%20ordered%20set%20where%20the%20first%20item%20is%20a%20URL%20with%20the%20value%20https%3A//www.w3.org/ns/credentials/v2.`; |
| 70 | + //positive issue test |
| 71 | + const vc = await endpoints.issue(require( |
| 72 | + '../tests/input/credential-ok.json')); |
| 73 | + assert(Array.isArray(vc['@context']), |
| 74 | + 'Failed to support `@context` as an Array.'); |
| 75 | + assert.strictEqual(vc['@context'][0], baseContextUrl, |
| 76 | + 'Failed to keep `@context` order intact.' |
| 77 | + ); |
| 78 | + // negative issue test |
| 79 | + await injectOrReject(endpoints, |
| 80 | + './input/credential-missing-base-context-fail-or-inject.json'); |
| 81 | + }); |
| 82 | + it('Verifiable presentations: The value of the @context ' + |
| 83 | + 'property MUST be an ordered set where the first item is a URL with ' + |
| 84 | + 'the value https://www.w3.org/ns/credentials/v2.', async function() { |
| 85 | + this.test.link = `https://w3c.github.io/vc-data-model/#types:~:text=The%20value%20of%20the%20%40context%20property%20MUST%20be%20an%20ordered%20set%20where%20the%20first%20item%20is%20a%20URL%20with%20the%20value%20https%3A//www.w3.org/ns/credentials/v2.`; |
| 86 | + const vpInvalidContextOrder = await createInvalidVp({ |
| 87 | + presentation: require('../tests/input/presentation-context-order-fail.json') |
| 88 | + }); |
| 89 | + await assert.rejects(endpoints.verifyVp(vpInvalidContextOrder), |
| 90 | + |
| 91 | + 'Failed to reject a VP that has the wrong context order.'); |
| 92 | + const vp = createLocalVp({ |
| 93 | + presentation: require('../tests/input/presentation-ok.json') |
| 94 | + }); |
| 95 | + vp['@context'] = [ |
| 96 | + 'https://www.w3.org/ns/credentials/examples/v2', |
| 97 | + 'https://www.w3.org/ns/credentials/v2' |
| 98 | + ]; |
| 99 | + await assert.rejects(endpoints.verifyVp(vp), |
| 100 | + 'Failed to reject a VP with unordered @context.'); |
| 101 | + await assert.rejects(endpoints.verifyVp( |
| 102 | + require('../tests/input/presentation-missing-base-context-fail.json')), |
| 103 | + |
| 104 | + 'Failed to reject a VP that lacked the VC base context URL.'); |
| 105 | + }); |
| 106 | + it('Verifiable Credential `@context`: "Subsequent items in the ' + |
| 107 | + 'ordered set MUST be composed of any combination of URLs and/or ' + |
| 108 | + 'objects where each is processable as a JSON-LD Context."', |
| 109 | + async function() { |
| 110 | + this.test.link = `https://w3c.github.io/vc-data-model/#types:~:text=Subsequent%20items%20in%20the%20ordered%20set%20MUST%20be%20composed%20of%20any%20combination%20of%20URLs%20and/or%20objects%2C%20where%20each%20is%20processable%20as%20a%20JSON%2DLD%20Context.`; |
| 111 | + await assert.doesNotReject(endpoints.issue(require( |
| 112 | + '../tests/input/credential-context-combo1-ok.json')), |
| 113 | + 'Failed to support multiple `@context` URLs.'); |
| 114 | + await assert.doesNotReject(endpoints.issue(require( |
| 115 | + '../tests/input/credential-context-combo2-ok.json')), |
| 116 | + 'Failed to support objects in the `@context` Array.'); |
| 117 | + await assert.rejects(endpoints.issue(require( |
| 118 | + '../tests/input/credential-context-combo3-fail.json')), |
| 119 | + |
| 120 | + 'Failed to reject a VC with an invalid `@context` URL.'); |
| 121 | + await assert.rejects(endpoints.issue(require( |
| 122 | + '../tests/input/credential-context-combo4-fail.json')), |
| 123 | + |
| 124 | + 'Failed to reject a VC with an unsupported `@context` value type ' + |
| 125 | + '(number).'); |
| 126 | + }); |
| 127 | + it('Verifiable Presentation `@context`: "Subsequent items in the ' + |
| 128 | + 'ordered set MUST be composed of any combination of URLs and/or ' + |
| 129 | + 'objects where each is processable as a JSON-LD Context."', |
| 130 | + async function() { |
| 131 | + this.test.link = `https://w3c.github.io/vc-data-model/#types:~:text=Subsequent%20items%20in%20the%20ordered%20set%20MUST%20be%20composed%20of%20any%20combination%20of%20URLs%20and/or%20objects%2C%20where%20each%20is%20processable%20as%20a%20JSON%2DLD%20Context.`; |
| 132 | + await assert.doesNotReject( |
| 133 | + endpoints.verifyVp(await createLocalVp({ |
| 134 | + presentation: |
| 135 | + require('../tests/input/presentation-context-combo1-ok.json') |
| 136 | + })), |
| 137 | + 'Failed to support multiple `@context` URLs in a VP.'); |
| 138 | + await assert.doesNotReject( |
| 139 | + endpoints.verifyVp(await createLocalVp({ |
| 140 | + presentation: |
| 141 | + require('../tests/input/presentation-context-combo2-ok.json') |
| 142 | + })), |
| 143 | + 'Failed to support objects in the `@context` Array in a VP.'); |
| 144 | + // first create a valid VP |
| 145 | + const vp = await createLocalVp({ |
| 146 | + presentation: require('../tests/input/presentation-vc-ok.json') |
| 147 | + }); |
| 148 | + // then inject incorrect `@context` values and test verification |
| 149 | + vp['@context'][1] = 'https://example.com'; |
| 150 | + await assert.rejects(endpoints.verifyVp(vp), |
| 151 | + 'Failed to reject a VP with an invalid `@context` URL.'); |
| 152 | + vp['@context'][1] = 'https ://not-a-url/contexts/example/v1'; |
| 153 | + await assert.rejects(endpoints.verifyVp(vp), |
| 154 | + 'Failed to reject a VP with an invalid `@context` URL.'); |
| 155 | + vp['@context'][1] = 123192875; |
| 156 | + await assert.rejects(endpoints.verifyVp(vp), |
| 157 | + 'Failed to reject a VP with an unsupported `@context` value type ' + |
| 158 | + '(number).'); |
| 159 | + }); |
| 160 | + }); |
| 161 | + } |
| 162 | +}); |
0 commit comments