WA-521 Test suite design for tx' integration and first test implentation

This commit is contained in:
apanizo 2018-08-13 12:22:55 +02:00
parent 654ae88cc7
commit 7ff49de9d2
2 changed files with 110 additions and 226 deletions

View File

@ -0,0 +1,110 @@
// @flow
import { List } from 'immutable'
import { getSafeEthereumInstance, createTransaction } from '~/wallets/createTransactions'
import { type Safe } from '~/routes/safe/store/model/safe'
import { makeOwner } from '~/routes/safe/store/model/owner'
import fetchTransactions from '~/routes/safe/store/actions/fetchTransactions'
import { makeConfirmation } from '~/routes/safe/store/model/confirmation'
import { aNewStore } from '~/store'
import { aMinedSafe } from '~/test/builder/safe.redux.builder'
import { getSafeFrom } from '~/test/utils/safeHelper'
import { promisify } from '~/utils/promisify'
import { getWeb3 } from '~/wallets/getWeb3'
import { safeTransactionsSelector } from '~/routes/safe/store/selectors'
import { testTransactionFrom, testSizeOfTransactions } from './utils/historyServiceHelper'
describe('Transactions Suite', () => {
let store: Store
let safeAddress: string
let accounts: string[]
beforeAll(async () => {
accounts = await promisify(cb => getWeb3().eth.getAccounts(cb))
})
beforeEach(async () => {
localStorage.clear()
store = aNewStore()
safeAddress = await aMinedSafe(store)
})
it('retrieves tx info from service having subject available', async () => {
const safe: Safe = getSafeFrom(store.getState(), safeAddress)
const gnosisSafe = await getSafeEthereumInstance(safeAddress)
const firstTxData = gnosisSafe.contract.addOwnerWithThreshold.getData(accounts[1], 2)
const executor = accounts[0]
const nonce = Date.now()
const firstTxHash = await createTransaction(safe, 'Add Owner Second account', safeAddress, 0, nonce, executor, firstTxData)
const secondTxData = gnosisSafe.contract.addOwnerWithThreshold.getData(accounts[2], 2)
const secondTxHash = await createTransaction(safe, 'Add Owner Third account', safeAddress, 0, nonce + 100, executor, secondTxData)
// WHEN
store.dispatch(fetchTransactions())
let transactions = safeTransactionsSelector(store.getState(), { safeAddress })
testSizeOfTransactions(transactions, 2)
// THEN
const firstTxConfirmations = List([
makeConfirmation({
owner: makeOwner({ address: executor }),
type: 'execution',
hash: firstTxHash,
}),
])
testTransactionFrom(transactions, 0, 'Add Owner Second account', nonce, 0, safeAddress, firstTxData, true, firstTxConfirmations)
const secondTxConfirmations = List([
makeConfirmation({
owner: makeOwner({ address: accounts[0] }),
type: 'confirmation',
hash: secondTxHash,
}),
])
testTransactionFrom(transactions, 1, 'Add Owner Third account', nonce + 100, 0, safeAddress, secondTxData, false, secondTxConfirmations)
store.dispatch(fetchTransactions())
transactions = safeTransactionsSelector(store.getState(), { safeAddress })
testSizeOfTransactions(transactions, 2)
testTransactionFrom(transactions, 0, 'Unknown', nonce, 0, safeAddress, firstTxData, true, firstTxConfirmations)
testTransactionFrom(transactions, 1, 'Unknown', nonce + 100, 0, safeAddress, secondTxData, false, secondTxConfirmations)
})
it('returns empty list of trnsactions when safe is not configured', async () => {
// routes/safe/transactions.selector.js the 4 cases
// confirmations.selector.js the last one
})
it('pending transactions are treated correctly', async () => {
// create a safe 3 owners 3 threshold
// create a tx adding 4th owner
// confirm tx and check on every step
})
it('returns count of confirmed but not executed txs', async () => {
// pendingTransactionSelector
})
it('returns count of executed txs', async () => {
// confirmationsTransactionSelector
})
it('returns correctly transaction list when safe is not available', async () => {
// routes/safe/test/transactions.selector.js
})
it('process only updated txs', async () => {
// Basically I would like when I call the GET TXs endpoint to retrieve those transactions ORDERED based on
// when they have been updated (just created, or just added another extra confirmation).
// In that way I do not need to parse and threat all txs in client side and also we mitigate the risk of
// do not get old txs updates. For doing that I would need to keep stored a number indicating
// if the tx has been updated in DB.
// For instance:
/*
create tx1 ---> [{ tx:1, updated: 1 }]
create tx2 ---> [{ tx:2, updated: 1 }, { tx:1, updated: 1 }]
user 2 confirms tx1 ---> [{ tx:1, updated: 2 }, { tx:2, updated: 1 }]
In that way I keep stored tx1 -> 1 and if I see tx2 -> 2 I do not skip it
*/
})
})

View File

@ -1,226 +0,0 @@
// @flow
import { List, Map } from 'immutable'
import { storeTransaction, buildConfirmationsFrom, EXECUTED_CONFIRMATION_HASH, buildExecutedConfirmationFrom } from '~/wallets/createTransactions'
import { type Transaction } from '~/routes/safe/store/model/transaction'
import { SafeFactory } from '~/routes/safe/store/test/builder/safe.builder'
import { type Safe } from '~/routes/safe/store/model/safe'
import { type Owner } from '~/routes/safe/store/model/owner'
import { loadSafeTransactions } from '~/routes/safe/store/actions/fetchTransactions'
import { type Confirmation } from '~/routes/safe/store/model/confirmation'
import { EMPTY_DATA } from '~/wallets/ethTransactions'
import { testSizeOfSafesWith, testSizeOfTransactions, testTransactionFrom } from './utils/historyServiceHelper'
describe('Transactions Suite', () => {
let safe: Safe
let destination: string
let value: number
let owners: List<Owner>
beforeEach(async () => {
localStorage.clear()
safe = SafeFactory.twoOwnersSafe('foo', 'bar')
destination = 'baz'
value = 2
owners = safe.get('owners')
const firstOwner = owners.get(0)
if (!firstOwner) { throw new Error() }
const secondOwner = owners.get(1)
if (!secondOwner) { throw new Error() }
})
it('adds first transaction to stored safe', async () => {
// GIVEN
const txName = 'Buy butteries for project'
const nonce: number = 10
const confirmations: List<Confirmation> = buildConfirmationsFrom(owners, 'foo', 'confirmationHash')
storeTransaction(txName, nonce, destination, value, 'foo', confirmations, '', safe.get('address'), safe.get('threshold'), EMPTY_DATA)
// WHEN
const transactions: Map<string, List<Transaction>> = loadSafeTransactions()
// THEN
testSizeOfSafesWith(transactions, 1)
const safeTransactions: List<Transaction> | typeof undefined = transactions.get(safe.get('address'))
if (!safeTransactions) { throw new Error() }
testSizeOfTransactions(safeTransactions, 1)
testTransactionFrom(safeTransactions, 0, txName, nonce, value, 2, destination, EMPTY_DATA, 'foo', 'confirmationHash', owners.get(0), owners.get(1))
})
it('adds second transaction to stored safe with one transaction', async () => {
// GIVEN
const firstTxName = 'Buy butteries for project'
const firstNonce: number = Date.now()
const safeAddress = safe.get('address')
const creator = 'foo'
const confirmations: List<Confirmation> = buildConfirmationsFrom(owners, creator, 'confirmationHash')
storeTransaction(firstTxName, firstNonce, destination, value, creator, confirmations, '', safeAddress, safe.get('threshold'), EMPTY_DATA)
const secondTxName = 'Buy printers for project'
const secondNonce: number = firstNonce + 100
const secondConfirmations: List<Confirmation> = buildConfirmationsFrom(owners, creator, 'confirmationHash')
storeTransaction(secondTxName, secondNonce, destination, value, creator, secondConfirmations, '', safeAddress, safe.get('threshold'), EMPTY_DATA)
// WHEN
const transactions: Map<string, List<Transaction>> = loadSafeTransactions()
// THEN
testSizeOfSafesWith(transactions, 1)
const safeTxs: List<Transaction> | typeof undefined = transactions.get(safeAddress)
if (!safeTxs) { throw new Error() }
testSizeOfTransactions(safeTxs, 2)
testTransactionFrom(safeTxs, 0, firstTxName, firstNonce, value, 2, destination, EMPTY_DATA, 'foo', 'confirmationHash', owners.get(0), owners.get(1))
testTransactionFrom(safeTxs, 1, secondTxName, secondNonce, value, 2, destination, EMPTY_DATA, 'foo', 'confirmationHash', owners.get(0), owners.get(1))
})
it('adds second transaction to stored safe having two safes with one transaction each', async () => {
const txName = 'Buy batteris for Alplha project'
const nonce = 10
const safeAddress = safe.address
const creator = 'foo'
const confirmations: List<Confirmation> = buildConfirmationsFrom(owners, creator, 'confirmationHash')
storeTransaction(txName, nonce, destination, value, creator, confirmations, '', safeAddress, safe.get('threshold'), EMPTY_DATA)
const secondSafe = SafeFactory.dailyLimitSafe(10, 2)
const txSecondName = 'Buy batteris for Beta project'
const txSecondNonce = 10
const secondSafeAddress = secondSafe.address
const secondCreator = '0x03db1a8b26d08df23337e9276a36b474510f0023'
const secondConfirmations: List<Confirmation> = buildConfirmationsFrom(secondSafe.get('owners'), secondCreator, 'confirmationHash')
storeTransaction(
txSecondName, txSecondNonce, destination, value, secondCreator,
secondConfirmations, '', secondSafeAddress, secondSafe.get('threshold'), EMPTY_DATA,
)
let transactions: Map<string, List<Transaction>> = loadSafeTransactions()
testSizeOfSafesWith(transactions, 2)
const firstSafeTxs: List<Transaction> | typeof undefined = transactions.get(safeAddress)
if (!firstSafeTxs) { throw new Error() }
testSizeOfTransactions(firstSafeTxs, 1)
const secondSafeTxs: List<Transaction> | typeof undefined = transactions.get(secondSafeAddress)
if (!secondSafeTxs) { throw new Error() }
testSizeOfTransactions(secondSafeTxs, 1)
// WHEN
const txFirstName = 'Buy paper for Alplha project'
const txFirstNonce = 11
const txConfirmations: List<Confirmation> = buildConfirmationsFrom(owners, creator, 'secondConfirmationHash')
storeTransaction(
txFirstName, txFirstNonce, destination, value, creator,
txConfirmations, '', safeAddress, safe.get('threshold'), EMPTY_DATA,
)
transactions = loadSafeTransactions()
// THEN
testSizeOfSafesWith(transactions, 2)
testSizeOfTransactions(transactions.get(safeAddress), 2)
testSizeOfTransactions(transactions.get(secondSafeAddress), 1)
// Test 2 transactions of first safe
testTransactionFrom(
transactions.get(safe.address), 0,
txName, nonce, value, 2, destination, EMPTY_DATA,
'foo', 'confirmationHash', owners.get(0), owners.get(1),
)
testTransactionFrom(
transactions.get(safe.address), 1,
txFirstName, txFirstNonce, value, 2, destination, EMPTY_DATA,
'foo', 'secondConfirmationHash', owners.get(0), owners.get(1),
)
// Test one transaction of second safe
testTransactionFrom(
transactions.get(secondSafe.address), 0,
txSecondName, txSecondNonce, value, 2, destination, EMPTY_DATA,
'0x03db1a8b26d08df23337e9276a36b474510f0023', 'confirmationHash', secondSafe.get('owners').get(0), secondSafe.get('owners').get(1),
)
})
it('does not allow to store same transaction twice', async () => {
// GIVEN
const txName = 'Buy butteries for project'
const nonce: number = 10
const creator = 'foo'
const confirmations: List<Confirmation> = buildConfirmationsFrom(owners, creator, 'confirmationHash')
storeTransaction(txName, nonce, destination, value, creator, confirmations, '', safe.get('address'), safe.get('threshold'), EMPTY_DATA)
// WHEN
const createTxFnc = () => storeTransaction(txName, nonce, destination, value, creator, confirmations, '', safe.get('address'), safe.get('threshold'), EMPTY_DATA)
expect(createTxFnc).toThrow(/Transaction with same nonce/)
})
it('checks the owner who creates the tx has confirmed it', async () => {
// GIVEN
const txName = 'Buy butteries for project'
const nonce: number = 10
const creator = 'foo'
const confirmations: List<Confirmation> = buildConfirmationsFrom(owners, creator, 'confirmationHash')
storeTransaction(txName, nonce, destination, value, creator, confirmations, '', safe.get('address'), safe.get('threshold'), EMPTY_DATA)
// WHEN
const transactions: Map<string, List<Transaction>> = loadSafeTransactions()
// THEN
testSizeOfSafesWith(transactions, 1)
})
it('checks the owner who creates the tx is an owner', async () => {
// GIVEN
const ownerName = 'invented'
const buildConfirmationsTxFnc = () => buildConfirmationsFrom(owners, ownerName, 'confirmationHash')
expect(buildConfirmationsTxFnc).toThrow(/The creator of the tx is not an owner/)
})
it('checks if safe has one owner transaction has been executed', async () => {
const ownerName = 'foo'
const oneOwnerSafe = SafeFactory.oneOwnerSafe(ownerName)
const txName = 'Buy butteries for project'
const nonce: number = 10
const tx = ''
const confirmations: List<Confirmation> = buildExecutedConfirmationFrom(oneOwnerSafe.get('owners'), ownerName)
const createTxFnc = () => storeTransaction(txName, nonce, destination, value, ownerName, confirmations, tx, oneOwnerSafe.get('address'), oneOwnerSafe.get('threshold'), EMPTY_DATA)
expect(createTxFnc).toThrow(/The tx should be mined before storing it in safes with one owner/)
})
it('checks if safe has one owner transaction the confirmation list is correctly build', async () => {
const ownerName = 'foo'
const oneOwnerSafe = SafeFactory.oneOwnerSafe(ownerName)
const txName = 'Buy butteries for project'
const nonce: number = 10
const tx = 'validTxHash'
const confirmations: List<Confirmation> = buildExecutedConfirmationFrom(oneOwnerSafe.get('owners'), ownerName)
storeTransaction(txName, nonce, destination, value, ownerName, confirmations, tx, oneOwnerSafe.get('address'), oneOwnerSafe.get('threshold'), EMPTY_DATA)
// WHEN
const safeTransactions: Map<string, List<Transaction>> = loadSafeTransactions()
// THEN
expect(safeTransactions.size).toBe(1)
const transactions: List<Transaction> | typeof undefined = safeTransactions.get(oneOwnerSafe.address)
if (!transactions) throw new Error()
expect(transactions.count()).toBe(1)
const batteriesTx: Transaction | typeof undefined = transactions.get(0)
if (!batteriesTx) throw new Error()
expect(batteriesTx.get('name')).toBe(txName)
const txConfirmations = batteriesTx.confirmations
if (!txConfirmations) throw new Error()
expect(txConfirmations.count()).toBe(1)
const firstConfirmation: Confirmation | typeof undefined = txConfirmations.get(0)
if (!firstConfirmation) throw new Error()
expect(firstConfirmation.get('hash')).toBe(EXECUTED_CONFIRMATION_HASH)
})
})