|
| 1 | +import os |
| 2 | +import struct |
| 3 | + |
| 4 | +import pytest |
| 5 | +from pytest_mock import MockerFixture |
| 6 | +from mypylib import Dict |
| 7 | +from mytoncore.mytoncore import MyTonCore |
| 8 | +from modules.single_pool import SingleNominatorModule |
| 9 | +from tests.helpers import create_pool_file |
| 10 | + |
| 11 | + |
| 12 | +def test_new_single_pool(cli, ton, monkeypatch, mocker: MockerFixture): |
| 13 | + # Bad args |
| 14 | + output = cli.execute("new_single_pool", no_color=True) |
| 15 | + assert "Bad args" in output |
| 16 | + output = cli.execute("new_single_pool test_pool", no_color=True) |
| 17 | + assert "Bad args" in output |
| 18 | + |
| 19 | + # happy path |
| 20 | + download_contract_mock = mocker.Mock() |
| 21 | + get_validator_wallet_mock = mocker.Mock() |
| 22 | + monkeypatch.setattr(MyTonCore, "DownloadContract", download_contract_mock) |
| 23 | + monkeypatch.setattr(MyTonCore, "GetValidatorWallet", get_validator_wallet_mock) |
| 24 | + |
| 25 | + def fake_fift_run(args): |
| 26 | + file_path = args[-1] |
| 27 | + with open(file_path + ".addr", 'wb') as f: |
| 28 | + f.write(b'\x00'*36) |
| 29 | + return "Saved single nominator pool" |
| 30 | + |
| 31 | + monkeypatch.setattr(ton.fift, "Run", fake_fift_run) |
| 32 | + |
| 33 | + pool_name = "test_single_pool" |
| 34 | + pool_path = ton.poolsDir + pool_name |
| 35 | + addr_file = pool_path + ".addr" |
| 36 | + owner_address = "owner_address" |
| 37 | + |
| 38 | + assert not os.path.exists(addr_file) |
| 39 | + |
| 40 | + output = cli.execute(f"new_single_pool {pool_name} {owner_address}", no_color=True) |
| 41 | + |
| 42 | + assert "new_single_pool - OK" in output |
| 43 | + download_contract_mock.assert_called_once() |
| 44 | + get_validator_wallet_mock.assert_called_once() |
| 45 | + assert os.path.isfile(addr_file) |
| 46 | + |
| 47 | + # pool already exists |
| 48 | + output = cli.execute(f"new_single_pool {pool_name} {owner_address}", no_color=True) |
| 49 | + assert "create_single_pool warning: Pool already exists" in output |
| 50 | + assert os.path.isfile(addr_file) |
| 51 | + |
| 52 | + with pytest.raises(Exception) as e: |
| 53 | + cli.execute(f"new_single_pool pool2 {owner_address}") |
| 54 | + assert os.path.isfile(addr_file) |
| 55 | + assert 'Pool with the same parameters already exists' in str(e) |
| 56 | + |
| 57 | + |
| 58 | +def test_activate_single_pool(cli, ton, monkeypatch, mocker: MockerFixture): |
| 59 | + # Bad args |
| 60 | + output = cli.execute("activate_single_pool", no_color=True) |
| 61 | + assert "Bad args" in output |
| 62 | + |
| 63 | + pool_name = "test_activate_single_pool" |
| 64 | + pool_path = ton.poolsDir + pool_name |
| 65 | + create_pool_file(pool_path, b'\x00' * 36) |
| 66 | + |
| 67 | + boc_file = pool_path + "-query.boc" |
| 68 | + with open(boc_file, 'wb') as f: |
| 69 | + f.write(b'\x00' * 100) |
| 70 | + |
| 71 | + get_validator_wallet_mock = mocker.Mock() |
| 72 | + validator_wallet = get_validator_wallet_mock.return_value |
| 73 | + check_account_active_mock = mocker.Mock() |
| 74 | + monkeypatch.setattr(MyTonCore, "GetValidatorWallet", get_validator_wallet_mock) |
| 75 | + monkeypatch.setattr(MyTonCore, "check_account_active", check_account_active_mock) |
| 76 | + |
| 77 | + result_file_path = "/tmp/signed.boc" |
| 78 | + sign_boc_mock = mocker.Mock(return_value=result_file_path) |
| 79 | + monkeypatch.setattr(MyTonCore, "SignBocWithWallet", sign_boc_mock) |
| 80 | + |
| 81 | + send_file_mock = mocker.Mock() |
| 82 | + monkeypatch.setattr(MyTonCore, "SendFile", send_file_mock) |
| 83 | + |
| 84 | + output = cli.execute(f"activate_single_pool {pool_name}", no_color=True) |
| 85 | + |
| 86 | + assert "activate_single_pool - OK" in output |
| 87 | + get_validator_wallet_mock.assert_called_once() |
| 88 | + check_account_active_mock.assert_called_once_with(validator_wallet.addrB64) |
| 89 | + sign_boc_mock.assert_called_once() |
| 90 | + |
| 91 | + sign_call_args = sign_boc_mock.call_args[0] |
| 92 | + assert sign_call_args[0] == validator_wallet |
| 93 | + assert sign_call_args[1] == boc_file |
| 94 | + assert sign_call_args[2] == 'UQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAJKZ' |
| 95 | + assert sign_call_args[3] == 1 |
| 96 | + assert sign_boc_mock.call_args[1]['boc_mode'] == "--with-init" |
| 97 | + |
| 98 | + send_file_mock.assert_called_once_with(result_file_path, validator_wallet) |
| 99 | + |
| 100 | + # pool already activated |
| 101 | + os.remove(boc_file) |
| 102 | + get_validator_wallet_mock.reset_mock() |
| 103 | + check_account_active_mock.reset_mock() |
| 104 | + sign_boc_mock.reset_mock() |
| 105 | + send_file_mock.reset_mock() |
| 106 | + |
| 107 | + output = cli.execute(f"activate_single_pool {pool_name}", no_color=True) |
| 108 | + |
| 109 | + assert "Pool test_activate_single_pool already activated" in output |
| 110 | + get_validator_wallet_mock.assert_not_called() |
| 111 | + check_account_active_mock.assert_not_called() |
| 112 | + sign_boc_mock.assert_not_called() |
| 113 | + send_file_mock.assert_not_called() |
| 114 | + |
| 115 | + |
| 116 | +def test_withdraw_from_single_pool(cli, ton, monkeypatch, mocker: MockerFixture): |
| 117 | + # Bad args |
| 118 | + output = cli.execute("withdraw_from_single_pool", no_color=True) |
| 119 | + assert "Bad args" in output |
| 120 | + output = cli.execute("withdraw_from_single_pool test", no_color=True) |
| 121 | + assert "Bad args" in output |
| 122 | + |
| 123 | + # happy path |
| 124 | + withdraw_from_pool_process_mock = mocker.Mock() |
| 125 | + monkeypatch.setattr(MyTonCore, "WithdrawFromPoolProcess", withdraw_from_pool_process_mock) |
| 126 | + pool_addr = "test_addr" |
| 127 | + amount = 250.75 |
| 128 | + output = cli.execute(f"withdraw_from_single_pool {pool_addr} {amount}", no_color=True) |
| 129 | + |
| 130 | + assert "withdraw_from_single_pool - OK" in output |
| 131 | + withdraw_from_pool_process_mock.assert_called_once_with(pool_addr, amount) |
0 commit comments