You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
100 lines
2.9 KiB
100 lines
2.9 KiB
7 years ago
|
const RBACMock = artifacts.require('./mocks/RBACMock.sol')
|
||
7 years ago
|
|
||
|
import expectThrow from './helpers/expectThrow'
|
||
7 years ago
|
import expectEvent from './helpers/expectEvent'
|
||
7 years ago
|
|
||
|
require('chai')
|
||
|
.use(require('chai-as-promised'))
|
||
|
.should()
|
||
|
|
||
7 years ago
|
const ROLE_ADVISOR = 'advisor';
|
||
|
|
||
7 years ago
|
contract('RBAC', function(accounts) {
|
||
|
let mock
|
||
|
|
||
|
const [
|
||
7 years ago
|
admin,
|
||
7 years ago
|
anyone,
|
||
7 years ago
|
futureAdvisor,
|
||
7 years ago
|
...advisors
|
||
|
] = accounts
|
||
|
|
||
|
before(async () => {
|
||
7 years ago
|
mock = await RBACMock.new(advisors, { from: admin })
|
||
7 years ago
|
})
|
||
|
|
||
|
context('in normal conditions', () => {
|
||
7 years ago
|
it('allows admin to call #onlyAdminsCanDoThis', async () => {
|
||
|
await mock.onlyAdminsCanDoThis({ from: admin })
|
||
7 years ago
|
.should.be.fulfilled
|
||
|
})
|
||
7 years ago
|
it('allows admin to call #onlyAdvisorsCanDoThis', async () => {
|
||
|
await mock.onlyAdvisorsCanDoThis({ from: admin })
|
||
7 years ago
|
.should.be.fulfilled
|
||
|
})
|
||
|
it('allows advisors to call #onlyAdvisorsCanDoThis', async () => {
|
||
|
await mock.onlyAdvisorsCanDoThis({ from: advisors[0] })
|
||
|
.should.be.fulfilled
|
||
|
})
|
||
7 years ago
|
it('allows admin to call #eitherAdminOrAdvisorCanDoThis', async () => {
|
||
|
await mock.eitherAdminOrAdvisorCanDoThis({ from: admin })
|
||
7 years ago
|
.should.be.fulfilled
|
||
|
})
|
||
7 years ago
|
it('allows advisors to call #eitherAdminOrAdvisorCanDoThis', async () => {
|
||
|
await mock.eitherAdminOrAdvisorCanDoThis({ from: advisors[0] })
|
||
7 years ago
|
.should.be.fulfilled
|
||
|
})
|
||
7 years ago
|
it('does not allow admins to call #nobodyCanDoThis', async () => {
|
||
7 years ago
|
expectThrow(
|
||
7 years ago
|
mock.nobodyCanDoThis({ from: admin })
|
||
7 years ago
|
)
|
||
|
})
|
||
|
it('does not allow advisors to call #nobodyCanDoThis', async () => {
|
||
|
expectThrow(
|
||
|
mock.nobodyCanDoThis({ from: advisors[0] })
|
||
|
)
|
||
|
})
|
||
|
it('does not allow anyone to call #nobodyCanDoThis', async () => {
|
||
|
expectThrow(
|
||
|
mock.nobodyCanDoThis({ from: anyone })
|
||
|
)
|
||
|
})
|
||
7 years ago
|
it('allows an admin to remove an advisor\'s role', async () => {
|
||
|
await mock.removeAdvisor(advisors[0], { from: admin })
|
||
|
.should.be.fulfilled
|
||
|
})
|
||
|
it('allows admins to #adminRemoveRole', async () => {
|
||
7 years ago
|
await mock.adminRemoveRole(advisors[3], ROLE_ADVISOR, { from: admin })
|
||
7 years ago
|
.should.be.fulfilled
|
||
|
})
|
||
7 years ago
|
|
||
|
it('announces a RoleAdded event on addRole', async () => {
|
||
|
expectEvent.inTransaction(
|
||
|
mock.adminAddRole(futureAdvisor, ROLE_ADVISOR, { from: admin }),
|
||
|
'RoleAdded'
|
||
|
)
|
||
|
})
|
||
|
|
||
|
it('announces a RoleRemoved event on removeRole', async () => {
|
||
|
expectEvent.inTransaction(
|
||
|
mock.adminRemoveRole(futureAdvisor, ROLE_ADVISOR, { from: admin }),
|
||
|
'RoleRemoved'
|
||
|
)
|
||
|
})
|
||
7 years ago
|
})
|
||
|
|
||
|
context('in adversarial conditions', () => {
|
||
|
it('does not allow an advisor to remove another advisor', async () => {
|
||
|
expectThrow(
|
||
|
mock.removeAdvisor(advisors[1], { from: advisors[0] })
|
||
|
)
|
||
|
})
|
||
|
it('does not allow "anyone" to remove an advisor', async () => {
|
||
|
expectThrow(
|
||
|
mock.removeAdvisor(advisors[0], { from: anyone })
|
||
|
)
|
||
|
})
|
||
|
})
|
||
|
|
||
|
})
|