Pausable.test.js 4.2 KB

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