RBAC.test.js 2.7 KB

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