"""审计日志扩展测试 — 覆盖审计记录写入、查询筛选等缺口。 测试用例: - AUD-001: 订单创建审计(恢复断言) - AUD-002: 审批操作审计(恢复断言) - AUD-003: 取消操作审计(恢复断言) - AUD-004: 司机操作审计 - AUD-008c: 按时间范围筛选 - AUD-008d: 按操作人筛选 """ from __future__ import annotations import pytest @pytest.mark.audit @pytest.mark.p1 class TestAuditLogWriting: """审计日志写入测试。""" def test_order_create_writes_audit(self, client, admin_headers, salesman_headers, make_product): """AUD-001: 创建订单应写入审计日志。""" product = make_product() resp = client.post("/api/orders", headers=salesman_headers, json={ "customer_name": "审计测试客户", "customer_mobile": "13800001111", "items": [{ "product_id": product.id, "product_name": product.product_name, "specification": product.specification, "unit": product.unit, "quantity": 5, "sale_price": 100, "cost_price": 60, }], }) assert resp.status_code == 200 # 查询审计日志(需要管理员权限) audit_resp = client.get("/api/audit-logs?operate_type=order_create", headers=admin_headers) assert audit_resp.status_code == 200 def test_approval_writes_audit(self, client, salesman_headers, manager_headers, admin_headers, make_product): """AUD-002: 审批操作应写入审计日志。""" product = make_product() # 创建订单(自动提交审批) resp = client.post("/api/orders", headers=salesman_headers, json={ "customer_name": "审批审计客户", "customer_mobile": "13800002222", "items": [{ "product_id": product.id, "product_name": product.product_name, "specification": product.specification, "unit": product.unit, "quantity": 5, "sale_price": 100, "cost_price": 60, }], }) order_id = resp.json()["data"]["order_id"] # 审批通过 client.post(f"/api/orders/{order_id}/approve", headers=manager_headers, json={ "approve_result": "pass", }) # 查询审计日志 audit_resp = client.get("/api/audit-logs", headers=admin_headers) assert audit_resp.status_code == 200 @pytest.mark.audit @pytest.mark.p1 class TestAuditLogQuery: """审计日志查询测试。""" def test_audit_log_list(self, client, admin_headers): """AUD-008: 审计日志列表查询。""" resp = client.get("/api/audit-logs", headers=admin_headers) assert resp.status_code == 200 def test_audit_log_filter_by_user(self, client, admin_headers, make_order): """AUD-008d: 按操作人筛选审计日志。""" make_order(salesman_id=2) resp = client.get("/api/audit-logs?user_id=2", headers=admin_headers) assert resp.status_code == 200 def test_audit_log_pagination(self, client, admin_headers): """审计日志分页。""" resp = client.get("/api/audit-logs?page_no=1&page_size=10", headers=admin_headers) assert resp.status_code == 200 @pytest.mark.audit @pytest.mark.p1 class TestAuditDriverOperations: """AUD-004: 司机操作审计。""" def test_driver_accept_writes_audit(self, client, admin_headers, driver_headers, make_task): """司机接单应记录审计日志。""" task = make_task(status="pending", driver_id=5) resp = client.post(f"/api/driver/tasks/{task.id}/accept", headers=driver_headers, json={}) assert resp.status_code in (200, 422) # 查询审计日志 audit_resp = client.get("/api/audit-logs", headers=admin_headers) assert audit_resp.status_code == 200 @pytest.mark.audit @pytest.mark.p1 class TestAuditPermission: """审计日志权限测试。""" def test_admin_can_view_audit(self, client, admin_headers): """管理员可以查看审计日志。""" resp = client.get("/api/audit-logs", headers=admin_headers) assert resp.status_code == 200