"""Unit tests for the dynamic-group CEL compile/parse/evaluate logic. No DB / no Google — pure framework-free logic. Uses the real production membership-query shapes captured from the tenant (2026-07-22) so a parser regression is caught against reality, not a synthetic sample. """ from __future__ import annotations import pytest from app.domains.email_group import dynamic_cel as cel # Verbatim production queries (subset covering every shape). PROD_QUERIES = { "g-hr": "user.custom_schemas.Department_Information.L1_Dept=='HR'", "g-rdg": ( "user.custom_schemas.Department_Information.L1_Dept=='R&D Group' || " "user.custom_schemas.Department_Information.L1_Dept=='Engineering Group'" ), "g-all": ( "user.custom_schemas.Department_Information.L1_Dept=='Central Enabling Group' || " "user.custom_schemas.Department_Information.L1_Dept=='EMEA Business Group' || \n" "user.custom_schemas.Department_Information.L1_Dept=='NA Business Group' || " "user.emails.exists(email, email.address.startsWith('lanxi.xiong@pingcap.com'))\n" ), "g-all-abroad": "!(user.custom_schemas.Employee_Details.Country=='CN')", "g-my-office": ( "user.custom_schemas.Employee_Details.Country=='MY' || " "user.emails.exists(email, email.address.startsWith('chunlu.zhang'))" ), } UNPARSEABLE = { "not-suspended": "user.suspended==false", "home-mail": "(user.emails.exists(email, email.type==1)) && user.suspended == false", "test-ceg": "(user.org_unit_id==orgUnitId('03ph8a2z0k8hmbv')) && user.suspended == false", "empty": "", } def test_parse_single_field(): r = cel.parse_membership_query(PROD_QUERIES["g-hr"]) assert r.parseable assert r.field == "Department_Information.L1_Dept" assert r.match_values == ["HR"] assert not r.negate assert r.include_emails == [] def test_parse_multi_value_or(): r = cel.parse_membership_query(PROD_QUERIES["g-rdg"]) assert r.parseable assert r.match_values == ["R&D Group", "Engineering Group"] def test_parse_field_plus_extra_emails(): r = cel.parse_membership_query(PROD_QUERIES["g-all"]) assert r.parseable assert r.field == "Department_Information.L1_Dept" assert "EMEA Business Group" in r.match_values assert r.include_emails == ["lanxi.xiong@pingcap.com"] def test_parse_negation(): r = cel.parse_membership_query(PROD_QUERIES["g-all-abroad"]) assert r.parseable assert r.negate assert r.field == "Employee_Details.Country" assert r.match_values == ["CN"] @pytest.mark.parametrize("q", list(UNPARSEABLE.values())) def test_unparseable_shapes_stay_readonly(q): r = cel.parse_membership_query(q) assert not r.parseable assert r.raw_query == q.strip() @pytest.mark.parametrize("name,q", list(PROD_QUERIES.items())) def test_roundtrip(name, q): """parse → compile must reproduce a semantically-equal query that itself re-parses to the same structured rule (idempotent).""" r1 = cel.parse_membership_query(q) compiled = cel.compile_rule( field=r1.field, match_values=r1.match_values, include_emails=r1.include_emails, negate=r1.negate, ) r2 = cel.parse_membership_query(compiled) assert r2.field == r1.field assert r2.match_values == r1.match_values assert r2.include_emails == r1.include_emails assert r2.negate == r1.negate def test_compile_rejects_unknown_field(): with pytest.raises(cel.RuleValidationError): cel.compile_rule(field="Bogus.Field", match_values=["x"]) def test_compile_rejects_empty_rule(): with pytest.raises(cel.RuleValidationError): cel.compile_rule(field=None, match_values=[], include_emails=[]) def test_compile_rejects_field_without_values(): with pytest.raises(cel.RuleValidationError): cel.compile_rule( field="Department_Information.L1_Dept", match_values=[], ) def test_compile_rejects_single_quote_injection(): with pytest.raises(cel.RuleValidationError): cel.compile_rule( field="Department_Information.L1_Dept", match_values=["Evil' || user.suspended==false || 'x"], ) def _users(): return [ cel.UserSnapshot("a@pingcap.com", {"Department_Information.L1_Dept": "HR"}), cel.UserSnapshot("b@pingcap.com", {"Department_Information.L1_Dept": "HR"}), cel.UserSnapshot("c@pingcap.com", {"Department_Information.L1_Dept": "Finance"}), cel.UserSnapshot("d@pingcap.com", {"Employee_Details.Country": "CN"}), cel.UserSnapshot( "susp@pingcap.com", {"Department_Information.L1_Dept": "HR"}, suspended=True, ), ] def test_evaluate_field_match_excludes_suspended(): rule = cel.parse_membership_query(PROD_QUERIES["g-hr"]) matched = cel.evaluate_rule(rule, _users()) assert matched == {"a@pingcap.com", "b@pingcap.com"} # susp excluded def test_evaluate_negation(): rule = cel.parse_membership_query(PROD_QUERIES["g-all-abroad"]) matched = cel.evaluate_rule(rule, _users()) # Everyone active whose Country != CN → a, b, c (d is CN, susp excluded) assert matched == {"a@pingcap.com", "b@pingcap.com", "c@pingcap.com"} def test_evaluate_include_emails(): rule = cel.StructuredRule( field="Department_Information.L1_Dept", match_values=["Finance"], include_emails=["a@pingcap"], parseable=True, ) matched = cel.evaluate_rule(rule, _users()) assert matched == {"c@pingcap.com", "a@pingcap.com"} def test_evaluate_unparseable_returns_none(): rule = cel.parse_membership_query(UNPARSEABLE["home-mail"]) assert cel.evaluate_rule(rule, _users()) is None def test_dead_values_detects_zero_match(): rule = cel.StructuredRule( field="Department_Information.L1_Dept", match_values=["HR", "Ghost Dept"], parseable=True, ) counts = {"Department_Information.L1_Dept": {"HR": 2, "Finance": 1}} assert cel.dead_values(rule, counts) == ["Ghost Dept"] def test_dead_values_skips_negated_rule(): rule = cel.parse_membership_query(PROD_QUERIES["g-all-abroad"]) # Country CN has 0 in these counts, but a negated rule shouldn't warn. counts = {"Employee_Details.Country": {"US": 5}} assert cel.dead_values(rule, counts) == []