Ownable.test.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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('emits ownership transfer events during construction', async function () {
  14. await expect(this.ownable.deploymentTransaction())
  15. .to.emit(this.ownable, 'OwnershipTransferred')
  16. .withArgs(ethers.ZeroAddress, this.owner);
  17. });
  18. it('rejects zero address for initialOwner', async function () {
  19. await expect(ethers.deployContract('$Ownable', [ethers.ZeroAddress]))
  20. .to.be.revertedWithCustomError({ interface: this.ownable.interface }, 'OwnableInvalidOwner')
  21. .withArgs(ethers.ZeroAddress);
  22. });
  23. it('has an owner', async function () {
  24. expect(await this.ownable.owner()).to.equal(this.owner);
  25. });
  26. describe('transfer ownership', function () {
  27. it('changes owner after transfer', async function () {
  28. await expect(this.ownable.connect(this.owner).transferOwnership(this.other))
  29. .to.emit(this.ownable, 'OwnershipTransferred')
  30. .withArgs(this.owner, this.other);
  31. expect(await this.ownable.owner()).to.equal(this.other);
  32. });
  33. it('prevents non-owners from transferring', async function () {
  34. await expect(this.ownable.connect(this.other).transferOwnership(this.other))
  35. .to.be.revertedWithCustomError(this.ownable, 'OwnableUnauthorizedAccount')
  36. .withArgs(this.other);
  37. });
  38. it('guards ownership against stuck state', async function () {
  39. await expect(this.ownable.connect(this.owner).transferOwnership(ethers.ZeroAddress))
  40. .to.be.revertedWithCustomError(this.ownable, 'OwnableInvalidOwner')
  41. .withArgs(ethers.ZeroAddress);
  42. });
  43. });
  44. describe('renounce ownership', function () {
  45. it('loses ownership after renouncement', async function () {
  46. await expect(this.ownable.connect(this.owner).renounceOwnership())
  47. .to.emit(this.ownable, 'OwnershipTransferred')
  48. .withArgs(this.owner, ethers.ZeroAddress);
  49. expect(await this.ownable.owner()).to.equal(ethers.ZeroAddress);
  50. });
  51. it('prevents non-owners from renouncement', async function () {
  52. await expect(this.ownable.connect(this.other).renounceOwnership())
  53. .to.be.revertedWithCustomError(this.ownable, 'OwnableUnauthorizedAccount')
  54. .withArgs(this.other);
  55. });
  56. it('allows to recover access using the internal _transferOwnership', async function () {
  57. await this.ownable.connect(this.owner).renounceOwnership();
  58. await expect(this.ownable.$_transferOwnership(this.other))
  59. .to.emit(this.ownable, 'OwnershipTransferred')
  60. .withArgs(ethers.ZeroAddress, this.other);
  61. expect(await this.ownable.owner()).to.equal(this.other);
  62. });
  63. });
  64. });