Ownable.test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. async function fixture() {
  5. const [owner, other] = await ethers.getSigners();
  6. const ownable = await ethers.deployContract('$Ownable', [owner]);
  7. return { owner, other, ownable };
  8. }
  9. describe('Ownable', function () {
  10. beforeEach(async function () {
  11. Object.assign(this, await loadFixture(fixture));
  12. });
  13. it('rejects zero address for initialOwner', async function () {
  14. await expect(ethers.deployContract('$Ownable', [ethers.ZeroAddress]))
  15. .to.be.revertedWithCustomError({ interface: this.ownable.interface }, 'OwnableInvalidOwner')
  16. .withArgs(ethers.ZeroAddress);
  17. });
  18. it('has an owner', async function () {
  19. expect(await this.ownable.owner()).to.equal(this.owner.address);
  20. });
  21. describe('transfer ownership', function () {
  22. it('changes owner after transfer', async function () {
  23. await expect(this.ownable.connect(this.owner).transferOwnership(this.other))
  24. .to.emit(this.ownable, 'OwnershipTransferred')
  25. .withArgs(this.owner.address, this.other.address);
  26. expect(await this.ownable.owner()).to.equal(this.other.address);
  27. });
  28. it('prevents non-owners from transferring', async function () {
  29. await expect(this.ownable.connect(this.other).transferOwnership(this.other))
  30. .to.be.revertedWithCustomError(this.ownable, 'OwnableUnauthorizedAccount')
  31. .withArgs(this.other.address);
  32. });
  33. it('guards ownership against stuck state', async function () {
  34. await expect(this.ownable.connect(this.owner).transferOwnership(ethers.ZeroAddress))
  35. .to.be.revertedWithCustomError(this.ownable, 'OwnableInvalidOwner')
  36. .withArgs(ethers.ZeroAddress);
  37. });
  38. });
  39. describe('renounce ownership', function () {
  40. it('loses ownership after renouncement', async function () {
  41. await expect(this.ownable.connect(this.owner).renounceOwnership())
  42. .to.emit(this.ownable, 'OwnershipTransferred')
  43. .withArgs(this.owner.address, ethers.ZeroAddress);
  44. expect(await this.ownable.owner()).to.equal(ethers.ZeroAddress);
  45. });
  46. it('prevents non-owners from renouncement', async function () {
  47. await expect(this.ownable.connect(this.other).renounceOwnership())
  48. .to.be.revertedWithCustomError(this.ownable, 'OwnableUnauthorizedAccount')
  49. .withArgs(this.other.address);
  50. });
  51. it('allows to recover access using the internal _transferOwnership', async function () {
  52. await this.ownable.connect(this.owner).renounceOwnership();
  53. await expect(this.ownable.$_transferOwnership(this.other))
  54. .to.emit(this.ownable, 'OwnershipTransferred')
  55. .withArgs(ethers.ZeroAddress, this.other.address);
  56. expect(await this.ownable.owner()).to.equal(this.other.address);
  57. });
  58. });
  59. });