Ownable.test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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('rejects zero address for initialOwner', async function () {
  12. await expectRevertCustomError(Ownable.new(constants.ZERO_ADDRESS), 'OwnableInvalidOwner', [constants.ZERO_ADDRESS]);
  13. });
  14. it('has an owner', async function () {
  15. expect(await this.ownable.owner()).to.equal(owner);
  16. });
  17. describe('transfer ownership', function () {
  18. it('changes owner after transfer', async function () {
  19. const receipt = await this.ownable.transferOwnership(other, { from: owner });
  20. expectEvent(receipt, 'OwnershipTransferred');
  21. expect(await this.ownable.owner()).to.equal(other);
  22. });
  23. it('prevents non-owners from transferring', async function () {
  24. await expectRevertCustomError(
  25. this.ownable.transferOwnership(other, { from: other }),
  26. 'OwnableUnauthorizedAccount',
  27. [other],
  28. );
  29. });
  30. it('guards ownership against stuck state', async function () {
  31. await expectRevertCustomError(
  32. this.ownable.transferOwnership(ZERO_ADDRESS, { from: owner }),
  33. 'OwnableInvalidOwner',
  34. [ZERO_ADDRESS],
  35. );
  36. });
  37. });
  38. describe('renounce ownership', function () {
  39. it('loses ownership after renouncement', async function () {
  40. const receipt = await this.ownable.renounceOwnership({ from: owner });
  41. expectEvent(receipt, 'OwnershipTransferred');
  42. expect(await this.ownable.owner()).to.equal(ZERO_ADDRESS);
  43. });
  44. it('prevents non-owners from renouncement', async function () {
  45. await expectRevertCustomError(this.ownable.renounceOwnership({ from: other }), 'OwnableUnauthorizedAccount', [
  46. other,
  47. ]);
  48. });
  49. it('allows to recover access using the internal _transferOwnership', async function () {
  50. await this.ownable.renounceOwnership({ from: owner });
  51. const receipt = await this.ownable.$_transferOwnership(other);
  52. expectEvent(receipt, 'OwnershipTransferred');
  53. expect(await this.ownable.owner()).to.equal(other);
  54. });
  55. });
  56. });