Ownable.test.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const { constants, expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expectRevertCustomError } = require('../helpers/customError');
  3. const { ZERO_ADDRESS } = constants;
  4. const { expect } = require('chai');
  5. const Ownable = artifacts.require('$Ownable');
  6. contract('Ownable', function (accounts) {
  7. const [owner, other] = accounts;
  8. beforeEach(async function () {
  9. this.ownable = await Ownable.new(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 expectRevertCustomError(
  22. this.ownable.transferOwnership(other, { from: other }),
  23. 'OwnableUnauthorizedAccount',
  24. [other],
  25. );
  26. });
  27. it('guards ownership against stuck state', async function () {
  28. await expectRevertCustomError(
  29. this.ownable.transferOwnership(ZERO_ADDRESS, { from: owner }),
  30. 'OwnableInvalidOwner',
  31. [ZERO_ADDRESS],
  32. );
  33. });
  34. });
  35. describe('renounce ownership', function () {
  36. it('loses ownership after renouncement', async function () {
  37. const receipt = await this.ownable.renounceOwnership({ from: owner });
  38. expectEvent(receipt, 'OwnershipTransferred');
  39. expect(await this.ownable.owner()).to.equal(ZERO_ADDRESS);
  40. });
  41. it('prevents non-owners from renouncement', async function () {
  42. await expectRevertCustomError(this.ownable.renounceOwnership({ from: other }), 'OwnableUnauthorizedAccount', [
  43. other,
  44. ]);
  45. });
  46. it('allows to recover access using the internal _transferOwnership', async function () {
  47. await this.ownable.renounceOwnership({ from: owner });
  48. const receipt = await this.ownable.$_transferOwnership(other);
  49. expectEvent(receipt, 'OwnershipTransferred');
  50. expect(await this.ownable.owner()).to.equal(other);
  51. });
  52. });
  53. });