diff --git a/pyproject.toml b/pyproject.toml index 29471aaeabb0fa1c063d4b347e81ab6efabc79b4..c214bfc5b2dd1b95d5797b491e3da71a3313dbd0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,6 +28,7 @@ pytest-cov = "^2.7" pytest-asyncio = "^0.10.0" coverage-badge = "^1.0" pytest-sugar = "^0.9.2" +asynctest = "^0.13.0" [tool.poetry.scripts] silkaj = "silkaj.cli:cli" diff --git a/tests/patched/money.py b/tests/patched/money.py index e7a9a73733b6d825750f477866ae7656d7c069e1..13b4e732cbad300763c75a1a02cfaeefee5db2c5 100644 --- a/tests/patched/money.py +++ b/tests/patched/money.py @@ -9,7 +9,7 @@ async def patched_ud_value(self): return mock_ud_value -# mock get_sources() +# mock get_sources and use a count async def patched_get_sources(pubkey): """ Returns transaction sources lists. diff --git a/tests/patched/test_constants.py b/tests/patched/test_constants.py new file mode 100644 index 0000000000000000000000000000000000000000..333fa07a3334b776cf96cf705ffe4a6a681a172f --- /dev/null +++ b/tests/patched/test_constants.py @@ -0,0 +1 @@ +mock_ud_value = 314 diff --git a/tests/patched/tx.py b/tests/patched/tx.py new file mode 100644 index 0000000000000000000000000000000000000000..5fa5b9e21004739562f6064afc749aefe04a1fa8 --- /dev/null +++ b/tests/patched/tx.py @@ -0,0 +1,31 @@ +from duniterpy.key import SigningKey +from silkaj.tools import message_exit + + +async def patched_generate_and_send_transaction( + key, + issuers, + tx_amounts, + listinput_and_amount, + outputAddresses, + Comment, + OutputbackChange, +): + if not ( + ( + isinstance(key, SigningKey) + and isinstance(issuers, str) + and isinstance(tx_amounts, list) + and isinstance(listinput_and_amount, tuple) + and isinstance(outputAddresses, list) + and isinstance(Comment, str) + and isinstance(OutputBackchange, str) + ) + and len(tx_amounts) == len(outputAddresses) + and sum(tx_amounts) <= listinput_and_amount[2] + and key.pubkey() == issuers + ): + message_exit( + "Test error : patched_generate_and_send_transaction() : Parameters are not coherent" + ) + pass diff --git a/tests/test_unit_tx.py b/tests/test_unit_tx.py index 23bf46edaecda15445c0a42b24f5d03279452592..dce4b0360b5e38212d90d107b7831929bba4676f 100644 --- a/tests/test_unit_tx.py +++ b/tests/test_unit_tx.py @@ -1,4 +1,5 @@ import pytest +from asynctest.mock import Mock, CoroutineMock from silkaj.tx import ( truncBase, transaction_confirmation, @@ -6,6 +7,7 @@ from silkaj.tx import ( transaction_amount, generate_transaction_document, get_list_input_for_transaction, + handle_intermediaries_transactions, ) from silkaj.tui import display_amount, display_pubkey from silkaj.money import UDValue @@ -20,10 +22,10 @@ from duniterpy.documents.transaction import ( from duniterpy.documents.block_uid import BlockUID from patched.wot import patched_is_member -from patched.money import patched_get_sources, patched_ud_value, mock_ud_value +from patched.money import patched_get_sources, patched_ud_value from patched.tools import patched_currency_symbol from patched.network_tools import patched_head_block - +from utils_tests import pubkey_list, mock_ud_value, key_fifi # truncBase() @pytest.mark.parametrize( @@ -439,7 +441,7 @@ async def test_get_list_input_for_transaction( # patched functions monkeypatch.setattr("silkaj.money.get_sources", patched_get_sources) - # reset patched_get_sources counter + # reset get_sources count patched_get_sources.counter = 0 # testing error exit if isinstance(expected, str): @@ -451,3 +453,386 @@ async def test_get_list_input_for_transaction( else: result = await get_list_input_for_transaction(pubkey, TXamount) assert (len(result[0]), result[1], result[2]) == expected + + +# handle_intermediaries_transactions() +@pytest.mark.parametrize( + "key, issuers, tx_amounts, outputAddresses, Comment, OutputbackChange, expected_listinput_amount", + [ + # test 1 : with two amounts/outputs and an outputbackchange, no need for intermediary transaction. + ( + key_fifi, + "HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat", + (100, 100), + ( + "DBM6F5ChMJzpmkUdL5zD9UXKExmZGfQ1AgPDQy4MxSBw", + "4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw", + ), + "Test comment", + "HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat", + ( + [ + InputSource( + amount=100, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=0, + ), + InputSource( + amount=200, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=1, + ), + ], + 300, + False, + ), + ), + # test 2 : with one amounts/outputs and no outputbackchange, need for intermediary transaction. + ( + key_fifi, + "HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat", + (90000,), + ("4szFkvQ5tzzhwcfUtZD32hdoG2ZzhvG3ZtfR61yjnxdw",), + "Test comment", + "HcRgKh4LwbQVYuAc3xAdCynYXpKoiPE6qdxCMa8JeHat", + ( + [ + InputSource( + amount=100, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=0, + ), + InputSource( + amount=200, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=1, + ), + InputSource( + amount=300, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=2, + ), + InputSource( + amount=400, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=3, + ), + InputSource( + amount=500, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=4, + ), + InputSource( + amount=600, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=5, + ), + InputSource( + amount=700, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=6, + ), + InputSource( + amount=800, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=7, + ), + InputSource( + amount=900, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=8, + ), + InputSource( + amount=1000, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=9, + ), + InputSource( + amount=1100, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=10, + ), + InputSource( + amount=1200, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=11, + ), + InputSource( + amount=1300, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=12, + ), + InputSource( + amount=1400, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=13, + ), + InputSource( + amount=1500, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=14, + ), + InputSource( + amount=1600, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=15, + ), + InputSource( + amount=1700, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=16, + ), + InputSource( + amount=1800, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=17, + ), + InputSource( + amount=1900, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=18, + ), + InputSource( + amount=2000, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=19, + ), + InputSource( + amount=2100, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=20, + ), + InputSource( + amount=2200, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=21, + ), + InputSource( + amount=2300, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=22, + ), + InputSource( + amount=2400, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=23, + ), + InputSource( + amount=2500, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=24, + ), + InputSource( + amount=2600, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=25, + ), + InputSource( + amount=2700, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=26, + ), + InputSource( + amount=2800, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=27, + ), + InputSource( + amount=2900, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=28, + ), + InputSource( + amount=3000, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=29, + ), + InputSource( + amount=3100, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=30, + ), + InputSource( + amount=3200, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=31, + ), + InputSource( + amount=3300, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=32, + ), + InputSource( + amount=3400, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=33, + ), + InputSource( + amount=3500, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=34, + ), + InputSource( + amount=3600, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=35, + ), + InputSource( + amount=3700, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=36, + ), + InputSource( + amount=3800, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=37, + ), + InputSource( + amount=3900, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=38, + ), + InputSource( + amount=4000, + base=0, + source="T", + origin_id="1F3059ABF35D78DFB5AFFB3DEAB4F76878B04DB6A14757BBD6B600B1C19157E7", + index=39, + ), + ], + 82000, + True, + ), + ), + ], +) +@pytest.mark.asyncio +async def test_handle_intermediaries_transactions( + key, + issuers, + tx_amounts, + outputAddresses, + Comment, + OutputbackChange, + expected_listinput_amount, + monkeypatch, +): + # patched functions + patched_generate_and_send_transaction = CoroutineMock(return_value=None) + monkeypatch.setattr("silkaj.money.get_sources", patched_get_sources) + monkeypatch.setattr( + "silkaj.tx.generate_and_send_transaction", patched_generate_and_send_transaction + ) + + patched_get_sources.counter = 0 + + await handle_intermediaries_transactions( + key, issuers, tx_amounts, outputAddresses, Comment, OutputbackChange + ) + + # patched functions + monkeypatch.setattr("silkaj.money.get_sources", patched_get_sources) + # reset patched_get_sources counter + patched_get_sources.counter = 0 + + if expected_listinput_amount[2] == True: + patched_generate_and_send_transaction.assert_any_await( + key, + issuers, + [expected_listinput_amount[1],], + expected_listinput_amount, + [issuers], + "Change operation", + ) + else: + patched_generate_and_send_transaction.assert_awaited_once_with( + key, + issuers, + tx_amounts, + expected_listinput_amount, + outputAddresses, + Comment, + issuers, + ) diff --git a/tests/utils_tests.py b/tests/utils_tests.py index f2b4d313a5ccdb6c12b4fff4ae31594ec588b857..76b929383b2648bf6b46ee96c548810d37457d13 100644 --- a/tests/utils_tests.py +++ b/tests/utils_tests.py @@ -22,3 +22,6 @@ def insecure_key(pass_salt): pubkey(1) = BUhLyJT17bzDVXW66xxfk1F7947vytmwJVadTaWb8sJS """ return SigningKey.from_credentials(pass_salt, pass_salt) + + +key_fifi = insecure_key("fifi")