Pausable.test.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const { expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { expectRevertCustomError } = require('../helpers/customError');
  4. const PausableMock = artifacts.require('PausableMock');
  5. contract('Pausable', function (accounts) {
  6. const [pauser] = accounts;
  7. beforeEach(async function () {
  8. this.pausable = await PausableMock.new();
  9. });
  10. context('when unpaused', function () {
  11. beforeEach(async function () {
  12. expect(await this.pausable.paused()).to.equal(false);
  13. });
  14. it('can perform normal process in non-pause', async function () {
  15. expect(await this.pausable.count()).to.be.bignumber.equal('0');
  16. await this.pausable.normalProcess();
  17. expect(await this.pausable.count()).to.be.bignumber.equal('1');
  18. });
  19. it('cannot take drastic measure in non-pause', async function () {
  20. await expectRevertCustomError(this.pausable.drasticMeasure(), 'ExpectedPause', []);
  21. expect(await this.pausable.drasticMeasureTaken()).to.equal(false);
  22. });
  23. context('when paused', function () {
  24. beforeEach(async function () {
  25. this.receipt = await this.pausable.pause({ from: pauser });
  26. });
  27. it('emits a Paused event', function () {
  28. expectEvent(this.receipt, 'Paused', { account: pauser });
  29. });
  30. it('cannot perform normal process in pause', async function () {
  31. await expectRevertCustomError(this.pausable.normalProcess(), 'EnforcedPause', []);
  32. });
  33. it('can take a drastic measure in a pause', async function () {
  34. await this.pausable.drasticMeasure();
  35. expect(await this.pausable.drasticMeasureTaken()).to.equal(true);
  36. });
  37. it('reverts when re-pausing', async function () {
  38. await expectRevertCustomError(this.pausable.pause(), 'EnforcedPause', []);
  39. });
  40. describe('unpausing', function () {
  41. it('is unpausable by the pauser', async function () {
  42. await this.pausable.unpause();
  43. expect(await this.pausable.paused()).to.equal(false);
  44. });
  45. context('when unpaused', function () {
  46. beforeEach(async function () {
  47. this.receipt = await this.pausable.unpause({ from: pauser });
  48. });
  49. it('emits an Unpaused event', function () {
  50. expectEvent(this.receipt, 'Unpaused', { account: pauser });
  51. });
  52. it('should resume allowing normal process', async function () {
  53. expect(await this.pausable.count()).to.be.bignumber.equal('0');
  54. await this.pausable.normalProcess();
  55. expect(await this.pausable.count()).to.be.bignumber.equal('1');
  56. });
  57. it('should prevent drastic measure', async function () {
  58. await expectRevertCustomError(this.pausable.drasticMeasure(), 'ExpectedPause', []);
  59. });
  60. it('reverts when re-unpausing', async function () {
  61. await expectRevertCustomError(this.pausable.unpause(), 'ExpectedPause', []);
  62. });
  63. });
  64. });
  65. });
  66. });
  67. });