Ownable.test.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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('has an owner', async function () {
  12. expect(await this.ownable.owner()).to.equal(owner);
  13. });
  14. describe('transfer ownership', function () {
  15. it('changes owner after transfer', async function () {
  16. const receipt = await this.ownable.transferOwnership(other, { from: owner });
  17. expectEvent(receipt, 'OwnershipTransferred');
  18. expect(await this.ownable.owner()).to.equal(other);
  19. });
  20. it('prevents non-owners from transferring', async function () {
  21. await expectRevert(
  22. this.ownable.transferOwnership(other, { from: other }),
  23. 'Ownable: caller is not the owner'
  24. );
  25. });
  26. it('guards ownership against stuck state', async function () {
  27. await expectRevert(
  28. this.ownable.transferOwnership(ZERO_ADDRESS, { from: owner }),
  29. 'Ownable: new owner is the zero address'
  30. );
  31. });
  32. });
  33. describe('renounce ownership', function () {
  34. it('loses owner after renouncement', async function () {
  35. const receipt = await this.ownable.renounceOwnership({ from: owner });
  36. expectEvent(receipt, 'OwnershipTransferred');
  37. expect(await this.ownable.owner()).to.equal(ZERO_ADDRESS);
  38. });
  39. it('prevents non-owners from renouncement', async function () {
  40. await expectRevert(
  41. this.ownable.renounceOwnership({ from: other }),
  42. 'Ownable: caller is not the owner'
  43. );
  44. });
  45. });
  46. });