test: 💍 InstaAccessControl contract

This commit is contained in:
Daksh Miglani 2021-05-10 00:46:11 +05:30
parent 169b411142
commit 2520d57e88
4 changed files with 26740 additions and 851 deletions

27439
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -39,6 +39,8 @@
"@openzeppelin/test-helpers": "^0.5.6",
"@studydefi/money-legos": "^2.3.7",
"@tenderly/hardhat-tenderly": "^1.0.6",
"chai-as-promised": "^7.1.1",
"ethereum-waffle": "^3.3.0",
"ethers": "^5.0.32",
"hardhat": "^2.0.8",
"husky": "^6.0.0",

View File

@ -0,0 +1,123 @@
const { ethers, network } = require("hardhat");
const chai = require("chai");
const chaiPromise = require("chai-as-promised");
const { solidity } = require("ethereum-waffle");
chai.use(chaiPromise);
chai.use(solidity);
const { expect } = chai;
const getAccessControl = (address, signer) => {
return ethers.getContractAt("InstaAccessControl", address, signer);
};
describe("Test InstaAccessControl contract", () => {
let account, fakeAccount, instaMaster;
let accessControlAddress;
let masterAccessControl;
const indexInterfaceAddress = "0x2971AdFa57b20E5a416aE5a708A8655A9c74f723";
const TEST_ROLE = ethers.utils.formatBytes32String("test");
before("get signers", async () => {
[account] = await ethers.getSigners();
const IndexContract = await ethers.getContractAt(
"contracts/mapping/InstaAccessControl.sol:IndexInterface",
indexInterfaceAddress
);
const masterAddress = await IndexContract.master();
await network.provider.request({
method: "hardhat_impersonateAccount",
params: [masterAddress],
});
instaMaster = await ethers.getSigner(masterAddress);
});
beforeEach("deploy contract", async () => {
const accessControlFactory = await ethers.getContractFactory(
"InstaAccessControl"
);
const accessControl = await accessControlFactory.deploy();
await accessControl.deployed();
accessControlAddress = accessControl.address;
masterAccessControl = await getAccessControl(
accessControlAddress,
instaMaster
);
});
it("grant,revoke role should fail with non master signer", async () => {
const accessControl = await getAccessControl(accessControlAddress, account);
await expect(
accessControl.grantRole(TEST_ROLE, account.address)
).to.rejectedWith(/AccessControl: sender must be master/);
await expect(
accessControl.revokeRole(TEST_ROLE, account.address)
).to.rejectedWith(/AccessControl: sender must be master/);
});
it("hasRole should return false for roles not assigned to users", async () => {
expect(await masterAccessControl.hasRole(TEST_ROLE, account.address)).to.eq(
false
);
});
it("should grant roles", async () => {
await expect(masterAccessControl.grantRole(TEST_ROLE, account.address))
.to.emit(masterAccessControl, "RoleGranted")
.withArgs(TEST_ROLE, account.address);
expect(await masterAccessControl.hasRole(TEST_ROLE, account.address)).to.eq(
true
);
});
it("should revoke role", async () => {
// add a role first
await masterAccessControl.grantRole(TEST_ROLE, account.address);
expect(await masterAccessControl.hasRole(TEST_ROLE, account.address)).to.eq(
true
);
// then remove the role
await expect(masterAccessControl.revokeRole(TEST_ROLE, account.address))
.to.emit(masterAccessControl, "RoleRevoked")
.withArgs(TEST_ROLE, account.address, instaMaster.address);
expect(await masterAccessControl.hasRole(TEST_ROLE, account.address)).to.eq(
false
);
});
it("should renounce role only with the account not master", async () => {
// add a role first
await masterAccessControl.grantRole(TEST_ROLE, account.address);
expect(await masterAccessControl.hasRole(TEST_ROLE, account.address)).to.eq(
true
);
// then renounce the the role
await expect(
masterAccessControl.renounceRole(TEST_ROLE, account.address)
).to.rejectedWith(/AccessControl: can only renounce roles for self/);
const selfAccessControl = await getAccessControl(
accessControlAddress,
account
);
expect(await selfAccessControl.renounceRole(TEST_ROLE, account.address))
.to.emit(masterAccessControl, "RoleRevoked")
.withArgs(TEST_ROLE, account.address, account.address);
expect(await masterAccessControl.hasRole(TEST_ROLE, account.address)).to.eq(
false
);
});
});

View File

@ -1,27 +0,0 @@
const { ethers } = require("hardhat");
describe("Test InstaMapping contract", () => {
let signer, addressAdmin, fakeAccount;
let mappingContract;
const otherContractAddress = "0x514910771af9ca656af840dff83e8264ecf986ca";
beforeEach("deploy contract", async () => {
[signer, addressAdmin, fakeAccount] = await ethers.getSigners();
const mappingContractFactory = await ethers.getContractFactory("InstaMappings");
mappingContract = await mappingContractFactory.deploy();
await mappingContract.deployed();
});
it("should grant role", async () => {
const GRANT_ROLE_KEY = 'grantRole(address,address)'
const HAS_ROLE_KEY = 'hasRole(address,address)';
await mappingContract[GRANT_ROLE_KEY](otherContractAddress, addressAdmin.address, {
gasLimit: 12000000
});
expect(await mappingContract[HAS_ROLE_KEY](otherContractAddress, addressAdmin.address)).to.eq(true);
});
});