Sign in to SystemGroup per user on the orders page
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
124
slips/tests.py
124
slips/tests.py
@@ -1,16 +1,26 @@
|
||||
import unittest
|
||||
from datetime import date
|
||||
from datetime import date, timedelta
|
||||
from types import SimpleNamespace
|
||||
from unittest.mock import patch
|
||||
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
|
||||
from slips.crm_orders import (
|
||||
SESSION_TTL_SECONDS,
|
||||
CrmAuthError,
|
||||
CrmOrdersError,
|
||||
code_matches,
|
||||
format_jalali,
|
||||
jalali_from_gregorian,
|
||||
login_rejected,
|
||||
note_date_as_jalali,
|
||||
orders_after,
|
||||
parse_gregorian,
|
||||
parse_jalali,
|
||||
parse_prefixes,
|
||||
recent_customer_orders,
|
||||
report_base_url,
|
||||
)
|
||||
|
||||
|
||||
@@ -54,5 +64,117 @@ class OrdersAfterTests(unittest.TestCase):
|
||||
self.assertEqual(parse_prefixes(""), [])
|
||||
|
||||
|
||||
class SystemGroupLoginTests(unittest.TestCase):
|
||||
def test_login_rejected_markers(self):
|
||||
self.assertEqual(SESSION_TTL_SECONDS, 30 * 24 * 60 * 60)
|
||||
self.assertTrue(login_rejected("Login appears to have failed (still on Login.aspx)."))
|
||||
self.assertTrue(login_rejected("Customer orders report redirected to login; check credentials."))
|
||||
self.assertFalse(login_rejected("Set REPORT_BASE_URL."))
|
||||
|
||||
def test_base_url_ignores_shared_password(self):
|
||||
import slips.crm_orders as mod
|
||||
|
||||
def setting(key):
|
||||
if key == "REPORT_BASE_URL":
|
||||
return "http://crm.example"
|
||||
return None
|
||||
|
||||
with patch.object(mod, "_setting", setting):
|
||||
self.assertEqual(report_base_url(), "http://crm.example/")
|
||||
with self.assertRaises(CrmOrdersError):
|
||||
mod._setting = lambda key: None
|
||||
report_base_url()
|
||||
|
||||
def test_fetch_uses_supplied_credentials(self):
|
||||
import slips.crm_orders as mod
|
||||
|
||||
seen = {}
|
||||
|
||||
class FakeClient:
|
||||
def __init__(self, **kwargs):
|
||||
seen.update(kwargs)
|
||||
|
||||
def fetch_customer_orders(self):
|
||||
return "", "text/csv"
|
||||
|
||||
def setting(key):
|
||||
if key == "REPORT_BASE_URL":
|
||||
return "http://crm.example"
|
||||
if key == "REPORT_ORDERS_SINCE":
|
||||
return "2026-03-21"
|
||||
return None
|
||||
|
||||
with patch.object(mod, "_setting", setting), \
|
||||
patch("lithium_systemgroup.SystemGroupClient", FakeClient), \
|
||||
patch("lithium_systemgroup.parse_customer_orders_document", lambda body, content_type="": []):
|
||||
result = recent_customer_orders("ada", "secret")
|
||||
self.assertEqual(seen["username"], "ada")
|
||||
self.assertEqual(seen["password"], "secret")
|
||||
self.assertEqual(seen["base_url"], "http://crm.example/")
|
||||
self.assertNotIn("prefer_report_env", seen)
|
||||
self.assertEqual(result["orders"], [])
|
||||
|
||||
class RejectClient(FakeClient):
|
||||
def fetch_customer_orders(self):
|
||||
from lithium_systemgroup import SystemGroupClientError
|
||||
raise SystemGroupClientError("Login appears to have failed")
|
||||
|
||||
with patch.object(mod, "_setting", setting), \
|
||||
patch("lithium_systemgroup.SystemGroupClient", RejectClient):
|
||||
with self.assertRaises(CrmAuthError):
|
||||
recent_customer_orders("ada", "wrong")
|
||||
|
||||
|
||||
class CrmSessionTests(TestCase):
|
||||
def test_session_lasts_thirty_days_and_slides(self):
|
||||
from slips import api
|
||||
|
||||
def fake(username, password, client=None):
|
||||
return {"since": "1405/01/01", "orders": []}
|
||||
|
||||
with patch.object(api, "recent_customer_orders", fake):
|
||||
res = self.client.post(
|
||||
"/api/crm-orders/session",
|
||||
data={"username": "ada", "password": "secret"},
|
||||
content_type="application/json",
|
||||
)
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertEqual(res.json()["username"], "ada")
|
||||
self.assertEqual(self.client.session["sg_username"], "ada")
|
||||
self.assertEqual(self.client.session["sg_password"], "secret")
|
||||
self.assertGreater(
|
||||
self.client.session.get_expiry_date() - timezone.now(),
|
||||
timedelta(days=29),
|
||||
)
|
||||
res = self.client.get("/api/crm-orders")
|
||||
self.assertEqual(res.status_code, 200)
|
||||
self.assertGreater(
|
||||
self.client.session.get_expiry_date() - timezone.now(),
|
||||
timedelta(days=29),
|
||||
)
|
||||
self.client.delete("/api/crm-orders/session")
|
||||
self.assertNotIn("sg_username", self.client.session)
|
||||
self.assertNotIn("sg_password", self.client.session)
|
||||
res = self.client.get("/api/crm-orders")
|
||||
self.assertEqual(res.status_code, 401)
|
||||
self.assertEqual(res.json()["code"], "login_required")
|
||||
|
||||
def test_rejected_password_is_not_stored(self):
|
||||
from slips import api
|
||||
|
||||
def fake(username, password, client=None):
|
||||
raise CrmAuthError("nope")
|
||||
|
||||
with patch.object(api, "recent_customer_orders", fake):
|
||||
res = self.client.post(
|
||||
"/api/crm-orders/session",
|
||||
data={"username": "ada", "password": "wrong"},
|
||||
content_type="application/json",
|
||||
)
|
||||
self.assertEqual(res.status_code, 401)
|
||||
self.assertEqual(res.json()["code"], "login_rejected")
|
||||
self.assertNotIn("sg_password", self.client.session)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user