Pausable.test.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const PausableMock = contract.fromArtifact('PausableMock');
  5. describe('Pausable', function () {
  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 expectRevert(this.pausable.drasticMeasure(),
  21. 'Pausable: not paused'
  22. );
  23. expect(await this.pausable.drasticMeasureTaken()).to.equal(false);
  24. });
  25. context('when paused', function () {
  26. beforeEach(async function () {
  27. ({ logs: this.logs } = await this.pausable.pause({ from: pauser }));
  28. });
  29. it('emits a Paused event', function () {
  30. expectEvent.inLogs(this.logs, 'Paused', { account: pauser });
  31. });
  32. it('cannot perform normal process in pause', async function () {
  33. await expectRevert(this.pausable.normalProcess(), 'Pausable: paused');
  34. });
  35. it('can take a drastic measure in a pause', async function () {
  36. await this.pausable.drasticMeasure();
  37. expect(await this.pausable.drasticMeasureTaken()).to.equal(true);
  38. });
  39. it('reverts when re-pausing', async function () {
  40. await expectRevert(this.pausable.pause(), 'Pausable: paused');
  41. });
  42. describe('unpausing', function () {
  43. it('is unpausable by the pauser', async function () {
  44. await this.pausable.unpause();
  45. expect(await this.pausable.paused()).to.equal(false);
  46. });
  47. context('when unpaused', function () {
  48. beforeEach(async function () {
  49. ({ logs: this.logs } = await this.pausable.unpause({ from: pauser }));
  50. });
  51. it('emits an Unpaused event', function () {
  52. expectEvent.inLogs(this.logs, 'Unpaused', { account: pauser });
  53. });
  54. it('should resume allowing normal process', async function () {
  55. expect(await this.pausable.count()).to.be.bignumber.equal('0');
  56. await this.pausable.normalProcess();
  57. expect(await this.pausable.count()).to.be.bignumber.equal('1');
  58. });
  59. it('should prevent drastic measure', async function () {
  60. await expectRevert(this.pausable.drasticMeasure(),
  61. 'Pausable: not paused'
  62. );
  63. });
  64. it('reverts when re-unpausing', async function () {
  65. await expectRevert(this.pausable.unpause(), 'Pausable: not paused');
  66. });
  67. });
  68. });
  69. });
  70. });
  71. });