Pausable.test.js 3.0 KB

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