ERC20Pausable.test.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. const { BN, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
  2. const ERC20PausableMock = artifacts.require('ERC20PausableMock');
  3. const { shouldBehaveLikePublicRole } = require('../../behaviors/access/roles/PublicRole.behavior');
  4. contract('ERC20Pausable', function ([_, pauser, otherPauser, recipient, anotherAccount, ...otherAccounts]) {
  5. const initialSupply = new BN(100);
  6. beforeEach(async function () {
  7. this.token = await ERC20PausableMock.new(pauser, initialSupply, { from: pauser });
  8. });
  9. describe('pauser role', function () {
  10. beforeEach(async function () {
  11. this.contract = this.token;
  12. await this.contract.addPauser(otherPauser, { from: pauser });
  13. });
  14. shouldBehaveLikePublicRole(pauser, otherPauser, otherAccounts, 'pauser');
  15. });
  16. describe('pause', function () {
  17. describe('when the sender is the token pauser', function () {
  18. const from = pauser;
  19. describe('when the token is unpaused', function () {
  20. it('pauses the token', async function () {
  21. await this.token.pause({ from });
  22. (await this.token.paused()).should.equal(true);
  23. });
  24. it('emits a Pause event', async function () {
  25. const { logs } = await this.token.pause({ from });
  26. expectEvent.inLogs(logs, 'Paused');
  27. });
  28. });
  29. describe('when the token is paused', function () {
  30. beforeEach(async function () {
  31. await this.token.pause({ from });
  32. });
  33. it('reverts', async function () {
  34. await shouldFail.reverting.withMessage(this.token.pause({ from }), 'Pausable: paused');
  35. });
  36. });
  37. });
  38. describe('when the sender is not the token pauser', function () {
  39. const from = anotherAccount;
  40. it('reverts', async function () {
  41. await shouldFail.reverting.withMessage(this.token.pause({ from }),
  42. 'PauserRole: caller does not have the Pauser role'
  43. );
  44. });
  45. });
  46. });
  47. describe('unpause', function () {
  48. describe('when the sender is the token pauser', function () {
  49. const from = pauser;
  50. describe('when the token is paused', function () {
  51. beforeEach(async function () {
  52. await this.token.pause({ from });
  53. });
  54. it('unpauses the token', async function () {
  55. await this.token.unpause({ from });
  56. (await this.token.paused()).should.equal(false);
  57. });
  58. it('emits an Unpause event', async function () {
  59. const { logs } = await this.token.unpause({ from });
  60. expectEvent.inLogs(logs, 'Unpaused');
  61. });
  62. });
  63. describe('when the token is unpaused', function () {
  64. it('reverts', async function () {
  65. await shouldFail.reverting.withMessage(this.token.unpause({ from }), 'Pausable: not paused');
  66. });
  67. });
  68. });
  69. describe('when the sender is not the token pauser', function () {
  70. const from = anotherAccount;
  71. it('reverts', async function () {
  72. await shouldFail.reverting.withMessage(this.token.unpause({ from }),
  73. 'PauserRole: caller does not have the Pauser role'
  74. );
  75. });
  76. });
  77. });
  78. describe('pausable token', function () {
  79. const from = pauser;
  80. describe('paused', function () {
  81. it('is not paused by default', async function () {
  82. (await this.token.paused({ from })).should.equal(false);
  83. });
  84. it('is paused after being paused', async function () {
  85. await this.token.pause({ from });
  86. (await this.token.paused({ from })).should.equal(true);
  87. });
  88. it('is not paused after being paused and then unpaused', async function () {
  89. await this.token.pause({ from });
  90. await this.token.unpause({ from });
  91. (await this.token.paused()).should.equal(false);
  92. });
  93. });
  94. describe('transfer', function () {
  95. it('allows to transfer when unpaused', async function () {
  96. await this.token.transfer(recipient, initialSupply, { from: pauser });
  97. (await this.token.balanceOf(pauser)).should.be.bignumber.equal('0');
  98. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(initialSupply);
  99. });
  100. it('allows to transfer when paused and then unpaused', async function () {
  101. await this.token.pause({ from: pauser });
  102. await this.token.unpause({ from: pauser });
  103. await this.token.transfer(recipient, initialSupply, { from: pauser });
  104. (await this.token.balanceOf(pauser)).should.be.bignumber.equal('0');
  105. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(initialSupply);
  106. });
  107. it('reverts when trying to transfer when paused', async function () {
  108. await this.token.pause({ from: pauser });
  109. await shouldFail.reverting.withMessage(this.token.transfer(recipient, initialSupply, { from: pauser }),
  110. 'Pausable: paused'
  111. );
  112. });
  113. });
  114. describe('approve', function () {
  115. const allowance = new BN(40);
  116. it('allows to approve when unpaused', async function () {
  117. await this.token.approve(anotherAccount, allowance, { from: pauser });
  118. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(allowance);
  119. });
  120. it('allows to approve when paused and then unpaused', async function () {
  121. await this.token.pause({ from: pauser });
  122. await this.token.unpause({ from: pauser });
  123. await this.token.approve(anotherAccount, allowance, { from: pauser });
  124. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(allowance);
  125. });
  126. it('reverts when trying to approve when paused', async function () {
  127. await this.token.pause({ from: pauser });
  128. await shouldFail.reverting.withMessage(this.token.approve(anotherAccount, allowance, { from: pauser }),
  129. 'Pausable: paused'
  130. );
  131. });
  132. });
  133. describe('transfer from', function () {
  134. const allowance = new BN(40);
  135. beforeEach(async function () {
  136. await this.token.approve(anotherAccount, allowance, { from: pauser });
  137. });
  138. it('allows to transfer from when unpaused', async function () {
  139. await this.token.transferFrom(pauser, recipient, allowance, { from: anotherAccount });
  140. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(allowance);
  141. (await this.token.balanceOf(pauser)).should.be.bignumber.equal(initialSupply.sub(allowance));
  142. });
  143. it('allows to transfer when paused and then unpaused', async function () {
  144. await this.token.pause({ from: pauser });
  145. await this.token.unpause({ from: pauser });
  146. await this.token.transferFrom(pauser, recipient, allowance, { from: anotherAccount });
  147. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(allowance);
  148. (await this.token.balanceOf(pauser)).should.be.bignumber.equal(initialSupply.sub(allowance));
  149. });
  150. it('reverts when trying to transfer from when paused', async function () {
  151. await this.token.pause({ from: pauser });
  152. await shouldFail.reverting.withMessage(this.token.transferFrom(
  153. pauser, recipient, allowance, { from: anotherAccount }), 'Pausable: paused'
  154. );
  155. });
  156. });
  157. describe('decrease approval', function () {
  158. const allowance = new BN(40);
  159. const decrement = new BN(10);
  160. beforeEach(async function () {
  161. await this.token.approve(anotherAccount, allowance, { from: pauser });
  162. });
  163. it('allows to decrease approval when unpaused', async function () {
  164. await this.token.decreaseAllowance(anotherAccount, decrement, { from: pauser });
  165. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(allowance.sub(decrement));
  166. });
  167. it('allows to decrease approval when paused and then unpaused', async function () {
  168. await this.token.pause({ from: pauser });
  169. await this.token.unpause({ from: pauser });
  170. await this.token.decreaseAllowance(anotherAccount, decrement, { from: pauser });
  171. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(allowance.sub(decrement));
  172. });
  173. it('reverts when trying to transfer when paused', async function () {
  174. await this.token.pause({ from: pauser });
  175. await shouldFail.reverting.withMessage(this.token.decreaseAllowance(
  176. anotherAccount, decrement, { from: pauser }), 'Pausable: paused'
  177. );
  178. });
  179. });
  180. describe('increase approval', function () {
  181. const allowance = new BN(40);
  182. const increment = new BN(30);
  183. beforeEach(async function () {
  184. await this.token.approve(anotherAccount, allowance, { from: pauser });
  185. });
  186. it('allows to increase approval when unpaused', async function () {
  187. await this.token.increaseAllowance(anotherAccount, increment, { from: pauser });
  188. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(allowance.add(increment));
  189. });
  190. it('allows to increase approval when paused and then unpaused', async function () {
  191. await this.token.pause({ from: pauser });
  192. await this.token.unpause({ from: pauser });
  193. await this.token.increaseAllowance(anotherAccount, increment, { from: pauser });
  194. (await this.token.allowance(pauser, anotherAccount)).should.be.bignumber.equal(allowance.add(increment));
  195. });
  196. it('reverts when trying to increase approval when paused', async function () {
  197. await this.token.pause({ from: pauser });
  198. await shouldFail.reverting.withMessage(this.token.increaseAllowance(
  199. anotherAccount, increment, { from: pauser }), 'Pausable: paused'
  200. );
  201. });
  202. });
  203. });
  204. });