Pausable.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. const BigNumber = web3.BigNumber;
  6. require('chai')
  7. .use(require('chai-bignumber')(BigNumber))
  8. .should();
  9. contract('Pausable', function ([_, pauser, otherPauser, anyone, ...otherAccounts]) {
  10. beforeEach(async function () {
  11. this.pausable = await PausableMock.new({ from: pauser });
  12. });
  13. describe('pauser role', function () {
  14. beforeEach(async function () {
  15. this.contract = this.pausable;
  16. await this.contract.addPauser(otherPauser, { from: pauser });
  17. });
  18. shouldBehaveLikePublicRole(pauser, otherPauser, otherAccounts, 'pauser');
  19. });
  20. context('when unapused', function () {
  21. beforeEach(async function () {
  22. (await this.pausable.paused()).should.equal(false);
  23. });
  24. it('can perform normal process in non-pause', async function () {
  25. (await this.pausable.count()).should.be.bignumber.equal(0);
  26. await this.pausable.normalProcess({ from: anyone });
  27. (await this.pausable.count()).should.be.bignumber.equal(1);
  28. });
  29. it('cannot take drastic measure in non-pause', async function () {
  30. await shouldFail.reverting(this.pausable.drasticMeasure({ from: anyone }));
  31. (await this.pausable.drasticMeasureTaken()).should.equal(false);
  32. });
  33. describe('pausing', function () {
  34. it('is pausable by the pauser', async function () {
  35. await this.pausable.pause({ from: pauser });
  36. (await this.pausable.paused()).should.equal(true);
  37. });
  38. it('reverts when pausing from non-pauser', async function () {
  39. await shouldFail.reverting(this.pausable.pause({ from: anyone }));
  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 shouldFail.reverting(this.pausable.normalProcess({ from: anyone }));
  50. });
  51. it('can take a drastic measure in a pause', async function () {
  52. await this.pausable.drasticMeasure({ from: anyone });
  53. (await this.pausable.drasticMeasureTaken()).should.equal(true);
  54. });
  55. it('reverts when re-pausing', async function () {
  56. await shouldFail.reverting(this.pausable.pause({ from: pauser }));
  57. });
  58. describe('unpausing', function () {
  59. it('is unpausable by the pauser', async function () {
  60. await this.pausable.unpause({ from: pauser });
  61. (await this.pausable.paused()).should.equal(false);
  62. });
  63. it('reverts when unpausing from non-pauser', async function () {
  64. await shouldFail.reverting(this.pausable.unpause({ from: anyone }));
  65. });
  66. context('when unpaused', function () {
  67. beforeEach(async function () {
  68. ({ logs: this.logs } = await this.pausable.unpause({ from: pauser }));
  69. });
  70. it('emits an Unpaused event', function () {
  71. expectEvent.inLogs(this.logs, 'Unpaused', { account: pauser });
  72. });
  73. it('should resume allowing normal process', async function () {
  74. (await this.pausable.count()).should.be.bignumber.equal(0);
  75. await this.pausable.normalProcess({ from: anyone });
  76. (await this.pausable.count()).should.be.bignumber.equal(1);
  77. });
  78. it('should prevent drastic measure', async function () {
  79. await shouldFail.reverting(this.pausable.drasticMeasure({ from: anyone }));
  80. });
  81. it('reverts when re-unpausing', async function () {
  82. await shouldFail.reverting(this.pausable.unpause({ from: pauser }));
  83. });
  84. });
  85. });
  86. });
  87. });
  88. });
  89. });