From a872fe50f682b85078d9b0ef44295fbc2ff849e4 Mon Sep 17 00:00:00 2001 From: miguelToscano Date: Tue, 11 Oct 2022 15:32:05 -0300 Subject: [PATCH] added tests --- test/SitesNFTs.js | 72 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/test/SitesNFTs.js b/test/SitesNFTs.js index d7bfcfb..deeaa0e 100644 --- a/test/SitesNFTs.js +++ b/test/SitesNFTs.js @@ -28,9 +28,9 @@ describe("SitesNFTs contract", function () { const DEFAULT_ADMIN_ROLE_STRING = "" - const userRole = await hardhatSitesNFTs.hasRole(ethers.utils.formatBytes32String(DEFAULT_ADMIN_ROLE_STRING) , await owner.getAddress()); + const hasAdminRole = await hardhatSitesNFTs.hasRole(ethers.utils.formatBytes32String(DEFAULT_ADMIN_ROLE_STRING) , await owner.getAddress()); - expect(userRole).to.equal(true); + expect(hasAdminRole).to.equal(true); }); it("Deployment should assign initial tokenId to 0", async function () { @@ -46,17 +46,77 @@ describe("SitesNFTs contract", function () { }); }); - describe("Role management", () => { - it("User with admin role should be able to assign MINTER_ROLE to another user", async function () { + describe("Access control", () => { + it("User with DEFAULT_ADMIN_ROLE should be able to assign MINTER_ROLE to another user", async function () { const [owner, address1] = await ethers.getSigners(); const SitesNFTs = await ethers.getContractFactory("SitesNFTs"); const hardhatSitesNFTs = await SitesNFTs.deploy("Sites NFTs", "SNFT"); - const currentTokenId = await hardhatSitesNFTs.getCurrentTokenId(); + const MINTER_ROLE = "MINTER_ROLE" + + await hardhatSitesNFTs.grantRole(ethers.utils.formatBytes32String(MINTER_ROLE), await address1.getAddress()); + + const hasMinterRole = await hardhatSitesNFTs.hasRole(ethers.utils.formatBytes32String(MINTER_ROLE), await address1.getAddress()); + + expect(hasMinterRole).to.equal(true); + }); + + it("User with DEFAULT_ADMIN_ROLE should be able to assign DEFAULT_ADMIN_ROLE to another user", async function () { + const [owner, address1] = await ethers.getSigners(); - expect(currentTokenId).to.equal(0); + const SitesNFTs = await ethers.getContractFactory("SitesNFTs"); + + const hardhatSitesNFTs = await SitesNFTs.deploy("Sites NFTs", "SNFT"); + + const DEFAULT_ADMIN_ROLE = ""; + + await hardhatSitesNFTs.grantRole(ethers.utils.formatBytes32String(DEFAULT_ADMIN_ROLE), await address1.getAddress()); + + const hasAdminRole = await hardhatSitesNFTs.hasRole(ethers.utils.formatBytes32String(DEFAULT_ADMIN_ROLE), await address1.getAddress()); + + expect(hasAdminRole).to.equal(true); + }); + + it("User without DEFAULT_ADMIN_ROLE shouldnt be able to assign DEFAULT_ADMIN_ROLE to another user", async function () { + const [owner, address1, address2] = await ethers.getSigners(); + + const SitesNFTs = await ethers.getContractFactory("SitesNFTs"); + + const hardhatSitesNFTs = await SitesNFTs.deploy("Sites NFTs", "SNFT"); + + const DEFAULT_ADMIN_ROLE = ""; + + try { + await hardhatSitesNFTs.connect(address1).grantRole(ethers.utils.formatBytes32String(DEFAULT_ADMIN_ROLE), await address2.getAddress()); + } catch (e) { + + } + + const hasAdminRole = await hardhatSitesNFTs.hasRole(ethers.utils.formatBytes32String(DEFAULT_ADMIN_ROLE), await address2.getAddress()); + + expect(hasAdminRole).to.equal(false); + }); + + it("User without DEFAULT_ADMIN_ROLE shouldnt be able to assign MINTER_ROLE to another user", async function () { + const [owner, address1, address2] = await ethers.getSigners(); + + const SitesNFTs = await ethers.getContractFactory("SitesNFTs"); + + const hardhatSitesNFTs = await SitesNFTs.deploy("Sites NFTs", "SNFT"); + + const MINTER_ROLE = "MINTER_ROLE" + + try { + await hardhatSitesNFTs.connect(address1).grantRole(ethers.utils.formatBytes32String(MINTER_ROLE), await address2.getAddress()); + } catch (e) { + + } + + const hasMinterRole = await hardhatSitesNFTs.hasRole(ethers.utils.formatBytes32String(MINTER_ROLE), await address2.getAddress()); + + expect(hasMinterRole).to.equal(false); }); }); }); \ No newline at end of file