|
| 1 | +import unittest |
| 2 | + |
| 3 | +import mock |
| 4 | + |
| 5 | +from posthog.client import Client |
| 6 | +from posthog.test.test_utils import FAKE_TEST_API_KEY |
| 7 | + |
| 8 | + |
| 9 | +class TestClient(unittest.TestCase): |
| 10 | + @classmethod |
| 11 | + def setUpClass(cls): |
| 12 | + # This ensures no real HTTP POST requests are made |
| 13 | + cls.client_post_patcher = mock.patch("posthog.client.batch_post") |
| 14 | + cls.consumer_post_patcher = mock.patch("posthog.consumer.batch_post") |
| 15 | + cls.client_post_patcher.start() |
| 16 | + cls.consumer_post_patcher.start() |
| 17 | + |
| 18 | + @classmethod |
| 19 | + def tearDownClass(cls): |
| 20 | + cls.client_post_patcher.stop() |
| 21 | + cls.consumer_post_patcher.stop() |
| 22 | + |
| 23 | + def set_fail(self, e, batch): |
| 24 | + """Mark the failure handler""" |
| 25 | + print("FAIL", e, batch) # noqa: T201 |
| 26 | + self.failed = True |
| 27 | + |
| 28 | + def setUp(self): |
| 29 | + self.failed = False |
| 30 | + self.client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail) |
| 31 | + |
| 32 | + def test_before_send_callback_modifies_event(self): |
| 33 | + """Test that before_send callback can modify events.""" |
| 34 | + processed_events = [] |
| 35 | + |
| 36 | + def my_before_send(event): |
| 37 | + processed_events.append(event.copy()) |
| 38 | + if "properties" not in event: |
| 39 | + event["properties"] = {} |
| 40 | + event["properties"]["processed_by_before_send"] = True |
| 41 | + return event |
| 42 | + |
| 43 | + client = Client( |
| 44 | + FAKE_TEST_API_KEY, on_error=self.set_fail, before_send=my_before_send |
| 45 | + ) |
| 46 | + success, msg = client.capture("user1", "test_event", {"original": "value"}) |
| 47 | + |
| 48 | + self.assertTrue(success) |
| 49 | + self.assertEqual(msg["properties"]["processed_by_before_send"], True) |
| 50 | + self.assertEqual(msg["properties"]["original"], "value") |
| 51 | + self.assertEqual(len(processed_events), 1) |
| 52 | + self.assertEqual(processed_events[0]["event"], "test_event") |
| 53 | + |
| 54 | + def test_before_send_callback_drops_event(self): |
| 55 | + """Test that before_send callback can drop events by returning None.""" |
| 56 | + |
| 57 | + def drop_test_events(event): |
| 58 | + if event.get("event") == "test_drop_me": |
| 59 | + return None |
| 60 | + return event |
| 61 | + |
| 62 | + client = Client( |
| 63 | + FAKE_TEST_API_KEY, on_error=self.set_fail, before_send=drop_test_events |
| 64 | + ) |
| 65 | + |
| 66 | + # Event should be dropped |
| 67 | + success, msg = client.capture("user1", "test_drop_me") |
| 68 | + self.assertTrue(success) |
| 69 | + self.assertIsNone(msg) |
| 70 | + |
| 71 | + # Event should go through |
| 72 | + success, msg = client.capture("user1", "keep_me") |
| 73 | + self.assertTrue(success) |
| 74 | + self.assertIsNotNone(msg) |
| 75 | + self.assertEqual(msg["event"], "keep_me") |
| 76 | + |
| 77 | + def test_before_send_callback_handles_exceptions(self): |
| 78 | + """Test that exceptions in before_send don't crash the client.""" |
| 79 | + |
| 80 | + def buggy_before_send(event): |
| 81 | + raise ValueError("Oops!") |
| 82 | + |
| 83 | + client = Client( |
| 84 | + FAKE_TEST_API_KEY, on_error=self.set_fail, before_send=buggy_before_send |
| 85 | + ) |
| 86 | + success, msg = client.capture("user1", "robust_event") |
| 87 | + |
| 88 | + # Event should still be sent despite the exception |
| 89 | + self.assertTrue(success) |
| 90 | + self.assertIsNotNone(msg) |
| 91 | + self.assertEqual(msg["event"], "robust_event") |
| 92 | + |
| 93 | + def test_before_send_callback_works_with_all_event_types(self): |
| 94 | + """Test that before_send works with capture, identify, set, etc.""" |
| 95 | + |
| 96 | + def add_marker(event): |
| 97 | + if "properties" not in event: |
| 98 | + event["properties"] = {} |
| 99 | + event["properties"]["marked"] = True |
| 100 | + return event |
| 101 | + |
| 102 | + client = Client( |
| 103 | + FAKE_TEST_API_KEY, on_error=self.set_fail, before_send=add_marker |
| 104 | + ) |
| 105 | + |
| 106 | + # Test capture |
| 107 | + success, msg = client.capture("user1", "event") |
| 108 | + self.assertTrue(success) |
| 109 | + self.assertTrue(msg["properties"]["marked"]) |
| 110 | + |
| 111 | + # Test identify |
| 112 | + success, msg = client.identify("user1", {"trait": "value"}) |
| 113 | + self.assertTrue(success) |
| 114 | + self.assertTrue(msg["properties"]["marked"]) |
| 115 | + |
| 116 | + # Test set |
| 117 | + success, msg = client.set("user1", {"prop": "value"}) |
| 118 | + self.assertTrue(success) |
| 119 | + self.assertTrue(msg["properties"]["marked"]) |
| 120 | + |
| 121 | + # Test page |
| 122 | + success, msg = client.page("user1", "https://example.com") |
| 123 | + self.assertTrue(success) |
| 124 | + self.assertTrue(msg["properties"]["marked"]) |
| 125 | + |
| 126 | + def test_before_send_callback_disabled_when_none(self): |
| 127 | + """Test that client works normally when before_send is None.""" |
| 128 | + client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, before_send=None) |
| 129 | + success, msg = client.capture("user1", "normal_event") |
| 130 | + |
| 131 | + self.assertTrue(success) |
| 132 | + self.assertIsNotNone(msg) |
| 133 | + self.assertEqual(msg["event"], "normal_event") |
| 134 | + |
| 135 | + def test_before_send_callback_pii_scrubbing_example(self): |
| 136 | + """Test a realistic PII scrubbing use case.""" |
| 137 | + |
| 138 | + def scrub_pii(event): |
| 139 | + properties = event.get("properties", {}) |
| 140 | + |
| 141 | + # Mask email but keep domain |
| 142 | + if "email" in properties: |
| 143 | + email = properties["email"] |
| 144 | + if "@" in email: |
| 145 | + domain = email.split("@")[1] |
| 146 | + properties["email"] = f"***@{domain}" |
| 147 | + else: |
| 148 | + properties["email"] = "***" |
| 149 | + |
| 150 | + # Remove credit card |
| 151 | + properties.pop("credit_card", None) |
| 152 | + |
| 153 | + return event |
| 154 | + |
| 155 | + client = Client( |
| 156 | + FAKE_TEST_API_KEY, on_error=self.set_fail, before_send=scrub_pii |
| 157 | + ) |
| 158 | + success, msg = client.capture( |
| 159 | + "user1", |
| 160 | + "form_submit", |
| 161 | + { |
| 162 | + |
| 163 | + "credit_card": "1234-5678-9012-3456", |
| 164 | + "form_name": "contact", |
| 165 | + }, |
| 166 | + ) |
| 167 | + |
| 168 | + self.assertTrue(success) |
| 169 | + self.assertEqual(msg["properties"]["email"], "***@example.com") |
| 170 | + self.assertNotIn("credit_card", msg["properties"]) |
| 171 | + self.assertEqual(msg["properties"]["form_name"], "contact") |
0 commit comments