PausableToken.test.js 8.6 KB

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