|
| 1 | +# Part of OpenSPP. See LICENSE file for full copyright and licensing details. |
| 2 | +"""Tests for OutgoingApiLogService""" |
| 3 | + |
| 4 | +from odoo.tests.common import TransactionCase |
| 5 | + |
| 6 | +from ..services.outgoing_api_log_service import OutgoingApiLogService |
| 7 | + |
| 8 | + |
| 9 | +class TestOutgoingApiLogService(TransactionCase): |
| 10 | + """Test OutgoingApiLogService functionality""" |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + super().setUp() |
| 14 | + self.outgoing_log_model = self.env["spp.api.outgoing.log"] |
| 15 | + |
| 16 | + def test_log_call_creates_record(self): |
| 17 | + """log_call creates outgoing log record via the service""" |
| 18 | + service = OutgoingApiLogService( |
| 19 | + self.env, |
| 20 | + service_name="DCI Client", |
| 21 | + service_code="crvs_main", |
| 22 | + ) |
| 23 | + |
| 24 | + result = service.log_call( |
| 25 | + url="https://crvs.example.org/api/registry/sync/search", |
| 26 | + endpoint="/registry/sync/search", |
| 27 | + http_method="POST", |
| 28 | + request_summary={"header": {"action": "search"}}, |
| 29 | + response_summary={"header": {"status": "success"}}, |
| 30 | + response_status_code=200, |
| 31 | + duration_ms=350, |
| 32 | + origin_model="spp.dci.data.source", |
| 33 | + origin_record_id=42, |
| 34 | + status="success", |
| 35 | + ) |
| 36 | + |
| 37 | + self.assertTrue(result) |
| 38 | + self.assertEqual(result.url, "https://crvs.example.org/api/registry/sync/search") |
| 39 | + self.assertEqual(result.service_name, "DCI Client") |
| 40 | + self.assertEqual(result.service_code, "crvs_main") |
| 41 | + self.assertEqual(result.status, "success") |
| 42 | + |
| 43 | + def test_log_call_failure_returns_none(self): |
| 44 | + """Logging failures return None and don't raise exceptions""" |
| 45 | + # Create a service with a broken env to trigger a failure |
| 46 | + bad_service = OutgoingApiLogService( |
| 47 | + self.env, |
| 48 | + service_name="Bad Service", |
| 49 | + service_code="bad", |
| 50 | + ) |
| 51 | + |
| 52 | + # Monkey-patch the model to raise an error |
| 53 | + original_log_call = self.outgoing_log_model.__class__.log_call |
| 54 | + |
| 55 | + def broken_log_call(self_model, **kwargs): |
| 56 | + raise RuntimeError("Database error") |
| 57 | + |
| 58 | + self.outgoing_log_model.__class__.log_call = broken_log_call |
| 59 | + try: |
| 60 | + result = bad_service.log_call( |
| 61 | + url="https://example.org/test", |
| 62 | + ) |
| 63 | + self.assertIsNone(result) |
| 64 | + finally: |
| 65 | + self.outgoing_log_model.__class__.log_call = original_log_call |
| 66 | + |
| 67 | + def test_truncate_payload(self): |
| 68 | + """_truncate_payload truncates large payloads""" |
| 69 | + service = OutgoingApiLogService( |
| 70 | + self.env, |
| 71 | + service_name="Test", |
| 72 | + service_code="test", |
| 73 | + ) |
| 74 | + |
| 75 | + # Small payload should pass through unchanged |
| 76 | + small = {"key": "value"} |
| 77 | + self.assertEqual(service._truncate_payload(small), small) |
| 78 | + |
| 79 | + # None should return None |
| 80 | + self.assertIsNone(service._truncate_payload(None)) |
| 81 | + |
| 82 | + # Large payload should be truncated |
| 83 | + large = {"data": "x" * 20000} |
| 84 | + result = service._truncate_payload(large, max_length=100) |
| 85 | + self.assertTrue(result["_truncated"]) |
| 86 | + self.assertIn("_original_length", result) |
| 87 | + self.assertIn("_preview", result) |
| 88 | + |
| 89 | + def test_service_stores_user_id(self): |
| 90 | + """Service records the correct user_id""" |
| 91 | + service = OutgoingApiLogService( |
| 92 | + self.env, |
| 93 | + service_name="Test", |
| 94 | + service_code="test", |
| 95 | + user_id=self.env.uid, |
| 96 | + ) |
| 97 | + |
| 98 | + result = service.log_call( |
| 99 | + url="https://example.org/test", |
| 100 | + ) |
| 101 | + |
| 102 | + self.assertTrue(result) |
| 103 | + self.assertEqual(result.user_id.id, self.env.uid) |
| 104 | + |
| 105 | + def test_service_stores_service_context(self): |
| 106 | + """Service stores service_name and service_code on log records""" |
| 107 | + service = OutgoingApiLogService( |
| 108 | + self.env, |
| 109 | + service_name="My Integration", |
| 110 | + service_code="my_integration_v1", |
| 111 | + ) |
| 112 | + |
| 113 | + result = service.log_call( |
| 114 | + url="https://example.org/test", |
| 115 | + ) |
| 116 | + |
| 117 | + self.assertTrue(result) |
| 118 | + self.assertEqual(result.service_name, "My Integration") |
| 119 | + self.assertEqual(result.service_code, "my_integration_v1") |
| 120 | + |
| 121 | + def test_service_default_user_id(self): |
| 122 | + """Service defaults to env.uid when user_id not provided""" |
| 123 | + service = OutgoingApiLogService( |
| 124 | + self.env, |
| 125 | + service_name="Test", |
| 126 | + service_code="test", |
| 127 | + ) |
| 128 | + |
| 129 | + self.assertEqual(service.user_id, self.env.uid) |
| 130 | + |
| 131 | + def test_truncate_payload_non_serializable(self): |
| 132 | + """_truncate_payload handles non-JSON-serializable payloads""" |
| 133 | + service = OutgoingApiLogService( |
| 134 | + self.env, |
| 135 | + service_name="Test", |
| 136 | + service_code="test", |
| 137 | + ) |
| 138 | + |
| 139 | + # Object that can't be serialized |
| 140 | + result = service._truncate_payload({"key": object()}) |
| 141 | + self.assertTrue(result["_truncated"]) |
| 142 | + self.assertIn("_error", result) |
| 143 | + |
| 144 | + def test_truncate_payload_exact_boundary(self): |
| 145 | + """_truncate_payload passes through payload at exactly max_length""" |
| 146 | + service = OutgoingApiLogService( |
| 147 | + self.env, |
| 148 | + service_name="Test", |
| 149 | + service_code="test", |
| 150 | + ) |
| 151 | + |
| 152 | + # Build a payload whose JSON serialization is exactly max_length |
| 153 | + import json |
| 154 | + |
| 155 | + max_length = 50 |
| 156 | + # {"k": "..."} — adjust value to hit exact length |
| 157 | + base = json.dumps({"k": ""}) # '{"k": ""}' = 10 chars |
| 158 | + filler = "x" * (max_length - len(base)) |
| 159 | + payload = {"k": filler} |
| 160 | + serialized = json.dumps(payload) |
| 161 | + self.assertEqual(len(serialized), max_length) |
| 162 | + |
| 163 | + result = service._truncate_payload(payload, max_length=max_length) |
| 164 | + # Should pass through unchanged (equal to limit) |
| 165 | + self.assertEqual(result, payload) |
| 166 | + self.assertNotIn("_truncated", result) |
| 167 | + |
| 168 | + def test_truncate_payload_one_over_boundary(self): |
| 169 | + """_truncate_payload truncates payload one byte over max_length""" |
| 170 | + service = OutgoingApiLogService( |
| 171 | + self.env, |
| 172 | + service_name="Test", |
| 173 | + service_code="test", |
| 174 | + ) |
| 175 | + |
| 176 | + import json |
| 177 | + |
| 178 | + max_length = 50 |
| 179 | + base = json.dumps({"k": ""}) |
| 180 | + filler = "x" * (max_length - len(base) + 1) |
| 181 | + payload = {"k": filler} |
| 182 | + serialized = json.dumps(payload) |
| 183 | + self.assertEqual(len(serialized), max_length + 1) |
| 184 | + |
| 185 | + result = service._truncate_payload(payload, max_length=max_length) |
| 186 | + self.assertTrue(result["_truncated"]) |
| 187 | + self.assertEqual(result["_original_length"], max_length + 1) |
| 188 | + self.assertEqual(len(result["_preview"]), max_length) |
0 commit comments