Coverage for tests/wallet/test_connection.py: 95%
94 statements
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
« prev ^ index » next coverage.py v7.17.3, created at 2026-11-22 19:49 +0000
1from __future__ import annotations
3import logging
5import pytest
6from monero import MoneroRpcConnection, MoneroWallet, MoneroWalletConfig, MoneroWalletRpc
7from typing_extensions import override
9from utils import AssertUtils, TestUtils, TestUtils as Utils, WalletErrorUtils
10from .base_test_monero_wallet import BaseTestMoneroWallet
11from .wallet_support import FullWalletSupport, RpcWalletSupport, KeysWalletSupport
13logger: logging.Logger = logging.getLogger("TestMoneroWalletConnection")
16class BaseTestMoneroWalletConnection(BaseTestMoneroWallet):
17 """Common wallet connection tests."""
19 # Can get the daemon's max peer height
20 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
21 def test_get_daemon_max_peer_height(self, wallet: MoneroWallet) -> None:
22 height: int = wallet.get_daemon_max_peer_height()
23 assert height > 0
25 # Can get the daemon's height
26 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
27 def test_get_daemon_height(self, wallet: MoneroWallet) -> None:
28 assert wallet.is_connected_to_daemon(), "Wallet is not connected to daemon"
29 daemon_height: int = wallet.get_daemon_height()
30 assert daemon_height > 0
32 # Can set the daemon connection
33 @pytest.mark.skipif(TestUtils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
34 def test_set_daemon_connection(self) -> None:
35 # create random wallet with default daemon connection
36 config: MoneroWalletConfig = MoneroWalletConfig()
37 wallet: MoneroWallet = self._create_wallet(config)
38 daemon_rpc_uri: str = self.get_daemon_rpc_uri()
39 connection: MoneroRpcConnection = MoneroRpcConnection(
40 daemon_rpc_uri, TestUtils.DAEMON_RPC_USERNAME, TestUtils.DAEMON_RPC_PASSWORD
41 )
42 AssertUtils.assert_equals(connection, wallet.get_daemon_connection())
43 assert wallet.is_connected_to_daemon() # uses default localhost connection
45 # set empty server uri
46 wallet.set_daemon_connection("")
47 assert wallet.get_daemon_connection() is None
48 assert wallet.is_connected_to_daemon() is False
50 # set offline server uri
51 wallet.set_daemon_connection(TestUtils.OFFLINE_SERVER_URI)
52 connection = MoneroRpcConnection(TestUtils.OFFLINE_SERVER_URI, "", "")
53 AssertUtils.assert_equals(connection, wallet.get_daemon_connection())
54 assert wallet.is_connected_to_daemon() is False
56 # set daemon with wrong credentials
57 wallet.set_daemon_connection(daemon_rpc_uri, "wronguser", "wrongpass")
58 connection = MoneroRpcConnection(daemon_rpc_uri, "wronguser", "wrongpass")
59 AssertUtils.assert_equals(connection, wallet.get_daemon_connection())
60 if "" == TestUtils.DAEMON_RPC_USERNAME:
61 # TODO: monerod without authentication works with bad credentials?
62 assert wallet.is_connected_to_daemon()
63 else:
64 assert wallet.is_connected_to_daemon() is False
66 # set daemon with authentication
67 wallet.set_daemon_connection(
68 daemon_rpc_uri, TestUtils.DAEMON_RPC_USERNAME, TestUtils.DAEMON_RPC_PASSWORD
69 )
70 connection = MoneroRpcConnection(
71 daemon_rpc_uri, TestUtils.DAEMON_RPC_USERNAME, TestUtils.DAEMON_RPC_PASSWORD
72 )
73 AssertUtils.assert_equals(connection, wallet.get_daemon_connection())
74 assert wallet.is_connected_to_daemon()
76 # nullify daemon connection
77 wallet.set_daemon_connection(None)
78 assert wallet.get_daemon_connection() is None
79 wallet.set_daemon_connection(daemon_rpc_uri)
80 connection = MoneroRpcConnection(daemon_rpc_uri)
81 AssertUtils.assert_equals(connection, wallet.get_daemon_connection())
82 wallet.set_daemon_connection(None)
83 assert wallet.get_daemon_connection() is None
85 # set daemon uri to non-daemon
86 if not TestUtils.IN_CONTAINER: # TODO sometimes this fails in container...
87 wallet.set_daemon_connection("www.getmonero.org")
88 connection = MoneroRpcConnection("www.getmonero.org")
89 AssertUtils.assert_equals(connection, wallet.get_daemon_connection())
90 assert wallet.is_connected_to_daemon() is False
92 # set daemon to invalid uri
93 wallet.set_daemon_connection("abc123")
94 assert wallet.is_connected_to_daemon() is False
96 # attempt to sync
97 try:
98 with pytest.raises(Exception, match="Wallet is not connected to daemon"):
99 wallet.sync()
100 finally:
101 self._close_wallet(wallet)
104@pytest.mark.integration
105class TestMoneroWalletFullConnection(FullWalletSupport, BaseTestMoneroWalletConnection):
106 """Full wallet connection integration tests."""
109@pytest.mark.integration
110class TestMoneroWalletRpcConnection(RpcWalletSupport, BaseTestMoneroWalletConnection):
111 """Rpc wallet connection integration tests."""
113 @pytest.mark.skipif(Utils.TEST_NON_RELAYS is False, reason="TEST_NON_RELAYS disabled")
114 def test_offline_wallet(self) -> None:
115 offline_wallet: MoneroWalletRpc = MoneroWalletRpc(Utils.OFFLINE_SERVER_URI, Utils.WALLET_RPC_USERNAME, Utils.WALLET_PASSWORD)
116 with pytest.raises(Exception) as exc_info:
117 offline_wallet.is_view_only()
118 WalletErrorUtils.test_wallet_is_not_connected_error(exc_info.value)
120 @pytest.mark.not_supported
121 @override
122 def test_get_daemon_max_peer_height(self, wallet: MoneroWallet) -> None:
123 return super().test_get_daemon_max_peer_height(wallet)
125 @pytest.mark.not_supported
126 @override
127 def test_get_daemon_height(self, wallet: MoneroWallet) -> None:
128 return super().test_get_daemon_height(wallet)
131@pytest.mark.unit
132class TestMoneroWalletKeysConnection(KeysWalletSupport, BaseTestMoneroWalletConnection):
133 """Keys-only wallet connection unit tests."""
135 @pytest.mark.not_supported
136 @override
137 def test_get_daemon_height(self, wallet: MoneroWallet) -> None:
138 return super().test_get_daemon_height(wallet)
140 @pytest.mark.not_supported
141 @override
142 def test_get_daemon_max_peer_height(self, wallet: MoneroWallet) -> None:
143 return super().test_get_daemon_max_peer_height(wallet)
145 @pytest.mark.not_supported
146 @override
147 def test_set_daemon_connection(self) -> None:
148 return super().test_set_daemon_connection()