Coverage for tests/wallet/wallet_support.py: 97%

190 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-22 19:49 +0000

1from __future__ import annotations 

2 

3import logging 

4from typing import Optional 

5 

6import pytest 

7from monero import ( 

8 MoneroAccount, 

9 MoneroDaemon, 

10 MoneroDaemonRpc, 

11 MoneroNetworkType, 

12 MoneroWallet, 

13 MoneroWalletConfig, 

14 MoneroWalletFull, 

15 MoneroWalletKeys, 

16 MoneroWalletRpc, 

17) 

18from typing_extensions import override 

19 

20from utils import StringUtils, TestUtils as Utils, WalletType 

21from .base_test_monero_wallet import BaseTestMoneroWallet 

22 

23logger: logging.Logger = logging.getLogger("TestMoneroWallet") 

24 

25 

26class FullWalletSupport(BaseTestMoneroWallet): 

27 """What the full wallet tests do differently from the common ones.""" 

28 

29 @classmethod 

30 @override 

31 def get_wallet_type(cls) -> WalletType: 

32 return WalletType.FULL 

33 

34 @pytest.fixture(scope="class") 

35 @classmethod 

36 @override 

37 def wallet(cls) -> MoneroWalletFull: 

38 """Test full wallet instance""" 

39 return Utils.get_wallet_full() 

40 

41 @override 

42 def after_all(self) -> None: 

43 super().after_all() 

44 Utils.WALLET_FULL_TESTS_RUN = True 

45 

46 @override 

47 def _create_wallet(self, config: Optional[MoneroWalletConfig], start_syncing: bool = True) -> MoneroWalletFull: 

48 # assign defaults 

49 if config is None: 

50 config = MoneroWalletConfig() 

51 random: bool = self.is_random_wallet_config(config) 

52 if config.path is None: 

53 config.path = Utils.TEST_WALLETS_DIR + "/" + StringUtils.get_random_string() 

54 if config.password is None: 

55 config.password = Utils.WALLET_PASSWORD 

56 if config.network_type is None: 

57 config.network_type = Utils.NETWORK_TYPE 

58 if config.server is None: 

59 config.server = Utils.get_daemon_rpc_connection() 

60 if config.restore_height is None and not random: 

61 config.restore_height = 0 

62 

63 config.regtest = config.network_type == MoneroNetworkType.MAINNET and Utils.REGTEST 

64 

65 # create wallet 

66 wallet: MoneroWalletFull = MoneroWalletFull.create_wallet(config) 

67 if not random: 

68 assert config.restore_height == wallet.get_restore_height() 

69 if start_syncing is not False and wallet.is_connected_to_daemon(): 

70 wallet.start_syncing(Utils.SYNC_PERIOD_IN_MS) 

71 return wallet 

72 

73 @override 

74 def _open_wallet(self, config: Optional[MoneroWalletConfig], start_syncing: bool = True) -> MoneroWalletFull: 

75 # assign defaults 

76 if config is None: 

77 config = MoneroWalletConfig() 

78 if config.password is None: 

79 config.password = Utils.WALLET_PASSWORD 

80 if config.network_type is None: 

81 config.network_type = Utils.NETWORK_TYPE 

82 if config.server is None: 

83 config.server = Utils.get_daemon_rpc_connection() 

84 

85 # open wallet 

86 assert config.network_type is not None 

87 assert config.path is not None 

88 

89 wallet: MoneroWalletFull = MoneroWalletFull.open_wallet(config.path, config.password, config.network_type) 

90 wallet.set_daemon_connection(config.server) 

91 if start_syncing is not False and wallet.is_connected_to_daemon(): 

92 wallet.start_syncing(Utils.SYNC_PERIOD_IN_MS) 

93 return wallet 

94 

95 @override 

96 def _close_wallet(self, wallet: MoneroWallet, save: bool = False) -> None: 

97 wallet.close(save) 

98 

99 @override 

100 def _get_seed_languages(self) -> list[str]: 

101 return self.get_test_wallet().get_seed_languages() 

102 

103 @override 

104 @classmethod 

105 def get_test_wallet(cls) -> MoneroWalletFull: 

106 return super().get_test_wallet() # type: ignore 

107 

108 

109class RpcWalletSupport(BaseTestMoneroWallet): 

110 """What the rpc wallet tests do differently from the common ones.""" 

111 

112 @classmethod 

113 @override 

114 def get_wallet_type(cls) -> WalletType: 

115 return WalletType.RPC 

116 

117 @pytest.fixture(scope="class") 

118 @classmethod 

119 @override 

120 def wallet(cls) -> MoneroWalletRpc: 

121 """Test rpc wallet instance.""" 

122 return cls.get_test_wallet() 

123 

124 @classmethod 

125 @override 

126 def get_test_wallet(cls) -> MoneroWalletRpc: 

127 return super().get_test_wallet() # type: ignore 

128 

129 @override 

130 def _open_wallet(self, config: MoneroWalletConfig | None) -> MoneroWalletRpc: 

131 return Utils.open_wallet_rpc(config) 

132 

133 @override 

134 def _create_wallet(self, config: MoneroWalletConfig) -> MoneroWalletRpc: 

135 return Utils.create_wallet_rpc(config) 

136 

137 @override 

138 def _close_wallet(self, wallet: MoneroWallet, save: bool = False) -> None: 

139 wallet.close(save) 

140 Utils.free_wallet_rpc_resource(wallet) 

141 

142 @override 

143 def _get_seed_languages(self) -> list[str]: 

144 return self.get_test_wallet().get_seed_languages() 

145 

146 @override 

147 def before_all(self) -> None: 

148 super().before_all() 

149 

150 @override 

151 def after_all(self) -> None: 

152 super().after_all() 

153 Utils.free_wallet_rpc_resources() 

154 

155 @override 

156 def before_each(self, request: pytest.FixtureRequest) -> None: 

157 super().before_each(request) 

158 # The server has the same keys as the full wallet, and what that one sends and flushes from the pool (test_prove_unrelayed_txs) it 

159 # keeps as pending: 500 seconds after, at any test, it marks it failed and sets its inputs as unspent, even when another tx spent 

160 # them. The daemon says which outputs are spent (the ones spent in the pool too), so a test never starts with the wrong ones. 

161 wallet: MoneroWalletRpc = self.get_test_wallet() 

162 if Utils.is_wallet_rpc_open(wallet): 

163 wallet.rescan_spent() 

164 

165 @override 

166 def after_each(self, request: pytest.FixtureRequest) -> None: 

167 super().after_each(request) 

168 Utils.free_wallet_rpc_resources() 

169 

170 @override 

171 def get_daemon_rpc_uri(self) -> str: 

172 return Utils.DAEMON_RPC_URI if not Utils.IN_CONTAINER else Utils.CONTAINER_DAEMON_RPC_URI 

173 

174 

175class KeysWalletSupport(BaseTestMoneroWallet): 

176 """What the keys-only wallet tests do differently from the common ones.""" 

177 

178 _account_indices: list[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

179 

180 _subaddress_indices: list[int] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

181 

182 @pytest.fixture(scope="class") 

183 @classmethod 

184 @override 

185 def wallet(cls) -> MoneroWalletKeys: 

186 """Test keys wallet instance""" 

187 return Utils.get_wallet_keys() 

188 

189 @pytest.fixture(scope="class") 

190 @classmethod 

191 @override 

192 def daemon(cls) -> MoneroDaemonRpc: 

193 return MoneroDaemon() # type: ignore 

194 

195 @classmethod 

196 @override 

197 def get_wallet_type(cls) -> WalletType: 

198 return WalletType.KEYS 

199 

200 @override 

201 def after_all(self) -> None: 

202 logger.info(f"Teardown test class {type(self).__name__}") 

203 

204 @override 

205 def before_each(self, request: pytest.FixtureRequest) -> None: 

206 logger.info(f"Before {request.node.name}") 

207 

208 @override 

209 def after_each(self, request: pytest.FixtureRequest) -> None: 

210 logger.info(self.after_message(request)) 

211 

212 @classmethod 

213 @override 

214 def is_random_wallet_config(cls, config: Optional[MoneroWalletConfig]) -> bool: 

215 assert config is not None 

216 return super().is_random_wallet_config(config) and config.private_spend_key is None 

217 

218 @override 

219 def _create_wallet(self, config: Optional[MoneroWalletConfig]) -> MoneroWalletKeys: 

220 # assign defaults 

221 if config is None: 

222 config = MoneroWalletConfig() 

223 

224 random: bool = self.is_random_wallet_config(config) 

225 if config.network_type is None: 

226 config.network_type = Utils.NETWORK_TYPE 

227 

228 # create wallet 

229 if random: 

230 wallet: MoneroWalletKeys = MoneroWalletKeys.create_wallet_random(config) 

231 elif config.seed is not None and config.seed != "": 

232 wallet = MoneroWalletKeys.create_wallet_from_seed(config) 

233 elif config.primary_address is not None and config.private_view_key is not None: 

234 wallet = MoneroWalletKeys.create_wallet_from_keys(config) 

235 elif config.primary_address is not None and config.private_spend_key is not None: 

236 wallet = MoneroWalletKeys.create_wallet_from_keys(config) 

237 else: 

238 raise Exception("Invalid configuration") 

239 

240 return wallet 

241 

242 @override 

243 def _open_wallet(self, config: Optional[MoneroWalletConfig]) -> MoneroWallet: 

244 raise NotImplementedError("TestMoneroWalletKeys._open_wallet(): not supported") 

245 

246 @override 

247 def _close_wallet(self, wallet: MoneroWallet, save: bool = False) -> None: 

248 # a keys-only wallet has nothing to persist, so `save` is ignored 

249 wallet.close() 

250 

251 @override 

252 def _get_seed_languages(self) -> list[str]: 

253 return self.get_test_wallet().get_seed_languages() 

254 

255 @classmethod 

256 @override 

257 def get_test_wallet(cls) -> MoneroWalletKeys: 

258 return super().get_test_wallet() # type: ignore 

259 

260 def _get_test_accounts(self, wallet: MoneroWallet, include_subaddresses: bool = False) -> list[MoneroAccount]: 

261 account_indices: list[int] = self._account_indices 

262 subaddress_indices: list[int] = self._subaddress_indices 

263 accounts: list[MoneroAccount] = [] 

264 for account_idx in account_indices: 

265 account: MoneroAccount = wallet.get_account(account_idx) 

266 

267 if include_subaddresses: 

268 account.subaddresses = wallet.get_subaddresses(account_idx, subaddress_indices) 

269 

270 accounts.append(account) 

271 

272 return accounts