Ownable.behavior.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const { constants, expectEvent, expectRevert } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. function shouldBehaveLikeOwnable (owner, [other]) {
  4. describe('as an ownable', function () {
  5. it('should have an owner', async function () {
  6. (await this.ownable.owner()).should.equal(owner);
  7. });
  8. it('changes owner after transfer', async function () {
  9. (await this.ownable.isOwner({ from: other })).should.be.equal(false);
  10. const { logs } = await this.ownable.transferOwnership(other, { from: owner });
  11. expectEvent.inLogs(logs, 'OwnershipTransferred');
  12. (await this.ownable.owner()).should.equal(other);
  13. (await this.ownable.isOwner({ from: other })).should.be.equal(true);
  14. });
  15. it('should prevent non-owners from transferring', async function () {
  16. await expectRevert(
  17. this.ownable.transferOwnership(other, { from: other }),
  18. 'Ownable: caller is not the owner'
  19. );
  20. });
  21. it('should guard ownership against stuck state', async function () {
  22. await expectRevert(
  23. this.ownable.transferOwnership(ZERO_ADDRESS, { from: owner }),
  24. 'Ownable: new owner is the zero address'
  25. );
  26. });
  27. it('loses owner after renouncement', async function () {
  28. const { logs } = await this.ownable.renounceOwnership({ from: owner });
  29. expectEvent.inLogs(logs, 'OwnershipTransferred');
  30. (await this.ownable.owner()).should.equal(ZERO_ADDRESS);
  31. });
  32. it('should prevent non-owners from renouncement', async function () {
  33. await expectRevert(
  34. this.ownable.renounceOwnership({ from: other }),
  35. 'Ownable: caller is not the owner'
  36. );
  37. });
  38. });
  39. }
  40. module.exports = {
  41. shouldBehaveLikeOwnable,
  42. };