from __future__ import annotations import argparse import json from pathlib import Path from normalize_1c_its_cookie import normalize_cookie ROOT = Path(__file__).resolve().parents[1] DEFAULT_OUTPUT = ROOT / "reports" / "1c-its-cookie-normalizer-check.json" CASES = [ { "name": "request_cookie_header", "input": "Cookie: sid=abc; theme=dark", "ok": True, "cookie": "sid=abc; theme=dark", }, { "name": "reject_yandex_set_cookie", "input": "bh=abc; Domain=.yandex.com; Path=/", "ok": False, "error": "domain_attributes_do_not_match_target", }, { "name": "accept_its_set_cookie", "input": "sid=abc; Domain=.its.1c.ru; Path=/", "ok": True, "cookie": "sid=abc", }, { "name": "json_filters_target_domain", "input": '[{"domain":"its.1c.ru","name":"sid","value":"abc"},{"domain":"yandex.com","name":"bh","value":"no"}]', "ok": True, "cookie": "sid=abc", }, { "name": "netscape_filters_target_domain", "input": ".its.1c.ru\tTRUE\t/\tTRUE\t0\tsid\tabc\n.yandex.com\tTRUE\t/\tTRUE\t0\tbh\tno", "ok": True, "cookie": "sid=abc", }, ] def run_cases() -> dict: checks = [] for case in CASES: result = normalize_cookie(case["input"], "its.1c.ru") passed = result["ok"] == case["ok"] if "cookie" in case: passed = passed and result["cookie"] == case["cookie"] if "error" in case: passed = passed and case["error"] in result["errors"] checks.append( { "name": case["name"], "status": "passed" if passed else "failed", "expected": {key: case[key] for key in ("ok", "cookie", "error") if key in case}, "actual": { "ok": result["ok"], "cookie": result["cookie"], "warnings": result["warnings"], "errors": result["errors"], "format": result.get("format"), }, } ) return { "schema": "onec_its_cookie_normalizer_check.v1", "passed": all(check["status"] == "passed" for check in checks), "checks": checks, } def main() -> int: parser = argparse.ArgumentParser(description="Check 1C:ITS cookie normalizer behavior.") parser.add_argument("--output", type=Path, default=DEFAULT_OUTPUT) parser.add_argument("--print", action="store_true", dest="print_report") args = parser.parse_args() report = run_cases() args.output.parent.mkdir(parents=True, exist_ok=True) args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2) + "\n", encoding="utf-8") if args.print_report: print(json.dumps(report, ensure_ascii=False, indent=2)) else: print(json.dumps({"passed": report["passed"], "output": str(args.output)}, ensure_ascii=False)) return 0 if report["passed"] else 1 if __name__ == "__main__": raise SystemExit(main())