const truffleAssert = require("truffle-assertions");
const People = artifacts.require("People");
const oneEther = web3.utils.toWei("1", "ether");
contract("People", async (accounts) => {
let instance;
before(async () => {
instance = instance = await People.deployed();
});
it("Should not allow to delete person if not owner", async () => {
const owner = accounts[0];
const someoneElse = accounts[1];
await truffleAssert.reverts(
instance.deletePerson(owner, { from: someoneElse })
);
});
it("Should delete person and emit event", async () => {
const creator = accounts[2];
const owner = accounts[0];
let instance = await People.new();
const name = "Person ToBeDeleted";
await instance.createPerson(name, 10, 120, {
from: creator,
value: web3.utils.toWei("1", "ether"),
});
const result = await instance.deletePerson(creator, { from: owner });
truffleAssert.eventEmitted(result, "personDeleted", { name });
});
it("after creating a person, the contract balance should be increased by 1 ether", async () => {
let instance = await People.new();
const contractAddress = instance.address;
const initialBalance = await web3.eth.getBalance(contractAddress);
assert(initialBalance === "0", "Initial balance should be 0");
await instance.createPerson("Satoshi", 11, 170, {
value: web3.utils.toWei("1", "ether"),
});
const balanceAfterCreation = await web3.eth.getBalance(contractAddress);
assert(balanceAfterCreation === oneEther);
});
it("contract balance should be withdrawable by the owner", async () => {
let instance = await People.new();
const owner = accounts[0];
await instance.createPerson("Satoshi", 11, 170, {
value: web3.utils.toWei("1", "ether"),
});
const initialOwnerBalance = parseFloat(await web3.eth.getBalance(owner));
await truffleAssert.passes(instance.withdrawAll());
const ownerBalanceAfterWithdrawal = parseFloat(
await web3.eth.getBalance(owner)
);
assert(
ownerBalanceAfterWithdrawal > initialOwnerBalance,
"Owner balance should be greater than it was before withdrawing"
);
});
it("contract balance should not be withdrawable by others", async () => {
let instance = await People.new();
await instance.createPerson("Satoshi", 11, 170, {
value: web3.utils.toWei("1", "ether"),
});
await truffleAssert.reverts(instance.withdrawAll({ from: accounts[1] }));
});
it("after withdrawing all, the contract balance should be 0", async () => {
let instance = await People.new();
const contractAddress = instance.address;
await instance.createPerson("Satoshi", 11, 170, {
value: web3.utils.toWei("1", "ether"),
});
await instance.withdrawAll();
const contractBalance = await web3.eth.getBalance(contractAddress);
assert(
contractBalance === "0",
"Contract balance should be 0 after owner withdraws all"
);
});
it("balance stored as local variable matches balance on the blockchain", async () => {
let instance = await People.new();
const checkBalanceVarsAreSame = async () => {
const blockchainBalance = parseFloat(
await web3.eth.getBalance(instance.address)
);
const localBalance = (await instance.balance()).toNumber();
return blockchainBalance === localBalance;
};
const initialBalancesMatch = await checkBalanceVarsAreSame();
assert(initialBalancesMatch === true, "Initial balances should match");
await instance.createPerson("Satoshi", 11, 170, {
value: web3.utils.toWei("1", "ether"),
});
// checking balances matching here gives "Error: Number can only safely store up to 53 bits"
// await checkBalanceVarsAreSame();
await instance.withdrawAll();
const balancesMatchAfterWithdrawal = await checkBalanceVarsAreSame();
assert(
balancesMatchAfterWithdrawal === true,
"Balances should match after withdrawal"
);
});
});