UNPKG

@consensys-software/permissioning-smart-contracts

Version:

Smart contracts and dapp implementing EEA spec onchain permissioning

167 lines (121 loc) 6.41 kB
const Admin = artifacts.require('Admin.sol'); contract("Admin (admin management)", async accounts => { let adminContract; beforeEach(async () => { adminContract = await Admin.new(); }) it("account that deployed contract should be admin", async () => { let isAuthorized = await adminContract.isAuthorized(accounts[0]); assert.ok(isAuthorized); }); it("non-deployer account should not be admin", async () => { let isAuthorized = await adminContract.isAuthorized(accounts[1]); assert.notOk(isAuthorized); }); it("non admin cannot add another admin", async () => { try { await adminContract.addAdmin(accounts[2], { from: accounts[1] }); expect.fail(null, null, "Modifier was not enforced") } catch(err) { expect(err.reason).to.contain('Sender not authorized'); } }); it("admin can add another admin", async () => { await adminContract.addAdmin(accounts[2], { from: accounts[0] }); let isAuthorized = await adminContract.isAuthorized(accounts[2]); assert.ok(isAuthorized); }); it("non admin cannot remove another admin", async () => { try { await adminContract.removeAdmin(accounts[2], { from: accounts[1] }); expect.fail(null, null, "Modifier was not enforced") } catch(err) { expect(err.reason).to.contain("Sender not authorized"); } }); it("admin can remove another admin", async () => { await adminContract.addAdmin(accounts[2], { from: accounts[0] }); let isAuthorized = await adminContract.isAuthorized(accounts[2]); assert.ok(isAuthorized); tx = await adminContract.removeAdmin(accounts[2], { from: accounts[0] }); isAuthorized = await adminContract.isAuthorized(accounts[2]); assert.notOk(isAuthorized); }); it("admin cannot remove themselves", async () => { try { await adminContract.removeAdmin(accounts[0], { from: accounts[0] }); expect.fail(null, null, "Modifier was not enforced") } catch(err) { expect(err.reason).to.contain("Cannot invoke method with own account as parameter"); } }); it("get admins list", async () => { let admins = await adminContract.getAdmins.call(); assert.sameMembers([accounts[0]], admins) }); it("get admins list reflect changes", async () => { let admins = await adminContract.getAdmins.call(); assert.sameMembers([accounts[0]], admins) await adminContract.addAdmin(accounts[1], { from: accounts[0] }); admins = await adminContract.getAdmins.call(); assert.sameMembers([accounts[0], accounts[1]], admins); await adminContract.addAdmin(accounts[2], { from: accounts[0] }); admins = await adminContract.getAdmins.call(); assert.sameMembers([accounts[0], accounts[1], accounts[2]], admins); await adminContract.removeAdmin(accounts[1], { from: accounts[0] }); admins = await adminContract.getAdmins.call(); assert.sameMembers([accounts[0], accounts[2]], admins); }); it("Should emit events when an Admin is added", async () => { const ownAddress = accounts[0] const address = accounts[1]; // Add a new account await adminContract.addAdmin(address); // Attempt to add a duplicate entry await adminContract.addAdmin(address); // Attempt to add self await adminContract.addAdmin(ownAddress); // Get the events let result = await adminContract.getPastEvents("AdminAdded", {fromBlock: 0, toBlock: "latest" }); // Verify the first AccountAdded event is 'true' assert.equal(result[0].returnValues.adminAdded, true, "adminAdded SHOULD be true"); // Verify the unsuccessful duplicate AccountAdded event is 'false' assert.equal(result[1].returnValues.adminAdded, false, "adminAdded with duplicate account SHOULD be false"); // Verify the unsuccessful duplicate AccountAdded event has correct message assert.equal(result[1].returnValues.message, "Account is already an Admin", "duplicate Admin error message SHOULD be correct"); // Verify the adding own account AccountAdded event is 'false' assert.equal(result[2].returnValues.adminAdded, false, "adminAdded with own account SHOULD be false"); // Verify the adding own account AccountAdded event has correct message assert.equal(result[2].returnValues.message, "Adding own account as Admin is not permitted", "adding self Admin error message SHOULD be correct"); }); it("Should emit events when multiple Admins are added", async () => { const ownAddress = accounts[0] const address = accounts[1]; //add same account twice and attempt to add self await adminContract.addAdmins([address, address, ownAddress]) // Get the events let result = await adminContract.getPastEvents("AdminAdded", {fromBlock: 0, toBlock: "latest" }); // Verify the first AccountAdded event is 'true' assert.equal(result[0].returnValues.adminAdded, true, "adminAdded SHOULD be true"); // Verify the unsuccessful duplicate AccountAdded event is 'false' assert.equal(result[1].returnValues.adminAdded, false, "adminAdded with duplicate account SHOULD be false"); // Verify the unsuccessful duplicate AccountAdded event has correct message assert.equal(result[1].returnValues.message, "Account is already an Admin", "duplicate Admin error message SHOULD be correct"); // Verify the adding own account AccountAdded event is 'false' assert.equal(result[2].returnValues.adminAdded, false, "adminAdded with own account SHOULD be false"); // Verify the adding own account AccountAdded event has correct message assert.equal(result[2].returnValues.message, "Adding own account as Admin is not permitted", "adding self Admin error message SHOULD be correct"); }); it("Should emit events when an Admin is removed", async () => { const address = accounts[1]; // Add a new account await adminContract.addAdmin(address); await adminContract.removeAdmin(address); await adminContract.removeAdmin(address); let result = await adminContract.getPastEvents("AdminRemoved", {fromBlock: 0, toBlock: "latest" }); // Verify the first AccountRemoved event is 'true' assert.equal(result[0].returnValues.adminRemoved, true, "adminRemoved SHOULD be true"); // Verify the second AccountRemoved event is 'false' assert.equal(result[1].returnValues.adminRemoved, false, "adminRemoved SHOULD be false"); }); });