Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
qgerome committed Jul 24, 2024
1 parent 8f99d5d commit 7e87fae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion hexa/analytics/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def track(
if can_track is False:
return

if request:
if request and "User-Agent" in request.headers:
# Add request related properties
parsed = user_agent_parser.Parse(request.headers["User-Agent"])
properties.update(
Expand Down
2 changes: 1 addition & 1 deletion hexa/analytics/tests/test_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def test_create_user_profile(
with self.settings(MIXPANEL_TOKEN=mixpanel_token):
set_user_properties(self.USER)

mock_mixpanel.people_set_once.assert_called_once_with(
mock_mixpanel.people_set.assert_called_once_with(
distinct_id=str(self.USER.id),
properties={
"$email": self.USER.email,
Expand Down
24 changes: 14 additions & 10 deletions hexa/analytics/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from unittest import mock

from django.urls import reverse

from hexa.core.test import TestCase
Expand Down Expand Up @@ -35,16 +37,18 @@ def test_track_event(self):
)
self.assertEqual(r.status_code, 200)

def test_track_event_analytics_not_enabled(self):
@mock.patch("hexa.analytics.api.mixpanel")
def test_track_event_analytics_not_enabled(self, mixpanel_mock):
self.USER.analytics_enabled = False
self.USER.save()
self.client.force_login(self.USER)
r = self.client.post(
reverse(
"analytics:track",
),
{"event": "page_viewed", "properties": {"page": "database"}},
content_type="application/json",
)
self.assertEqual(r.status_code, 401)
self.assertEqual(r.json(), {"error": "Analytics not enabled."})
with self.settings(MIXPANEL_TOKEN="123"):
r = self.client.post(
reverse(
"analytics:track",
),
{"event": "page_viewed", "properties": {"page": "database"}},
content_type="application/json",
)
self.assertEqual(r.status_code, 200)
mixpanel_mock.track.assert_not_called()

0 comments on commit 7e87fae

Please sign in to comment.