Skip to content

Commit

Permalink
Add tests to achieve 100% coverage of the CcsmBpiStateAnchor smart co…
Browse files Browse the repository at this point in the history
…ntract
  • Loading branch information
ognjenkurtic committed Jul 17, 2024
1 parent 70db42c commit e595f2b
Showing 1 changed file with 107 additions and 9 deletions.
116 changes: 107 additions & 9 deletions examples/bri-3/ccsm/test/CcsmBpiStateAnchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,123 @@ import { expect } from "chai";
import hre from "hardhat";

describe("CcsmBpiStateAnchor", function () {
// We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state,
// and reset Hardhat Network to that snapshot in every test.
async function deployCcsmBpiStateAnchor() {
// Contracts are deployed using the first signer/account by default
const [owner, otherAccount] = await hre.ethers.getSigners();
const [owner, otherAccount, adminAccount] = await hre.ethers.getSigners();

const CcsmBpiStateAnchor = await hre.ethers.getContractFactory("CcsmBpiStateAnchor");
const ccsmBpiStateAnchor = await CcsmBpiStateAnchor.deploy([await owner.getAddress()]);
const ccsmBpiStateAnchor = await CcsmBpiStateAnchor.deploy([await owner.getAddress(), await adminAccount.getAddress()]);

return { ccsmBpiStateAnchor, owner, otherAccount };
return { ccsmBpiStateAnchor, owner, otherAccount, adminAccount };
}

describe("Deployment", function () {
// TODO: Extend tests
it("Should deploy the contract", async function () {
const { ccsmBpiStateAnchor } = await loadFixture(deployCcsmBpiStateAnchor);
expect(await ccsmBpiStateAnchor.getAddress()).to.be.properAddress;
});

it("Should set the correct admin roles", async function () {
const { ccsmBpiStateAnchor, owner, adminAccount } = await loadFixture(deployCcsmBpiStateAnchor);
const adminRole = await ccsmBpiStateAnchor.ADMIN_ROLE();
expect(await ccsmBpiStateAnchor.hasRole(adminRole, owner.address)).to.be.true;
expect(await ccsmBpiStateAnchor.hasRole(adminRole, adminAccount.address)).to.be.true;
});
});

describe("setAnchorHash", function () {
it("Should allow an admin to set an anchor hash", async function () {
const { ccsmBpiStateAnchor, owner } = await loadFixture(deployCcsmBpiStateAnchor);
const workgroupId = "testWorkgroup";
const anchorHash = "0x1234567890abcdef";

await expect(ccsmBpiStateAnchor.connect(owner).setAnchorHash(workgroupId, anchorHash))
.to.emit(ccsmBpiStateAnchor, "AnchorHashSet")
.withArgs(workgroupId, anchorHash);

expect(await ccsmBpiStateAnchor.anchorHashStore(workgroupId)).to.equal(anchorHash);
});

it("Should not allow a non-admin to set an anchor hash", async function () {
const { ccsmBpiStateAnchor, otherAccount } = await loadFixture(deployCcsmBpiStateAnchor);
const workgroupId = "testWorkgroup";
const anchorHash = "0x1234567890abcdef";

await expect(ccsmBpiStateAnchor.connect(otherAccount).setAnchorHash(workgroupId, anchorHash))
.to.be.revertedWith("Only admin can call this function");
});

it("Should revert when workgroupId is empty", async function () {
const { ccsmBpiStateAnchor, owner } = await loadFixture(deployCcsmBpiStateAnchor);
const anchorHash = "0x1234567890abcdef";

await expect(ccsmBpiStateAnchor.connect(owner).setAnchorHash("", anchorHash))
.to.be.revertedWith("WorkgroupId cannot be empty");
});

it("Should revert when workgroupId exceeds 36 bytes", async function () {
const { ccsmBpiStateAnchor, owner } = await loadFixture(deployCcsmBpiStateAnchor);
const longWorkgroupId = "a".repeat(37);
const anchorHash = "0x1234567890abcdef";

await expect(ccsmBpiStateAnchor.connect(owner).setAnchorHash(longWorkgroupId, anchorHash))
.to.be.revertedWith("WorkgroupId cannot exceed 36 bytes");
});

it("Should revert when anchorHash is empty", async function () {
const { ccsmBpiStateAnchor, owner } = await loadFixture(deployCcsmBpiStateAnchor);
const workgroupId = "testWorkgroup";

await expect(ccsmBpiStateAnchor.connect(owner).setAnchorHash(workgroupId, ""))
.to.be.revertedWith("AnchorHash cannot be empty");
});

it("Should revert when anchorHash exceeds 256 bytes", async function () {
const { ccsmBpiStateAnchor, owner } = await loadFixture(deployCcsmBpiStateAnchor);
const workgroupId = "testWorkgroup";
const longAnchorHash = "0x" + "a".repeat(257);

await expect(ccsmBpiStateAnchor.connect(owner).setAnchorHash(workgroupId, longAnchorHash))
.to.be.revertedWith("AnchorHash cannot exceed 256 bytes");
});
});

describe("getAnchorHash", function () {
it("Should return the correct anchor hash for a given workgroupId", async function () {
const { ccsmBpiStateAnchor, owner } = await loadFixture(deployCcsmBpiStateAnchor);
const workgroupId = "testWorkgroup";
const anchorHash = "0x1234567890abcdef";

await ccsmBpiStateAnchor.connect(owner).setAnchorHash(workgroupId, anchorHash);
expect(await ccsmBpiStateAnchor.getAnchorHash(workgroupId)).to.equal(anchorHash);
});

it("Should return an empty string for a non-existent workgroupId", async function () {
const { ccsmBpiStateAnchor } = await loadFixture(deployCcsmBpiStateAnchor);
const nonExistentWorkgroupId = "nonExistent";

expect(await ccsmBpiStateAnchor.getAnchorHash(nonExistentWorkgroupId)).to.equal("");
});
});

describe("AccessControl", function () {
it("Should allow the owner to grant admin role", async function () {
const { ccsmBpiStateAnchor, owner, otherAccount } = await loadFixture(deployCcsmBpiStateAnchor);
const adminRole = await ccsmBpiStateAnchor.ADMIN_ROLE();

await expect(ccsmBpiStateAnchor.connect(owner).grantRole(adminRole, otherAccount.address))
.to.not.be.reverted;

expect(await ccsmBpiStateAnchor.hasRole(adminRole, otherAccount.address)).to.be.true;
});

expect(await ccsmBpiStateAnchor.getAddress()).not.to.equal(undefined);
it("Should allow the owner to revoke admin role", async function () {
const { ccsmBpiStateAnchor, owner, adminAccount } = await loadFixture(deployCcsmBpiStateAnchor);
const adminRole = await ccsmBpiStateAnchor.ADMIN_ROLE();

await expect(ccsmBpiStateAnchor.connect(owner).revokeRole(adminRole, adminAccount.address))
.to.not.be.reverted;

expect(await ccsmBpiStateAnchor.hasRole(adminRole, adminAccount.address)).to.be.false;
});
});
});

0 comments on commit e595f2b

Please sign in to comment.