From 268353287a2fafceecb69b8082ca98198938f395 Mon Sep 17 00:00:00 2001 From: Haythem Sellami Date: Tue, 6 Aug 2019 11:32:44 +0100 Subject: [PATCH] fix claim contribution test --- apps/contribution/test/contribution.js | 113 +++++++++++++++++++------ 1 file changed, 88 insertions(+), 25 deletions(-) diff --git a/apps/contribution/test/contribution.js b/apps/contribution/test/contribution.js index 9501d22..92cfb35 100644 --- a/apps/contribution/test/contribution.js +++ b/apps/contribution/test/contribution.js @@ -10,14 +10,56 @@ const Token = artifacts.require("Token.sol"); // eslint-disable-next-line no-undef const getContract = name => artifacts.require(name); const { assertRevert } = require('@aragon/test-helpers/assertThrow'); +// eslint-disable-next-line no-undef +//const { timeTravel } = require('@aragon/test-helpers/timeTravel')(web3); const ZERO_ADDR = '0x0000000000000000000000000000000000000000'; +const timeTravel = function(time){ + return new Promise((resolve, reject) => { + web3.currentProvider.sendAsync({ + jsonrpc: "2.0", + method: "evm_increaseTime", + params: [time], //86400 is num seconds in day + id: new Date().getSeconds(), + }, (err, result) => { + if(err) { + return reject(err); + } + return resolve(result); + }); + }); +} + +const mineBlock = function() { + return new Promise((resolve, reject) => { + web3.currentProvider.sendAsync({ + jsonrpc: "2.0", + method: "evm_mine", + params: [], + id: new Date().getSeconds(), + }, (err, result) => { + if(err){ return reject(err); } + return resolve(result); + }); + }) +} + +const getBlockNumber = function() { + return new Promise((resolve, reject) => { + web3.eth.getBlockNumber(async (err, res) => { + if (err || !res) return reject(err); + resolve(res); + }); + }); +}; + contract('Contribution app', (accounts) => { let kernelBase, aclBase, daoFactory, r, dao, acl, contribution, token, contributor; const root = accounts[0]; const member1 = accounts[1]; + const blocksToWait = 40320; // eslint-disable-next-line no-undef before(async () => { @@ -163,7 +205,7 @@ contract('Contribution app', (accounts) => { }); }); - it("add contribution", async () => { + it("should add contribution", async () => { let contributionCountBefore = await contribution.contributionsCount(); await contribution.add(amount, contributorId, hashDigest, hashFunction, hashSize, {from: root}); let contributionCountAfter = await contribution.contributionsCount(); @@ -196,30 +238,6 @@ contract('Contribution app', (accounts) => { }); }); - it("should revert when claim contribution that does not exist", async () => { - let contributionId = await contribution.contributionsCount(); - return assertRevert(async () => { - await contribution.claim(contributionId.toNumber()+1, {from: root}); - 'contribution not found'; - }); - }); - - it("claim contribution", async () => { - let contributionId = await contribution.contributionsCount(); - await contribution.claim(contributionId); - let contributionObject = await contribution.getContribution(contributionId.toNumber()); - // eslint-disable-next-line no-undef - assert(contributionObject[3], true); - }); - - it("should revert when claim already claimed contribution", async () => { - let contributionId = await contribution.contributionsCount(); - return assertRevert(async () => { - await contribution.claim(contributionId.toNumber(), {from: root}); - 'contribution already claimed'; - }); - }); - it("should revert when veto already claimed contribution", async () => { let contributionId = await contribution.contributionsCount(); return assertRevert(async () => { @@ -228,4 +246,49 @@ contract('Contribution app', (accounts) => { }); }); }); + + describe("Claim contribution", async () => { + it("should revert when claim contribution that does not exist", async () => { + let contributionId = await contribution.contributionsCount(); + return assertRevert(async () => { + await contribution.claim(contributionId.toNumber()+1, {from: root}); + 'contribution not found'; + }); + }); + + it("should revert when claim contribution before confirmation block", async () => { + let contributionId = await contribution.contributionsCount(); + return assertRevert(async () => { + await contribution.claim(contributionId.toNumber(), {from: root}); + 'contribution not confirmed yet'; + }); + }); + + it("claim contribution", async () => { + let contributionId = await contribution.contributionsCount(); + + if(contributionId < 10) { + await timeTravel(100); + } + else { + await timeTravel(blocksToWait); + } + await mineBlock(); + + await contribution.claim(contributionId); + let contributionObject = await contribution.getContribution(contributionId.toNumber()); + // eslint-disable-next-line no-undef + assert(contributionObject[3], true); + }); + }); + + describe("Veto claimed contribution", async () => { + it("should revert when claim already claimed contribution", async () => { + let contributionId = await contribution.contributionsCount(); + return assertRevert(async () => { + await contribution.claim(contributionId.toNumber(), {from: root}); + 'contribution already claimed'; + }); + }); + }); });