Example #1
0
    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": "*****@*****.**"})
Example #2
0
    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",},
        )
Example #3
0
    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": "*****@*****.**"})
Example #4
0
    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",},
        )
Example #5
0
    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",
            },
        )