RefundEscrow.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const { balance, constants, ether, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const RefundEscrow = artifacts.require('RefundEscrow');
  4. contract('RefundEscrow', function ([_, primary, beneficiary, refundee1, refundee2]) {
  5. const amount = ether('54');
  6. const refundees = [refundee1, refundee2];
  7. it('requires a non-null beneficiary', async function () {
  8. await shouldFail.reverting(
  9. RefundEscrow.new(ZERO_ADDRESS, { from: primary })
  10. );
  11. });
  12. context('once deployed', function () {
  13. beforeEach(async function () {
  14. this.escrow = await RefundEscrow.new(beneficiary, { from: primary });
  15. });
  16. context('active state', function () {
  17. it('has beneficiary and state', async function () {
  18. (await this.escrow.beneficiary()).should.be.equal(beneficiary);
  19. (await this.escrow.state()).should.be.bignumber.equal('0');
  20. });
  21. it('accepts deposits', async function () {
  22. await this.escrow.deposit(refundee1, { from: primary, value: amount });
  23. (await this.escrow.depositsOf(refundee1)).should.be.bignumber.equal(amount);
  24. });
  25. it('does not refund refundees', async function () {
  26. await this.escrow.deposit(refundee1, { from: primary, value: amount });
  27. await shouldFail.reverting(this.escrow.withdraw(refundee1));
  28. });
  29. it('does not allow beneficiary withdrawal', async function () {
  30. await this.escrow.deposit(refundee1, { from: primary, value: amount });
  31. await shouldFail.reverting(this.escrow.beneficiaryWithdraw());
  32. });
  33. });
  34. it('only the primary account can enter closed state', async function () {
  35. await shouldFail.reverting(this.escrow.close({ from: beneficiary }));
  36. const { logs } = await this.escrow.close({ from: primary });
  37. expectEvent.inLogs(logs, 'RefundsClosed');
  38. });
  39. context('closed state', function () {
  40. beforeEach(async function () {
  41. await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: primary, value: amount })));
  42. await this.escrow.close({ from: primary });
  43. });
  44. it('rejects deposits', async function () {
  45. await shouldFail.reverting(this.escrow.deposit(refundee1, { from: primary, value: amount }));
  46. });
  47. it('does not refund refundees', async function () {
  48. await shouldFail.reverting(this.escrow.withdraw(refundee1));
  49. });
  50. it('allows beneficiary withdrawal', async function () {
  51. (await balance.difference(beneficiary, () =>
  52. this.escrow.beneficiaryWithdraw()
  53. )).should.be.bignumber.equal(amount.muln(refundees.length));
  54. });
  55. it('prevents entering the refund state', async function () {
  56. await shouldFail.reverting(this.escrow.enableRefunds({ from: primary }));
  57. });
  58. it('prevents re-entering the closed state', async function () {
  59. await shouldFail.reverting(this.escrow.close({ from: primary }));
  60. });
  61. });
  62. it('only the primary account can enter refund state', async function () {
  63. await shouldFail.reverting(this.escrow.enableRefunds({ from: beneficiary }));
  64. const { logs } = await this.escrow.enableRefunds({ from: primary });
  65. expectEvent.inLogs(logs, 'RefundsEnabled');
  66. });
  67. context('refund state', function () {
  68. beforeEach(async function () {
  69. await Promise.all(refundees.map(refundee => this.escrow.deposit(refundee, { from: primary, value: amount })));
  70. await this.escrow.enableRefunds({ from: primary });
  71. });
  72. it('rejects deposits', async function () {
  73. await shouldFail.reverting(this.escrow.deposit(refundee1, { from: primary, value: amount }));
  74. });
  75. it('refunds refundees', async function () {
  76. for (const refundee of [refundee1, refundee2]) {
  77. (await balance.difference(refundee, () =>
  78. this.escrow.withdraw(refundee, { from: primary }))
  79. ).should.be.bignumber.equal(amount);
  80. }
  81. });
  82. it('does not allow beneficiary withdrawal', async function () {
  83. await shouldFail.reverting(this.escrow.beneficiaryWithdraw());
  84. });
  85. it('prevents entering the closed state', async function () {
  86. await shouldFail.reverting(this.escrow.close({ from: primary }));
  87. });
  88. it('prevents re-entering the refund state', async function () {
  89. await shouldFail.reverting(this.escrow.enableRefunds({ from: primary }));
  90. });
  91. });
  92. });
  93. });