11from posthog .exception_integrations .django import DjangoRequestExtractor
2+ from django .test import RequestFactory
3+ from django .conf import settings
4+ from django .core .management import call_command
5+ import django
26
37DEFAULT_USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
48
9+ # setup a test app
10+ if not settings .configured :
11+ settings .configure (
12+ SECRET_KEY = "test" ,
13+ DEFAULT_CHARSET = "utf-8" ,
14+ INSTALLED_APPS = [
15+ "django.contrib.auth" ,
16+ "django.contrib.contenttypes" ,
17+ ],
18+ DATABASES = {
19+ "default" : {
20+ "ENGINE" : "django.db.backends.sqlite3" ,
21+ "NAME" : ":memory:" ,
22+ }
23+ },
24+ )
25+ django .setup ()
26+
27+ call_command ("migrate" , verbosity = 0 , interactive = False )
28+
529
630def mock_request_factory (override_headers ):
7- class Request :
8- META = {}
9- # TRICKY: Actual django request dict object has case insensitive matching, and strips http from the names
10- headers = {
31+ factory = RequestFactory (
32+ headers = {
1133 "User-Agent" : DEFAULT_USER_AGENT ,
1234 "Referrer" : "http://example.com" ,
1335 "X-Forwarded-For" : "193.4.5.12" ,
1436 ** (override_headers or {}),
1537 }
38+ )
1639
17- return Request ()
40+ request = factory .get ("/api/endpoint" )
41+ return request
1842
1943
2044def test_request_extractor_with_no_trace ():
@@ -25,19 +49,22 @@ def test_request_extractor_with_no_trace():
2549 "user_agent" : DEFAULT_USER_AGENT ,
2650 "traceparent" : None ,
2751 "distinct_id" : None ,
52+ "$request_path" : "/api/endpoint" ,
2853 }
2954
3055
3156def test_request_extractor_with_trace ():
3257 request = mock_request_factory (
3358 {"traceparent" : "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01" }
3459 )
60+
3561 extractor = DjangoRequestExtractor (request )
3662 assert extractor .extract_person_data () == {
3763 "ip" : "193.4.5.12" ,
3864 "user_agent" : DEFAULT_USER_AGENT ,
3965 "traceparent" : "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01" ,
4066 "distinct_id" : None ,
67+ "$request_path" : "/api/endpoint" ,
4168 }
4269
4370
@@ -54,6 +81,7 @@ def test_request_extractor_with_tracestate():
5481 "user_agent" : DEFAULT_USER_AGENT ,
5582 "traceparent" : "00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01" ,
5683 "distinct_id" : "1234" ,
84+ "$request_path" : "/api/endpoint" ,
5785 }
5886
5987
@@ -67,4 +95,27 @@ def test_request_extractor_with_complicated_tracestate():
6795 "user_agent" : DEFAULT_USER_AGENT ,
6896 "traceparent" : None ,
6997 "distinct_id" : "alohaMountainsXUYZ" ,
98+ "$request_path" : "/api/endpoint" ,
99+ }
100+
101+
102+ def test_request_extractor_with_request_user ():
103+ from django .contrib .auth .models import User
104+
105+ user = User .objects .create_user (
106+ username = "test" , email = "test@posthog.com" , password = "top_secret"
107+ )
108+
109+ request = mock_request_factory (None )
110+ request .user = user
111+
112+ extractor = DjangoRequestExtractor (request )
113+ assert extractor .extract_person_data () == {
114+ "ip" : "193.4.5.12" ,
115+ "user_agent" : DEFAULT_USER_AGENT ,
116+ "traceparent" : None ,
117+ "distinct_id" : None ,
118+ "$request_path" : "/api/endpoint" ,
119+ "email" : "test@posthog.com" ,
120+ "$user_id" : "1" ,
70121 }
0 commit comments