Ownable.behavior.js 1.7 KB

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