<commit_before>'use strict';

let sendReward = function(sender, receiver, value){
  web3.eth.sendTransaction({
    from:sender,
    to:receiver,
    value: value
  });
};
var SecureTargetBounty = artifacts.require('helpers/SecureTargetBounty.sol');
var InsecureTargetBounty = artifacts.require('helpers/InsecureTargetBounty.sol');

function awaitEvent(event, handler) {
  return new Promise((resolve, reject) => {
    function wrappedHandler(...args) {
      Promise.resolve(handler(...args)).then(resolve).catch(reject);
    }
  
    event.watch(wrappedHandler);
  });
}

contract('Bounty', function(accounts) {

  it('sets reward', async function() {
    let owner = accounts[0];
    let reward = web3.toWei(1, 'ether');
    let bounty = await SecureTargetBounty.new();
    sendReward(owner, bounty.address, reward);

    assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
  });

  it('empties itself when destroyed', async function(){
    let owner = accounts[0];
    let reward = web3.toWei(1, 'ether');
    let bounty = await SecureTargetBounty.new();
    sendReward(owner, bounty.address, reward);

    assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());

    await bounty.destroy();
    assert.equal(0, web3.eth.getBalance(bounty.address).toNumber());
  });

  describe('Against secure contract', function(){

    it('cannot claim reward', async function(){
      let owner = accounts[0];
      let researcher = accounts[1];
      let reward = web3.toWei(1, 'ether');
      let bounty = await SecureTargetBounty.new();
      let event = bounty.TargetCreated({});

      event.watch(async function(err, result) {
        event.stopWatching();
        if (err) { throw err; }

        var targetAddress = result.args.createdAddress;
        sendReward(owner, bounty.address, reward);

        assert.equal(reward,
          web3.eth.getBalance(bounty.address).toNumber());

        try {
          await bounty.claim(targetAddress, {from:researcher});
          assert.isTrue(false); // should never reach here
        } catch(error) {
            let reClaimedBounty = await bounty.claimed.call();
            assert.isFalse(reClaimedBounty);

        }
        try {
          await bounty.withdrawPayments({from:researcher});
          assert.isTrue(false); // should never reach here
        } catch (err) {
          assert.equal(reward,
            web3.eth.getBalance(bounty.address).toNumber());
        }
      });
      bounty.createTarget({from:researcher});
    });
  });

  describe('Against broken contract', function(){
    it('claims reward', async function() {
      let owner = accounts[0];
      let researcher = accounts[1];
      let reward = web3.toWei(1, 'ether');
      let bounty = await InsecureTargetBounty.new();
      let event = bounty.TargetCreated({});
      
      let watcher = async function(err, result) {
        event.stopWatching();
        if (err) { throw err; }
        let targetAddress = result.args.createdAddress;
        sendReward(owner, bounty.address, reward);
<commit_msg>await event handling result in Bounty test. Fixes #202<commit_after>'use strict';

let sendReward = function(sender, receiver, value){
  web3.eth.sendTransaction({
    from:sender,
    to:receiver,
    value: value
  });
};
var SecureTargetBounty = artifacts.require('helpers/SecureTargetBounty.sol');
var InsecureTargetBounty = artifacts.require('helpers/InsecureTargetBounty.sol');

function awaitEvent(event, handler) {
  return new Promise((resolve, reject) => {
    function wrappedHandler(...args) {
      Promise.resolve(handler(...args)).then(resolve).catch(reject);
    }
  
    event.watch(wrappedHandler);
  });
}

contract('Bounty', function(accounts) {

  it('sets reward', async function() {
    let owner = accounts[0];
    let reward = web3.toWei(1, 'ether');
    let bounty = await SecureTargetBounty.new();
    sendReward(owner, bounty.address, reward);

    assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());
  });

  it('empties itself when destroyed', async function(){
    let owner = accounts[0];
    let reward = web3.toWei(1, 'ether');
    let bounty = await SecureTargetBounty.new();
    sendReward(owner, bounty.address, reward);

    assert.equal(reward, web3.eth.getBalance(bounty.address).toNumber());

    await bounty.destroy();
    assert.equal(0, web3.eth.getBalance(bounty.address).toNumber());
  });

  describe('Against secure contract', function(){

    it('cannot claim reward', async function(){
      let owner = accounts[0];
      let researcher = accounts[1];
      let reward = web3.toWei(1, 'ether');
      let bounty = await SecureTargetBounty.new();
      let event = bounty.TargetCreated({});

      let watcher = async function(err, result) {
        event.stopWatching();
        if (err) { throw err; }

        var targetAddress = result.args.createdAddress;
        sendReward(owner, bounty.address, reward);

        assert.equal(reward,
          web3.eth.getBalance(bounty.address).toNumber());

        try {
          await bounty.claim(targetAddress, {from:researcher});
          assert.isTrue(false); // should never reach here
        } catch(error) {
          let reClaimedBounty = await bounty.claimed.call();
          assert.isFalse(reClaimedBounty);

        }
        try {
          await bounty.withdrawPayments({from:researcher});
          assert.isTrue(false); // should never reach here
        } catch (err) {
          assert.equal(reward,
            web3.eth.getBalance(bounty.address).toNumber());
        }
      };
      bounty.createTarget({from:researcher});
      await awaitEvent(event, watcher);
    });
  });

  describe('Against broken contract', function(){
    it('claims reward', async function() {
      let owner = accounts[0];
      let researcher = accounts[1];
      let reward = web3.toWei(1, 'ether');
      let bounty = await InsecureTargetBounty.new();
      let event = bounty.TargetCreated({});
      
      let watcher = async function(err, result) {
        event.stopWatching();
        if (err) { throw err; }
        let targetAddress = result.args.createdAddress;
        sendReward(owner, bounty.address, reward);

{'fraction_non_alphanumeric': 0.13756443298969073, 'fraction_numerical': 0.007248711340206186, 'mean_word_length': 3.0449511400651468, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1947204', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1819, 'n_words': 389}