def test_notify_exception(self): with self.assertRaises(NoRawTXData): MoneroTransaction(tx_id="txexample").notify( urls=["json://blah", "sns://blah"] ) with self.assertRaises(NoRawTXData): MoneroTransaction(tx_id="txexample").notify(urls=["xml://blah"])
def setUp(self): settings.init() self.transaction = MoneroTransaction( tx_id="4ea70add5d0c7db33557551b15cd174972fcfc73bf0f6a6b47b7837564b708d3", payment_provider="Monero", amount=Decimal("4.000000000000"), fee=Decimal("0.000962550000"), note="", recipient="9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC", timestamp=datetime(2018, 1, 29, 13, 17, 18), confirmations=1, _raw_data=self.raw_data, )
def test_set_transaction_data_mem_pool(self, mock_backend): transaction: MoneroTransaction = MoneroTransaction( tx_id= "d29264ad317e8fdb55ea04484c00420430c35be7b3fe6dd663f99aebf41a786c", server_config=ServerConfig(config_file=None), ) settings.security_level = 0 mock_backend.side_effect = self.MockBackend transaction.set_transaction_data(get_raw_data=False) self.assertEqual( transaction.tx_id, "d29264ad317e8fdb55ea04484c00420430c35be7b3fe6dd663f99aebf41a786c", ) self.assertEqual(transaction.currency, "XMR") # no payment provider because we didn't go through the factory self.assertEqual(transaction.payment_provider, None) self.assertEqual(transaction.amount, Decimal("3.140000000000")) self.assertEqual(transaction.fee, Decimal("0.000961950000")) self.assertEqual(transaction.note, "") self.assertEqual( transaction.recipient, "9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC", ) self.assertEqual(transaction.timestamp, datetime(2018, 1, 29, 21, 13, 28)) self.assertEqual(transaction.confirmations, 0)
def test_set_transaction_data(self, mock_backend): transaction: MoneroTransaction = MoneroTransaction( tx_id= "a0b876ebcf7c1d499712d84cedec836f9d50b608bb22d6cb49fd2feae3ffed14", server_config=ServerConfig(config_file=None), ) settings.security_level = 1 mock_backend.side_effect = self.MockBackend transaction.set_transaction_data(get_raw_data=False) self.assertEqual( transaction.tx_id, "a0b876ebcf7c1d499712d84cedec836f9d50b608bb22d6cb49fd2feae3ffed14", ) self.assertEqual(transaction.currency, "XMR") # no payment provider because we didn't go through the factory self.assertEqual(transaction.payment_provider, None) self.assertEqual(transaction.amount, Decimal("1")) self.assertEqual(transaction.fee, Decimal("0.00352891")) self.assertEqual(transaction.note, "") self.assertEqual( transaction.recipient, "Bf6ngv7q2TBWup13nEm9AjZ36gLE6i4QCaZ7XScZUKD" "UeGbYEHmPRdegKGwLT8tBBK7P6L32RELNzCR6QzNFkmogDjvypyV", ) self.assertEqual(transaction.timestamp, datetime(2018, 1, 29, 15, 0, 25)) self.assertEqual(transaction.confirmations, 0)
def test_get_tx_data_exceptions(self, mock_backend, mock_wallet): transaction: MoneroTransaction = MoneroTransaction( tx_id="not_there", server_config=ServerConfig(config_file=None)) settings.security_level = -1 mock_backend.return_value = MagicMock() mock_wallet.return_value.incoming.return_value = [] with self.assertRaises(NoTXFound): transaction.set_transaction_data(get_raw_data=False)
def test_set_raw_transaction_data(self, mock_backend): transaction: MoneroTransaction = MoneroTransaction( tx_id= "d0fb667f2975f79495e973d1437200f7b6c464956d33cd89289411e07a8c0b3a", server_config=ServerConfig(config_file=None), ) mock_backend.return_value.raw_request.return_value = self.raw_data transaction.set_transaction_data(get_raw_data=True) self.assertEqual(transaction._raw_data, self.raw_data)
def test_get_tx_data_exception_notxtoprocess(self, mock_backend): settings.security_level = 0 # tx in_block, not in mem pool transaction: MoneroTransaction = MoneroTransaction( tx_id= "a0b876ebcf7c1d499712d84cedec836f9d50b608bb22d6cb49fd2feae3ffed14", server_config=ServerConfig(config_file=None), ) mock_backend.side_effect = self.MockBackend with self.assertRaises(NoTXToProcess): transaction.set_transaction_data(get_raw_data=False)
def test_set_transaction_data_exception_numconfirmations( self, mock_backend): transaction: MoneroTransaction = MoneroTransaction( tx_id= "4ea70add5d0c7db33557551b15cd174972fcfc73bf0f6a6b47b7837564b708d3", server_config=ServerConfig(config_file=None), ) settings.security_level = 2 mock_backend.side_effect = self.MockBackend with self.assertRaises(NumConfirmationsNotMet): transaction.set_transaction_data(get_raw_data=False)
def test_parse_placeholders(self): # simple body = "Check%20your%20wallet%20for%20more%20details" title = "New%20%7Bcurrency%7D%20received" title_expected = "New XMR received" body_expected = "Check your wallet for more details" body_after, title_after = parse_placeholders(self.transaction, body, title) self.assertEqual(body_after, body_expected) self.assertEqual(title_after, title_expected) # all transaction attributes body = "tx_id%3A%7Btx_id%7D%2Cpayment_provider" \ "%3A%7Bpayment_provider%7D%2Camount%3A%7B" \ "amount%7D%2Cfee%3A%7Bfee%7D" title = "note%3A%7Bnote%7D%2Crecipient%3A%7Brecipient" \ "%7D%2Ctimestamp%3A%7Btimestamp%7D%2Cconfirmations%3A%7Bconfirmations%7D" title_expected = "note:,recipient:9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNC" \ "UukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC," \ "timestamp:2018-01-29 13:17:18,confirmations:1" body_expected = "tx_id:4ea70add5d0c7db33557551b15cd174972fcfc73bf0f6a6b47b7837564b708d3," \ "payment_provider:Monero,amount:4,fee:0.00096255" body_after, title_after = parse_placeholders(self.transaction, body, title) self.assertEqual(body_after, body_expected) self.assertEqual(title_after, title_expected) # case when the user specifies a placeholder, but that transaction's attribute was never populated with data def side_effect_lonely_tx(log): if mock_logger.debug.call_count == 1: self.assertEqual( log, 'PaymentProvider: Monero TXID: lonelytx Status: The placeholder "{amount}" was specified, ' "but no data was found. Network error or user error. Action: Continuing", ) if mock_logger.debug.call_count == 2: self.assertEqual( log, 'PaymentProvider: Monero TXID: lonelytx Status: The placeholder "{recipient}" was specified, ' "but no data was found. Network error or user error. Action: Continuing", ) transaction_with_no_details = MoneroTransaction( "lonelytx", payment_provider="Monero") with patch.object(utils, "logging") as mock_logger: mock_logger.debug = MagicMock(side_effect=side_effect_lonely_tx) parse_placeholders(transaction_with_no_details, "{amount}", "{recipient}")
class TestCase_Notify(TestCase): raw_data = { "transfer": { "address": "599BXkvzAPeD6EojXjW99gGeE19rxuS4g99o5xevp3jTFQYTNtSyYtqWAt2jg9MTP8aLTJKiuCJXg1Ro6xLtEUEm9rWHL7V", "amount": 100000000, "amounts": [100000000], "confirmations": 21, "double_spend_seen": False, "fee": 1006740000, "height": 597061, "locked": False, "note": "", "payment_id": "0000000000000000", "subaddr_index": {"major": 0, "minor": 0}, "subaddr_indices": [{"major": 0, "minor": 0}], "suggested_confirmations_threshold": 1, "timestamp": 1591546700, "txid": "d0fb667f2975f79495e973d1437200f7b6c464956d33cd89289411e07a8c0b3a", "type": "in", "unlock_time": 0, }, "transfers": [ { "address": "599BXkvzAPeD6EojXjW99gGeE19rxuS4g99o5xevp3jTFQYTNtSyYtqWAt2jg9MTP8aLTJKiuCJXg1Ro6xLtEUEm9rWHL7V", "amount": 100000000, "amounts": [100000000], "confirmations": 21, "double_spend_seen": False, "fee": 1006740000, "height": 597061, "locked": False, "note": "", "payment_id": "0000000000000000", "subaddr_index": {"major": 0, "minor": 0}, "subaddr_indices": [{"major": 0, "minor": 0}], "suggested_confirmations_threshold": 1, "timestamp": 1591546700, "txid": "d0fb667f2975f79495e973d1437200f7b6c464956d33cd89289411e07a8c0b3a", "type": "in", "unlock_time": 0, } ], } def setUp(self): settings.init() self.transaction = MoneroTransaction( tx_id="4ea70add5d0c7db33557551b15cd174972fcfc73bf0f6a6b47b7837564b708d3", payment_provider="Monero", amount=Decimal("4.000000000000"), fee=Decimal("0.000962550000"), note="", recipient="9tQoHWyZ4yXUgbz9nvMcFZUfDy5hxcdZabQCxmNCUukKYicXegsDL7nQpcUa3A1pF6K3fhq3scsyY88tdB1MqucULcKzWZC", timestamp=datetime(2018, 1, 29, 13, 17, 18), confirmations=1, _raw_data=self.raw_data, ) @patch("apprisetransactions.transactions.transaction.Apprise") def test_notify_json(self, mock_apprise): def notify_json_sideeffect(json_body): self.assertEqual(json_body, json.dumps(self.raw_data)) return True mock_apprise.notify = MagicMock(side_effect=notify_json_sideeffect) mock_apprise.return_value = mock_apprise self.assertTrue(self.transaction.notify(urls=["json://blah", "sns://blah"])) self.assertEqual(mock_apprise.notify.call_count, 1) def test_notify_exception(self): with self.assertRaises(NoRawTXData): MoneroTransaction(tx_id="txexample").notify( urls=["json://blah", "sns://blah"] ) with self.assertRaises(NoRawTXData): MoneroTransaction(tx_id="txexample").notify(urls=["xml://blah"]) @patch("apprisetransactions.transactions.transaction.Apprise") def test_notify_xml(self, mock_apprise): def notify_xml_sideeffect(xml_body): self.assertEqual( xml_body, parseString(dicttoxml(self.raw_data)).toprettyxml() ) return True mock_apprise.notify = MagicMock(side_effect=notify_xml_sideeffect) mock_apprise.return_value = mock_apprise self.assertTrue(self.transaction.notify(urls=["xml://blah"])) self.assertEqual(mock_apprise.notify.call_count, 1) @patch("apprisetransactions.transactions.transaction.Apprise") def test_notify_other(self, mock_apprise): pass def notify_sideeffect(body, title, attach): self.assertEqual(body, "New XMR received") self.assertEqual(title, "Private Notification") return True mock_apprise.notify = MagicMock(side_effect=notify_sideeffect) mock_apprise.return_value = mock_apprise self.assertTrue( self.transaction.notify( urls=["pbul://asdfasdf"], body="New {currency} received", title="Private Notification", ) ) self.assertEqual(mock_apprise.notify.call_count, 1) @patch("apprisetransactions.transactions.transaction.parse_placeholders") @patch("apprisetransactions.transactions.transaction.Apprise") def test_znotify_other(self, mock_apprise, mock_parse_placeholders): def notify_sideeffect(body, title, attach): self.assertEqual(body, "New XMR received") self.assertEqual(title, "Private Notification") return True # prase_placeholders is tested in test_utils mock_parse_placeholders.return_value = ( "New XMR received", "Private Notification", ) mock_apprise.notify = MagicMock(side_effect=notify_sideeffect) mock_apprise.return_value = mock_apprise self.assertTrue( self.transaction.notify( urls=["pbul://asdfasdf"], body="New {currency} received", title="Private Notification", ) ) self.assertEqual(mock_apprise.notify.call_count, 1) self.assertEqual(mock_parse_placeholders.call_count, 1)