forked from OpenZeppelin/openzeppelin-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoles.test.js
More file actions
121 lines (99 loc) · 3.97 KB
/
Roles.test.js
File metadata and controls
121 lines (99 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const { assertRevert } = require('../../helpers/assertRevert');
const RolesMock = artifacts.require('RolesMock');
require('chai')
.should();
contract('Roles', function ([_, authorized, otherAuthorized, anyone]) {
const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
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);
});
it('doesn\'t revert when adding roles to the null account', async function () {
await this.roles.add(ZERO_ADDRESS);
});
});
});
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('doesn\'t revert when removing unassigned roles', async function () {
await this.roles.remove(anyone);
});
it('doesn\'t revert when removing roles from the null account', async function () {
await this.roles.remove(ZERO_ADDRESS);
});
});
describe('transfering roles', function () {
context('from account with role', function () {
const from = authorized;
it('transfers to other account with no role', async function () {
await this.roles.transfer(anyone, { from });
await this.testRole(anyone, true);
await this.testRole(authorized, false);
});
it('reverts when transfering to an account with role', async function () {
await assertRevert(this.roles.transfer(otherAuthorized, { from }));
});
it('reverts when transfering to the null account', async function () {
await assertRevert(this.roles.transfer(ZERO_ADDRESS, { from }));
});
});
context('from account without role', function () {
const from = anyone;
it('reverts', async function () {
await assertRevert(this.roles.transfer(anyone, { from }));
});
});
});
describe('renouncing roles', function () {
it('renounces an assigned role', async function () {
await this.roles.renounce({ from: authorized });
await this.testRole(authorized, false);
});
it('doesn\'t revert when renouncing unassigned role', async function () {
await this.roles.renounce({ from: anyone });
});
});
});
});