Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Nov 6, 2024
1 parent 48ec1ad commit 6bc10f6
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 21 deletions.
80 changes: 59 additions & 21 deletions test/verifier/universal-verifier.events.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,25 @@ describe("Universal Verifier events", function () {
},
];

const encodedDataAbi = [
{
components: [
{ name: "schema", type: "uint256" },
{ name: "claimPathKey", type: "uint256" },
{ name: "operator", type: "uint256" },
{ name: "slotIndex", type: "uint256" },
{ name: "value", type: "uint256[]" },
{ name: "queryHash", type: "uint256" },
{ name: "allowedIssuers", type: "uint256[]" },
{ name: "circuitIds", type: "string[]" },
{ name: "skipClaimRevocationCheck", type: "bool" },
{ name: "claimPathNotExists", type: "uint256" },
],
name: "",
type: "tuple",
},
];

beforeEach(async () => {
[signer] = await ethers.getSigners();

Expand Down Expand Up @@ -92,33 +111,13 @@ describe("Universal Verifier events", function () {
data: data[i],
});
}

const abi = [
{
components: [
{ name: "schema", type: "uint256" },
{ name: "claimPathKey", type: "uint256" },
{ name: "operator", type: "uint256" },
{ name: "slotIndex", type: "uint256" },
{ name: "value", type: "uint256[]" },
{ name: "queryHash", type: "uint256" },
{ name: "allowedIssuers", type: "uint256[]" },
{ name: "circuitIds", type: "string[]" },
{ name: "skipClaimRevocationCheck", type: "bool" },
{ name: "claimPathNotExists", type: "uint256" },
],
name: "",
type: "tuple",
},
];

const filter = verifier.filters.ZKPRequestSet(null, null);
const logs = await verifier.queryFilter(filter, 0, "latest");

const coder = AbiCoder.defaultAbiCoder();
logs.map((log, index) => {
// @ts-ignore
const [decodedData] = coder.decode(abi, log.args.data);
const [decodedData] = coder.decode(encodedDataAbi, log.args.data);
expect(decodedData.schema).to.equal(queries[index].schema);
expect(decodedData.claimPathKey).to.equal(queries[index].claimPathKey);
expect(decodedData.operator).to.equal(queries[index].operator);
Expand All @@ -136,4 +135,43 @@ describe("Universal Verifier events", function () {
expect(decodedData.claimPathNotExists).to.equal(queries[index].claimPathNotExists);
});
});

it("Check ZKPRequestUpdate event", async () => {
const originalRequestData = packValidatorParams(queries[0]);
const updatedRequestData = packValidatorParams(queries[1]);

await verifier.setZKPRequest(0, {
metadata: "metadataN0",
validator: await sig.getAddress(),
data: originalRequestData,
});

await verifier.updateZKPRequest(0, {
metadata: "metadataN1",
validator: await sig.getAddress(),
data: updatedRequestData,
});

const filter = verifier.filters.ZKPRequestUpdate(null, null);
const logs = await verifier.queryFilter(filter, 0, "latest");

const coder = AbiCoder.defaultAbiCoder();
logs.map((log) => {
// @ts-ignore
const [decodedData] = coder.decode(encodedDataAbi, log.args.data);
expect(decodedData.schema).to.equal(queries[1].schema);
expect(decodedData.claimPathKey).to.equal(queries[1].claimPathKey);
expect(decodedData.operator).to.equal(queries[1].operator);
expect(decodedData.slotIndex).to.equal(queries[1].slotIndex);
decodedData.value.forEach((v, i) => {
expect(v).to.equal(queries[1].value[i]);
});
expect(decodedData.queryHash).to.equal(queries[1].queryHash);
decodedData.circuitIds.forEach((circuitId, i) => {
expect(circuitId).to.equal(queries[1].circuitIds[i]);
});
expect(decodedData.skipClaimRevocationCheck).to.equal(queries[1].skipClaimRevocationCheck);
expect(decodedData.claimPathNotExists).to.equal(queries[1].claimPathNotExists);
});
});
});
47 changes: 47 additions & 0 deletions test/verifier/universal-verifier.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,51 @@ describe("Universal Verifier MTP & SIG validators", function () {
),
).to.be.rejectedWith("Validator is not whitelisted");
});

it("Check updateZKPRequest", async () => {
const owner = signer;
const requestOwner = signer2;
const requestId = 0;
const data = packValidatorParams(query);

await verifier.connect(requestOwner).setZKPRequest(requestId, {
metadata: "metadata",
validator: await sig.getAddress(),
data: data,
});

let request = await verifier.getZKPRequest(requestId);
expect(request.metadata).to.be.equal("metadata");

await expect(
verifier.connect(requestOwner).updateZKPRequest(requestId, {
metadata: "metadata",
validator: await sig.getAddress(),
data: data,
}),
).to.be.revertedWithCustomError(verifier, "OwnableUnauthorizedAccount");

await verifier.connect(owner).updateZKPRequest(requestId, {
metadata: "metadata2",
validator: await sig.getAddress(),
data: data,
});

request = await verifier.getZKPRequest(requestId);
expect(request.metadata).to.be.equal("metadata2");
});

it("updateZKPRequest - not existed request", async () => {
const owner = signer;
const requestId = 0;
const data = packValidatorParams(query);

await expect(
verifier.connect(owner).updateZKPRequest(requestId, {
metadata: "metadata",
validator: await sig.getAddress(),
data: data,
}),
).to.be.rejectedWith("equest id doesn't exis");
});
});

0 comments on commit 6bc10f6

Please sign in to comment.