11import pytest
2+ import json
23
34from werkzeug .test import Client
45from django .contrib .auth .models import User
@@ -225,7 +226,7 @@ def test_sql_psycopg2_string_composition(sentry_init, capture_events, query):
225226 if "postgres" not in connections :
226227 pytest .skip ("postgres tests disabled" )
227228
228- import psycopg2
229+ import psycopg2 . sql
229230
230231 sql = connections ["postgres" ].cursor ()
231232
@@ -248,7 +249,7 @@ def test_sql_psycopg2_placeholders(sentry_init, capture_events):
248249 if "postgres" not in connections :
249250 pytest .skip ("postgres tests disabled" )
250251
251- import psycopg2
252+ import psycopg2 . sql
252253
253254 sql = connections ["postgres" ].cursor ()
254255
@@ -389,3 +390,68 @@ def test_template_exception(sentry_init, client, capture_events):
389390 (None , None ),
390391 (u"invalid_block_tag" , u"django.template.base" ),
391392 ]
393+
394+
395+ @pytest .mark .parametrize (
396+ "type,event_request" ,
397+ [
398+ [
399+ "json" ,
400+ {
401+ "cookies" : {},
402+ "data" : {"foo" : "bar" },
403+ "env" : {"SERVER_NAME" : "localhost" , "SERVER_PORT" : "80" },
404+ "headers" : {
405+ "Content-Length" : "14" ,
406+ "Content-Type" : "application/json" ,
407+ "Host" : "localhost" ,
408+ },
409+ "method" : "POST" ,
410+ "query_string" : "" ,
411+ "url" : "http://localhost/rest-framework-exc" ,
412+ },
413+ ],
414+ [
415+ "formdata" ,
416+ {
417+ "cookies" : {},
418+ "data" : {"foo" : "bar" },
419+ "env" : {"SERVER_NAME" : "localhost" , "SERVER_PORT" : "80" },
420+ "headers" : {
421+ "Content-Length" : "7" ,
422+ "Content-Type" : "application/x-www-form-urlencoded" ,
423+ "Host" : "localhost" ,
424+ },
425+ "method" : "POST" ,
426+ "query_string" : "" ,
427+ "url" : "http://localhost/rest-framework-exc" ,
428+ },
429+ ],
430+ ],
431+ )
432+ def test_rest_framework_basic (
433+ sentry_init , client , capture_events , capture_exceptions , type , event_request
434+ ):
435+ pytest .importorskip ("rest_framework" )
436+ sentry_init (integrations = [DjangoIntegration ()], send_default_pii = True )
437+ exceptions = capture_exceptions ()
438+ events = capture_events ()
439+
440+ if type == "json" :
441+ client .post (
442+ reverse ("rest_framework_exc" ),
443+ data = json .dumps ({"foo" : "bar" }),
444+ content_type = "application/json" ,
445+ )
446+ elif type == "formdata" :
447+ client .post (reverse ("rest_framework_exc" ), data = {"foo" : "bar" })
448+ else :
449+ assert False
450+
451+ error , = exceptions
452+ assert isinstance (error , ZeroDivisionError )
453+
454+ event , = events
455+ assert event ["exception" ]["values" ][0 ]["mechanism" ]["type" ] == "django"
456+
457+ assert event ["request" ] == event_request
0 commit comments