Pausable.test.js 4.0 KB

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