Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Role tests #1228

Merged
merged 4 commits into from
Aug 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
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);
});
});
});
});