Ownable.test.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const Ownable = artifacts.require('$Ownable');
  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(this.ownable.transferOwnership(other, { from: other }), 'Ownable: caller is not the owner');
  21. });
  22. it('guards 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. });
  29. describe('renounce ownership', function () {
  30. it('loses ownership after renouncement', async function () {
  31. const receipt = await this.ownable.renounceOwnership({ from: owner });
  32. expectEvent(receipt, 'OwnershipTransferred');
  33. expect(await this.ownable.owner()).to.equal(ZERO_ADDRESS);
  34. });
  35. it('prevents non-owners from renouncement', async function () {
  36. await expectRevert(this.ownable.renounceOwnership({ from: other }), 'Ownable: caller is not the owner');
  37. });
  38. it('allows to recover access using the internal _transferOwnership', async function () {
  39. await this.ownable.renounceOwnership({ from: owner });
  40. const receipt = await this.ownable.$_transferOwnership(other);
  41. expectEvent(receipt, 'OwnershipTransferred');
  42. expect(await this.ownable.owner()).to.equal(other);
  43. });
  44. });
  45. });