Skip to content

Commit

Permalink
chore: add openAttestationDnsTxt tests for v3
Browse files Browse the repository at this point in the history
  • Loading branch information
Nebulis committed Dec 26, 2019
1 parent 0fd49fa commit fc71611
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/types/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface SkippedVerificationFragment {
message: string;
}
export interface Verifier<
Document = WrappedDocument<v3.OpenAttestationDocument>,
Document = WrappedDocument<v3.OpenAttestationDocument> | WrappedDocument<v2.OpenAttestationDocument>,
Options = VerificationManagerOptions
> {
skip: (document: Document, options: Options) => Promise<SkippedVerificationFragment>;
Expand Down
10 changes: 5 additions & 5 deletions src/verifiers/openAttestationDnsTxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ type Identity =
// Resolve identity of an issuer, currently supporting only DNS-TXT
const resolveIssuerIdentity = async (
issuer: v2.Issuer | v3.Issuer,
smartContractAddress: string,
options: VerificationManagerOptions
): Promise<Identity> => {
// we expect the test function to prevent this issue => smart contract address MUST be populated
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const smartContractAddress = getSmartContractAddress(issuer)!;
const type = issuer?.identityProof?.type ?? "";
const location = issuer?.identityProof?.location ?? "";
if (type !== "DNS-TXT") throw new Error("Identity type not supported");
Expand Down Expand Up @@ -75,7 +73,9 @@ export const openAttestationDnsTxt: Verifier<
if (isWrappedV2Document(document)) {
const documentData = getData(document);
const identities = await Promise.all(
documentData.issuers.map(issuer => resolveIssuerIdentity(issuer, options))
// we expect the test function to prevent this issue => smart contract address MUST be populated
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
documentData.issuers.map(issuer => resolveIssuerIdentity(issuer, getSmartContractAddress(issuer)!, options))
);

const invalidIdentity = identities.findIndex(identity => !identity.identified);
Expand All @@ -102,7 +102,7 @@ export const openAttestationDnsTxt: Verifier<
};
}
const documentData = getData(document);
const identity = await resolveIssuerIdentity(documentData.issuer, options);
const identity = await resolveIssuerIdentity(documentData.issuer, documentData.proof.value, options);
if (!identity.identified) {
return {
name,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { openAttestationDnsTxt } from "./openAttestationDnsTxt";
import { documentRopstenValidWithToken } from "../../test/fixtures/v2/documentRopstenValidWithToken";

describe("OpenAttestationDnsTxt", () => {
describe("OpenAttestationDnsTxt v2 document", () => {
it("should return a valid fragment when document has valid identity", async () => {
const fragment = await openAttestationDnsTxt.verify(documentRopstenValidWithToken, {
network: "ropsten"
Expand All @@ -27,7 +27,7 @@ describe("OpenAttestationDnsTxt", () => {
issuers: [
{
...documentRopstenValidWithToken.data.issuers[0],
tokenRegistry: "1d337929-6770-4a05-ace0-1f07c25c7615:string:value"
tokenRegistry: "1d337929-6770-4a05-ace0-1f07c25c7615:string:0xabcd"
}
]
}
Expand All @@ -38,7 +38,7 @@ describe("OpenAttestationDnsTxt", () => {
expect(fragment).toStrictEqual({
type: "ISSUER_IDENTITY",
name: "OpenAttestationDnsTxt",
data: { location: "example.tradetrust.io", value: "value", type: "DNS-TXT" },
data: { location: "example.tradetrust.io", value: "0xabcd", type: "DNS-TXT" },
message: "Certificate issuer identity is invalid",
status: "INVALID"
});
Expand Down
129 changes: 129 additions & 0 deletions src/verifiers/openAttestationDnsTxt.v3.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { openAttestationDnsTxt } from "./openAttestationDnsTxt";
import { documentRopstenValidWithDocumentStore } from "../../test/fixtures/v3/documentRopstenValid";

describe("OpenAttestationDnsTxt v3 document", () => {
it("should return a valid fragment when document has valid identity", async () => {
const fragment = await openAttestationDnsTxt.verify(
{
...documentRopstenValidWithDocumentStore,
data: {
...documentRopstenValidWithDocumentStore.data,
issuer: {
...documentRopstenValidWithDocumentStore.data.issuer,
identityProof: {
...documentRopstenValidWithDocumentStore.data.issuer.identityProof,
location: "1d337929-6770-4a05-ace0-1f07c25c7615:string:example.openattestation.com"
}
}
}
},
{
network: "ropsten"
}
);
expect(fragment).toStrictEqual({
type: "ISSUER_IDENTITY",
name: "OpenAttestationDnsTxt",
data: {
dns: "example.openattestation.com",
identified: true,
smartContract: "0x8Fc57204c35fb9317D91285eF52D6b892EC08cD3"
},
status: "VALID"
});
});
it("should return an invalid fragment when document identity does not match", async () => {
const fragment = await openAttestationDnsTxt.verify(documentRopstenValidWithDocumentStore, {
network: "ropsten"
});
expect(fragment).toStrictEqual({
type: "ISSUER_IDENTITY",
name: "OpenAttestationDnsTxt",
data: { location: "some.io", value: "0x8Fc57204c35fb9317D91285eF52D6b892EC08cD3", type: "DNS-TXT" },
message: "Certificate issuer identity is invalid",
status: "INVALID"
});
});
it("should return an error fragment when document has no identity type", async () => {
const document = {
...documentRopstenValidWithDocumentStore,
data: {
...documentRopstenValidWithDocumentStore.data,
issuer: {
...documentRopstenValidWithDocumentStore.data.issuer,
identityProof: {
...documentRopstenValidWithDocumentStore.data.issuer.identityProof,
type: null
}
}
}
};
// @ts-ignore valid error, need to ignore
const fragment = await openAttestationDnsTxt.verify(document, {
network: "ropsten"
});
expect(fragment).toStrictEqual({
type: "ISSUER_IDENTITY",
name: "OpenAttestationDnsTxt",
data: new Error("Identity type not supported"),
message: "Identity type not supported",
status: "ERROR"
});
});
it("should return an error fragment when document has no identity location", async () => {
const document = {
...documentRopstenValidWithDocumentStore,
data: {
...documentRopstenValidWithDocumentStore.data,
issuer: {
...documentRopstenValidWithDocumentStore.data.issuer,
identityProof: {
...documentRopstenValidWithDocumentStore.data.issuer.identityProof,
location: null
}
}
}
};
// @ts-ignore valid error, need to ignore
const fragment = await openAttestationDnsTxt.verify(document, {
network: "ropsten"
});
expect(fragment).toStrictEqual({
type: "ISSUER_IDENTITY",
name: "OpenAttestationDnsTxt",
data: new Error("Location is missing"),
message: "Location is missing",
status: "ERROR"
});
});

describe("test", () => {
it("should return true if identityProof type is DNS-TXT", () => {
expect(
openAttestationDnsTxt.test(documentRopstenValidWithDocumentStore, {
network: "ropsten"
})
).toStrictEqual(true);
});
it("should return false if identityProof type is not DNS-TXT", () => {
const document = {
...documentRopstenValidWithDocumentStore,
data: {
...documentRopstenValidWithDocumentStore.data,
issuer: {
...documentRopstenValidWithDocumentStore.data.issuer,
identityProof: {
...documentRopstenValidWithDocumentStore.data.issuer.identityProof,
type: "whatever"
}
}
}
};
expect(
openAttestationDnsTxt.test(document, {
network: "ropsten"
})
).toStrictEqual(false);
});
});
});

0 comments on commit fc71611

Please sign in to comment.