Skip to content

Commit

Permalink
Apps: Tests enhancements (aragon#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
facuspagnuolo authored and sohkai committed Apr 18, 2019
1 parent 7da0a0c commit 001e08f
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 356 deletions.
48 changes: 14 additions & 34 deletions apps/agent/test/agent_shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { hash: namehash } = require('eth-ens-namehash')
const ethUtil = require('ethereumjs-util')
const ethABI = new (require('web3-eth-abi').AbiCoder)()
const { assertRevert } = require('@aragon/test-helpers/assertThrow')
const { encodeCallScript, EMPTY_SCRIPT } = require('@aragon/test-helpers/evmScript')
const { encodeCallScript } = require('@aragon/test-helpers/evmScript')
const assertEvent = require('@aragon/test-helpers/assertEvent')
const getEvent = (receipt, event, arg) => { return receipt.logs.filter(l => l.event == event)[0].args[arg] }

Expand All @@ -15,29 +15,23 @@ module.exports = (
web3
}
) => {
const getBalance = require('@aragon/test-helpers/balance')(web3)
const web3Call = require('@aragon/test-helpers/call')(web3)
const web3Sign = require('@aragon/test-helpers/sign')(web3)
const getBalance = require('@aragon/test-helpers/balance')(web3)

const ACL = artifacts.require('ACL')
const AppProxyUpgradeable = artifacts.require('AppProxyUpgradeable')
const EVMScriptRegistryFactory = artifacts.require('EVMScriptRegistryFactory')
const DAOFactory = artifacts.require('DAOFactory')
const Kernel = artifacts.require('Kernel')
const KernelProxy = artifacts.require('KernelProxy')

const EtherTokenConstantMock = artifacts.require('EtherTokenConstantMock')
const DestinationMock = artifacts.require('DestinationMock')
const KernelDepositableMock = artifacts.require('KernelDepositableMock')
const DAOFactory = artifacts.require('DAOFactory')
const EVMScriptRegistryFactory = artifacts.require('EVMScriptRegistryFactory')

const ExecutionTarget = artifacts.require('ExecutionTarget')
const DesignatedSigner = artifacts.require('DesignatedSigner')
const DestinationMock = artifacts.require('DestinationMock')
const EtherTokenConstantMock = artifacts.require('EtherTokenConstantMock')

const NULL_ADDRESS = '0x00'
const NO_SIG = '0x'

const ERC165_SUPPORT_INTERFACE_ID = '0x01ffc9a7'
const ERC165_SUPPORT_INVALID_ID = '0xffffffff'
const ERC165_SUPPORT_INTERFACE_ID = '0x01ffc9a7'

const AgentLike = artifacts.require(agentName)

Expand Down Expand Up @@ -181,17 +175,13 @@ module.exports = (
it('fails to execute without permissions', async () => {
const data = executionTarget.contract.execute.getData()

await assertRevert(() =>
agent.execute(executionTarget.address, depositAmount, data, { from: nonExecutor })
)
await assertRevert(agent.execute(executionTarget.address, depositAmount, data, { from: nonExecutor }))
})

it('fails to execute actions with more ETH than the agent owns', async () => {
const data = executionTarget.contract.execute.getData()

await assertRevert(() =>
agent.execute(executionTarget.address, depositAmount + 1, data, { from: executor })
)
await assertRevert(agent.execute(executionTarget.address, depositAmount + 1, data, { from: executor }))
})

it('execution forwards success return data', async () => {
Expand All @@ -211,9 +201,7 @@ module.exports = (
// ganache currently doesn't support fetching this data

const data = executionTarget.contract.fail.getData()
await assertRevert(() =>
agent.execute(executionTarget.address, depositAmount, data, { from: executor })
)
await assertRevert(agent.execute(executionTarget.address, depositAmount, data, { from: executor }))
})

context('depending on the sig ACL param', () => {
Expand Down Expand Up @@ -258,18 +246,14 @@ module.exports = (
it('equal: fails to execute if signature doesn\'t match', async () => {
const data = executionTarget.contract.execute.getData()

await assertRevert(() =>
agent.execute(executionTarget.address, depositAmount, data, { from: granteeEqualToSig })
)
await assertRevert(agent.execute(executionTarget.address, depositAmount, data, { from: granteeEqualToSig }))
})

it('not equal: fails to execute if the signature matches', async () => {
const N = 1102

const data = executionTarget.contract.setCounter.getData(N)
await assertRevert(() =>
agent.execute(executionTarget.address, depositAmount, data, { from: granteeUnequalToSig })
)
await assertRevert(agent.execute(executionTarget.address, depositAmount, data, { from: granteeUnequalToSig }))
})
})
})
Expand Down Expand Up @@ -304,9 +288,7 @@ module.exports = (
assert.isFalse(await agent.canForward(nonScriptRunner, script))
assert.equal(await executionTarget.counter(), 0)

await assertRevert(() =>
agent.forward(script, { from: nonScriptRunner })
)
await assertRevert(agent.forward(script, { from: nonScriptRunner }))
assert.equal(await executionTarget.counter(), 0)
})
})
Expand Down Expand Up @@ -365,9 +347,7 @@ module.exports = (
})

it('fails to presign a hash if not authorized', async () => {
await assertRevert(() =>
agent.presignHash(HASH, { from: nobody })
)
await assertRevert(agent.presignHash(HASH, { from: nobody }))
assertIsValidSignature(false, await agent.isValidSignature(HASH, NO_SIG))
})

Expand Down
48 changes: 22 additions & 26 deletions apps/finance/test/finance.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const tokenTestGroups = [
},
]

contract('Finance App', accounts => {
contract('Finance App', ([root, owner, recipient]) => {
let daoFact, financeBase, finance, vaultBase, vault, token1, token2

let ETH, MAX_UINT64, ANY_ENTITY, APP_MANAGER_ROLE
Expand Down Expand Up @@ -73,10 +73,6 @@ contract('Finance App', accounts => {
FINANCE_VAULT_NOT_CONTRACT: 'FINANCE_VAULT_NOT_CONTRACT',
})

const root = accounts[0]
const recipient = accounts[1]

const n = '0x00'
const NOW = 1
const ONE_DAY = 60 * 60 * 24 // One day in seconds
const PERIOD_DURATION = ONE_DAY
Expand Down Expand Up @@ -162,11 +158,11 @@ contract('Finance App', accounts => {
await vault.initialize()

// Set up initial balances
token1 = await TokenMock.new(accounts[0], 10000 + VAULT_INITIAL_TOKEN1_BALANCE)
await token1.transfer(vault.address, VAULT_INITIAL_TOKEN1_BALANCE)
token2 = await TokenMock.new(accounts[0], 10000 + VAULT_INITIAL_TOKEN2_BALANCE)
await token2.transfer(vault.address, VAULT_INITIAL_TOKEN2_BALANCE)
await vault.deposit(ETH, VAULT_INITIAL_ETH_BALANCE, { value: VAULT_INITIAL_ETH_BALANCE, from: accounts[0] });
token1 = await TokenMock.new(owner, 10000 + VAULT_INITIAL_TOKEN1_BALANCE)
await token1.transfer(vault.address, VAULT_INITIAL_TOKEN1_BALANCE, { from: owner })
token2 = await TokenMock.new(owner, 10000 + VAULT_INITIAL_TOKEN2_BALANCE)
await token2.transfer(vault.address, VAULT_INITIAL_TOKEN2_BALANCE, { from: owner })
await vault.deposit(ETH, VAULT_INITIAL_ETH_BALANCE, { value: VAULT_INITIAL_ETH_BALANCE, from: owner });

await finance.initialize(vault.address, PERIOD_DURATION)
})
Expand Down Expand Up @@ -266,13 +262,13 @@ contract('Finance App', accounts => {

beforeEach(async () => {
// Set up a new token similar to token1's distribution
tokenInstance = await tokenContract.new(accounts[0], 10000 + VAULT_INITIAL_TOKEN1_BALANCE)
await tokenInstance.transfer(vault.address, VAULT_INITIAL_TOKEN1_BALANCE)
tokenInstance = await tokenContract.new(owner, 10000 + VAULT_INITIAL_TOKEN1_BALANCE)
await tokenInstance.transfer(vault.address, VAULT_INITIAL_TOKEN1_BALANCE, { from: owner })
})

it('records deposits', async () => {
await tokenInstance.approve(finance.address, transferAmount)
const receipt = await finance.deposit(tokenInstance.address, transferAmount, 'ref')
await tokenInstance.approve(finance.address, transferAmount, { from: owner })
const receipt = await finance.deposit(tokenInstance.address, transferAmount, 'ref', { from: owner })

// vault has 100 tokens initially
assert.equal((await tokenInstance.balanceOf(vault.address)).valueOf(), VAULT_INITIAL_TOKEN1_BALANCE + transferAmount, 'deposited tokens must be in vault')
Expand All @@ -283,7 +279,7 @@ contract('Finance App', accounts => {
assert.equal(paymentId, 0, 'payment id should be 0')
assert.equal(paymentExecutionNumber, 0, 'payment execution number should be 0')
assert.equal(token, tokenInstance.address, 'token should be correct')
assert.equal(entity, accounts[0], 'entity should be correct')
assert.equal(entity, owner, 'entity should be correct')
assert.isTrue(incoming, 'tx should be incoming')
assert.equal(date, 1, 'date should be correct')
assert.equal(getEventData(receipt, 'NewTransaction', 'reference'), 'ref', 'ref should be correct')
Expand All @@ -300,7 +296,7 @@ contract('Finance App', accounts => {
const sentWei = 10

it('records deposits using deposit function', async () => {
const receipt = await finance.deposit(ETH, sentWei, reference, { value: sentWei })
const receipt = await finance.deposit(ETH, sentWei, reference, { from: owner, value: sentWei })

const transactionId = receipt.logs.filter(log => log.event == 'NewTransaction')[0].args.transactionId

Expand All @@ -312,14 +308,14 @@ contract('Finance App', accounts => {
assert.equal(paymentId, 0, 'payment id should be 0')
assert.equal(paymentExecutionNumber, 0, 'payment execution number should be 0')
assert.equal(token, ETH, 'token should be ETH token')
assert.equal(entity, accounts[0], 'entity should be correct')
assert.equal(entity, owner, 'entity should be correct')
assert.isTrue(incoming, 'tx should be incoming')
assert.equal(date, 1, 'date should be correct')
assert.equal(getEventData(receipt, 'NewTransaction', 'reference'), reference, 'ref should be correct')
})

it('records ETH deposits using fallback', async () => {
const receipt = await finance.send(sentWei)
const receipt = await finance.sendTransaction({ from: owner, value: sentWei })
const transactionId = receipt.logs.filter(log => log.event == 'NewTransaction')[0].args.transactionId

const [periodId, amount, paymentId, paymentExecutionNumber, token, entity, incoming, date] = await finance.getTransaction(transactionId)
Expand All @@ -330,7 +326,7 @@ contract('Finance App', accounts => {
assert.equal(paymentId, 0, 'payment id should be 0')
assert.equal(paymentExecutionNumber, 0, 'payment execution number should be 0')
assert.equal(token, ETH, 'token should be ETH token')
assert.equal(entity, accounts[0], 'entity should be correct')
assert.equal(entity, owner, 'entity should be correct')
assert.isTrue(incoming, 'tx should be incoming')
assert.equal(date, 1, 'date should be correct')
assert.equal(getEventData(receipt, 'NewTransaction', 'reference'), 'Ether transfer to Finance app', 'ref should be correct')
Expand All @@ -348,11 +344,11 @@ contract('Finance App', accounts => {

beforeEach(async () => {
// Set up a new token similar to token1's distribution
tokenInstance = await tokenContract.new(accounts[0], 10000 + VAULT_INITIAL_TOKEN1_BALANCE + lockedTokenAmount)
await tokenInstance.transfer(vault.address, VAULT_INITIAL_TOKEN1_BALANCE)
tokenInstance = await tokenContract.new(owner, 10000 + VAULT_INITIAL_TOKEN1_BALANCE + lockedTokenAmount)
await tokenInstance.transfer(vault.address, VAULT_INITIAL_TOKEN1_BALANCE, { from: owner })

// 'lock' tokens
await tokenInstance.transfer(finance.address, lockedTokenAmount)
await tokenInstance.transfer(finance.address, lockedTokenAmount, { from: owner })
})

it('allow recoverability is disabled', async () => {
Expand Down Expand Up @@ -453,7 +449,7 @@ contract('Finance App', accounts => {
assert.equal(getEventData(receipt, 'NewPayment', 'reference'), 'ref', 'ref should match')
assert.isFalse(disabled, 'should be enabled')
assert.equal(executions, 1, 'should be on first execution')
assert.equal(createdBy, accounts[0], 'should have correct creator')
assert.equal(createdBy, root, 'should have correct creator')
})

it('fails trying to get payment out of bounds', async () => {
Expand Down Expand Up @@ -570,8 +566,8 @@ contract('Finance App', accounts => {

await finance.executePayment(1) // first create payment doesn't get an id because it is simple immediate tx

await token1.approve(finance.address, 5)
await finance.deposit(token1.address, 5, '')
await token1.approve(finance.address, 5, { from: owner })
await finance.deposit(token1.address, 5, '', { from: owner })
})

it('has correct token statements', async () => {
Expand Down Expand Up @@ -946,7 +942,7 @@ contract('Finance App', accounts => {

beforeEach(async () => {
// 'lock' tokens
await token1.transfer(nonInit.address, lockedTokenAmount)
await token1.transfer(nonInit.address, lockedTokenAmount, { from: owner })
})

it('allow recoverability is enabled', async () => {
Expand Down
Loading

0 comments on commit 001e08f

Please sign in to comment.