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_distinct_with_multiple_anonymous_ids_which_were_already_created(self,) -> None: # logging in the first time create_person(team_id=self.team.pk, distinct_ids=["anonymous_id"]) create_person(team_id=self.team.pk, distinct_ids=["new_distinct_id"], properties={"email": "*****@*****.**"}) process_event_ee( "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 = get_person_by_distinct_id(self.team.pk, "new_distinct_id") distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)] self.assertEqual(sorted(distinct_ids), sorted(["anonymous_id", "new_distinct_id"])) self.assertEqual(person["properties"]["email"], "*****@*****.**") # logging in another time create_person(team_id=self.team.pk, distinct_ids=["anonymous_id_2"]) process_event_ee( "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 = get_person_by_distinct_id(self.team.pk, "new_distinct_id") distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)] self.assertEqual(sorted(distinct_ids), sorted(["anonymous_id", "new_distinct_id", "anonymous_id_2"])) self.assertEqual(person["properties"]["email"], "*****@*****.**")
def test_alias_both_existing(self) -> None: create_person(distinct_ids=["old_distinct_id"], team_id=self.team.pk) create_person(distinct_ids=["new_distinct_id"], team_id=self.team.pk) 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() distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)] self.assertEqual(len(events), 1) self.assertEqual(sorted(distinct_ids), sorted(["old_distinct_id", "new_distinct_id"]))
def test_alias_before_person(self) -> None: 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(), ) person1 = get_person_by_distinct_id(team_id=self.team.pk, distinct_id="old_distinct_id") person2 = get_person_by_distinct_id(team_id=self.team.pk, distinct_id="new_distinct_id") self.assertEqual(person1, person2) events = get_events() distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)] self.assertEqual(len(events), 1) self.assertEqual(sorted(distinct_ids), sorted(["new_distinct_id", "old_distinct_id"]))
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_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() create_person(team_id=team2.pk, distinct_ids=["2"], properties={"email": "*****@*****.**"}) create_person(team_id=self.team.pk, distinct_ids=["1", "2"]) try: 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 = {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"]) 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_distinct_with_anonymous_id(self) -> None: create_person(team_id=self.team.pk, distinct_ids=["anonymous_id"]) process_event_ee( "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(), ) events = get_events() distinct_ids = [item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk)] self.assertEqual(len(events), 1) self.assertEqual(sorted(distinct_ids), sorted(["anonymous_id", "new_distinct_id"])) # check no errors as this call can happen multiple times process_event_ee( "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(), )
def test_alias_merge_properties(self) -> None: create_person( distinct_ids=["old_distinct_id"], team_id=self.team.pk, properties={"key_on_both": "old value both", "key_on_old": "old value"}, ) create_person( distinct_ids=["new_distinct_id"], team_id=self.team.pk, properties={"key_on_both": "new value both", "key_on_new": "new value"}, ) 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"])) 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_alias_merge_properties(self) -> None: with freeze_time("2020-01-04T13:01:01Z"): 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" }, ) with freeze_time("2020-01-04T16:01:01Z"): 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"])) ch_client.execute("OPTIMIZE TABLE person") ch_client.execute("OPTIMIZE TABLE persons_up_to_date") 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_alias_twice(self) -> None: Person.objects.create(team_id=self.team.pk, 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(), ) 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(), ) Person.objects.create(team_id=self.team.pk, 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(), ) process_event_ee( "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(), ) distinct_ids = [ item["distinct_id"] for item in get_person_distinct_ids(team_id=self.team.pk) ] events = get_events() self.assertEqual(len(events), 2) self.assertEqual( sorted(distinct_ids), sorted(["old_distinct_id", "new_distinct_id", "old_distinct_id_2"]), )