Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Role tests (#1228)
* Moved RBAC tests to access.

* Added Roles.addMany and tests.

* Fixed linter error.

* Now using uint256 indexes.
  • Loading branch information
nventuro authored Aug 22, 2018
commit c8c23090ee8858c035d77006b6b5fe6bf2407c52
11 changes: 11 additions & 0 deletions contracts/access/rbac/Roles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ library Roles {
_role.bearer[_account] = true;
}

/**
* @dev give multiple accounts access to this role
*/
function addMany(Role storage _role, address[] _accounts)
internal
{
for (uint256 i = 0; i < _accounts.length; ++i) {
add(_role, _accounts[i]);
}
}

/**
* @dev remove an account's access to this role
*/
Expand Down
30 changes: 30 additions & 0 deletions contracts/mocks/RolesMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
pragma solidity ^0.4.24;

import "../access/rbac/Roles.sol";


contract RolesMock {
using Roles for Roles.Role;

Roles.Role private dummyRole;

function add(address _account) public {
dummyRole.add(_account);
}

function addMany(address[] _accounts) public {
dummyRole.addMany(_accounts);
}

function remove(address _account) public {
dummyRole.remove(_account);
}

function check(address _account) public view {
dummyRole.check(_account);
}

function has(address _account) public view returns (bool) {
return dummyRole.has(_account);
}
}
File renamed without changes.
72 changes: 72 additions & 0 deletions test/access/rbac/Roles.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
const { assertRevert } = require('../../helpers/assertRevert');

const RolesMock = artifacts.require('RolesMock');

require('chai')
.should();

contract('Roles', function ([_, authorized, otherAuthorized, anyone]) {
beforeEach(async function () {
this.roles = await RolesMock.new();
this.testRole = async (account, expected) => {
if (expected) {
(await this.roles.has(account)).should.equal(true);
await this.roles.check(account); // this call shouldn't revert, but is otherwise a no-op
} else {
(await this.roles.has(account)).should.equal(false);
await assertRevert(this.roles.check(account));
}
};
});

context('initially', function () {
it('doesn\'t pre-assign roles', async function () {
await this.testRole(authorized, false);
await this.testRole(otherAuthorized, false);
await this.testRole(anyone, false);
});

describe('adding roles', function () {
it('adds roles to a single account', async function () {
await this.roles.add(authorized);
await this.testRole(authorized, true);
await this.testRole(anyone, false);
});

it('adds roles to an already-assigned account', async function () {
await this.roles.add(authorized);
await this.roles.add(authorized);
await this.testRole(authorized, true);
});

it('adds roles to multiple accounts', async function () {
await this.roles.addMany([authorized, otherAuthorized]);
await this.testRole(authorized, true);
await this.testRole(otherAuthorized, true);
});

it('adds roles to multiple identical accounts', async function () {
await this.roles.addMany([authorized, authorized]);
await this.testRole(authorized, true);
});
});
});

context('with added roles', function () {
beforeEach(async function () {
await this.roles.addMany([authorized, otherAuthorized]);
});

describe('removing roles', function () {
it('removes a single role', async function () {
await this.roles.remove(authorized);
await this.testRole(authorized, false);
await this.testRole(otherAuthorized, true);
});

it('removes unassigned roles', async function () {
await this.roles.remove(anyone);
});
});
});
});