RBAC.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const RBACMock = artifacts.require('./helpers/RBACMock.sol')
  2. import expectThrow from './helpers/expectThrow'
  3. require('chai')
  4. .use(require('chai-as-promised'))
  5. .should()
  6. contract('RBAC', function(accounts) {
  7. let mock
  8. const [
  9. owner,
  10. anyone,
  11. ...advisors
  12. ] = accounts
  13. before(async () => {
  14. mock = await RBACMock.new(advisors, { from: owner })
  15. })
  16. context('in normal conditions', () => {
  17. it('allows owner to call #onlyOwnersCanDoThis', async () => {
  18. await mock.onlyOwnersCanDoThis({ from: owner })
  19. .should.be.fulfilled
  20. })
  21. it('allows owner to call #onlyAdvisorsCanDoThis', async () => {
  22. await mock.onlyAdvisorsCanDoThis({ from: owner })
  23. .should.be.fulfilled
  24. })
  25. it('allows advisors to call #onlyAdvisorsCanDoThis', async () => {
  26. await mock.onlyAdvisorsCanDoThis({ from: advisors[0] })
  27. .should.be.fulfilled
  28. })
  29. it('allows owner to call #eitherOwnerOrAdvisorCanDoThis', async () => {
  30. await mock.eitherOwnerOrAdvisorCanDoThis({ from: owner })
  31. .should.be.fulfilled
  32. })
  33. it('allows advisors to call #eitherOwnerOrAdvisorCanDoThis', async () => {
  34. await mock.eitherOwnerOrAdvisorCanDoThis({ from: advisors[0] })
  35. .should.be.fulfilled
  36. })
  37. it('does not allow owners to call #nobodyCanDoThis', async () => {
  38. expectThrow(
  39. mock.nobodyCanDoThis({ from: owner })
  40. )
  41. })
  42. it('does not allow advisors to call #nobodyCanDoThis', async () => {
  43. expectThrow(
  44. mock.nobodyCanDoThis({ from: advisors[0] })
  45. )
  46. })
  47. it('does not allow anyone to call #nobodyCanDoThis', async () => {
  48. expectThrow(
  49. mock.nobodyCanDoThis({ from: anyone })
  50. )
  51. })
  52. it('allows an owner to remove an advisor\'s role', async () => {
  53. await mock.removeAdvisor(advisors[0], { from: owner })
  54. .should.be.fulfilled
  55. })
  56. })
  57. context('in adversarial conditions', () => {
  58. it('does not allow an advisor to remove another advisor', async () => {
  59. expectThrow(
  60. mock.removeAdvisor(advisors[1], { from: advisors[0] })
  61. )
  62. })
  63. it('does not allow "anyone" to remove an advisor', async () => {
  64. expectThrow(
  65. mock.removeAdvisor(advisors[0], { from: anyone })
  66. )
  67. })
  68. })
  69. })