Superuser.test.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import expectThrow from '../helpers/expectThrow';
  2. import expectEvent from '../helpers/expectEvent';
  3. const Superuser = artifacts.require('Superuser');
  4. require('chai')
  5. .use(require('chai-as-promised'))
  6. .should();
  7. contract('Superuser', function (accounts) {
  8. const [
  9. firstOwner,
  10. newSuperuser,
  11. newOwner,
  12. anyone,
  13. ] = accounts;
  14. before(async function () {
  15. this.superuser = await Superuser.new();
  16. });
  17. context('in normal conditions', () => {
  18. it('should set the owner as the default superuser', async function () {
  19. const ownerIsSuperuser = await this.superuser.isSuperuser(firstOwner);
  20. ownerIsSuperuser.should.be.equal(true);
  21. });
  22. it('should change superuser after transferring', async function () {
  23. await this.superuser.transferSuperuser(newSuperuser, { from: firstOwner });
  24. const ownerIsSuperuser = await this.superuser.isSuperuser(firstOwner);
  25. ownerIsSuperuser.should.be.equal(false);
  26. const address1IsSuperuser = await this.superuser.isSuperuser(newSuperuser);
  27. address1IsSuperuser.should.be.equal(true);
  28. });
  29. it('should change owner after transferring', async function () {
  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. });
  38. context('in adversarial conditions', () => {
  39. it('should prevent non-superusers from transfering the superuser role', async function () {
  40. await expectThrow(
  41. this.superuser.transferSuperuser(newOwner, { from: anyone })
  42. );
  43. });
  44. it('should prevent non-superusers from setting a new owner', async function () {
  45. await expectThrow(
  46. this.superuser.transferOwnership(newOwner, { from: anyone })
  47. );
  48. });
  49. });
  50. });