RBAC.test.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 ([_, admin, anyone, advisor, otherAdvisor, futureAdvisor]) {
  8. let mock;
  9. beforeEach(async () => {
  10. mock = await RBACMock.new([advisor, otherAdvisor], { from: admin });
  11. });
  12. context('in normal conditions', () => {
  13. it('allows admin to call #onlyAdminsCanDoThis', async () => {
  14. await mock.onlyAdminsCanDoThis({ from: admin });
  15. });
  16. it('allows admin to call #onlyAdvisorsCanDoThis', async () => {
  17. await mock.onlyAdvisorsCanDoThis({ from: admin });
  18. });
  19. it('allows advisors to call #onlyAdvisorsCanDoThis', async () => {
  20. await mock.onlyAdvisorsCanDoThis({ from: advisor });
  21. });
  22. it('allows admin to call #eitherAdminOrAdvisorCanDoThis', async () => {
  23. await mock.eitherAdminOrAdvisorCanDoThis({ from: admin });
  24. });
  25. it('allows advisors to call #eitherAdminOrAdvisorCanDoThis', async () => {
  26. await mock.eitherAdminOrAdvisorCanDoThis({ from: advisor });
  27. });
  28. it('does not allow admins to call #nobodyCanDoThis', async () => {
  29. await expectThrow(mock.nobodyCanDoThis({ from: admin }));
  30. });
  31. it('does not allow advisors to call #nobodyCanDoThis', async () => {
  32. await expectThrow(mock.nobodyCanDoThis({ from: advisor }));
  33. });
  34. it('does not allow anyone to call #nobodyCanDoThis', async () => {
  35. await expectThrow(mock.nobodyCanDoThis({ from: anyone }));
  36. });
  37. it('allows an admin to remove an advisor\'s role', async () => {
  38. await mock.removeAdvisor(advisor, { from: admin });
  39. });
  40. it('allows admins to #adminRemoveRole', async () => {
  41. await mock.adminRemoveRole(advisor, ROLE_ADVISOR, { from: admin });
  42. });
  43. it('announces a RoleAdded event on addRole', async () => {
  44. await expectEvent.inTransaction(
  45. mock.adminAddRole(futureAdvisor, ROLE_ADVISOR, { from: admin }),
  46. 'RoleAdded'
  47. );
  48. });
  49. it('announces a RoleRemoved event on removeRole', async () => {
  50. await expectEvent.inTransaction(
  51. mock.adminRemoveRole(futureAdvisor, ROLE_ADVISOR, { from: admin }),
  52. 'RoleRemoved'
  53. );
  54. });
  55. });
  56. context('in adversarial conditions', () => {
  57. it('does not allow an advisor to remove another advisor', async () => {
  58. await expectThrow(mock.removeAdvisor(otherAdvisor, { from: advisor }));
  59. });
  60. it('does not allow "anyone" to remove an advisor', async () => {
  61. await expectThrow(mock.removeAdvisor(advisor, { from: anyone }));
  62. });
  63. });
  64. });