Ownable.test.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const { constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. const Ownable = artifacts.require('$Ownable');
  5. contract('Ownable', function (accounts) {
  6. const [owner, other] = accounts;
  7. beforeEach(async function () {
  8. this.ownable = await Ownable.new({ from: owner });
  9. });
  10. it('has an owner', async function () {
  11. expect(await this.ownable.owner()).to.equal(owner);
  12. });
  13. describe('transfer ownership', function () {
  14. it('changes owner after transfer', async function () {
  15. const receipt = await this.ownable.transferOwnership(other, { from: owner });
  16. expectEvent(receipt, 'OwnershipTransferred');
  17. expect(await this.ownable.owner()).to.equal(other);
  18. });
  19. it('prevents non-owners from transferring', async function () {
  20. await expectRevert(this.ownable.transferOwnership(other, { from: other }), 'Ownable: caller is not the owner');
  21. });
  22. it('guards ownership against stuck state', async function () {
  23. await expectRevert(
  24. this.ownable.transferOwnership(ZERO_ADDRESS, { from: owner }),
  25. 'Ownable: new owner is the zero address',
  26. );
  27. });
  28. });
  29. describe('renounce ownership', function () {
  30. it('loses owner after renouncement', async function () {
  31. const receipt = await this.ownable.renounceOwnership({ from: owner });
  32. expectEvent(receipt, 'OwnershipTransferred');
  33. expect(await this.ownable.owner()).to.equal(ZERO_ADDRESS);
  34. });
  35. it('prevents non-owners from renouncement', async function () {
  36. await expectRevert(this.ownable.renounceOwnership({ from: other }), 'Ownable: caller is not the owner');
  37. });
  38. });
  39. });