Pausable.test.js 4.2 KB

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