// Test originally from https://github.com/PeterBorah/smart-contract-security-examples/blob/master/test/bounty.js
contract('Bounty', function(accounts) {
  it("should not pay out the bounty if you don't find a bug", function(done) {
    var bounty = Bounty.deployed();
    var errorEvent = bounty.Error({});
    errorEvent.watch(function(err, result) {
      console.log('Error', result.args)
      errorEvent.stopWatching();
    })
    var event = bounty.TargetCreation({});
    event.watch(function(err, result) {
      event.stopWatching();
      if (err) { throw err }

      var target = Conference.at(result.args.createdAddress);
      // call some function of the target.
      target.register('username', {value: web3.toWei('1', 'ether')}).
        then(function() { return bounty.claim(target.address)}).
        then(function() { return bounty.claimed() }).
        then(function(result) {
          assert.equal(result, false);
        }).
        then(function() { return bounty.totalBounty() }).
        then(function(result) {
          assert.equal(result, 10000);
          done();
        }).catch(done);
    });

    bounty.contribute({value: 10000}).
      then(function() { return bounty.createTarget() }).
      catch(done);
  });

  it("should pay out the bounty if you find the deposit bug", function(done) {
    function sendTransaction(fromAddress, toAddress, ether){
      return new Promise(function(resolve,reject){
        web3.eth.sendTransaction({
          from:fromAddress,
          to:toAddress,
          value: web3.toWei(ether, "ether")
        },function(err, result){
          resolve(result)
        })
      });
    }

    var bounty = Bounty.deployed();
    var errorEvent = bounty.Error({});
    errorEvent.watch(function(err, result) {
      console.log('Error', result.args)
      errorEvent.stopWatching();
    })

    var event = bounty.TargetCreation({});
    event.watch(function(err, result) {
      event.stopWatching();
      if (err) { throw err }

      var target = Conference.at(result.args.createdAddress);
      // Security bug!! It should not allow users to throw randowm Ether at you.
      sendTransaction(accounts[0], target.address, 10).then(function(){
        return target.register('username', {value: web3.toWei('1', 'ether')})
      }).
        then(function() { return bounty.claim(target.address)}).
        then(function() { return bounty.claimed() }).
        then(function(result) {
          assert.equal(result, true);
        }).
        then(function() { return bounty.totalBounty() }).
        then(function(result) {
          assert.equal(result, 0);
          errorEvent.stopWatching();
          done();
        }).catch(done);
    });

    bounty.contribute({value: 10000}).
      then(function() { return bounty.createTarget() }).
      catch(done);

  });
});

@@ -1246,24 +1246,29 @@
 %0A  %7D);%0A%0A  it
+.skip
 (%22should pay
@@ -1334,338 +1334,8 @@
 ) %7B%0A
-    function sendTransaction(fromAddress, toAddress, ether)%7B%0A      return new Promise(function(resolve,reject)%7B%0A        web3.eth.sendTransaction(%7B%0A          from:fromAddress,%0A          to:toAddress,%0A          value: web3.toWei(ether, %22ether%22)%0A        %7D,function(err, result)%7B%0A          resolve(result)%0A        %7D)%0A      %7D);%0A    %7D%0A%0A
     
@@ -1747,166 +1747,48 @@
  // 
-Security bug!! It should not allow users to throw randowm Ether at you.%0A      sendTransaction(accounts%5B0%5D, target.address, 10).then(function()%7B%0A        return
+Do something to cause security bug%0A     
  tar
@@ -1850,17 +1850,8 @@
 ')%7D)
-%0A      %7D)
 .%0A  

disable successful claim spec