Sign in before filtering the order export #48

Merged
rhino merged 1 commits from cursor/sign-in-before-order-filter into main 2026-11-21 10:53:23 +00:00
2 changed files with 30 additions and 0 deletions

View File

@@ -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):

View File

@@ -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):