Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses metadata test failure on Issue #112 #122

Merged
merged 4 commits into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,43 +1,38 @@
const didMetadataStructureTest_Map_Recursive = (didDocumentMetadata) => {
it('7.3 Metadata Structure - ' +
'The structure used to communicate this metadata MUST be a map of properties.', () => {
expect(didDocumentMetadata).toBeInfraMap();
});

it('7.3 Metadata Structure - ' +
'Each property name MUST be a string.', () => {
Object.keys(didDocumentMetadata).forEach((property_name) => {
expect(property_name).toBeString();
});
});

it('7.3 Metadata Structure - ' +
'Each property value MUST be a string, map, list, ordered set, boolean, or null.', () => {
Object.keys(didDocumentMetadata).forEach((property_name) => {
expect(didDocumentMetadata[property_name]).toBeDidDocumentPropertyValueType();
});
});

it('7.3 Metadata Structure - ' +
'The values within any complex data structures such as maps and lists ' +
'MUST be one of these data types as well.', () => {
Object.keys(didDocumentMetadata).forEach((property_name) => {
if (typeof didDocumentMetadata[property_name] == "object") {
didMetadataStructureTest_Map_Recursive(didDocumentMetadata[property_name]);
}
});
});

};

const didMetadataStructureTests = (suiteConfig) => {
suiteConfig.dids.forEach((didExample) => {
describe(didExample, () => {
suiteConfig.supportedContentTypes.forEach((contentType) => {
describe(contentType, () => {
const didDocumentMetadata = suiteConfig[didExample][contentType].didDocumentMetadata;
if (didDocumentMetadata) {
didMetadataStructureTest_Map_Recursive(didDocumentMetadata);
it('7.3 Metadata Structure - ' +
'The structure used to communicate this metadata MUST be a map of properties.', () => {
expect(didDocumentMetadata).toBeInfraMap();
});

it('7.3 Metadata Structure - ' +
'Each property name MUST be a string.', () => {
Object.keys(didDocumentMetadata).forEach((property_name) => {
expect(property_name).toBeString();
});
});

it('7.3 Metadata Structure - ' +
'Each property value MUST be a string, map, list, ordered set, boolean, or null.', () => {
Object.keys(didDocumentMetadata).forEach((property_name) => {
expect(didDocumentMetadata[property_name]).toBeDidDocumentPropertyValueType();
});
});

it('7.3 Metadata Structure - ' +
'The values within any complex data structures such as maps and lists ' +
'MUST be one of these data types as well.', () => {
Object.keys(didDocumentMetadata).forEach((property_name) => {
if (typeof didDocumentMetadata[property_name] == "object") {
expect(didDocumentMetadata[property_name]).toBeDidDocumentMap();
}
});
});

it('7.3 Metadata Structure - ' +
'The entire metadata structure MUST be serializable according to the JSON ' +
Expand Down
1 change: 1 addition & 0 deletions packages/jest-did-matcher/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ You can also prepare by running `npm run prepare` at the top level of `did-test-
- toBeValidDid / isValidDid
- toBeValidDidUrl / isValidDidUrl
- toBeDidCoreDatetime / isDidCoreDatetime
- toBeDidDocumentMap / isDidDocumentMap
- toBeDidDocumentPropertyValueType / isDidDocumentPropertyValueType
- Various encoding formats
- toBeBase58String / isBase58String
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { matcherHint, printExpected, printReceived } from 'jest-matcher-utils';
import { predicate } from './predicate';

const passMessage = received => () =>
matcherHint('.not.toBeDidDocumentMap', 'received', '') +
'\n\n' +
'Expected value to not be a DID document map:\n' +
` ${printReceived(received)}`;

const failMessage = received => () =>
matcherHint('.toBeDidDocumentMap', 'received', '') +
'\n\n' +
'Expected value to be of a DID document map:\n' +
` ${printExpected('A DID document map')}\n` +
'Received:\n' +
` ${printReceived(received)}`;

export default {
toBeDidDocumentMap: expected => {
const pass = predicate(expected);
if (pass) {
return { pass: true, message: passMessage(expected) };
}

return { pass: false, message: failMessage(expected) };
},

isDidDocumentMap: obj => {
return predicate(obj);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import each from 'jest-each';

import matcher from '.';

expect.extend(matcher);


describe('.toBeDidDocumentMap', () => {
each([
[
{
"a": "1"
}
],
[
{
"b": [ "1", "2", "3" ]
},
],
[
{
"didDocumentMetadata": {
"canonicalId":"x",
"equivalentId":[
"x",
"y"
],
"method":{
"updateCommitment":"z",
"recoveryCommitment":"t",
"published":true
}
}
},
],
[new Map(Object.entries({ a: "1", b: "2", c: {"x": "1", "y": "2"}}))],
[new Set(["1", "2", "3", ["4", "5", "6"], {"x":"1", "y": "2"}])]
]).test('passes when item is a type allowed in a DID Document map: %s', given => {
expect(given).toBeDidDocumentMap();
});
});

describe('.not.toBeDidDocumentMap', () => {
each([
[{ "v" : 0 }],
[{ "v" : undefined }],
[{ "v" : NaN }],
[{"didDocumentMetadata": {
"canonicalId":NaN,
"equivalentId":[
"x",
"y"
],
"method":{
"updateCommitment":"z",
"recoveryCommitment":"t",
"published":true
}
}}],
[{"didDocumentMetadata": {
"canonicalId":"i",
"equivalentId":[
NaN,
"y"
],
"method":{
"updateCommitment":"z",
"recoveryCommitment":"t",
"published":true
}
}}],
[new Map(Object.entries({ a: NaN, b: "2", c: {"x": "1", "y": "2"}}))],
[new Map(Object.entries({ a: "1", b: "2", c: {"x": NaN, "y": "2"}}))],
[new Map(Object.entries({ a: "1", b: undefined, c: {"x": "1", "y": "2"}}))],
[new Map(Object.entries({ a: "1", b: "2", c: {"x": "1", "y": undefined}}))],
[new Set(["1", "2", "3", ["4", NaN, "6"], {"x":"1", "y": "2"}])],
[new Set(["1", "2", "3", ["4", "5", undefined], {"x":"1", "y": "2"}])],
[new Set(["1", "2", "3", ["4", "5", "6"], {"x": undefined, "y": "2"}])],
[new Set(["1", "2", "3", ["4", "5", "6"], {"x": "1", "y": undefined }])]
]).test('passes when item is not of type DID Document map: %s', given => {
expect(given).not.toBeDidDocumentMap();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// (DID document) property value MUST be a string, map, list, ordered set, boolean, or null.

export function predicate(expected) {
if (expected === null) {
return true;
}

if (Array.isArray(expected)) {
return expected.map(element => predicate(element)).reduce( (p, c) => (p && c), true);
}

if (expected instanceof Map || expected instanceof Set) {
let r = true;
expected.forEach(v => { r = r && predicate(v); });
return r;
}

if (typeof expected == "object") {
return Object.keys(expected).map(key => predicate(expected[key])).reduce( (p, c) => (p && c), true);
}

switch (typeof expected) {
case 'boolean':
case 'string':
return true;
}

if (expected instanceof String) {
return true;
}

return false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const failMessage = received => () =>
matcherHint('.toBeDidDocumentPropertyValueType', 'received', '') +
'\n\n' +
'Expected value to be of type allowed in DID Document:\n' +
` ${printExpected('type of type allowed in DID Document')}` +
` ${printExpected('value type allowed in DID Document')}\n` +
'Received:\n' +
` ${printReceived(received)}`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ expect.extend(matcher);
describe('.toBeDidDocumentPropertyValueType', () => {
each([
["String"],
[new String],
[new String()],
[{}],
[{ a: "1", b: "2", c: {"x": "1", "y": "2"}}],
[new Map()],
[new Map(Object.entries({ a: "1", b: "2", c: {"x": "1", "y": "2"}}))],
[[]],
[new Array],
[new Set],
[new Array(1)],
[new Set()],
[new Set(["1", "2", "3", ["4", "5", "6"], {"x":"1", "y": "2"}])],
[true],
[false],
[null]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default expected => {
return true;
}

if (expected instanceof Set) {
if (expected instanceof Map || expected instanceof Set) {
return true;
}

Expand Down