Superuser.test.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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', function () {
  12. it('should set the owner as the default superuser', async function () {
  13. (await this.superuser.isSuperuser(firstOwner)).should.be.be.true;
  14. });
  15. it('should change superuser after transferring', async function () {
  16. await this.superuser.transferSuperuser(newSuperuser, { from: firstOwner });
  17. (await this.superuser.isSuperuser(firstOwner)).should.be.be.false;
  18. (await this.superuser.isSuperuser(newSuperuser)).should.be.be.true;
  19. });
  20. it('should prevent changing to a null superuser', async function () {
  21. await expectThrow(
  22. this.superuser.transferSuperuser(ZERO_ADDRESS, { from: firstOwner })
  23. );
  24. });
  25. it('should change owner after the superuser transfers the ownership', async function () {
  26. await this.superuser.transferSuperuser(newSuperuser, { from: firstOwner });
  27. await expectEvent.inTransaction(
  28. this.superuser.transferOwnership(newOwner, { from: newSuperuser }),
  29. 'OwnershipTransferred'
  30. );
  31. (await this.superuser.owner()).should.equal(newOwner);
  32. });
  33. it('should change owner after the owner transfers the ownership', async function () {
  34. await expectEvent.inTransaction(
  35. this.superuser.transferOwnership(newOwner, { from: firstOwner }),
  36. 'OwnershipTransferred'
  37. );
  38. (await this.superuser.owner()).should.equal(newOwner);
  39. });
  40. });
  41. context('in adversarial conditions', function () {
  42. it('should prevent non-superusers from transfering the superuser role', async function () {
  43. await expectThrow(
  44. this.superuser.transferSuperuser(newOwner, { from: anyone })
  45. );
  46. });
  47. it('should prevent users that are not superuser nor owner from setting a new owner', async function () {
  48. await expectThrow(
  49. this.superuser.transferOwnership(newOwner, { from: anyone })
  50. );
  51. });
  52. });
  53. });