Ownable.test.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const Ownable = artifacts.require('OwnableMock');
  5. contract('Ownable', function (accounts) {
  6. const [ owner, other ] = accounts;
  7. beforeEach(async function () {
  8. this.ownable = await Ownable.new({ from: owner });
  9. });
  10. it('has an owner', async function () {
  11. expect(await this.ownable.owner()).to.equal(owner);
  12. });
  13. describe('transfer ownership', function () {
  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('prevents 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('guards 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. });
  32. describe('renounce ownership', function () {
  33. it('loses owner after renouncement', async function () {
  34. const receipt = await this.ownable.renounceOwnership({ from: owner });
  35. expectEvent(receipt, 'OwnershipTransferred');
  36. expect(await this.ownable.owner()).to.equal(ZERO_ADDRESS);
  37. });
  38. it('prevents non-owners from renouncement', async function () {
  39. await expectRevert(
  40. this.ownable.renounceOwnership({ from: other }),
  41. 'Ownable: caller is not the owner',
  42. );
  43. });
  44. });
  45. });