Roles.add and remove now require pre-conditions on the account. (#1421)

(cherry picked from commit 3bd30f7382)
pull/1474/head
Nicolás Venturo 6 years ago committed by Leo Arias
parent e990525c2e
commit 1b79b536cd
  1. 4
      contracts/access/Roles.sol
  2. 9
      test/access/Roles.test.js
  3. 13
      test/access/roles/PublicRole.behavior.js

@ -14,6 +14,8 @@ library Roles {
*/ */
function add(Role storage role, address account) internal { function add(Role storage role, address account) internal {
require(account != address(0)); require(account != address(0));
require(!has(role, account));
role.bearer[account] = true; role.bearer[account] = true;
} }
@ -22,6 +24,8 @@ library Roles {
*/ */
function remove(Role storage role, address account) internal { function remove(Role storage role, address account) internal {
require(account != address(0)); require(account != address(0));
require(has(role, account));
role.bearer[account] = false; role.bearer[account] = false;
} }

@ -29,10 +29,9 @@ contract('Roles', function ([_, authorized, otherAuthorized, anyone]) {
(await this.roles.has(anyone)).should.equal(false); (await this.roles.has(anyone)).should.equal(false);
}); });
it('adds roles to an already-assigned account', async function () { it('reverts when adding roles to an already assigned account', async function () {
await this.roles.add(authorized); await this.roles.add(authorized);
await this.roles.add(authorized); await shouldFail.reverting(this.roles.add(authorized));
(await this.roles.has(authorized)).should.equal(true);
}); });
it('reverts when adding roles to the null account', async function () { it('reverts when adding roles to the null account', async function () {
@ -54,8 +53,8 @@ contract('Roles', function ([_, authorized, otherAuthorized, anyone]) {
(await this.roles.has(otherAuthorized)).should.equal(true); (await this.roles.has(otherAuthorized)).should.equal(true);
}); });
it('doesn\'t revert when removing unassigned roles', async function () { it('reverts when removing unassigned roles', async function () {
await this.roles.remove(anyone); await shouldFail.reverting(this.roles.remove(anyone));
}); });
it('reverts when removing roles from the null account', async function () { it('reverts when removing roles from the null account', async function () {

@ -52,9 +52,8 @@ function shouldBehaveLikePublicRole (authorized, otherAuthorized, [anyone], role
expectEvent.inLogs(logs, `${rolename}Added`, { account: anyone }); expectEvent.inLogs(logs, `${rolename}Added`, { account: anyone });
}); });
it('adds role to an already-assigned account', async function () { it('reverts when adding role to an already assigned account', async function () {
await this.contract[`add${rolename}`](authorized, { from: authorized }); await shouldFail.reverting(this.contract[`add${rolename}`](authorized, { from: authorized }));
(await this.contract[`is${rolename}`](authorized)).should.equal(true);
}); });
it('reverts when adding role to the null account', async function () { it('reverts when adding role to the null account', async function () {
@ -74,8 +73,8 @@ function shouldBehaveLikePublicRole (authorized, otherAuthorized, [anyone], role
expectEvent.inLogs(logs, `${rolename}Removed`, { account: authorized }); expectEvent.inLogs(logs, `${rolename}Removed`, { account: authorized });
}); });
it('doesn\'t revert when removing from an unassigned account', async function () { it('reverts when removing from an unassigned account', async function () {
await this.contract[`remove${rolename}`](anyone); await shouldFail.reverting(this.contract[`remove${rolename}`](anyone));
}); });
it('reverts when removing role from the null account', async function () { it('reverts when removing role from the null account', async function () {
@ -94,8 +93,8 @@ function shouldBehaveLikePublicRole (authorized, otherAuthorized, [anyone], role
expectEvent.inLogs(logs, `${rolename}Removed`, { account: authorized }); expectEvent.inLogs(logs, `${rolename}Removed`, { account: authorized });
}); });
it('doesn\'t revert when renouncing unassigned role', async function () { it('reverts when renouncing unassigned role', async function () {
await this.contract[`renounce${rolename}`]({ from: anyone }); await shouldFail.reverting(this.contract[`renounce${rolename}`]({ from: anyone }));
}); });
}); });
}); });

Loading…
Cancel
Save