PausableToken.test.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. const { assertRevert } = require('../../helpers/assertRevert');
  2. const PausableToken = artifacts.require('PausableTokenMock');
  3. contract('PausableToken', function ([_, owner, recipient, anotherAccount]) {
  4. beforeEach(async function () {
  5. this.token = await PausableToken.new(owner, 100, { from: owner });
  6. });
  7. describe('pause', function () {
  8. describe('when the sender is the token owner', function () {
  9. const from = owner;
  10. describe('when the token is unpaused', function () {
  11. it('pauses the token', async function () {
  12. await this.token.pause({ from });
  13. (await this.token.paused()).should.equal(true);
  14. });
  15. it('emits a Pause event', async function () {
  16. const { logs } = await this.token.pause({ from });
  17. logs.length.should.equal(1);
  18. logs[0].event.should.equal('Pause');
  19. });
  20. });
  21. describe('when the token is paused', function () {
  22. beforeEach(async function () {
  23. await this.token.pause({ from });
  24. });
  25. it('reverts', async function () {
  26. await assertRevert(this.token.pause({ from }));
  27. });
  28. });
  29. });
  30. describe('when the sender is not the token owner', function () {
  31. const from = anotherAccount;
  32. it('reverts', async function () {
  33. await assertRevert(this.token.pause({ from }));
  34. });
  35. });
  36. });
  37. describe('unpause', function () {
  38. describe('when the sender is the token owner', function () {
  39. const from = owner;
  40. describe('when the token is paused', function () {
  41. beforeEach(async function () {
  42. await this.token.pause({ from });
  43. });
  44. it('unpauses the token', async function () {
  45. await this.token.unpause({ from });
  46. (await this.token.paused()).should.equal(false);
  47. });
  48. it('emits an Unpause event', async function () {
  49. const { logs } = await this.token.unpause({ from });
  50. logs.length.should.equal(1);
  51. logs[0].event.should.equal('Unpause');
  52. });
  53. });
  54. describe('when the token is unpaused', function () {
  55. it('reverts', async function () {
  56. await assertRevert(this.token.unpause({ from }));
  57. });
  58. });
  59. });
  60. describe('when the sender is not the token owner', function () {
  61. const from = anotherAccount;
  62. it('reverts', async function () {
  63. await assertRevert(this.token.unpause({ from }));
  64. });
  65. });
  66. });
  67. describe('pausable token', function () {
  68. const from = owner;
  69. describe('paused', function () {
  70. it('is not paused by default', async function () {
  71. (await this.token.paused({ from })).should.equal(false);
  72. });
  73. it('is paused after being paused', async function () {
  74. await this.token.pause({ from });
  75. (await this.token.paused({ from })).should.equal(true);
  76. });
  77. it('is not paused after being paused and then unpaused', async function () {
  78. await this.token.pause({ from });
  79. await this.token.unpause({ from });
  80. (await this.token.paused()).should.equal(false);
  81. });
  82. });
  83. describe('transfer', function () {
  84. it('allows to transfer when unpaused', async function () {
  85. await this.token.transfer(recipient, 100, { from: owner });
  86. (await this.token.balanceOf(owner)).should.be.bignumber.equal(0);
  87. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(100);
  88. });
  89. it('allows to transfer when paused and then unpaused', async function () {
  90. await this.token.pause({ from: owner });
  91. await this.token.unpause({ from: owner });
  92. await this.token.transfer(recipient, 100, { from: owner });
  93. (await this.token.balanceOf(owner)).should.be.bignumber.equal(0);
  94. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(100);
  95. });
  96. it('reverts when trying to transfer when paused', async function () {
  97. await this.token.pause({ from: owner });
  98. await assertRevert(this.token.transfer(recipient, 100, { from: owner }));
  99. });
  100. });
  101. describe('approve', function () {
  102. it('allows to approve when unpaused', async function () {
  103. await this.token.approve(anotherAccount, 40, { from: owner });
  104. (await this.token.allowance(owner, anotherAccount)).should.be.bignumber.equal(40);
  105. });
  106. it('allows to transfer when paused and then unpaused', async function () {
  107. await this.token.pause({ from: owner });
  108. await this.token.unpause({ from: owner });
  109. await this.token.approve(anotherAccount, 40, { from: owner });
  110. (await this.token.allowance(owner, anotherAccount)).should.be.bignumber.equal(40);
  111. });
  112. it('reverts when trying to transfer when paused', async function () {
  113. await this.token.pause({ from: owner });
  114. await assertRevert(this.token.approve(anotherAccount, 40, { from: owner }));
  115. });
  116. });
  117. describe('transfer from', function () {
  118. beforeEach(async function () {
  119. await this.token.approve(anotherAccount, 50, { from: owner });
  120. });
  121. it('allows to transfer from when unpaused', async function () {
  122. await this.token.transferFrom(owner, recipient, 40, { from: anotherAccount });
  123. (await this.token.balanceOf(owner)).should.be.bignumber.equal(60);
  124. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(40);
  125. });
  126. it('allows to transfer when paused and then unpaused', async function () {
  127. await this.token.pause({ from: owner });
  128. await this.token.unpause({ from: owner });
  129. await this.token.transferFrom(owner, recipient, 40, { from: anotherAccount });
  130. (await this.token.balanceOf(owner)).should.be.bignumber.equal(60);
  131. (await this.token.balanceOf(recipient)).should.be.bignumber.equal(40);
  132. });
  133. it('reverts when trying to transfer from when paused', async function () {
  134. await this.token.pause({ from: owner });
  135. await assertRevert(this.token.transferFrom(owner, recipient, 40, { from: anotherAccount }));
  136. });
  137. });
  138. describe('decrease approval', function () {
  139. beforeEach(async function () {
  140. await this.token.approve(anotherAccount, 100, { from: owner });
  141. });
  142. it('allows to decrease approval when unpaused', async function () {
  143. await this.token.decreaseApproval(anotherAccount, 40, { from: owner });
  144. (await this.token.allowance(owner, anotherAccount)).should.be.bignumber.equal(60);
  145. });
  146. it('allows to decrease approval when paused and then unpaused', async function () {
  147. await this.token.pause({ from: owner });
  148. await this.token.unpause({ from: owner });
  149. await this.token.decreaseApproval(anotherAccount, 40, { from: owner });
  150. (await this.token.allowance(owner, anotherAccount)).should.be.bignumber.equal(60);
  151. });
  152. it('reverts when trying to transfer when paused', async function () {
  153. await this.token.pause({ from: owner });
  154. await assertRevert(this.token.decreaseApproval(anotherAccount, 40, { from: owner }));
  155. });
  156. });
  157. describe('increase approval', function () {
  158. beforeEach(async function () {
  159. await this.token.approve(anotherAccount, 100, { from: owner });
  160. });
  161. it('allows to increase approval when unpaused', async function () {
  162. await this.token.increaseApproval(anotherAccount, 40, { from: owner });
  163. (await this.token.allowance(owner, anotherAccount)).should.be.bignumber.equal(140);
  164. });
  165. it('allows to increase approval when paused and then unpaused', async function () {
  166. await this.token.pause({ from: owner });
  167. await this.token.unpause({ from: owner });
  168. await this.token.increaseApproval(anotherAccount, 40, { from: owner });
  169. (await this.token.allowance(owner, anotherAccount)).should.be.bignumber.equal(140);
  170. });
  171. it('reverts when trying to increase approval when paused', async function () {
  172. await this.token.pause({ from: owner });
  173. await assertRevert(this.token.increaseApproval(anotherAccount, 40, { from: owner }));
  174. });
  175. });
  176. });
  177. });