Pausable.test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. const { expectEvent, shouldFail } = require('openzeppelin-test-helpers');
  2. const { shouldBehaveLikePublicRole } = require('../access/roles/PublicRole.behavior');
  3. const PausableMock = artifacts.require('PausableMock');
  4. contract('Pausable', function ([_, pauser, otherPauser, anyone, ...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 unapused', 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: anyone });
  22. (await this.pausable.count()).should.be.bignumber.equal('1');
  23. });
  24. it('cannot take drastic measure in non-pause', async function () {
  25. await shouldFail.reverting(this.pausable.drasticMeasure({ from: anyone }));
  26. (await this.pausable.drasticMeasureTaken()).should.equal(false);
  27. });
  28. describe('pausing', function () {
  29. it('is pausable by the pauser', async function () {
  30. await this.pausable.pause({ from: pauser });
  31. (await this.pausable.paused()).should.equal(true);
  32. });
  33. it('reverts when pausing from non-pauser', async function () {
  34. await shouldFail.reverting(this.pausable.pause({ from: anyone }));
  35. });
  36. context('when paused', function () {
  37. beforeEach(async function () {
  38. ({ logs: this.logs } = await this.pausable.pause({ from: pauser }));
  39. });
  40. it('emits a Paused event', function () {
  41. expectEvent.inLogs(this.logs, 'Paused', { account: pauser });
  42. });
  43. it('cannot perform normal process in pause', async function () {
  44. await shouldFail.reverting(this.pausable.normalProcess({ from: anyone }));
  45. });
  46. it('can take a drastic measure in a pause', async function () {
  47. await this.pausable.drasticMeasure({ from: anyone });
  48. (await this.pausable.drasticMeasureTaken()).should.equal(true);
  49. });
  50. it('reverts when re-pausing', async function () {
  51. await shouldFail.reverting(this.pausable.pause({ from: pauser }));
  52. });
  53. describe('unpausing', function () {
  54. it('is unpausable by the pauser', async function () {
  55. await this.pausable.unpause({ from: pauser });
  56. (await this.pausable.paused()).should.equal(false);
  57. });
  58. it('reverts when unpausing from non-pauser', async function () {
  59. await shouldFail.reverting(this.pausable.unpause({ from: anyone }));
  60. });
  61. context('when unpaused', function () {
  62. beforeEach(async function () {
  63. ({ logs: this.logs } = await this.pausable.unpause({ from: pauser }));
  64. });
  65. it('emits an Unpaused event', function () {
  66. expectEvent.inLogs(this.logs, 'Unpaused', { account: pauser });
  67. });
  68. it('should resume allowing normal process', async function () {
  69. (await this.pausable.count()).should.be.bignumber.equal('0');
  70. await this.pausable.normalProcess({ from: anyone });
  71. (await this.pausable.count()).should.be.bignumber.equal('1');
  72. });
  73. it('should prevent drastic measure', async function () {
  74. await shouldFail.reverting(this.pausable.drasticMeasure({ from: anyone }));
  75. });
  76. it('reverts when re-unpausing', async function () {
  77. await shouldFail.reverting(this.pausable.unpause({ from: pauser }));
  78. });
  79. });
  80. });
  81. });
  82. });
  83. });
  84. });