From 67add71a22fd76eea813079a893ed5d4d8f63cb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Tue, 23 Aug 2022 14:41:30 +0100 Subject: [PATCH] Add tests for Contribution#add() --- contracts/Contribution.sol | 5 ++-- test/Contribution.js | 54 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/contracts/Contribution.sol b/contracts/Contribution.sol index d275b39..d3745cf 100644 --- a/contracts/Contribution.sol +++ b/contracts/Contribution.sol @@ -169,8 +169,9 @@ contract Contribution is Initializable { } function add(uint32 amount, uint32 contributorId, bytes32 hashDigest, uint8 hashFunction, uint8 hashSize) public { - //require(canPerform(msg.sender, ADD_CONTRIBUTION_ROLE, new uint32[](0)), 'nope'); - require(balanceOf(msg.sender) > 0 || contributorContract.addressIsCore(msg.sender), 'must have kredits or core'); + // require(canPerform(msg.sender, ADD_CONTRIBUTION_ROLE, new uint32[](0)), 'nope'); + // TODO hubot neither has kredits nor a core account + require(balanceOf(msg.sender) > 0 || contributorContract.addressIsCore(msg.sender), 'requires kredits or core status'); uint32 contributionId = contributionsCount + 1; ContributionData storage c = contributions[contributionId]; c.exists = true; diff --git a/test/Contribution.js b/test/Contribution.js index ebcfe85..02de504 100644 --- a/test/Contribution.js +++ b/test/Contribution.js @@ -1,5 +1,6 @@ const { expect } = require("chai"); const { ethers, upgrades } = require("hardhat"); +const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs"); let owner, addr1, addr2, addr3, addr4, addr5, addr6, addr7; let Contribution, Contributor; @@ -14,7 +15,7 @@ describe("Contribution contract", async function () { } }); - describe("Deployment", function () { + describe("initialize()", function () { before(async function () { // const [owner] = await ethers.getSigners(); const contributionFactory = await ethers.getContractFactory("Contribution"); @@ -35,7 +36,7 @@ describe("Contribution contract", async function () { }); }); - describe("Data migration", function () { + describe("finishMigration()", function () { before(async function () { const contributionFactory = await ethers.getContractFactory("Contribution"); Contribution = await upgrades.deployProxy(contributionFactory, [40321]); @@ -53,4 +54,53 @@ describe("Contribution contract", async function () { }); }); + describe("add()", function () { + before(async function () { + const contributionFactory = await ethers.getContractFactory("Contribution"); + Contribution = await upgrades.deployProxy(contributionFactory, [40321]); + await Contribution.setContributorContract(Contributor.address).then(res => res.wait()) + }); + + it("does not allow non-contributors to add a contribution", async function () { + await expect(Contribution.connect(addr7).add( + 500, 1, + "0xe794f010e617449719c64076546254129f63a6d16cf200031afa646aeb35777f", + 18, 32 + )).to.be.revertedWith("requires kredits or core status"); + expect(await Contribution.contributionsCount()).to.equal(0); + }); + + it("allows core contributors to add a contribution", async function () { + await Contribution.connect(addr2).add( + 2001, 1, + "0xe794f010e617449719c64076546254129f63a6d16cf200031afa646aeb35777f", + 18, 32 + ); + expect(await Contribution.contributionsCount()).to.equal(1); + }); + + it("allows contributors to add a contribution", async function () { + // Issue some kredits for new contributor #8 (addr7) + await Contribution.connect(addr1).add( + 5000, 8, + "0xe794f010e617449719c64076546254129f63a6d16cf200031afa646aeb35777f", + 18, 32 + ); + await Contribution.connect(addr7).add( + 1500, 1, + "0xe794f010e617449719c64076546254129f63a6d16cf200031afa646aeb35777f", + 18, 32 + ); + expect(await Contribution.contributionsCount()).to.equal(3); + }); + + it("emits a ContributionAdded event", async function () { + await expect(Contribution.connect(addr1).add( + 2001, 1, + "0xe794f010e617449719c64076546254129f63a6d16cf200031afa646aeb35777f", + 18, 32 + )).to.emit(Contribution, "ContributionAdded").withArgs(anyValue, 1, 2001); + }); + }); + });