I am writing test cases using pytest for this function
def function_to_testcase(conn, event, customer):
if customer["userStatus"] is None:
raise IncompleteSignup("Customer marked as Incomplete Signup")
logger.info("Forgot password flow started")
send_update_customer_details_message(
db_id=customer["cid"],
zu_id=customer["AccountId"],
email=customer["UserName"],
encrypted_password=customer["Password"],
skip_targets=True,
)
I need to monkey-patch the external API calling function send_update_customer_details_message
, The test case i wrote is attached here.
import pytest
import unittest
from customers_migration.flows.legacy import (
run_legacy_forgot_password_flow,
)
class TestLegacy(unittest.TestCase):
monkeypatch = pytest.MonkeyPatch()
MSG = "Forgot password flow for Legacy started"
event_trigger_other = {
"request": {"password": "test_pwd"},
"triggerSource": "Other",
}
customer_status = {
"userStatus": 2,
"Password": "Test1ngpa$",
"cid": 1,
"AccountId": "12312",
"UserName": "test@test.com",
}
def test_run_forgot_password_flow_logger(self):
with self.assertLogs() as captured:
self.monkeypatch.setattr(
run_legacy_forgot_password_flow,
"send_update_customer_details_message",
None,
)
run_ipv_legacy_forgot_password_flow(
"", self.event_trigger_other, self.customer_status
)
self.assertEqual(len(captured.records), 1)
self.assertEqual(
self.MSG,
captured.records[0].getMessage(),
)
but I am getting an error like this:
AttributeError: <function run_legacy_forgot_password_flow at 0x7fc75f780280> has no attribute 'send_update_customer_details_message'
What am I doing here wrong?
You need to monkey-patch the import the same way in here.
def test_run_forgot_password_flow_logger(self):
with self.assertLogs() as captured:
self.monkeypatch.setattr(
'customers_migration.flows.legacy.send_update_customer_details_message',
None,
)
run_ipv_legacy_forgot_password_flow(
"", self.event_trigger_other, self.customer_status
)
self.assertEqual(len(captured.records), 1)
self.assertEqual(
self.MSG,
captured.records[0].getMessage(),
)