RBAC.test.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import expectThrow from './helpers/expectThrow';
  2. import expectEvent from './helpers/expectEvent';
  3. const RBACMock = artifacts.require('mocks/RBACMock.sol');
  4. require('chai')
  5. .use(require('chai-as-promised'))
  6. .should();
  7. const ROLE_ADVISOR = 'advisor';
  8. contract('RBAC', function (accounts) {
  9. let mock;
  10. const [
  11. admin,
  12. anyone,
  13. futureAdvisor,
  14. ...advisors
  15. ] = accounts;
  16. before(async () => {
  17. mock = await RBACMock.new(advisors, { from: admin });
  18. });
  19. context('in normal conditions', () => {
  20. it('allows admin to call #onlyAdminsCanDoThis', async () => {
  21. await mock.onlyAdminsCanDoThis({ from: admin })
  22. .should.be.fulfilled;
  23. });
  24. it('allows admin to call #onlyAdvisorsCanDoThis', async () => {
  25. await mock.onlyAdvisorsCanDoThis({ from: admin })
  26. .should.be.fulfilled;
  27. });
  28. it('allows advisors to call #onlyAdvisorsCanDoThis', async () => {
  29. await mock.onlyAdvisorsCanDoThis({ from: advisors[0] })
  30. .should.be.fulfilled;
  31. });
  32. it('allows admin to call #eitherAdminOrAdvisorCanDoThis', async () => {
  33. await mock.eitherAdminOrAdvisorCanDoThis({ from: admin })
  34. .should.be.fulfilled;
  35. });
  36. it('allows advisors to call #eitherAdminOrAdvisorCanDoThis', async () => {
  37. await mock.eitherAdminOrAdvisorCanDoThis({ from: advisors[0] })
  38. .should.be.fulfilled;
  39. });
  40. it('does not allow admins to call #nobodyCanDoThis', async () => {
  41. expectThrow(
  42. mock.nobodyCanDoThis({ from: admin })
  43. );
  44. });
  45. it('does not allow advisors to call #nobodyCanDoThis', async () => {
  46. expectThrow(
  47. mock.nobodyCanDoThis({ from: advisors[0] })
  48. );
  49. });
  50. it('does not allow anyone to call #nobodyCanDoThis', async () => {
  51. expectThrow(
  52. mock.nobodyCanDoThis({ from: anyone })
  53. );
  54. });
  55. it('allows an admin to remove an advisor\'s role', async () => {
  56. await mock.removeAdvisor(advisors[0], { from: admin })
  57. .should.be.fulfilled;
  58. });
  59. it('allows admins to #adminRemoveRole', async () => {
  60. await mock.adminRemoveRole(advisors[3], ROLE_ADVISOR, { from: admin })
  61. .should.be.fulfilled;
  62. });
  63. it('announces a RoleAdded event on addRole', async () => {
  64. expectEvent.inTransaction(
  65. mock.adminAddRole(futureAdvisor, ROLE_ADVISOR, { from: admin }),
  66. 'RoleAdded'
  67. );
  68. });
  69. it('announces a RoleRemoved event on removeRole', async () => {
  70. expectEvent.inTransaction(
  71. mock.adminRemoveRole(futureAdvisor, ROLE_ADVISOR, { from: admin }),
  72. 'RoleRemoved'
  73. );
  74. });
  75. });
  76. context('in adversarial conditions', () => {
  77. it('does not allow an advisor to remove another advisor', async () => {
  78. expectThrow(
  79. mock.removeAdvisor(advisors[1], { from: advisors[0] })
  80. );
  81. });
  82. it('does not allow "anyone" to remove an advisor', async () => {
  83. expectThrow(
  84. mock.removeAdvisor(advisors[0], { from: anyone })
  85. );
  86. });
  87. });
  88. });