Ownable.test.js 1.7 KB

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