From 8f6e617972a5e48a67e0195ad865ed216e006eeb Mon Sep 17 00:00:00 2001 From: diaco Date: Sat, 21 Nov 2026 14:22:30 +0330 Subject: [PATCH] Sign in before filtering the order export Co-authored-by: Cursor --- slips/crm_orders.py | 3 +++ slips/tests.py | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/slips/crm_orders.py b/slips/crm_orders.py index a3898ca..6962a50 100644 --- a/slips/crm_orders.py +++ b/slips/crm_orders.py @@ -331,6 +331,9 @@ def _load_order_export(client, day: date, prefixes: list[str]) -> tuple[str, str """Prefer the filtered export. A failed post still returns the full CSV.""" if getattr(client, "session", None) is None: return client.fetch_customer_orders() + # The report GET redirects to login until the client has signed in. + if hasattr(client, "login") and not getattr(client, "_logged_in", False): + client.login() try: return export_filtered_customer_orders(client, day, prefixes) except (_FilterMiss, OSError): diff --git a/slips/tests.py b/slips/tests.py index e004905..484e77b 100644 --- a/slips/tests.py +++ b/slips/tests.py @@ -10,6 +10,7 @@ from slips.crm_orders import ( SESSION_TTL_SECONDS, CrmAuthError, CrmOrdersError, + _load_order_export, code_matches, export_filtered_customer_orders, format_jalali, @@ -192,6 +193,32 @@ class SystemGroupLoginTests(unittest.TestCase): self.assertIn("PageID=", sess.gets[0]) self.assertIn("TimeStamp=", sess.gets[0]) + def test_filtered_export_logs_in_first(self): + calls = [] + + class Client: + def __init__(self): + self.session = object() + self._logged_in = False + + def login(self): + calls.append("login") + self._logged_in = True + + def fetch_customer_orders(self): + calls.append("full") + return "full", "text/csv" + + client = Client() + with patch( + "slips.crm_orders.export_filtered_customer_orders", + return_value=("filtered", "text/csv"), + ) as filtered: + body, ctype = _load_order_export(client, date(2026, 3, 21), ["KR"]) + self.assertEqual(calls, ["login"]) + self.assertEqual((body, ctype), ("filtered", "text/csv")) + filtered.assert_called_once() + class CrmSessionTests(TestCase): def test_session_lasts_thirty_days_and_slides(self):