WA-238 Tests checking owners conditions

This commit is contained in:
apanizo 2018-05-21 16:58:03 +02:00
parent 60d7f9056e
commit 3f77bc1dbd
2 changed files with 74 additions and 31 deletions

View File

@ -20,7 +20,8 @@ const testSizeOfTransactions = (safeTxs, size) => {
expect(safeTxs.get(0)).not.toBe(null)
}
const testTransactionFrom = (safeTxs, pos, name, nonce, value, threshold, destination, firstOwner, secondOwner) => {
const testTransactionFrom =
(safeTxs, pos, name, nonce, value, threshold, destination, creator, firstOwner, secondOwner) => {
if (!safeTxs) { throw new Error() }
const tx: Transaction | typeof undefined = safeTxs.get(pos)
@ -37,7 +38,7 @@ const testTransactionFrom = (safeTxs, pos, name, nonce, value, threshold, destin
if (!firstConfirmation) { throw new Error() }
expect(firstConfirmation.get('owner')).not.toBe(undefined)
expect(firstConfirmation.get('owner')).toEqual(firstOwner)
expect(firstConfirmation.get('status')).toBe(false)
expect(firstConfirmation.get('status')).toBe(true)
const secondConfirmation: Confirmation | typeof undefined = confirmations.get(1)
if (!secondConfirmation) { throw new Error() }
@ -69,7 +70,7 @@ describe('Transactions Suite', () => {
// GIVEN
const txName = 'Buy butteries for project'
const nonce: number = 10
createTransaction(txName, nonce, destination, value, owners, safe.get('name'), safe.get('address'), safe.get('confirmations'))
createTransaction(txName, nonce, destination, value, 'foo', owners, '', safe.get('name'), safe.get('address'), safe.get('confirmations'))
// WHEN
const transactions: Map<string, List<Transaction>> = loadSafeTransactions()
@ -81,18 +82,18 @@ describe('Transactions Suite', () => {
if (!safeTransactions) { throw new Error() }
testSizeOfTransactions(safeTransactions, 1)
testTransactionFrom(safeTransactions, 0, txName, nonce, value, 2, destination, owners.get(0), owners.get(1))
testTransactionFrom(safeTransactions, 0, txName, nonce, value, 2, destination, 'foo', owners.get(0), owners.get(1))
})
it('adds second confirmation to stored safe with one confirmation', async () => {
// GIVEN
const firstTxName = 'Buy butteries for project'
const firstNonce: number = Date.now()
createTransaction(firstTxName, firstNonce, destination, value, owners, safe.get('name'), safe.get('address'), safe.get('confirmations'))
createTransaction(firstTxName, firstNonce, destination, value, 'foo', owners, '', safe.get('name'), safe.get('address'), safe.get('confirmations'))
const secondTxName = 'Buy printers for project'
const secondNonce: number = firstNonce + 100
createTransaction(secondTxName, secondNonce, destination, value, owners, safe.get('name'), safe.get('address'), safe.get('confirmations'))
createTransaction(secondTxName, secondNonce, destination, value, 'foo', owners, '', safe.get('name'), safe.get('address'), safe.get('confirmations'))
// WHEN
const transactions: Map<string, List<Transaction>> = loadSafeTransactions()
@ -104,21 +105,21 @@ describe('Transactions Suite', () => {
if (!safeTxs) { throw new Error() }
testSizeOfTransactions(safeTxs, 2)
testTransactionFrom(safeTxs, 0, firstTxName, firstNonce, value, 2, destination, owners.get(0), owners.get(1))
testTransactionFrom(safeTxs, 1, secondTxName, secondNonce, value, 2, destination, owners.get(0), owners.get(1))
testTransactionFrom(safeTxs, 0, firstTxName, firstNonce, value, 2, destination, 'foo', owners.get(0), owners.get(1))
testTransactionFrom(safeTxs, 1, secondTxName, secondNonce, value, 2, destination, 'foo', owners.get(0), owners.get(1))
})
it('adds second confirmation to stored safe having two safes with one confirmation each', async () => {
const txName = 'Buy batteris for Alplha project'
const nonce = 10
createTransaction(txName, nonce, destination, value, owners, safe.get('name'), safe.get('address'), safe.get('confirmations'))
createTransaction(txName, nonce, destination, value, 'foo', owners, '', safe.get('name'), safe.get('address'), safe.get('confirmations'))
const secondSafe = SafeFactory.dailyLimitSafe(10, 2)
const txSecondName = 'Buy batteris for Beta project'
const txSecondNonce = 10
createTransaction(
txSecondName, txSecondNonce, destination, value,
secondSafe.get('owners'), secondSafe.get('name'), secondSafe.get('address'), secondSafe.get('confirmations'),
txSecondName, txSecondNonce, destination, value, '0x03db1a8b26d08df23337e9276a36b474510f0023',
secondSafe.get('owners'), '', secondSafe.get('name'), secondSafe.get('address'), secondSafe.get('confirmations'),
)
let transactions: Map<string, List<Transaction>> = loadSafeTransactions()
@ -136,8 +137,8 @@ describe('Transactions Suite', () => {
const txFirstName = 'Buy paper for Alplha project'
const txFirstNonce = 11
createTransaction(
txFirstName, txFirstNonce, destination, value,
safe.get('owners'), safe.get('name'), safe.get('address'), safe.get('confirmations'),
txFirstName, txFirstNonce, destination, value, 'foo',
safe.get('owners'), '', safe.get('name'), safe.get('address'), safe.get('confirmations'),
)
transactions = loadSafeTransactions()
@ -151,19 +152,19 @@ describe('Transactions Suite', () => {
testTransactionFrom(
transactions.get(safe.address), 0,
txName, nonce, value, 2, destination,
owners.get(0), owners.get(1),
'foo', owners.get(0), owners.get(1),
)
testTransactionFrom(
transactions.get(safe.address), 1,
txFirstName, txFirstNonce, value, 2, destination,
owners.get(0), owners.get(1),
'foo', owners.get(0), owners.get(1),
)
// Test one transaction of second safe
testTransactionFrom(
transactions.get(secondSafe.address), 0,
txSecondName, txSecondNonce, value, 2, destination,
secondSafe.get('owners').get(0), secondSafe.get('owners').get(1),
'0x03db1a8b26d08df23337e9276a36b474510f0023', secondSafe.get('owners').get(0), secondSafe.get('owners').get(1),
)
})
@ -171,11 +172,45 @@ describe('Transactions Suite', () => {
// GIVEN
const txName = 'Buy butteries for project'
const nonce: number = 10
createTransaction(txName, nonce, destination, value, owners, safe.get('name'), safe.get('address'), safe.get('confirmations'))
createTransaction(txName, nonce, destination, value, 'foo', owners, '', safe.get('name'), safe.get('address'), safe.get('confirmations'))
// WHEN
const createTxFnc = () => createTransaction(txName, nonce, destination, value, owners, safe.get('name'), safe.get('address'), safe.get('confirmations'))
const createTxFnc = () => createTransaction(txName, nonce, destination, value, 'foo', owners, '', safe.get('name'), safe.get('address'), safe.get('confirmations'))
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
createTransaction(txName, nonce, destination, value, 'foo', owners, '', safe.get('name'), safe.get('address'), safe.get('confirmations'))
// 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 txName = 'Buy butteries for project'
const nonce: number = 10
const ownerName = 'invented'
const createTxFnc = () => createTransaction(txName, nonce, destination, value, ownerName, owners, '', safe.get('name'), safe.get('address'), safe.get('confirmations'))
expect(createTxFnc).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 createTxFnc = () => createTransaction(txName, nonce, destination, value, ownerName, oneOwnerSafe.get('owners'), tx, oneOwnerSafe.get('name'), oneOwnerSafe.get('address'), oneOwnerSafe.get('confirmations'))
expect(createTxFnc).toThrow(/The tx should be mined before storing it in safes with one owner/)
})
})

View File

@ -5,22 +5,39 @@ import { load, TX_KEY } from '~/utils/localStorage'
import { type Confirmation, type ConfirmationProps, makeConfirmation } from '~/routes/safe/store/model/confirmation'
import { makeTransaction, type Transaction, type TransactionProps } from '~/routes/safe/store/model/transaction'
const buildConfirmationsFrom = (owners: List<Owner>): List<Confirmation> =>
owners.map((owner: Owner) => makeConfirmation({ owner, status: false }))
const buildConfirmationsFrom = (owners: List<Owner>, creator: string): List<Confirmation> => {
if (!owners) {
throw new Error('This safe has no owners')
}
if (!owners.find((owner: Owner) => owner.get('address') === creator)) {
throw new Error('The creator of the tx is not an owner')
}
return owners.map((owner: Owner) => makeConfirmation({ owner, status: owner.get('address') === creator }))
}
export const createTransaction = (
name: string,
nonce: number,
destination: string,
value: number,
creator: string,
owners: List<Owner>,
tx: string,
safeName: string,
safeAddress: string,
safeThreshold: number,
) => {
const confirmations: List<Confirmation> = buildConfirmationsFrom(owners)
const confirmations: List<Confirmation> = buildConfirmationsFrom(owners, creator)
const notMinedWhenOneOwnerSafe = owners.count() === 1 && !tx
if (notMinedWhenOneOwnerSafe) {
throw new Error('The tx should be mined before storing it in safes with one owner')
}
const transaction: Transaction = makeTransaction({
name, nonce, value, confirmations, destination, threshold: safeThreshold,
name, nonce, value, confirmations, destination, threshold: safeThreshold, tx,
})
const safeTransactions = load(TX_KEY) || {}
@ -56,12 +73,3 @@ export const loadSafeTransactions = () => {
return map.set(safe, List(safeTxsRecord))
}))
}
/* TO USE as a selector
export const getTransactionsOf = async (safeAddress: string) => {
const safesTransactions = loadSafeTransactions()
const safeTxs = List(safesTransactions.get(safeAddress))
return safeTxs
}
*/