From 25931c4db24400c71623ac354f27e83566eafc9b Mon Sep 17 00:00:00 2001 From: David Date: Wed, 3 Apr 2024 12:21:39 +0200 Subject: [PATCH] test: add payment channel tests --- .../payment_request/test_payment_request.py | 116 +++++++++++++++++- 1 file changed, 113 insertions(+), 3 deletions(-) diff --git a/erpnext/accounts/doctype/payment_request/test_payment_request.py b/erpnext/accounts/doctype/payment_request/test_payment_request.py index 932060895b0b..3709bc6257d9 100644 --- a/erpnext/accounts/doctype/payment_request/test_payment_request.py +++ b/erpnext/accounts/doctype/payment_request/test_payment_request.py @@ -2,6 +2,7 @@ # See license.txt import unittest +from unittest.mock import patch import frappe @@ -13,7 +14,12 @@ test_dependencies = ["Currency Exchange", "Journal Entry", "Contact", "Address"] -payment_gateway = {"doctype": "Payment Gateway", "gateway": "_Test Gateway"} +PAYMENT_URL = "https://example.com/payment" + +payment_gateways = [ + {"doctype": "Payment Gateway", "gateway": "_Test Gateway"}, + {"doctype": "Payment Gateway", "gateway": "_Test Gateway Phone"}, +] payment_method = [ { @@ -29,13 +35,21 @@ "payment_account": "_Test Bank USD - _TC", "currency": "USD", }, + { + "doctype": "Payment Gateway Account", + "payment_gateway": "_Test Gateway Phone", + "payment_account": "_Test Bank USD - _TC", + "payment_channel": "Phone", + "currency": "USD", + }, ] class TestPaymentRequest(unittest.TestCase): def setUp(self): - if not frappe.db.get_value("Payment Gateway", payment_gateway["gateway"], "name"): - frappe.get_doc(payment_gateway).insert(ignore_permissions=True) + for payment_gateway in payment_gateways: + if not frappe.db.get_value("Payment Gateway", payment_gateway["gateway"], "name"): + frappe.get_doc(payment_gateway).insert(ignore_permissions=True) for method in payment_method: if not frappe.db.get_value( @@ -45,6 +59,25 @@ def setUp(self): ): frappe.get_doc(method).insert(ignore_permissions=True) + send_email = patch( + "erpnext.accounts.doctype.payment_request.payment_request.PaymentRequest.send_email", + return_value=None, + ) + self.send_email = send_email.start() + self.addCleanup(send_email.stop) + get_payment_url = patch( + # this also shadows one (1) call to _get_payment_gateway_controller + "erpnext.accounts.doctype.payment_request.payment_request.PaymentRequest.get_payment_url", + return_value=PAYMENT_URL, + ) + self.get_payment_url = get_payment_url.start() + self.addCleanup(get_payment_url.stop) + _get_payment_gateway_controller = patch( + "erpnext.accounts.doctype.payment_request.payment_request._get_payment_gateway_controller", + ) + self._get_payment_gateway_controller = _get_payment_gateway_controller.start() + self.addCleanup(_get_payment_gateway_controller.stop) + def test_payment_request_linkings(self): so_inr = make_sales_order(currency="INR", do_not_save=True) so_inr.disable_rounded_total = 1 @@ -75,6 +108,83 @@ def test_payment_request_linkings(self): self.assertEqual(pr.reference_name, si_usd.name) self.assertEqual(pr.currency, "USD") + def test_payment_channels(self): + so = make_sales_order(currency="USD") + + pr = make_payment_request( + dt="Sales Order", + dn=so.name, + payment_gateway_account="_Test Gateway - USD", # email channel + submit_doc=False, + return_doc=True, + ) + pr.flags.mute_email = True # but temporarily prohibit sending + pr.submit() + pr.reload() + self.assertEqual(pr.payment_channel, "Email") + self.assertEqual(pr.mute_email, False) + + self.assertIsNone(pr.payment_url) + self.assertEqual(self.send_email.call_count, 0) # hence: no increment + self.assertEqual(self._get_payment_gateway_controller.call_count, 1) + pr.cancel() + + pr = make_payment_request( + dt="Sales Order", + dn=so.name, + payment_gateway_account="_Test Gateway Phone - USD", + submit_doc=True, + return_doc=True, + ) + pr.reload() + + self.assertEqual(pr.payment_channel, "Phone") + self.assertEqual(pr.mute_email, False) + + self.assertIsNone(pr.payment_url) + self.assertEqual(self.send_email.call_count, 0) # no increment on phone channel + self.assertEqual(self._get_payment_gateway_controller.call_count, 3) + pr.cancel() + + pr = make_payment_request( + dt="Sales Order", + dn=so.name, + payment_gateway_account="_Test Gateway - USD", # email channel + submit_doc=True, + return_doc=True, + ) + pr.reload() + + self.assertEqual(pr.payment_channel, "Email") + self.assertEqual(pr.mute_email, False) + + self.assertEqual(pr.payment_url, PAYMENT_URL) + self.assertEqual(self.send_email.call_count, 1) # increment on normal email channel + self.assertEqual(self._get_payment_gateway_controller.call_count, 4) + pr.cancel() + + so = make_sales_order(currency="USD", do_not_save=True) + # no-op; for optical consistency with how a webshop SO would look like + so.order_type = "Shopping Cart" + so.save() + pr = make_payment_request( + dt="Sales Order", + dn=so.name, + payment_gateway_account="_Test Gateway - USD", # email channel + order_type="Shopping Cart", + submit_doc=True, + return_doc=True, + ) + pr.reload() + + self.assertEqual(pr.payment_channel, "Email") + self.assertEqual(pr.mute_email, False) + + self.assertIsNone(pr.payment_url) + self.assertEqual(self.send_email.call_count, 1) # no increment on shopping cart + self.assertEqual(self._get_payment_gateway_controller.call_count, 5) + pr.cancel() + def test_payment_entry_against_purchase_invoice(self): si_usd = make_purchase_invoice( customer="_Test Supplier USD",