Skip to content

Commit

Permalink
fix: move fragment error handling to utils instead (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaackps authored Nov 5, 2021
1 parent d58795d commit b056e0b
Show file tree
Hide file tree
Showing 4 changed files with 448 additions and 454 deletions.
366 changes: 365 additions & 1 deletion src/common/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ import {
getOpenAttestationEthereumTokenRegistryStatusFragment,
getOpenAttestationHashFragment,
generateProvider,
isDocumentStoreAddressOrTokenRegistryAddressInvalid,
contractNotFound,
certificateNotIssued,
certificateRevoked,
invalidArgument,
serverError,
unhandledError,
} from "./utils";
import { AllVerificationFragment } from "..";
import { ProviderDetails } from "../types/core";
import { ProviderDetails, InvalidVerificationFragment } from "../types/core";

const fragments: AllVerificationFragment[] = [
{
Expand Down Expand Up @@ -397,3 +404,360 @@ describe("generateProvider", () => {
expect(provider?.connection?.url).toMatch(/(alchemy)/i);
});
});

describe("isDocumentStoreAddressOrTokenRegistryAddressInvalid", () => {
it("should return true if document store address is invalid", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 1,
codeString: "1",
message: "Invalid document store address",
},
};
expect(isDocumentStoreAddressOrTokenRegistryAddressInvalid([verificationFragment])).toStrictEqual(true);
});
it("should return false if document store address is valid", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 2,
codeString: "2",
message: "some other error",
},
};
expect(isDocumentStoreAddressOrTokenRegistryAddressInvalid([verificationFragment])).toStrictEqual(false);
});
it("should return true if token registry address is invalid", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumTokenRegistryStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 1,
codeString: "1",
message: "Invalid token registry address",
},
};
expect(isDocumentStoreAddressOrTokenRegistryAddressInvalid([verificationFragment])).toStrictEqual(true);
});
it("should return false if token registry address is valid", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumTokenRegistryStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 2,
codeString: "2",
message: "some other error",
},
};
expect(isDocumentStoreAddressOrTokenRegistryAddressInvalid([verificationFragment])).toStrictEqual(false);
});
});

describe("contractNotFound", () => {
it("should return true if contract is not found in document store", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 1,
codeString: "1",
message: "Contract is not found",
},
};
expect(contractNotFound([verificationFragment])).toStrictEqual(true);
});
it("should return false if contract is found in document store", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 2,
codeString: "2",
message: "some other error",
},
};
expect(contractNotFound([verificationFragment])).toStrictEqual(false);
});
});

describe("certificateNotIssued", () => {
it("should return true if document not issued in document store", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 1,
codeString: "1",
message: "Contract is not found",
},
};
expect(certificateNotIssued([verificationFragment])).toStrictEqual(true);
});
it("should return false if document is issued in document store", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 2,
codeString: "2",
message: "Some Error",
},
};
expect(certificateNotIssued([verificationFragment])).toStrictEqual(false);
});
it("should return true if document not minted in token registry", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumTokenRegistryStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 1,
codeString: "1",
message: "Invalid token registry address",
},
};
expect(isDocumentStoreAddressOrTokenRegistryAddressInvalid([verificationFragment])).toStrictEqual(true);
});
it("should return false if document is minted in token registry", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumTokenRegistryStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 2,
codeString: "2",
message: "Some error",
},
};
expect(isDocumentStoreAddressOrTokenRegistryAddressInvalid([verificationFragment])).toStrictEqual(false);
});
});

describe("certificateRevoked", () => {
it("should return true if error reason is that document is revoked in document store", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 5,
codeString: "5",
message: "Document has been revoked",
},
};
expect(certificateRevoked([verificationFragment])).toStrictEqual(true);
});
it("should return false if error reason is that document is not revoked in document store", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 3,
codeString: "3",
message: "Some error, Document not revoked",
},
};
expect(certificateRevoked([verificationFragment])).toStrictEqual(false);
});
});

describe("invalidArgument", () => {
it("should return true if error is caused by an invalid merkle root in document store issued fragment", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 1,
codeString: "1",
message: "Invalid call arguments",
},
};
expect(invalidArgument([verificationFragment])).toStrictEqual(true);
});
it("should return false if error is caused by an invalid merkle root in document store issued fragment", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 3,
codeString: "3",
message: "Some other error",
},
};
expect(invalidArgument([verificationFragment])).toStrictEqual(false);
});
it("should return true if error is not caused by an invalid merkle root in token registry issued fragment", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumTokenRegistryStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 6,
codeString: "6",
message: "Invalid contract arguments",
},
};
expect(invalidArgument([verificationFragment])).toStrictEqual(true);
});
it("should return false if error is not caused by an invalid merkle root in token registry issued fragment", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumTokenRegistryStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 2,
codeString: "2",
message: "Some other error",
},
};
expect(invalidArgument([verificationFragment])).toStrictEqual(false);
});
});

describe("serverError", () => {
it("should return true of the reason of the error is that we can't connect to Ethereum for document store issued fragment", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 500,
codeString: "500",
message: "Server error",
},
};
expect(serverError([verificationFragment])).toStrictEqual(true);
});
it("should return true of the reason of the error is that we can't connect to Ethereum for token registry minted fragment", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumTokenRegistryStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 500,
codeString: "500",
message: "Server error",
},
};
expect(serverError([verificationFragment])).toStrictEqual(true);
});
it("should return false of the reason of the error is not that we can't connect to Ethereum for document store issued fragment", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 3,
codeString: "3",
message: "Some other error",
},
};
expect(serverError([verificationFragment])).toStrictEqual(false);
});
it("should return false of the reason of the error is not that we can't connect to Ethereum for token registry minted fragment", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumTokenRegistryStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 5,
codeString: "5",
message: "Some other error",
},
};
expect(serverError([verificationFragment])).toStrictEqual(false);
});
});

describe("unhandledError", () => {
it("should return true if there are some unhandled errors from document store issued fragment", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 3,
codeString: "3",
message: "Some unhandled error",
},
};
expect(unhandledError([verificationFragment])).toStrictEqual(true);
});
it("should return false if not some unhandled errors from document store issued fragment", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumDocumentStoreStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 1,
codeString: "1",
message: "Some other error",
},
};
expect(unhandledError([verificationFragment])).toStrictEqual(false);
});
it("should return true if there are some unhandled errors from token registry minted fragment", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumTokenRegistryStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 3,
codeString: "3",
message: "Some unhandled error",
},
};
expect(unhandledError([verificationFragment])).toStrictEqual(true);
});
it("should return false if not some unhandled errors from token registry minted fragment", () => {
const verificationFragment: InvalidVerificationFragment<any> = {
status: "INVALID",
name: "OpenAttestationEthereumTokenRegistryStatus",
type: "DOCUMENT_STATUS",
data: {},
reason: {
code: 5,
codeString: "5",
message: "Some other error",
},
};
expect(unhandledError([verificationFragment])).toStrictEqual(false);
});
});
Loading

0 comments on commit b056e0b

Please sign in to comment.