RBAC.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. admin,
  10. anyone,
  11. ...advisors
  12. ] = accounts
  13. before(async () => {
  14. mock = await RBACMock.new(advisors, { from: admin })
  15. })
  16. context('in normal conditions', () => {
  17. it('allows admin to call #onlyAdminsCanDoThis', async () => {
  18. await mock.onlyAdminsCanDoThis({ from: admin })
  19. .should.be.fulfilled
  20. })
  21. it('allows admin to call #onlyAdvisorsCanDoThis', async () => {
  22. await mock.onlyAdvisorsCanDoThis({ from: admin })
  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 admin to call #eitherAdminOrAdvisorCanDoThis', async () => {
  30. await mock.eitherAdminOrAdvisorCanDoThis({ from: admin })
  31. .should.be.fulfilled
  32. })
  33. it('allows advisors to call #eitherAdminOrAdvisorCanDoThis', async () => {
  34. await mock.eitherAdminOrAdvisorCanDoThis({ from: advisors[0] })
  35. .should.be.fulfilled
  36. })
  37. it('does not allow admins to call #nobodyCanDoThis', async () => {
  38. expectThrow(
  39. mock.nobodyCanDoThis({ from: admin })
  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 admin to remove an advisor\'s role', async () => {
  53. await mock.removeAdvisor(advisors[0], { from: admin })
  54. .should.be.fulfilled
  55. })
  56. it('allows admins to #adminRemoveRole', async () => {
  57. await mock.adminRemoveRole(advisors[3], 'advisor', { from: admin })
  58. .should.be.fulfilled
  59. })
  60. })
  61. context('in adversarial conditions', () => {
  62. it('does not allow an advisor to remove another advisor', async () => {
  63. expectThrow(
  64. mock.removeAdvisor(advisors[1], { from: advisors[0] })
  65. )
  66. })
  67. it('does not allow "anyone" to remove an advisor', async () => {
  68. expectThrow(
  69. mock.removeAdvisor(advisors[0], { from: anyone })
  70. )
  71. })
  72. })
  73. })