Skip to content

Commit

Permalink
test: add payment channel tests
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao committed May 24, 2024
1 parent 683c1f0 commit 25931c4
Showing 1 changed file with 113 additions and 3 deletions.
116 changes: 113 additions & 3 deletions erpnext/accounts/doctype/payment_request/test_payment_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# See license.txt

import unittest
from unittest.mock import patch

import frappe

Expand All @@ -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 = [
{
Expand All @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 25931c4

Please sign in to comment.