Pausable.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { shouldBehaveLikePublicRole } = require('../behaviors/access/roles/PublicRole.behavior');
  4. const { expect } = require('chai');
  5. const PausableMock = contract.fromArtifact('PausableMock');
  6. describe('Pausable', function () {
  7. const [ pauser, otherPauser, other, ...otherAccounts ] = accounts;
  8. beforeEach(async function () {
  9. this.pausable = await PausableMock.new({ from: pauser });
  10. });
  11. describe('pauser role', function () {
  12. beforeEach(async function () {
  13. this.contract = this.pausable;
  14. await this.contract.addPauser(otherPauser, { from: pauser });
  15. });
  16. shouldBehaveLikePublicRole(pauser, otherPauser, otherAccounts, 'pauser');
  17. });
  18. context('when unpaused', function () {
  19. beforeEach(async function () {
  20. expect(await this.pausable.paused()).to.equal(false);
  21. });
  22. it('can perform normal process in non-pause', async function () {
  23. expect(await this.pausable.count()).to.be.bignumber.equal('0');
  24. await this.pausable.normalProcess({ from: other });
  25. expect(await this.pausable.count()).to.be.bignumber.equal('1');
  26. });
  27. it('cannot take drastic measure in non-pause', async function () {
  28. await expectRevert(this.pausable.drasticMeasure({ from: other }),
  29. 'Pausable: not paused'
  30. );
  31. expect(await this.pausable.drasticMeasureTaken()).to.equal(false);
  32. });
  33. describe('pausing', function () {
  34. it('is pausable by the pauser', async function () {
  35. await this.pausable.pause({ from: pauser });
  36. expect(await this.pausable.paused()).to.equal(true);
  37. });
  38. it('reverts when pausing from non-pauser', async function () {
  39. await expectRevert(this.pausable.pause({ from: other }),
  40. 'PauserRole: caller does not have the Pauser role'
  41. );
  42. });
  43. context('when paused', function () {
  44. beforeEach(async function () {
  45. ({ logs: this.logs } = await this.pausable.pause({ from: pauser }));
  46. });
  47. it('emits a Paused event', function () {
  48. expectEvent.inLogs(this.logs, 'Paused', { account: pauser });
  49. });
  50. it('cannot perform normal process in pause', async function () {
  51. await expectRevert(this.pausable.normalProcess({ from: other }), 'Pausable: paused');
  52. });
  53. it('can take a drastic measure in a pause', async function () {
  54. await this.pausable.drasticMeasure({ from: other });
  55. expect(await this.pausable.drasticMeasureTaken()).to.equal(true);
  56. });
  57. it('reverts when re-pausing', async function () {
  58. await expectRevert(this.pausable.pause({ from: pauser }), 'Pausable: paused');
  59. });
  60. describe('unpausing', function () {
  61. it('is unpausable by the pauser', async function () {
  62. await this.pausable.unpause({ from: pauser });
  63. expect(await this.pausable.paused()).to.equal(false);
  64. });
  65. it('reverts when unpausing from non-pauser', async function () {
  66. await expectRevert(this.pausable.unpause({ from: other }),
  67. 'PauserRole: caller does not have the Pauser role'
  68. );
  69. });
  70. context('when unpaused', function () {
  71. beforeEach(async function () {
  72. ({ logs: this.logs } = await this.pausable.unpause({ from: pauser }));
  73. });
  74. it('emits an Unpaused event', function () {
  75. expectEvent.inLogs(this.logs, 'Unpaused', { account: pauser });
  76. });
  77. it('should resume allowing normal process', async function () {
  78. expect(await this.pausable.count()).to.be.bignumber.equal('0');
  79. await this.pausable.normalProcess({ from: other });
  80. expect(await this.pausable.count()).to.be.bignumber.equal('1');
  81. });
  82. it('should prevent drastic measure', async function () {
  83. await expectRevert(this.pausable.drasticMeasure({ from: other }),
  84. 'Pausable: not paused'
  85. );
  86. });
  87. it('reverts when re-unpausing', async function () {
  88. await expectRevert(this.pausable.unpause({ from: pauser }), 'Pausable: not paused');
  89. });
  90. });
  91. });
  92. });
  93. });
  94. });
  95. });