ERC20Pausable.test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { BN, expectRevert } = require('@openzeppelin/test-helpers');
  3. const { expect } = require('chai');
  4. const ERC20PausableMock = contract.fromArtifact('ERC20PausableMock');
  5. describe('ERC20Pausable', function () {
  6. const [ holder, recipient, anotherAccount ] = accounts;
  7. const initialSupply = new BN(100);
  8. const name = 'My Token';
  9. const symbol = 'MTKN';
  10. beforeEach(async function () {
  11. this.token = await ERC20PausableMock.new(name, symbol, holder, initialSupply);
  12. });
  13. describe('pausable token', function () {
  14. describe('transfer', function () {
  15. it('allows to transfer when unpaused', async function () {
  16. await this.token.transfer(recipient, initialSupply, { from: holder });
  17. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('0');
  18. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(initialSupply);
  19. });
  20. it('allows to transfer when paused and then unpaused', async function () {
  21. await this.token.pause();
  22. await this.token.unpause();
  23. await this.token.transfer(recipient, initialSupply, { from: holder });
  24. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal('0');
  25. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(initialSupply);
  26. });
  27. it('reverts when trying to transfer when paused', async function () {
  28. await this.token.pause();
  29. await expectRevert(this.token.transfer(recipient, initialSupply, { from: holder }),
  30. 'ERC20Pausable: token transfer while paused'
  31. );
  32. });
  33. });
  34. describe('transfer from', function () {
  35. const allowance = new BN(40);
  36. beforeEach(async function () {
  37. await this.token.approve(anotherAccount, allowance, { from: holder });
  38. });
  39. it('allows to transfer from when unpaused', async function () {
  40. await this.token.transferFrom(holder, recipient, allowance, { from: anotherAccount });
  41. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(allowance);
  42. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(allowance));
  43. });
  44. it('allows to transfer when paused and then unpaused', async function () {
  45. await this.token.pause();
  46. await this.token.unpause();
  47. await this.token.transferFrom(holder, recipient, allowance, { from: anotherAccount });
  48. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(allowance);
  49. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(allowance));
  50. });
  51. it('reverts when trying to transfer from when paused', async function () {
  52. await this.token.pause();
  53. await expectRevert(this.token.transferFrom(
  54. holder, recipient, allowance, { from: anotherAccount }), 'ERC20Pausable: token transfer while paused'
  55. );
  56. });
  57. });
  58. describe('mint', function () {
  59. const amount = new BN('42');
  60. it('allows to mint when unpaused', async function () {
  61. await this.token.mint(recipient, amount);
  62. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  63. });
  64. it('allows to mint when paused and then unpaused', async function () {
  65. await this.token.pause();
  66. await this.token.unpause();
  67. await this.token.mint(recipient, amount);
  68. expect(await this.token.balanceOf(recipient)).to.be.bignumber.equal(amount);
  69. });
  70. it('reverts when trying to mint when paused', async function () {
  71. await this.token.pause();
  72. await expectRevert(this.token.mint(recipient, amount),
  73. 'ERC20Pausable: token transfer while paused'
  74. );
  75. });
  76. });
  77. describe('burn', function () {
  78. const amount = new BN('42');
  79. it('allows to burn when unpaused', async function () {
  80. await this.token.burn(holder, amount);
  81. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(amount));
  82. });
  83. it('allows to burn when paused and then unpaused', async function () {
  84. await this.token.pause();
  85. await this.token.unpause();
  86. await this.token.burn(holder, amount);
  87. expect(await this.token.balanceOf(holder)).to.be.bignumber.equal(initialSupply.sub(amount));
  88. });
  89. it('reverts when trying to burn when paused', async function () {
  90. await this.token.pause();
  91. await expectRevert(this.token.burn(holder, amount),
  92. 'ERC20Pausable: token transfer while paused'
  93. );
  94. });
  95. });
  96. });
  97. });