Superuser.test.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. beforeEach(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 newSuperuserIsSuperuser = await this.superuser.isSuperuser(newSuperuser);
  27. newSuperuserIsSuperuser.should.be.equal(true);
  28. });
  29. it('should change owner after the superuser transfers the ownership', async function () {
  30. await this.superuser.transferSuperuser(newSuperuser, { from: firstOwner });
  31. await expectEvent.inTransaction(
  32. this.superuser.transferOwnership(newOwner, { from: newSuperuser }),
  33. 'OwnershipTransferred'
  34. );
  35. const currentOwner = await this.superuser.owner();
  36. currentOwner.should.be.equal(newOwner);
  37. });
  38. it('should change owner after the owner transfers the ownership', async function () {
  39. await expectEvent.inTransaction(
  40. this.superuser.transferOwnership(newOwner, { from: firstOwner }),
  41. 'OwnershipTransferred'
  42. );
  43. const currentOwner = await this.superuser.owner();
  44. currentOwner.should.be.equal(newOwner);
  45. });
  46. });
  47. context('in adversarial conditions', () => {
  48. it('should prevent non-superusers from transfering the superuser role', async function () {
  49. await expectThrow(
  50. this.superuser.transferSuperuser(newOwner, { from: anyone })
  51. );
  52. });
  53. it('should prevent users that are not superuser nor owner from setting a new owner', async function () {
  54. await expectThrow(
  55. this.superuser.transferOwnership(newOwner, { from: anyone })
  56. );
  57. });
  58. });
  59. });