Bounty.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. var sendReward = function(sender, receiver, value){
  2. web3.eth.sendTransaction({
  3. from:sender,
  4. to:receiver,
  5. value: value
  6. })
  7. }
  8. contract('Bounty', function(accounts) {
  9. it("creates bounty contract with factory address", function(done){
  10. var target = SecureTargetMock.deployed();
  11. Bounty.new(target.address).
  12. then(function(bounty){
  13. return bounty.factoryAddress.call()
  14. }).
  15. then(function(address){
  16. assert.equal(address, target.address)
  17. }).
  18. then(done);
  19. })
  20. it("sets reward", function(done){
  21. var target = SecureTargetMock.deployed();
  22. var owner = accounts[0];
  23. var reward = web3.toWei(1, "ether");
  24. Bounty.new(target.address).
  25. then(function(bounty){
  26. sendReward(owner, bounty.address, reward);
  27. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  28. }).
  29. then(done);
  30. })
  31. it("ends", function(done){
  32. var target = SecureTargetMock.deployed();
  33. var owner = accounts[0];
  34. var reward = web3.toWei(1, "ether");
  35. var bounty;
  36. Bounty.new(target.address).
  37. then(function(_bounty){
  38. bounty = _bounty;
  39. sendReward(owner, bounty.address, reward);
  40. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  41. return bounty.kill()
  42. }).
  43. then(function(){
  44. assert.equal(0, web3.eth.getBalance(bounty.address).toNumber())
  45. }).
  46. then(done);
  47. })
  48. describe("SecureTargetMock", function(){
  49. it("checkInvariant returns true", function(done){
  50. var targetFactory = SecureTargetFactory.deployed();
  51. var bounty;
  52. Bounty.new(targetFactory.address).
  53. then(function(_bounty) {
  54. bounty = _bounty;
  55. return bounty.createTarget();
  56. }).
  57. then(function() {
  58. return bounty.checkInvariant.call()
  59. }).
  60. then(function(result) {
  61. assert.isTrue(result);
  62. }).
  63. then(done);
  64. })
  65. it("cannot calim reward", function(done){
  66. var targetFactory = SecureTargetFactory.deployed();
  67. var owner = accounts[0];
  68. var researcher = accounts[1];
  69. var reward = web3.toWei(1, "ether");
  70. Bounty.new(targetFactory.address).
  71. then(function(bounty) {
  72. var event = bounty.TargetCreated({});
  73. event.watch(function(err, result) {
  74. event.stopWatching();
  75. if (err) { throw err }
  76. var targetAddress = result.args.createdAddress;
  77. sendReward(owner, bounty.address, reward);
  78. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  79. bounty.claim(targetAddress, {from:researcher}).
  80. then(function(){ throw("should not come here")}).
  81. catch(function() {
  82. return bounty.claimed.call();
  83. }).
  84. then(function(result) {
  85. assert.isFalse(result);
  86. bounty.withdrawPayments({from:researcher}).
  87. then(function(){ throw("should not come here")}).
  88. catch(function() {
  89. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  90. done();
  91. })
  92. })
  93. })
  94. bounty.createTarget({from:researcher});
  95. })
  96. })
  97. })
  98. describe("InsecureTargetMock", function(){
  99. it("checkInvariant returns false", function(done){
  100. var targetFactory = InsecureTargetFactory.deployed();
  101. var bounty;
  102. Bounty.new(targetFactory.address).
  103. then(function(_bounty) {
  104. bounty = _bounty;
  105. return bounty.createTarget();
  106. }).
  107. then(function() {
  108. return bounty.checkInvariant.call()
  109. }).
  110. then(function(result) {
  111. assert.isFalse(result);
  112. }).
  113. then(done);
  114. })
  115. it("calims reward", function(done){
  116. var targetFactory = InsecureTargetFactory.deployed();
  117. var owner = accounts[0];
  118. var researcher = accounts[1];
  119. var reward = web3.toWei(1, "ether");
  120. Bounty.new(targetFactory.address).
  121. then(function(bounty) {
  122. var event = bounty.TargetCreated({});
  123. event.watch(function(err, result) {
  124. event.stopWatching();
  125. if (err) { throw err }
  126. var targetAddress = result.args.createdAddress;
  127. sendReward(owner, bounty.address, reward);
  128. assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber())
  129. bounty.claim(targetAddress, {from:researcher}).
  130. then(function() {
  131. return bounty.claimed.call();
  132. }).
  133. then(function(result) {
  134. assert.isTrue(result);
  135. return bounty.withdrawPayments({from:researcher})
  136. }).
  137. then(function() {
  138. assert.equal(0, web3.eth.getBalance(bounty.address).toNumber())
  139. }).then(done);
  140. })
  141. bounty.createTarget({from:researcher});
  142. })
  143. })
  144. })
  145. });