Skip to content

Commit

Permalink
fix: make sure types work for default anon check in decide (#25738)
Browse files Browse the repository at this point in the history
  • Loading branch information
raquelmsmith authored Oct 25, 2024
1 parent bb638fe commit 6013e43
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 181 deletions.
5 changes: 4 additions & 1 deletion posthog/api/decide.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,10 @@ def get_decide(request: HttpRequest):

response["surveys"] = True if team.surveys_opt_in else False
response["heatmaps"] = True if team.heatmaps_opt_in else False
default_identified_only = team.pk >= settings.DEFAULT_IDENTIFIED_ONLY_TEAM_ID_MIN
try:
default_identified_only = team.pk >= int(settings.DEFAULT_IDENTIFIED_ONLY_TEAM_ID_MIN)
except Exception:
default_identified_only = False
response["defaultIdentifiedOnly"] = bool(default_identified_only)

site_apps = []
Expand Down
29 changes: 25 additions & 4 deletions posthog/api/test/test_decide.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
from typing import Optional
from unittest.mock import patch

from django.http import HttpRequest
import pytest
from django.conf import settings
from django.core.cache import cache
from django.db import connection, connections
from django.test import TransactionTestCase, TestCase
from django.http import HttpRequest
from django.test import TestCase, TransactionTestCase
from django.test.client import Client
from freezegun import freeze_time
from rest_framework import status
Expand All @@ -20,7 +20,10 @@
from posthog.api.decide import get_decide, label_for_team_id_to_track
from posthog.api.test.test_feature_flag import QueryTimeoutWrapper
from posthog.database_healthcheck import postgres_healthcheck
from posthog.exceptions import RequestParsingError, UnspecifiedCompressionFallbackParsingError
from posthog.exceptions import (
RequestParsingError,
UnspecifiedCompressionFallbackParsingError,
)
from posthog.models import (
FeatureFlag,
GroupTypeMapping,
Expand All @@ -41,7 +44,12 @@
from posthog.models.team.team import Team
from posthog.models.user import User
from posthog.models.utils import generate_random_token_personal
from posthog.test.base import BaseTest, QueryMatchingTest, snapshot_postgres_queries, snapshot_postgres_queries_context
from posthog.test.base import (
BaseTest,
QueryMatchingTest,
snapshot_postgres_queries,
snapshot_postgres_queries_context,
)


@patch(
Expand Down Expand Up @@ -3591,6 +3599,19 @@ def test_decide_element_chain_as_string(self, *args):
self.assertEqual(response.status_code, 200)
self.assertFalse("elementsChainAsString" in response.json())

def test_decide_default_identified_only(self, *args):
self.client.logout()
with self.settings(DEFAULT_IDENTIFIED_ONLY_TEAM_ID_MIN=str(1000000)):
response = self._post_decide(api_version=3)
self.assertEqual(response.status_code, 200)
self.assertTrue("defaultIdentifiedOnly" in response.json())
self.assertFalse(response.json()["defaultIdentifiedOnly"])
team_id = self.team.id
with self.settings(DEFAULT_IDENTIFIED_ONLY_TEAM_ID_MIN=str(team_id)):
response = self._post_decide(api_version=3)
self.assertEqual(response.status_code, 200)
self.assertTrue(response.json()["defaultIdentifiedOnly"])


class TestDatabaseCheckForDecide(BaseTest, QueryMatchingTest):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
FROM events LEFT JOIN (
SELECT person_static_cohort.person_id AS cohort_person_id, 1 AS matched, person_static_cohort.cohort_id AS cohort_id
FROM person_static_cohort
WHERE and(equals(person_static_cohort.team_id, 420), in(person_static_cohort.cohort_id, [4]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(person_static_cohort.team_id, 420), in(person_static_cohort.cohort_id, [6]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(events.team_id, 420), 1, ifNull(equals(__in_cohort.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, format_csv_allow_double_quotes=0, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0
Expand All @@ -42,7 +42,7 @@
FROM events LEFT JOIN (
SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
FROM static_cohort_people
WHERE in(cohort_id, [4])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE in(cohort_id, [6])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE and(1, equals(__in_cohort.matched, 1))
LIMIT 100
'''
Expand All @@ -55,7 +55,7 @@
FROM events LEFT JOIN (
SELECT person_static_cohort.person_id AS cohort_person_id, 1 AS matched, person_static_cohort.cohort_id AS cohort_id
FROM person_static_cohort
WHERE and(equals(person_static_cohort.team_id, 420), in(person_static_cohort.cohort_id, [5]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(person_static_cohort.team_id, 420), in(person_static_cohort.cohort_id, [7]))) AS __in_cohort ON equals(__in_cohort.cohort_person_id, events.person_id)
WHERE and(equals(events.team_id, 420), 1, ifNull(equals(__in_cohort.matched, 1), 0))
LIMIT 100
SETTINGS readonly=2, max_execution_time=60, allow_experimental_object_type=1, format_csv_allow_double_quotes=0, max_ast_elements=4000000, max_expanded_ast_elements=4000000, max_bytes_before_external_group_by=0
Expand All @@ -66,7 +66,7 @@
FROM events LEFT JOIN (
SELECT person_id AS cohort_person_id, 1 AS matched, cohort_id
FROM static_cohort_people
WHERE in(cohort_id, [5])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE in(cohort_id, [7])) AS __in_cohort ON equals(__in_cohort.cohort_person_id, person_id)
WHERE and(1, equals(__in_cohort.matched, 1))
LIMIT 100
'''
Expand Down
Loading

0 comments on commit 6013e43

Please sign in to comment.