def test_set_is_identified(self) -> None: distinct_id = "777" Person.objects.create(team_id=self.team.pk, distinct_ids=[distinct_id]) person_before_event = get_person_by_distinct_id(team_id=self.team.pk, distinct_id=distinct_id) self.assertFalse(person_before_event["is_identified"]) process_event( distinct_id, "", "", {"event": "$identify", "properties": {},}, self.team.pk, now().isoformat(), now().isoformat(), ) process_event_ee( distinct_id, "", "", {"event": "$identify", "properties": {},}, self.team.pk, now().isoformat(), now().isoformat(), ) # Assume that clickhouse has done replacement ch_client.execute("OPTIMIZE TABLE person") person_after_event = get_person_by_distinct_id(team_id=self.team.pk, distinct_id=distinct_id) self.assertTrue(person_after_event["is_identified"])
def test_capture_new_person(self) -> None: user = self._create_user('tim') action1 = Action.objects.create(team=self.team) ActionStep.objects.create(action=action1, selector='a', event='$autocapture') action2 = Action.objects.create(team=self.team) ActionStep.objects.create(action=action2, selector='a', event='$autocapture') team_id = self.team.pk with self.assertNumQueries(19): process_event(2, '', '', { 'event': '$autocapture', 'properties': { 'distinct_id': 2, 'token': self.team.api_token, '$elements': [ {'tag_name': 'a', 'nth_child': 1, 'nth_of_type': 2, 'attr__class': 'btn btn-sm'}, {'tag_name': 'div', 'nth_child': 1, 'nth_of_type': 2, '$el_text': '💻'} ] }, }, team_id, now().isoformat(), now().isoformat()) self.assertEqual(Person.objects.get().distinct_ids, ["2"]) event = Event.objects.get() self.assertEqual(event.event, '$autocapture') elements = ElementGroup.objects.get(hash=event.elements_hash).element_set.all().order_by('order') self.assertEqual(elements[0].tag_name, 'a') self.assertEqual(elements[0].attr_class, ['btn', 'btn-sm']) self.assertEqual(elements[1].order, 1) self.assertEqual(elements[1].text, '💻') self.assertEqual(event.distinct_id, "2")
def test_capture_first_team_event(self, mock: Any) -> None: """ Assert that we report to posthoganalytics the first event ingested by a team. """ team = Team.objects.create(api_token="token456") user = User.objects.create_user("*****@*****.**") team.users.add(user) team.save() process_event( 2, "", "", { "event": "$autocapture", "properties": { "distinct_id": 1, "token": team.api_token, "$elements": [{"tag_name": "a", "nth_child": 1, "nth_of_type": 2, "attr__class": "btn btn-sm",},], }, }, team.pk, now().isoformat(), now().isoformat(), ) mock.assert_called_once_with(user.distinct_id, "first team event ingested", {"team": str(team.uuid)}) team.refresh_from_db() self.assertEqual(team.ingested_event, True)
def test_distinct_with_anonymous_id_which_was_already_created( self) -> None: Person.objects.create(team=self.team, distinct_ids=["anonymous_id"]) Person.objects.create( team=self.team, distinct_ids=["new_distinct_id"], properties={"email": "*****@*****.**"}, ) process_event( "new_distinct_id", "", "", { "event": "$identify", "properties": { "$anon_distinct_id": "anonymous_id", "token": self.team.api_token, "distinct_id": "new_distinct_id", }, }, self.team.pk, now().isoformat(), now().isoformat(), ) # self.assertEqual(Event.objects.count(), 0) person = Person.objects.get() self.assertEqual(person.distinct_ids, ["anonymous_id", "new_distinct_id"]) self.assertEqual(person.properties["email"], "*****@*****.**")
def test_distinct_team_leakage(self): team2 = Team.objects.create() Person.objects.create(team=team2, distinct_ids=['2'], properties={'email': '*****@*****.**'}) Person.objects.create(team=self.team, distinct_ids=['1', '2']) try: process_event( '2', '', '', { 'event': '$identify', 'properties': { '$anon_distinct_id': '1', 'token': self.team.api_token, 'distinct_id': '2' }, }, self.team.pk, now().isoformat()) except: pass people = Person.objects.all() self.assertEqual(people.count(), 2) self.assertEqual(people[1].team, self.team) self.assertEqual(people[1].properties, {}) self.assertEqual(people[1].distinct_ids, ["1", "2"]) self.assertEqual(people[0].team, team2) self.assertEqual(people[0].distinct_ids, ["2"])
def test_set_is_identified(self) -> None: distinct_id = "777" Person.objects.create(team_id=self.team.pk, distinct_ids=[distinct_id]) person_before_event = get_person_by_distinct_id( team_id=self.team.pk, distinct_id=distinct_id) self.assertFalse(person_before_event["is_identified"]) process_event( distinct_id, "", "", { "event": "$identify", "properties": {}, }, self.team.pk, now().isoformat(), now().isoformat(), ) process_event_ee( distinct_id, "", "", { "event": "$identify", "properties": {}, }, self.team.pk, now().isoformat(), now().isoformat(), ) person_after_event = get_person_by_distinct_id(team_id=self.team.pk, distinct_id=distinct_id) self.assertTrue(person_after_event["is_identified"])
def test_long_htext(self) -> None: process_event( 'new_distinct_id', '', '', { 'event': '$autocapture', 'properties': { 'distinct_id': 'new_distinct_id', 'token': self.team.api_token, '$elements': [ { 'tag_name': 'a', '$el_text': 'a' * 2050, 'attr__href': 'a' * 2050, 'nth_child': 1, 'nth_of_type': 2, 'attr__class': 'btn btn-sm' }, ] } }, self.team.pk, now().isoformat(), now().isoformat()) self.assertEqual(len(Element.objects.get().href), 2048) self.assertEqual(len(Element.objects.get().text), 400)
def test_distinct_with_anonymous_id(self): Person.objects.create(team=self.team, distinct_ids=['anonymous_id']) process_event( 'new_distinct_id', '', '', { 'event': '$identify', 'properties': { '$anon_distinct_id': 'anonymous_id', 'token': self.team.api_token, 'distinct_id': 'new_distinct_id' }, }, self.team.pk, now().isoformat()) self.assertEqual(Event.objects.count(), 1) self.assertEqual(Person.objects.get().distinct_ids, ["anonymous_id", "new_distinct_id"]) # check no errors as this call can happen multiple times process_event( 'new_distinct_id', '', '', { 'event': '$identify', 'properties': { '$anon_distinct_id': 'anonymous_id', 'token': self.team.api_token, 'distinct_id': 'new_distinct_id' }, }, self.team.pk, now().isoformat())
def test_distinct_team_leakage(self) -> None: team2 = Team.objects.create() Person.objects.create(team=team2, distinct_ids=["2"], properties={"email": "*****@*****.**"}) Person.objects.create(team=self.team, distinct_ids=["1", "2"]) try: process_event( "2", "", "", { "event": "$identify", "properties": { "$anon_distinct_id": "1", "token": self.team.api_token, "distinct_id": "2", }, }, self.team.pk, now().isoformat(), now().isoformat(), ) except: pass people = Person.objects.all() self.assertEqual(people.count(), 2) self.assertEqual(people[1].team, self.team) self.assertEqual(people[1].properties, {}) self.assertEqual(people[1].distinct_ids, ["1", "2"]) self.assertEqual(people[0].team, team2) self.assertEqual(people[0].distinct_ids, ["2"])
def test_capture_no_element(self) -> None: user = self._create_user("tim") Person.objects.create(team_id=self.team.pk, distinct_ids=["asdfasdfasdf"]) process_event( "asdfasdfasdf", "", "", {"event": "$pageview", "properties": {"distinct_id": "asdfasdfasdf", "token": self.team.api_token,},}, self.team.pk, now().isoformat(), now().isoformat(), ) process_event_ee( "asdfasdfasdf", "", "", {"event": "$pageview", "properties": {"distinct_id": "asdfasdfasdf", "token": self.team.api_token,},}, self.team.pk, now().isoformat(), now().isoformat(), ) distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)] self.assertEqual(distinct_ids, ["asdfasdfasdf"]) events = get_events() self.assertEqual(events[0]["event"], "$pageview")
def test_alias_merge_properties(self) -> None: Person.objects.create( distinct_ids=["old_distinct_id"], team_id=self.team.pk, properties={"key_on_both": "old value both", "key_on_old": "old value"}, ) Person.objects.create( distinct_ids=["new_distinct_id"], team_id=self.team.pk, properties={"key_on_both": "new value both", "key_on_new": "new value"}, ) process_event( "new_distinct_id", "", "", { "event": "$create_alias", "properties": { "distinct_id": "new_distinct_id", "token": self.team.api_token, "alias": "old_distinct_id", }, }, self.team.pk, now().isoformat(), now().isoformat(), ) process_event_ee( "new_distinct_id", "", "", { "event": "$create_alias", "properties": { "distinct_id": "new_distinct_id", "token": self.team.api_token, "alias": "old_distinct_id", }, }, self.team.pk, now().isoformat(), now().isoformat(), ) events = get_events() self.assertEqual(len(events), 1) distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)] self.assertEqual(sorted(distinct_ids), sorted(["old_distinct_id", "new_distinct_id"])) # Assume that clickhouse has done replacement ch_client.execute("OPTIMIZE TABLE person") persons = get_persons(team_id=self.team.pk) self.assertEqual( persons[0]["properties"], {"key_on_both": "new value both", "key_on_new": "new value", "key_on_old": "old value",}, )
def test_anonymized_ip_capture(self) -> None: self.team.anonymize_ips = True self.team.save() user = self._create_user("tim") Person.objects.create(team=self.team, distinct_ids=["asdfasdfasdf"]) process_event( "asdfasdfasdf", "11.12.13.14", "", { "event": "$pageview", "properties": { "distinct_id": "asdfasdfasdf", "token": self.team.api_token, }, }, self.team.pk, now().isoformat(), now().isoformat(), ) event = Event.objects.get() self.assertNotIn("$ip", event.properties.keys())
def test_offset_timestamp_no_sent_at(self) -> None: process_event( "distinct_id", "", "", { "offset": 150, "event": "$autocapture", "distinct_id": "distinct_id", }, self.team.pk, "2020-01-01T12:00:05.200Z", None, ) # no sent at makes no difference for offset process_event_ee( "distinct_id", "", "", { "offset": 150, "event": "$autocapture", "distinct_id": "distinct_id", }, self.team.pk, "2020-01-01T12:00:05.200Z", None, ) # no sent at makes no difference for offset events = get_events() returned_time = datetime.strptime(events[0]["timestamp"], "%Y-%m-%dT%H:%M:%S.%f%z") self.assertEqual(returned_time.isoformat(), "2020-01-01T12:00:05.050000+00:00")
def test_alias_before_person(self) -> None: process_event( "new_distinct_id", "", "", { "event": "$create_alias", "properties": { "distinct_id": "new_distinct_id", "token": self.team.api_token, "alias": "old_distinct_id", }, }, self.team.pk, now().isoformat(), now().isoformat(), ) person1 = Person.objects.get( team=self.team, persondistinctid__distinct_id="old_distinct_id") person2 = Person.objects.get( team=self.team, persondistinctid__distinct_id="new_distinct_id") self.assertEqual(person1, person2) self.assertEqual(Event.objects.count(), 1) self.assertEqual(Person.objects.get().distinct_ids, ["new_distinct_id", "old_distinct_id"])
def test_alias_twice(self) -> None: Person.objects.create(team=self.team, distinct_ids=['old_distinct_id']) process_event('new_distinct_id', '', '', { 'event': '$create_alias', 'properties': { 'distinct_id': 'new_distinct_id', 'token': self.team.api_token, 'alias': 'old_distinct_id' }, }, self.team.pk, now().isoformat(), now().isoformat()) Person.objects.create(team=self.team, distinct_ids=['old_distinct_id_2']) process_event('new_distinct_id', '', '', { 'event': '$create_alias', 'properties': { 'distinct_id': 'new_distinct_id', 'token': self.team.api_token, 'alias': 'old_distinct_id_2' }, }, self.team.pk, now().isoformat(), now().isoformat()) self.assertEqual(Event.objects.count(), 2) self.assertEqual(Person.objects.get().distinct_ids, ["old_distinct_id", "new_distinct_id", "old_distinct_id_2"])
def test_capture_sent_at(self) -> None: self._create_user("tim") Person.objects.create(team=self.team, distinct_ids=["asdfasdfasdf"]) right_now = now() tomorrow = right_now + timedelta(days=1, hours=2) tomorrow_sent_at = right_now + timedelta(days=1, hours=2, minutes=10) # event sent_at 10 minutes after timestamp process_event( "movie played", "", "", { "event": "$pageview", "timestamp": tomorrow.isoformat(), "properties": { "distinct_id": "asdfasdfasdf", "token": self.team.api_token, }, }, self.team.pk, right_now.isoformat(), tomorrow_sent_at.isoformat(), ) event = Event.objects.get() event_seconds_before_now = (right_now - event.timestamp).seconds # assert that the event is actually recorded 10 minutes before now self.assertGreater(event_seconds_before_now, 590) self.assertLess(event_seconds_before_now, 610)
def test_capture_no_sent_at(self) -> None: self._create_user("james") Person.objects.create(team=self.team, distinct_ids=["asdfasdfasdf"]) right_now = now() tomorrow = right_now + timedelta(days=1, hours=2) # event sent_at 10 minutes after timestamp process_event( "movie played", "", "", { "event": "$pageview", "timestamp": tomorrow.isoformat(), "properties": { "distinct_id": "asdfasdfasdf", "token": self.team.api_token, }, }, self.team.pk, right_now.isoformat(), None, ) event = Event.objects.get() difference = abs((tomorrow - event.timestamp).seconds) self.assertLess(difference, 1)
def test_distinct_with_multiple_anonymous_ids_which_were_already_created(self) -> None: # logging in the first time Person.objects.create(team=self.team, distinct_ids=['anonymous_id']) Person.objects.create(team=self.team, distinct_ids=['new_distinct_id'], properties={'email': '*****@*****.**'}) process_event('new_distinct_id', '', '', { 'event': '$identify', 'properties': { '$anon_distinct_id': 'anonymous_id', 'token': self.team.api_token, 'distinct_id': 'new_distinct_id' }, }, self.team.pk, now().isoformat(), now().isoformat()) # self.assertEqual(Event.objects.count(), 0) person = Person.objects.get() self.assertEqual(person.distinct_ids, ["anonymous_id", "new_distinct_id"]) self.assertEqual(person.properties['email'], '*****@*****.**') # logging in another time Person.objects.create(team=self.team, distinct_ids=['anonymous_id_2']) process_event('new_distinct_id', '', '', { 'event': '$identify', 'properties': { '$anon_distinct_id': 'anonymous_id_2', 'token': self.team.api_token, 'distinct_id': 'new_distinct_id' }, }, self.team.pk, now().isoformat(), now().isoformat()) person = Person.objects.get() self.assertEqual(person.distinct_ids, ["anonymous_id", "new_distinct_id", "anonymous_id_2"]) self.assertEqual(person.properties['email'], '*****@*****.**')
def test_capture_sent_at_no_timezones(self) -> None: self._create_user('tim') Person.objects.create(team=self.team, distinct_ids=['asdfasdfasdf']) right_now = now() tomorrow = right_now + timedelta(days=1, hours=2) tomorrow_sent_at = right_now + timedelta(days=1, hours=2, minutes=10) # remove timezones tomorrow = tomorrow.replace(tzinfo=None) tomorrow_sent_at = tomorrow_sent_at.replace(tzinfo=None) # event sent_at 10 minutes after timestamp process_event('movie played', '', '', { 'event': '$pageview', 'timestamp': tomorrow.isoformat(), 'properties': { 'distinct_id': 'asdfasdfasdf', 'token': self.team.api_token, }, }, self.team.pk, right_now.isoformat(), tomorrow_sent_at.isoformat()) event = Event.objects.get() event_seconds_before_now = (right_now - event.timestamp).seconds # assert that the event is actually recorded 10 minutes before now self.assertGreater(event_seconds_before_now, 590) self.assertLess(event_seconds_before_now, 610)
def test_alias_merge_properties(self) -> None: Person.objects.create( team=self.team, distinct_ids=['old_distinct_id'], properties={'key_on_both': 'old value both', 'key_on_old': 'old value'}) Person.objects.create( team=self.team, distinct_ids=['new_distinct_id'], properties={'key_on_both': 'new value both', 'key_on_new': 'new value'}) process_event('new_distinct_id', '', '', { 'event': '$create_alias', 'properties': { 'distinct_id': 'new_distinct_id', 'token': self.team.api_token, 'alias': 'old_distinct_id' }, }, self.team.pk, now().isoformat(), now().isoformat()) self.assertEqual(Event.objects.count(), 1) person = Person.objects.get() self.assertEqual(person.distinct_ids, ["old_distinct_id", "new_distinct_id"]) self.assertEqual(person.properties, { 'key_on_both': 'new value both', 'key_on_new': 'new value', 'key_on_old': 'old value' })
def test_long_htext(self) -> None: process_event( "new_distinct_id", "", "", { "event": "$autocapture", "properties": { "distinct_id": "new_distinct_id", "token": self.team.api_token, "$elements": [ { "tag_name": "a", "$el_text": "a" * 2050, "attr__href": "a" * 2050, "nth_child": 1, "nth_of_type": 2, "attr__class": "btn btn-sm", }, ], }, }, self.team.pk, now().isoformat(), now().isoformat(), ) self.assertEqual(len(Element.objects.get().href), 2048) self.assertEqual(len(Element.objects.get().text), 400)
def test_ip_capture(self) -> None: user = self._create_user("tim") Person.objects.create(team_id=self.team.pk, distinct_ids=["asdfasdfasdf"]) process_event( "asdfasdfasdf", "11.12.13.14", "", {"event": "$pageview", "properties": {"distinct_id": "asdfasdfasdf", "token": self.team.api_token,},}, self.team.pk, now().isoformat(), now().isoformat(), ) process_event_ee( "asdfasdfasdf", "11.12.13.14", "", {"event": "$pageview", "properties": {"distinct_id": "asdfasdfasdf", "token": self.team.api_token,},}, self.team.pk, now().isoformat(), now().isoformat(), ) events = get_events() self.assertEqual(events[0]["properties"]["$ip"], '"11.12.13.14"')
def test_snapshot_event_stored_as_session_recording_event(self) -> None: process_event( "some-id", "", "", { "event": "$snapshot", "properties": { "$session_id": "abcf-efg", "$snapshot_data": { "timestamp": 123, } } }, self.team.pk, now().isoformat(), now().isoformat(), ) self.assertEqual(SessionRecordingEvent.objects.count(), 1) self.assertEqual(Event.objects.count(), 0) event = SessionRecordingEvent.objects.get() self.assertEqual(event.session_id, "abcf-efg") self.assertEqual(event.distinct_id, "some-id") self.assertEqual(event.snapshot_data, { "timestamp": 123, })
def test_capture_new_person(self) -> None: user = self._create_user("tim") action1 = Action.objects.create(team=self.team) ActionStep.objects.create(action=action1, selector="a", event="$autocapture") action2 = Action.objects.create(team=self.team) ActionStep.objects.create(action=action2, selector="a", event="$autocapture") team_id = self.team.pk self.team.ingested_event = True # avoid sending `first team event ingested` to PostHog self.team.save() with self.assertNumQueries(30 if settings.EE_AVAILABLE else 28 ): # extra queries to check for hooks process_event( 2, "", "", { "event": "$autocapture", "properties": { "distinct_id": 2, "token": self.team.api_token, "$elements": [ { "tag_name": "a", "nth_child": 1, "nth_of_type": 2, "attr__class": "btn btn-sm", }, { "tag_name": "div", "nth_child": 1, "nth_of_type": 2, "$el_text": "💻", }, ], }, }, team_id, now().isoformat(), now().isoformat(), ) self.assertEqual(Person.objects.get().distinct_ids, ["2"]) event = Event.objects.order_by("-pk")[0] self.assertEqual(event.event, "$autocapture") elements = ElementGroup.objects.get( hash=event.elements_hash).element_set.all().order_by("order") self.assertEqual(elements[0].tag_name, "a") self.assertEqual(elements[0].attr_class, ["btn", "btn-sm"]) self.assertEqual(elements[1].order, 1) self.assertEqual(elements[1].text, "💻") self.assertEqual(event.distinct_id, "2")
def test_capture_new_person(self) -> None: user = self._create_user("tim") team_id = self.team.pk # TODO: with self.assertNumQueries(7): process_event( 2, "", "", { "event": "$autocapture", "properties": { "distinct_id": 2, "token": self.team.api_token, "$elements": [ {"tag_name": "a", "nth_child": 1, "nth_of_type": 2, "attr__class": "btn btn-sm",}, {"tag_name": "div", "nth_child": 1, "nth_of_type": 2, "$el_text": "💻",}, ], }, }, team_id, now().isoformat(), now().isoformat(), ) process_event_ee( 2, "", "", { "event": "$autocapture", "properties": { "distinct_id": 2, "token": self.team.api_token, "$elements": [ {"tag_name": "a", "nth_child": 1, "nth_of_type": 2, "attr__class": "btn btn-sm",}, {"tag_name": "div", "nth_child": 1, "nth_of_type": 2, "$el_text": "💻",}, ], }, }, team_id, now().isoformat(), now().isoformat(), ) distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)] self.assertEqual(distinct_ids, ["2"]) events = get_events() self.assertEqual(events[0]["event"], "$autocapture") elements = get_elements_by_elements_hash(elements_hash=events[0]["elements_hash"], team_id=team_id) self.assertEqual(elements[0]["tag_name"], "a") self.assertEqual(elements[0]["attr_class"], ["btn", "btn-sm"]) self.assertEqual(elements[1]["order"], 1) self.assertEqual(elements[1]["text"], "💻") self.assertEqual(events[0]["person"], "2")
def test_distinct_team_leakage(self) -> None: team2 = Team.objects.create() Person.objects.create(team_id=team2.pk, distinct_ids=["2"], properties={"email": "*****@*****.**"}) Person.objects.create(team_id=self.team.pk, distinct_ids=["1", "2"]) try: process_event( "2", "", "", { "event": "$identify", "properties": {"$anon_distinct_id": "1", "token": self.team.api_token, "distinct_id": "2",}, }, self.team.pk, now().isoformat(), now().isoformat(), ) process_event_ee( "2", "", "", { "event": "$identify", "properties": {"$anon_distinct_id": "1", "token": self.team.api_token, "distinct_id": "2",}, }, self.team.pk, now().isoformat(), now().isoformat(), ) except: pass ids: Dict[int, Any] = {self.team.pk: [], team2.pk: []} for pid in get_person_distinct_ids(team_id=self.team.pk): ids[pid["team_id"]].append(pid["distinct_id"]) for pid in get_person_distinct_ids(team_id=team2.pk): ids[pid["team_id"]].append(pid["distinct_id"]) self.assertEqual(sorted(ids[self.team.pk]), sorted(["1", "2"])) self.assertEqual(ids[team2.pk], ["2"]) # Assume that clickhouse has done replacement ch_client.execute("OPTIMIZE TABLE person") people1 = get_persons(team_id=self.team.pk) people2 = get_persons(team_id=team2.pk) self.assertEqual(len(people1), 1) self.assertEqual(len(people2), 1) self.assertEqual(people1[0]["team_id"], self.team.pk) self.assertEqual(people1[0]["properties"], {}) self.assertEqual(people2[0]["team_id"], team2.pk) self.assertEqual(people2[0]["properties"], {"email": "*****@*****.**"})
def test_capture_sent_at_no_timezones(self) -> None: self._create_user("tim") Person.objects.create(team_id=self.team.pk, distinct_ids=["asdfasdfasdf"]) right_now = now() tomorrow = right_now + timedelta(days=1, hours=2) tomorrow_sent_at = right_now + timedelta(days=1, hours=2, minutes=10) # remove timezones tomorrow = tomorrow.replace(tzinfo=None) tomorrow_sent_at = tomorrow_sent_at.replace(tzinfo=None) # event sent_at 10 minutes after timestamp process_event( "movie played", "", "", { "event": "$pageview", "timestamp": tomorrow.isoformat(), "properties": { "distinct_id": "asdfasdfasdf", "token": self.team.api_token, }, }, self.team.pk, right_now.isoformat(), tomorrow_sent_at.isoformat(), ) process_event_ee( "movie played", "", "", { "event": "$pageview", "timestamp": tomorrow.isoformat(), "properties": { "distinct_id": "asdfasdfasdf", "token": self.team.api_token, }, }, self.team.pk, right_now.isoformat(), tomorrow_sent_at.isoformat(), ) events = get_events() returned_time = datetime.strptime(events[0]["timestamp"], "%Y-%m-%dT%H:%M:%S.%f%z") event_seconds_before_now = (right_now - returned_time).seconds # assert that the event is actually recorded 10 minutes before now self.assertGreater(event_seconds_before_now, 590) self.assertLess(event_seconds_before_now, 610)
def test_offset_timestamp_no_sent_at(self) -> None: with freeze_time("2020-01-01T12:00:05.200Z"): process_event('distinct_id', '', '', { "offset": 150, "event":"$autocapture", "distinct_id": "distinct_id", }, self.team.pk, now().isoformat(), None) # no sent at makes no difference for offset event = Event.objects.get() self.assertEqual(event.timestamp.isoformat(), '2020-01-01T12:00:05.050000+00:00')
def test_capture_new_person(self) -> None: user = self._create_user("tim") action1 = Action.objects.create(team=self.team) ActionStep.objects.create(action=action1, selector="a", event="$autocapture") action2 = Action.objects.create(team=self.team) ActionStep.objects.create(action=action2, selector="a", event="$autocapture") team_id = self.team.pk with self.assertNumQueries(21): process_event( 2, "", "", { "event": "$autocapture", "properties": { "distinct_id": 2, "token": self.team.api_token, "$elements": [ { "tag_name": "a", "nth_child": 1, "nth_of_type": 2, "attr__class": "btn btn-sm", }, { "tag_name": "div", "nth_child": 1, "nth_of_type": 2, "$el_text": "💻", }, ], }, }, team_id, now().isoformat(), now().isoformat(), ) self.assertEqual(Person.objects.get().distinct_ids, ["2"]) event = Event.objects.get() self.assertEqual(event.event, "$autocapture") elements = ElementGroup.objects.get( hash=event.elements_hash).element_set.all().order_by("order") self.assertEqual(elements[0].tag_name, "a") self.assertEqual(elements[0].attr_class, ["btn", "btn-sm"]) self.assertEqual(elements[1].order, 1) self.assertEqual(elements[1].text, "💻") self.assertEqual(event.distinct_id, "2")
def test_category_param(self): person_anonymous = Person.objects.create(team=self.team, distinct_ids=["xyz"]) person_identified_already = Person.objects.create( team=self.team, distinct_ids=["tuv"], is_identified=True, ) person_identified_using_event = Person.objects.create( team=self.team, distinct_ids=["klm"], ) # all response = self.client.get("/api/person").json( ) # Make sure the endpoint works with and without the trailing slash self.assertEqual(len(response["results"]), 3) response_all = self.client.get("/api/person/?category=all").json() self.assertListEqual(response["results"], response_all["results"]) # person_identified_using_event should have is_identified set to True after an $identify event process_event( person_identified_using_event.distinct_ids[0], "", "", { "event": "$identify", }, self.team.pk, timezone.now().isoformat(), timezone.now().isoformat(), ) self.assertTrue( Person.objects.get( team_id=self.team.id, persondistinctid__distinct_id="klm").is_identified) # anonymous response_anonymous = self.client.get( "/api/person/?category=anonymous").json() self.assertEqual(len(response_anonymous["results"]), 1) self.assertEqual(response_anonymous["results"][0]["id"], person_anonymous.id) # identified response_identified = self.client.get( "/api/person/?category=identified").json() self.assertEqual(len(response_identified["results"]), 2) self.assertEqual(response_identified["results"][0]["id"], person_identified_using_event.id) self.assertEqual(response_identified["results"][1]["id"], person_identified_already.id)