From c3a71397c654ff7fc0aa8e7b0dcc8bdf91d405ba Mon Sep 17 00:00:00 2001 From: Juan Enrique Alcaraz Date: Tue, 22 Aug 2023 11:55:27 +0200 Subject: [PATCH] Fix tests --- jest.config.js | 4 +- package.json | 2 +- test/helpers/addTrust.js | 63 -------- test/helpers/setupAccount.js | 13 +- test/safe.test.js | 28 ++-- test/trust.test.js | 271 ++++++++++++++++++----------------- 6 files changed, 153 insertions(+), 228 deletions(-) delete mode 100644 test/helpers/addTrust.js diff --git a/jest.config.js b/jest.config.js index 5b171d8a..4190fbe9 100644 --- a/jest.config.js +++ b/jest.config.js @@ -9,7 +9,7 @@ module.exports = { }, modulePathIgnorePatterns: ['/test/helpers'], testEnvironment: 'node', - testMatch: ['**/safe.test.js'], - testTimeout: 360 * 1000, + testMatch: ['**/safe.test.js', '**/trust.test.js'], + testTimeout: 30 * 1000, verbose: true, }; diff --git a/package.json b/package.json index 3b01c278..970516ac 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "docs:serve": "documentation serve --watch ./src/**", "docs:lint": "documentation lint ./src/**", "lint": "eslint --ignore-path .gitignore --ignore-pattern lib .", - "test": "jest --runInBand", + "test": "jest", "test:watch": "npm run test -- --watch" }, "devDependencies": { diff --git a/test/helpers/addTrust.js b/test/helpers/addTrust.js deleted file mode 100644 index 3014a062..00000000 --- a/test/helpers/addTrust.js +++ /dev/null @@ -1,63 +0,0 @@ -import { getSafeContract } from '~/common/getContracts'; - -import getTrustConnection from './getTrustConnection'; - -export default async function addTrust( - { account, safeAddress, safeAddressToTrust, limitPercentage }, - core, -) { - const { - contracts: { hub }, - options: { hubAddress }, - safe, - utils, - web3, - } = core; - // Retrieve safe - const safeSdk = await safe.getSafeSdk(account, { safeAddress }); - - // Prepare transaction to trust a Safe - return ( - safeSdk - .createTransaction({ - safeTransactionData: { - to: hubAddress, - value: 0, - data: hub.methods - .trust(safeAddressToTrust, limitPercentage) - .encodeABI(), - }, - }) - .then((safeTx) => safeSdk.signTransaction(safeTx)) - // Execute manually the transaction - .then((signedSafeTx) => - web3.eth.sendTransaction({ - from: account.address, - to: safeAddress, - value: 0, - data: getSafeContract(web3, safeAddress) - .methods.execTransaction( - signedSafeTx.data.to, - signedSafeTx.data.value, - signedSafeTx.data.data, - signedSafeTx.data.operation, - signedSafeTx.data.safeTxGas, - signedSafeTx.data.baseGas, - signedSafeTx.data.gasPrice, - signedSafeTx.data.gasToken, - signedSafeTx.data.refundReceiver, - signedSafeTx.encodedSignatures(), - ) - .encodeABI(), - }), - ) - .then(() => - utils.loop( - () => - getTrustConnection(core, account, safeAddress, safeAddressToTrust), - (isReady) => isReady, - { label: 'Wait for the graph to index newly added trust connection' }, - ), - ) - ); -} diff --git a/test/helpers/setupAccount.js b/test/helpers/setupAccount.js index faff828b..689962ec 100644 --- a/test/helpers/setupAccount.js +++ b/test/helpers/setupAccount.js @@ -12,6 +12,7 @@ export default async function setupAccount({ account, nonce }, core) { safeMasterAddress, }, safe, + utils, web3, } = core; const safeAddress = await safe.predictAddress(account, { nonce }); @@ -19,11 +20,9 @@ export default async function setupAccount({ account, nonce }, core) { return ( // Deploy manually a Safe - web3.eth + utils .sendTransaction({ - from: account.address, - to: proxyFactoryAddress, - value: 0, + target: proxyFactoryAddress, data: proxyFactory.methods .createProxyWithNonce( safeMasterAddress, @@ -60,10 +59,8 @@ export default async function setupAccount({ account, nonce }, core) { .then((safeTx) => safeSdk.signTransaction(safeTx)) // Execute manually the transaction .then((signedSafeTx) => - web3.eth.sendTransaction({ - from: account.address, - to: safeAddress, - value: 0, + utils.sendTransaction({ + target: safeAddress, data: getSafeContract(web3, safeAddress) .methods.execTransaction( signedSafeTx.data.to, diff --git a/test/safe.test.js b/test/safe.test.js index ddd09b33..f41545c1 100644 --- a/test/safe.test.js +++ b/test/safe.test.js @@ -7,8 +7,6 @@ import { deployCRCVersionSafe } from './helpers/transactions'; import generateSaltNonce from './helpers/generateSaltNonce'; import setupAccount from './helpers/setupAccount'; import setupWeb3 from './helpers/setupWeb3'; -// Temporary method for adding trusts with new relayer since trusts functionality is not yet migrated -import addTrust from './helpers/addTrust'; describe('Safe', () => { const { web3, provider } = setupWeb3(); @@ -41,15 +39,10 @@ describe('Safe', () => { // Let's make the trust connections needed to get the Safe deployed await Promise.all( predeployedSafes.map((predeployedAddress, index) => - addTrust( - { - account: accounts[index + 1], - safeAddress: predeployedAddress, - safeAddressToTrust: safeAddress, - limitPercentage: 50, - }, - core, - ), + core.trust.addConnection(accounts[index + 1], { + canSendTo: predeployedAddress, + user: safeAddress, + }), ), ); @@ -94,15 +87,10 @@ describe('Safe', () => { // Let's make the trust connections needed to get the Safe deployed await Promise.all( predeployedSafes.map((predeployedAddress, index) => - addTrust( - { - account: accounts[index + 1], - safeAddress: predeployedAddress, - safeAddressToTrust: safeAddress, - limitPercentage: 50, - }, - core, - ), + core.trust.addConnection(accounts[index + 1], { + canSendTo: predeployedAddress, + user: safeAddress, + }), ), ); diff --git a/test/trust.test.js b/test/trust.test.js index f2e9c884..ab142339 100644 --- a/test/trust.test.js +++ b/test/trust.test.js @@ -1,145 +1,148 @@ +import { DEFAULT_USER_LIMIT_PERCENTAGE } from '~/common/constants'; + import createCore from './helpers/core'; -import getAccount from './helpers/account'; import getTrustConnection from './helpers/getTrustConnection'; -import web3 from './helpers/web3'; -import { deploySafeAndToken } from './helpers/transactions'; - -let account; -let otherAccount; -let core; -let safeAddress; -let otherSafeAddress; - -beforeAll(async () => { - account = getAccount(); - otherAccount = getAccount(3); - core = createCore(); -}); +import setupWeb3 from './helpers/setupWeb3'; +import getAccounts from './helpers/getAccounts'; +import setupAccount from './helpers/setupAccount'; +import generateSaltNonce from './helpers/generateSaltNonce'; describe('Trust', () => { - beforeAll(() => - Promise.all([ - deploySafeAndToken(core, account), - deploySafeAndToken(core, otherAccount), - ]).then((result) => { - safeAddress = result[0].safeAddress; - otherSafeAddress = result[1].safeAddress; - }), - ); - - it('should trust someone', async () => { - const response = await core.trust.addConnection(account, { - user: otherSafeAddress, - canSendTo: safeAddress, - limitPercentage: 44, - }); - - expect(web3.utils.isHexStrict(response)).toBe(true); - - const connection = await core.utils.loop( - () => { - return getTrustConnection(core, account, safeAddress, otherSafeAddress); - }, - (isReady) => isReady, - { label: 'Wait for the graph to index newly added trust connection' }, - ); - - expect(connection.safeAddress).toBe(otherSafeAddress); - expect(connection.isIncoming).toBe(true); - expect(connection.isOutgoing).toBe(false); - expect(connection.limitPercentageIn).toBe(44); - expect(connection.limitPercentageOut).toBe(0); - - let otherConnection = await core.utils.loop( - () => { - return getTrustConnection( - core, - otherAccount, - otherSafeAddress, - safeAddress, - ); - }, - (isReady) => isReady, - { label: 'Wait for trust connection to be indexed by the graph' }, - ); - - expect(otherConnection.safeAddress).toBe(safeAddress); - expect(otherConnection.isIncoming).toBe(false); - expect(otherConnection.isOutgoing).toBe(true); - expect(otherConnection.limitPercentageIn).toBe(0); - expect(otherConnection.limitPercentageOut).toBe(44); - - // Test bidirectional trust connections - await core.trust.addConnection(otherAccount, { - user: safeAddress, - canSendTo: otherSafeAddress, - limitPercentage: 72, - }); + const { web3, provider } = setupWeb3(); + const core = createCore(web3); + const [account, otherAccount] = getAccounts(web3); + let safeAddress; + let otherSafeAddress; + + afterAll(() => provider.engine.stop()); + beforeAll(async () => { + [safeAddress, otherSafeAddress] = await Promise.all([ + setupAccount({ account, nonce: generateSaltNonce() }, core), + setupAccount({ account: otherAccount, nonce: generateSaltNonce() }, core), + ]); + }); - otherConnection = await core.utils.loop( - () => { - return getTrustConnection( - core, - otherAccount, - otherSafeAddress, + describe('when trusting', () => { + it('should trust a safeAddress', () => + core.trust + .addConnection(account, { + user: otherSafeAddress, + canSendTo: safeAddress, + }) + .then(() => + core.utils.loop( + () => + getTrustConnection(core, account, safeAddress, otherSafeAddress), + (isReady) => isReady, + { + label: 'Wait for the graph to index newly added trust connection', + }, + ), + ) + .then((connection) => { + expect(connection.safeAddress).toBe(otherSafeAddress); + expect(connection.isIncoming).toBe(true); + expect(connection.isOutgoing).toBe(false); + expect(connection.limitPercentageIn).toBe( + DEFAULT_USER_LIMIT_PERCENTAGE, + ); + expect(connection.limitPercentageOut).toBe(0); + })); + + it('the other safeAddress should be trusted', () => + core.utils + .loop( + () => + getTrustConnection( + core, + otherAccount, + otherSafeAddress, + safeAddress, + ), + (isReady) => isReady, + { label: 'Wait for trust connection to be indexed by the graph' }, + ) + .then((otherConnection) => { + expect(otherConnection.safeAddress).toBe(safeAddress); + expect(otherConnection.isIncoming).toBe(false); + expect(otherConnection.isOutgoing).toBe(true); + expect(otherConnection.limitPercentageIn).toBe(0); + expect(otherConnection.limitPercentageOut).toBe( + DEFAULT_USER_LIMIT_PERCENTAGE, + ); + })); + + it('bidirectional trust should be possible', () => + core.trust + .addConnection(otherAccount, { + user: safeAddress, + canSendTo: otherSafeAddress, + limitPercentage: 72, + }) + .then(() => + core.utils.loop( + () => + getTrustConnection( + core, + otherAccount, + otherSafeAddress, + safeAddress, + ), + ({ isIncoming, isOutgoing }) => isIncoming && isOutgoing, + { label: 'Wait for trust connection to be indexed by the Graph' }, + ), + ) + .then((otherConnection) => { + expect(otherConnection.safeAddress).toBe(safeAddress); + expect(otherConnection.isIncoming).toBe(true); + expect(otherConnection.isOutgoing).toBe(true); + expect(otherConnection.limitPercentageIn).toBe(72); + expect(otherConnection.limitPercentageOut).toBe( + DEFAULT_USER_LIMIT_PERCENTAGE, + ); + })); + + it('safeAddress should be trusted', () => + core.trust + .isTrusted(otherAccount, { safeAddress, - ); - }, - ({ isIncoming, isOutgoing }) => { - return isIncoming && isOutgoing; - }, - { label: 'Wait for trust connection to be indexed by the Graph' }, - ); - - expect(otherConnection.safeAddress).toBe(safeAddress); - expect(otherConnection.isIncoming).toBe(true); - expect(otherConnection.isOutgoing).toBe(true); - expect(otherConnection.limitPercentageIn).toBe(72); - expect(otherConnection.limitPercentageOut).toBe(44); - - // This should not be true as we don't have enough - // trust connections yet - const { isTrusted } = await core.trust.isTrusted(otherAccount, { - safeAddress, - }); - - expect(isTrusted).toBe(false); - - // This should be true as we lowered the limit - const { isTrusted: isTrustedLowLimit } = await core.trust.isTrusted( - otherAccount, - { - safeAddress, - limit: 1, - }, - ); - - expect(isTrustedLowLimit).toBe(true); + limit: 1, + }) + .then(({ isTrusted }) => expect(isTrusted).toBe(true))); }); - it('should untrust someone', async () => { - const response = await core.trust.removeConnection(account, { - user: otherSafeAddress, - canSendTo: safeAddress, - }); - - expect(web3.utils.isHexStrict(response)).toBe(true); - - await core.trust.removeConnection(otherAccount, { - user: safeAddress, - canSendTo: otherSafeAddress, - }); - - const network = await core.utils.loop( - async () => { - return await core.trust.getNetwork(account, { + describe('when untrusting', () => { + it('should untrust someone', () => + Promise.all([ + core.trust.removeConnection(account, { + user: otherSafeAddress, + canSendTo: safeAddress, + }), + core.trust.removeConnection(otherAccount, { + user: safeAddress, + canSendTo: otherSafeAddress, + }), + ]) + .then(() => + core.utils.loop( + () => + core.trust.getNetwork(account, { + safeAddress, + }), + (network) => network.length === 0, + { + label: 'Wait for trust network to be empty after untrusting user', + }, + ), + ) + .then((network) => expect(network.length).toBe(0))); + + it('safeAddress should be untrusted', () => + core.trust + .isTrusted(otherAccount, { safeAddress, - }); - }, - (network) => network.length === 0, - { label: 'Wait for trust network to be empty after untrusting user' }, - ); - - expect(network.length).toBe(0); + limit: 1, + }) + .then(({ trustConnections }) => expect(trustConnections).toBe(1))); }); });