From af2a75081dbde87d133124ad707fd602c58ffd5a Mon Sep 17 00:00:00 2001 From: adrien-v Date: Thu, 4 Apr 2024 17:33:58 +0200 Subject: [PATCH 1/2] add Test on Granted events --- backend/contracts/Admins.sol | 6 ------ backend/test/Admins.js | 23 ++++++++++++++++------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/backend/contracts/Admins.sol b/backend/contracts/Admins.sol index 90be3b1..feb7058 100644 --- a/backend/contracts/Admins.sol +++ b/backend/contracts/Admins.sol @@ -67,12 +67,6 @@ contract Admins is Ownable{ * @return bool Returns true if the address is not a super admin, false if it already exists as a super admin. */ function ensureSuperAdminDoNotExist(address _addr) internal view returns (bool){ - // for (uint256 i = 0; i < superAdminsAccounts.length; i++) { - // if (superAdminsAccounts[i] == _addr) { - // return false; - // } - // } - // return true; return uint(adminRoles[_addr]) == uint(Role.None); } diff --git a/backend/test/Admins.js b/backend/test/Admins.js index 5816b4f..e5278df 100644 --- a/backend/test/Admins.js +++ b/backend/test/Admins.js @@ -102,13 +102,22 @@ describe("Admins", function () { - // describe("Events", function () { - // it("Should emit an event on addAdmin", async function () { - // await expect(lock.withdraw()) - // .to.emit(lock, "Withdrawal") - // .withArgs(lockedAmount, anyValue); // We accept any value as `when` arg - // }); - // }); + describe("Events", function () { + + it("Should emit an event on addAdmin", async function () { + await expect(admins.addAdmin(addr1.address,addr1.address)) + .to.emit(admins, 'Granted') + .withArgs( owner.address,addr1.address, 1, anyValue); + }); + + it("Should emit an event on addSuperAdmin", async function () { + await expect(admins.addSuperAdmin(addr2.address)) + .to.emit(admins, 'Granted') + .withArgs(owner.address,addr2.address, 2, anyValue ); + }); + + + }) // describe("Transfers", function () { // it("Should transfer the funds to the owner", async function () { From 43030f0e5f3e3ba4accb109b5b12f40295eb5400 Mon Sep 17 00:00:00 2001 From: adrien-v Date: Thu, 4 Apr 2024 19:19:51 +0200 Subject: [PATCH 2/2] remove useless argument --- backend/contracts/Admins.sol | 24 ++++++++++++++++++++---- backend/test/Admins.js | 16 ++++++++-------- 2 files changed, 28 insertions(+), 12 deletions(-) diff --git a/backend/contracts/Admins.sol b/backend/contracts/Admins.sol index feb7058..00c7616 100644 --- a/backend/contracts/Admins.sol +++ b/backend/contracts/Admins.sol @@ -1,6 +1,7 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.24; import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; +import "./Albums.sol"; /// @custom:security-contact contact@adrien-v.com /** * @title Admins Contract @@ -112,15 +113,30 @@ contract Admins is Ownable{ * @notice Only existing superadmins can add new admins. //TODO newContract will be removed to be used after deploy */ - function addAdmin(address newAdmin, address newContract) external { + function addAdmin(address newAdmin) external { require(ensureAdminDoNotExist(newAdmin), "admin exists"); require(ensureSuperAdmin(msg.sender), "not super admin"); require(adminRoles[newAdmin] == Role.None, "role already set"); // todo factory call to deploy the contract and get the deployment address deployment address adminRoles[newAdmin] = Role.Admin; - adminsContracts[newAdmin] = newAdmin; // change this with deployed adress - adminsAccounts.push(newAdmin); - emit Granted(msg.sender,newAdmin, Role.Admin , newContract); + bytes memory collectionBytecode = type(Albums).creationCode; + bytes32 salt = keccak256(abi.encodePacked(newAdmin, block.timestamp)); + address collectionAddress; + // assembly { + // collectionAddress := create2( + // 0, + // add(collectionBytecode, 0x20), + // mload(collectionBytecode), + // salt + // ) + // if iszero(extcodesize(collectionAddress)) { + // // revert if something gone wrong (collectionAddress doesn't contain an address) + // revert(0, 0) + // } + // } + adminsContracts[newAdmin] = collectionAddress; // change this with deployed address + adminsAccounts.push(collectionAddress); + emit Granted(msg.sender,newAdmin, Role.Admin, collectionAddress); } function removeAdmin(address oldAdmin) external { diff --git a/backend/test/Admins.js b/backend/test/Admins.js index e5278df..de3a3c8 100644 --- a/backend/test/Admins.js +++ b/backend/test/Admins.js @@ -32,22 +32,22 @@ describe("Admins", function () { describe("addAdmin", function () { it("Should not be used without super privilegies", async function () { - await expect(admins.connect(addr1).addAdmin(addr2.address, addr2.address)).to.be.revertedWith("not super admin"); + await expect(admins.connect(addr1).addAdmin(addr2.address)).to.be.revertedWith("not super admin"); }); it("Should not downgrade a super admin", async function () { - await expect(admins.addAdmin(owner.address, owner.address)).to.be.revertedWith("admin exists"); + await expect(admins.addAdmin(owner.address)).to.be.revertedWith("admin exists"); }); it("Should fail if admin already exists", async function () { - await admins.addAdmin(addr1.address, addr1.address); - await expect(admins.addAdmin(addr1.address, addr1.address)).to.be.revertedWith("admin exists"); + await admins.addAdmin(addr1.address); + await expect(admins.addAdmin(addr1.address)).to.be.revertedWith("admin exists"); }); it("Should setup adminRoles and adminsContracts", async function () { - await admins.addAdmin(addr1.address, addr1.address); + await admins.addAdmin(addr1.address); await expect( await admins.adminRoles(addr1.address)).to.equal(1); - await expect( await admins.adminsContracts(addr1.address)).to.equal(addr1.address); + await expect( await admins.adminsContracts(addr1.address) != 0 ); // todo : fix with the deployed contract }); @@ -60,7 +60,7 @@ describe("Admins", function () { }); it("Should clean adminRoles", async function () { - await admins.addAdmin(addr1.address,addr1.address ); + await admins.addAdmin(addr1.address); await admins.removeAdmin(addr1.address); await expect( await admins.adminRoles(addr1.address)).to.equal(0); }); @@ -105,7 +105,7 @@ describe("Admins", function () { describe("Events", function () { it("Should emit an event on addAdmin", async function () { - await expect(admins.addAdmin(addr1.address,addr1.address)) + await expect(admins.addAdmin(addr1.address)) .to.emit(admins, 'Granted') .withArgs( owner.address,addr1.address, 1, anyValue); });