Skip to content

Commit

Permalink
test: add wrap unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
phanshiyu committed Apr 27, 2024
1 parent ec3500a commit c92b120
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/4.0/__tests__/digest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const credentialRoot = "f49be3b06f7a7eb074775ad12aae43936084c86646e3640eae18e7ae

const { proof, ...credential } = SIGNED_WRAPPED_DOCUMENT_DID;

describe("digest v4.0", () => {
describe("V4.0 digest", () => {
describe("digestCredential", () => {
test("digests a document with all visible content correctly", () => {
const clonedCredential = cloneDeep(credential);
Expand Down
2 changes: 1 addition & 1 deletion src/4.0/__tests__/guard.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { W3cVerifiableCredential, V4Document, V4WrappedDocument, V4SignedWrappedDocument } from "../types";
import { RAW_DOCUMENT_DID, SIGNED_WRAPPED_DOCUMENT_DID, WRAPPED_DOCUMENT_DID } from "../fixtures";

describe("v4 guard", () => {
describe("V4.0 guard", () => {
describe("given a raw document", () => {
test("should pass w3c vc validation without removal of any data", () => {
const w3cVerifiableCredential: W3cVerifiableCredential = RAW_DOCUMENT_DID;
Expand Down
107 changes: 107 additions & 0 deletions src/4.0/__tests__/wrap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { V4Document, V4WrappedDocument, W3cVerifiableCredential } from "../types";
import { wrapDocument } from "../wrap";

describe("V4.0 wrap document", () => {
test("given a valid v4 document, should wrap correctly", async () => {
const wrapped = await wrapDocument({
"@context": [
"https://www.w3.org/ns/credentials/v2",
"https://schemata.openattestation.com/com/openattestation/4.0/alpha-context.json",
],
type: ["VerifiableCredential", "OpenAttestationCredential"],
credentialSubject: {
id: "0x1234567890123456789012345678901234567890",
name: "John Doe",
country: "SG",
},
issuer: {
id: "did:ethr:0xB26B4941941C51a4885E5B7D3A1B861E54405f90",
type: "OpenAttestationIssuer",
name: "Government Technology Agency of Singapore (GovTech)",
identityProof: { identityProofType: "DNS-DID", identifier: "example.openattestation.com" },
},
});
const parsedResults = V4WrappedDocument.safeParse(wrapped);
if (!parsedResults.success) {
throw new Error("Parsing failed");
}
const { proof } = parsedResults.data;
expect(proof.merkleRoot.length).toBe(64);
expect(proof.privacy.obfuscated).toEqual([]);
expect(proof.proofPurpose).toBe("assertionMethod");
expect(proof.proofs).toEqual([]);
expect(proof.salts.length).toBeGreaterThan(0);
expect(proof.targetHash.length).toBe(64);
expect(proof.type).toBe("OpenAttestationMerkleProofSignature2018");
});

test("given a document with explicit v4 contexts, but does not conform to the V4 document schema, should throw", async () => {
await expect(
wrapDocument({
"@context": [
"https://www.w3.org/ns/credentials/v2",
"https://schemata.openattestation.com/com/openattestation/4.0/alpha-context.json",
],
type: ["VerifiableCredential", "OpenAttestationCredential"],
credentialSubject: {
id: "0x1234567890123456789012345678901234567890",
name: "John Doe",
country: "SG",
},
issuer: {
id: "did:ethr:0xB26B4941941C51a4885E5B7D3A1B861E54405f90",
name: "Government Technology Agency of Singapore (GovTech)",
identityProof: { identityProofType: "DNS-DID", identifier: "example.openattestation.com" },
} as V4Document["issuer"],
})
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Input document does not conform to OpenAttestation v4.0 Data Model: [{"code":"invalid_literal","expected":"OpenAttestationIssuer","path":["issuer","type"],"message":"Invalid literal value, expected \\"OpenAttestationIssuer\\""}]"`
);
});

test("given a valid v4 document but has an extra field, should throw", async () => {
await expect(
wrapDocument({
"@context": [
"https://www.w3.org/ns/credentials/v2",
"https://schemata.openattestation.com/com/openattestation/4.0/alpha-context.json",
],
type: ["VerifiableCredential", "OpenAttestationCredential"],
credentialSubject: {
id: "0x1234567890123456789012345678901234567890",
name: "John Doe",
country: "SG",
},
issuer: {
id: "did:ethr:0xB26B4941941C51a4885E5B7D3A1B861E54405f90",
type: "OpenAttestationIssuer",
name: "Government Technology Agency of Singapore (GovTech)",
extraField: "extra",
identityProof: { identityProofType: "DNS-DID", identifier: "example.openattestation.com" },
},
// this should not exist
extraField: "extra",
} as V4Document)
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Input document does not conform to OpenAttestation v4.0 Data Model: [{"code":"unrecognized_keys","keys":["extraField"],"path":[],"message":"Unrecognized key(s) in object: 'extraField'"}]"`
);
});

test("given a generic w3c vc, should wrap with context and type corrected", async () => {
const genericW3cVc: W3cVerifiableCredential = {
"@context": ["https://www.w3.org/ns/credentials/v2"],
type: ["VerifiableCredential"],
credentialSubject: {
id: "0x1234567890123456789012345678901234567890",
name: "John Doe",
country: "SG",
},
issuer: {
id: "https://example.com/issuer/123",
},
};
const wrapped = await wrapDocument(genericW3cVc as unknown as V4Document);
const parsedResults = V4WrappedDocument.pick({ "@context": true, type: true }).passthrough().safeParse(wrapped);
expect(parsedResults.success).toBe(true);
});
});
2 changes: 1 addition & 1 deletion src/4.0/wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const wrapDocument = async <T extends V4Document>(
document: NoExtraProperties<V4Document, T>
): Promise<V4WrappedDocument<T>> => {
/* 1a. try OpenAttestation VC validation, since most user will be issuing oa v4*/
const oav4context = await V4Document.pick({ "@context": true }).safeParseAsync(document); // Superficial check on user intention
const oav4context = await V4Document.pick({ "@context": true }).passthrough().safeParseAsync(document); // Superficial check on user intention
let validatedRawDocument: W3cVerifiableCredential | undefined;
if (oav4context.success) {
const oav4 = await V4Document.safeParseAsync(document);
Expand Down

0 comments on commit c92b120

Please sign in to comment.