From c0fdbbd1b30ded642fe76a67676a7ed676e30977 Mon Sep 17 00:00:00 2001 From: Mohith26 <109003762+Mohith26@users.noreply.github.com> Date: Tue, 3 Nov 2026 02:31:14 -0500 Subject: [PATCH] fix: don't write the default event_time back onto the caller's event event() set event.event_time when it was empty, so an event object reused for a later call was sent with the first call's timestamp. Use the default for the request only. --- composer/composer.py | 8 ++++---- tests/test_composer.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/composer/composer.py b/composer/composer.py index 11e9316..fb2b9a0 100644 --- a/composer/composer.py +++ b/composer/composer.py @@ -162,16 +162,16 @@ def event( if self.config.is_enabled is False: return - # Ensure event has a timestamp - if not event.event_time: - event.event_time = int(time.time() * 1000) + # Default the timestamp for this request only. Writing it back onto the caller's + # event would give every later call with the same object the first call's time. + event_time = event.event_time or int(time.time() * 1000) # Send event to API data = make_request( url=f"{construct_api_url(self.config.api_url, self.config.api_version)}track", method="POST", headers=construct_api_headers(self.secret_key, "", source_token, user_id, session_id), - body={"type": event.type, "data": event.data, "eventTime": event.event_time}, + body={"type": event.type, "data": event.data, "eventTime": event_time}, request_timeout_ms=self.config.request_timeout_ms, ) # make_request returns only the body, so the HTTP status isn't available here. diff --git a/tests/test_composer.py b/tests/test_composer.py index c848fff..03a75cc 100644 --- a/tests/test_composer.py +++ b/tests/test_composer.py @@ -1,10 +1,12 @@ """Tests for Composer client.""" import json +from types import SimpleNamespace import pytest import responses +import composer.composer as composer_module from composer.composer import Composer, Dodgeball from composer.constants import VerificationOutcome, VerificationStatus from composer.exceptions import ( @@ -378,6 +380,44 @@ def test_event(): assert responses.calls[0].request.url == "https://api.dodgeballhq.com/v1/track" +@responses.activate +def test_event_does_not_write_default_time_back_to_event(monkeypatch): + """A reused event gets a fresh default timestamp on each call.""" + responses.add( + responses.POST, + "https://api.dodgeballhq.com/v1/track", + json={"success": True}, + status=200, + ) + clock = iter([1_000.0, 2_000.0]) + monkeypatch.setattr(composer_module, "time", SimpleNamespace(time=lambda: next(clock))) + + composer = Composer("test_secret") + event = TrackEvent(type="login", data={"userId": "user123"}) + composer.event(event, session_id="sess123") + composer.event(event, session_id="sess123") + + sent_times = [json.loads(call.request.body)["eventTime"] for call in responses.calls] + assert sent_times == [1_000_000, 2_000_000] + assert event.event_time is None + + +@responses.activate +def test_event_keeps_caller_event_time(): + """An event_time set by the caller is sent unchanged.""" + responses.add( + responses.POST, + "https://api.dodgeballhq.com/v1/track", + json={"success": True}, + status=200, + ) + + composer = Composer("test_secret") + composer.event(TrackEvent(type="login", event_time=1_700_000_000_000), session_id="sess123") + + assert json.loads(responses.calls[0].request.body)["eventTime"] == 1_700_000_000_000 + + def test_event_missing_event(capsys): """Test event with missing event data.""" composer = Composer("test_secret")