Superuser.test.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. const { expectThrow } = require('../helpers/expectThrow');
  2. const expectEvent = require('../helpers/expectEvent');
  3. const Superuser = artifacts.require('Superuser');
  4. require('chai')
  5. .should();
  6. contract('Superuser', function ([_, firstOwner, newSuperuser, newOwner, anyone]) {
  7. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  8. beforeEach(async function () {
  9. this.superuser = await Superuser.new({ from: firstOwner });
  10. });
  11. context('in normal conditions', () => {
  12. it('should set the owner as the default superuser', async function () {
  13. const ownerIsSuperuser = await this.superuser.isSuperuser(firstOwner);
  14. ownerIsSuperuser.should.be.equal(true);
  15. });
  16. it('should change superuser after transferring', async function () {
  17. await this.superuser.transferSuperuser(newSuperuser, { from: firstOwner });
  18. const ownerIsSuperuser = await this.superuser.isSuperuser(firstOwner);
  19. ownerIsSuperuser.should.be.equal(false);
  20. const newSuperuserIsSuperuser = await this.superuser.isSuperuser(newSuperuser);
  21. newSuperuserIsSuperuser.should.be.equal(true);
  22. });
  23. it('should prevent changing to a null superuser', async function () {
  24. await expectThrow(
  25. this.superuser.transferSuperuser(ZERO_ADDRESS, { from: firstOwner })
  26. );
  27. });
  28. it('should change owner after the superuser transfers the ownership', async function () {
  29. await this.superuser.transferSuperuser(newSuperuser, { from: firstOwner });
  30. await expectEvent.inTransaction(
  31. this.superuser.transferOwnership(newOwner, { from: newSuperuser }),
  32. 'OwnershipTransferred'
  33. );
  34. const currentOwner = await this.superuser.owner();
  35. currentOwner.should.be.equal(newOwner);
  36. });
  37. it('should change owner after the owner transfers the ownership', async function () {
  38. await expectEvent.inTransaction(
  39. this.superuser.transferOwnership(newOwner, { from: firstOwner }),
  40. 'OwnershipTransferred'
  41. );
  42. const currentOwner = await this.superuser.owner();
  43. currentOwner.should.be.equal(newOwner);
  44. });
  45. });
  46. context('in adversarial conditions', () => {
  47. it('should prevent non-superusers from transfering the superuser role', async function () {
  48. await expectThrow(
  49. this.superuser.transferSuperuser(newOwner, { from: anyone })
  50. );
  51. });
  52. it('should prevent users that are not superuser nor owner from setting a new owner', async function () {
  53. await expectThrow(
  54. this.superuser.transferOwnership(newOwner, { from: anyone })
  55. );
  56. });
  57. });
  58. });