Coverage for tests/common/monero_utils/test_payment_uri.py: 100%

101 statements  

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

1from __future__ import annotations 

2 

3import logging 

4 

5import pytest 

6from monero import MoneroNetworkType, MoneroTxConfig, MoneroUtils 

7 

8from utils import WalletUtils 

9from .base_test_monero_utils import BaseTestMoneroUtils 

10 

11logger: logging.Logger = logging.getLogger("TestMoneroUtilsPaymentUri") 

12 

13 

14@pytest.mark.unit 

15class TestMoneroUtilsPaymentUri(BaseTestMoneroUtils): 

16 """Monero utilities payment uri unit tests.""" 

17 

18 # Can get payment uri 

19 def test_get_payment_uri(self, config: BaseTestMoneroUtils.Config) -> None: 

20 address: str = config.mainnet.primary_address_1 

21 tx_config: MoneroTxConfig = WalletUtils.build_payment_uri_config(address) 

22 payment_uri: str = MoneroUtils.get_payment_uri(tx_config) 

23 query: str = "tx_amount=0.250000000000&recipient_name=John%20Doe&tx_description=My%20transfer%20to%20wallet" 

24 logger.debug(f"Testing payment uri: {payment_uri}") 

25 assert payment_uri == f"monero:{address}?{query}" 

26 

27 # Test invalid payment uri address network type 

28 def test_payment_uri_invalid_network_type(self, config: BaseTestMoneroUtils.Config) -> None: 

29 address: str = config.testnet.primary_address_1 

30 tx_config: MoneroTxConfig = WalletUtils.build_payment_uri_config(address) 

31 with pytest.raises(Exception) as exc_info: 

32 MoneroUtils.get_payment_uri(tx_config) 

33 

34 # get_payment_uri() wraps make_uri()'s error with context, unlike e.g. validate_address() 

35 assert str(exc_info.value) == f"Cannot make URI from supplied parameters: wrong address: {address}" 

36 

37 # Test deprecated standalone payment id 

38 def test_payment_uri_deprecated_payment_uri(self, config: BaseTestMoneroUtils.Config) -> None: 

39 address: str = config.testnet.primary_address_1 

40 tx_config: MoneroTxConfig = WalletUtils.build_payment_uri_config(address) 

41 tx_config.payment_id = "03284e41c342f03603284e41c342f03603284e41c342f03603284e41c342f036" 

42 with pytest.raises(Exception) as exc_info: 

43 MoneroUtils.get_payment_uri(tx_config, MoneroNetworkType.TESTNET) 

44 

45 # get_payment_uri() wraps make_uri()'s error with context, unlike e.g. validate_address() 

46 assert str(exc_info.value) == "Cannot make URI from supplied parameters: Standalone payment id deprecated, use integrated address instead" 

47 

48 # Test single payment id given with an integrated address 

49 def test_payment_uri_single_payment_id_with_integrated_address(self, config: BaseTestMoneroUtils.Config) -> None: 

50 address: str = config.mainnet.integrated_1 

51 tx_config: MoneroTxConfig = WalletUtils.build_payment_uri_config(address) 

52 tx_config.payment_id = "03284e41c342f03603284e41c342f03603284e41c342f03603284e41c342f036" 

53 with pytest.raises(Exception) as exc_info: 

54 MoneroUtils.get_payment_uri(tx_config) 

55 

56 # get_payment_uri() wraps make_uri()'s error with context, unlike e.g. validate_address() 

57 assert str(exc_info.value) == "Cannot make URI from supplied parameters: A single payment id is allowed" 

58 

59 # Can parse a payment uri without any query string 

60 def test_parse_payment_uri_no_query(self, config: BaseTestMoneroUtils.Config) -> None: 

61 address: str = config.mainnet.primary_address_1 

62 parsed: MoneroTxConfig = MoneroUtils.parse_payment_uri(f"monero:{address}") 

63 assert parsed.destinations[0].address == address 

64 assert parsed.destinations[0].amount == 0 

65 assert parsed.recipient_name is None 

66 assert parsed.note is None 

67 assert parsed.payment_id is None 

68 

69 # Can parse a payment uri with an empty query string 

70 def test_parse_payment_uri_empty_query(self, config: BaseTestMoneroUtils.Config) -> None: 

71 address: str = config.mainnet.primary_address_1 

72 parsed: MoneroTxConfig = MoneroUtils.parse_payment_uri(f"monero:{address}?") 

73 assert parsed.destinations[0].address == address 

74 

75 # Can parse a payment uri round trip built by get_payment_uri() 

76 def test_parse_payment_uri_round_trip(self, config: BaseTestMoneroUtils.Config) -> None: 

77 address: str = config.mainnet.primary_address_1 

78 tx_config: MoneroTxConfig = WalletUtils.build_payment_uri_config(address) 

79 uri: str = MoneroUtils.get_payment_uri(tx_config) 

80 parsed: MoneroTxConfig = MoneroUtils.parse_payment_uri(uri) 

81 assert parsed.destinations[0].address == address 

82 assert parsed.destinations[0].amount == tx_config.amount 

83 assert parsed.recipient_name == tx_config.recipient_name 

84 assert parsed.note == tx_config.note 

85 

86 # Payment uri must use the "monero:" scheme 

87 def test_parse_payment_uri_wrong_scheme(self, config: BaseTestMoneroUtils.Config) -> None: 

88 address: str = config.mainnet.primary_address_1 

89 with pytest.raises(RuntimeError) as exc_info: 

90 MoneroUtils.parse_payment_uri(f"bitcoin:{address}") 

91 assert str(exc_info.value) == f'Error parsing URI: URI has wrong scheme (expected "monero:"): bitcoin:{address}' 

92 

93 # Payment uri address must be valid 

94 def test_parse_payment_uri_wrong_address(self, config: BaseTestMoneroUtils.Config) -> None: 

95 address: str = config.mainnet.invalid_1 

96 with pytest.raises(RuntimeError) as exc_info: 

97 MoneroUtils.parse_payment_uri(f"monero:{address}") 

98 assert str(exc_info.value) == f"Error parsing URI: URI has wrong address: {address}" 

99 

100 # Payment uri parameters must be "key=value" pairs 

101 def test_parse_payment_uri_malformed_parameter(self, config: BaseTestMoneroUtils.Config) -> None: 

102 address: str = config.mainnet.primary_address_1 

103 with pytest.raises(RuntimeError) as exc_info: 

104 MoneroUtils.parse_payment_uri(f"monero:{address}?tx_amount") 

105 assert str(exc_info.value) == "Error parsing URI: URI has wrong parameter: tx_amount" 

106 

107 # Payment uri parameters must not repeat 

108 def test_parse_payment_uri_duplicate_parameter(self, config: BaseTestMoneroUtils.Config) -> None: 

109 address: str = config.mainnet.primary_address_1 

110 with pytest.raises(RuntimeError) as exc_info: 

111 MoneroUtils.parse_payment_uri(f"monero:{address}?tx_amount=1&tx_amount=2") 

112 assert str(exc_info.value) == "Error parsing URI: URI has more than one instance of tx_amount" 

113 

114 # Payment uri tx_amount must be a valid amount 

115 def test_parse_payment_uri_invalid_amount(self, config: BaseTestMoneroUtils.Config) -> None: 

116 address: str = config.mainnet.primary_address_1 

117 with pytest.raises(RuntimeError) as exc_info: 

118 MoneroUtils.parse_payment_uri(f"monero:{address}?tx_amount=abc") 

119 assert str(exc_info.value) == "Error parsing URI: URI has invalid amount: abc" 

120 

121 # Payment uri tx_payment_id must be a valid long payment id 

122 def test_parse_payment_uri_invalid_payment_id(self, config: BaseTestMoneroUtils.Config) -> None: 

123 address: str = config.mainnet.primary_address_1 

124 with pytest.raises(RuntimeError) as exc_info: 

125 MoneroUtils.parse_payment_uri(f"monero:{address}?tx_payment_id=nothex") 

126 assert str(exc_info.value) == "Error parsing URI: Invalid payment id: nothex" 

127 

128 # Payment uri can carry a separate, valid tx_payment_id for a non-integrated address 

129 def test_parse_payment_uri_valid_payment_id(self, config: BaseTestMoneroUtils.Config) -> None: 

130 address: str = config.mainnet.primary_address_1 

131 payment_id: str = "a" * 64 

132 parsed: MoneroTxConfig = MoneroUtils.parse_payment_uri(f"monero:{address}?tx_payment_id={payment_id}") 

133 assert parsed.payment_id == payment_id 

134 

135 # Payment uri must not combine a separate tx_payment_id with an integrated address 

136 def test_parse_payment_uri_separate_payment_id_with_integrated_address(self, config: BaseTestMoneroUtils.Config) -> None: 

137 address: str = config.mainnet.integrated_1 

138 with pytest.raises(RuntimeError) as exc_info: 

139 MoneroUtils.parse_payment_uri(f"monero:{address}?tx_payment_id={'a' * 64}") 

140 assert str(exc_info.value) == "Error parsing URI: Separate payment id given with an integrated address" 

141 

142 # Unknown payment uri parameters are silently discarded 

143 def test_parse_payment_uri_unknown_parameter(self, config: BaseTestMoneroUtils.Config) -> None: 

144 address: str = config.mainnet.primary_address_1 

145 parsed: MoneroTxConfig = MoneroUtils.parse_payment_uri(f"monero:{address}?foo=bar") 

146 assert parsed.destinations[0].address == address