Ownable.behavior.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. const { constants, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. function shouldBehaveLikeOwnable (owner, [anyone]) {
  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: anyone })).should.be.equal(false);
  10. const { logs } = await this.ownable.transferOwnership(anyone, { from: owner });
  11. expectEvent.inLogs(logs, 'OwnershipTransferred');
  12. (await this.ownable.owner()).should.equal(anyone);
  13. (await this.ownable.isOwner({ from: anyone })).should.be.equal(true);
  14. });
  15. it('should prevent non-owners from transfering', async function () {
  16. await shouldFail.reverting(this.ownable.transferOwnership(anyone, { from: anyone }));
  17. });
  18. it('should guard ownership against stuck state', async function () {
  19. await shouldFail.reverting(this.ownable.transferOwnership(ZERO_ADDRESS, { from: owner }));
  20. });
  21. it('loses owner after renouncement', async function () {
  22. const { logs } = await this.ownable.renounceOwnership({ from: owner });
  23. expectEvent.inLogs(logs, 'OwnershipTransferred');
  24. (await this.ownable.owner()).should.equal(ZERO_ADDRESS);
  25. });
  26. it('should prevent non-owners from renouncement', async function () {
  27. await shouldFail.reverting(this.ownable.renounceOwnership({ from: anyone }));
  28. });
  29. });
  30. }
  31. module.exports = {
  32. shouldBehaveLikeOwnable,
  33. };