Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions rest_framework_tracking/base_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from django.db import connection
from django.utils.timezone import now

logger = logging.getLogger(__name__)


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -159,7 +157,7 @@ def _clean_data(self, data):
eg: sensitive_fields = {'field1', 'field2'}
"""
if isinstance(data, bytes):
data = data.decode()
data = data.decode(errors='replace')

if isinstance(data, list):
return [self._clean_data(d) for d in data]
Expand Down
18 changes: 18 additions & 0 deletions tests/test_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pytest
import ast
import datetime
from io import BytesIO
import json
from django.contrib.auth.models import User
from django.utils.timezone import now
Expand Down Expand Up @@ -281,6 +282,23 @@ def test_log_json_post_response(self):
log = APIRequestLog.objects.first()
self.assertEqual(log.response, u'{"post":"response"}')

def test_log_multipart_post_response(self):
self.client.post('/multipart-logging', {}, format='multipart')
log = APIRequestLog.objects.first()
self.assertEqual(log.response, u'{"post":"response"}')

def test_log_multipart_utf8_encoded_file_post_response(self):
file = BytesIO('test data'.encode('utf-8'))
self.client.post('/multipart-logging', {'file': file}, format='multipart')
log = APIRequestLog.objects.first()
self.assertEqual(log.response, u'{"post":"response"}')

def test_log_multipart_utf16_encoded_file_post_response(self):
file = BytesIO('test data'.encode('utf-16'))
self.client.post('/multipart-logging', {'file': file}, format='multipart')
log = APIRequestLog.objects.first()
self.assertEqual(log.response, u'{"post":"response"}')

def test_log_status_validation_error(self):
self.client.get('/validation-error-logging')
log = APIRequestLog.objects.first()
Expand Down
1 change: 1 addition & 0 deletions tests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
url(r'^session-auth-logging$', test_views.MockSessionAuthLoggingView.as_view()),
url(r'^token-auth-logging$', test_views.MockTokenAuthLoggingView.as_view()),
url(r'^json-logging$', test_views.MockJSONLoggingView.as_view()),
url(r'^multipart-logging$', test_views.MockMultipartLoggingView.as_view()),
url(r'^validation-error-logging$', test_views.MockValidationErrorLoggingView.as_view()),
url(r'^404-error-logging$', test_views.Mock404ErrorLoggingView.as_view()),
url(r'^500-error-logging$', test_views.Mock500ErrorLoggingView.as_view()),
Expand Down
5 changes: 5 additions & 0 deletions tests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ def post(self, request):
return Response({'post': 'response'})


class MockMultipartLoggingView(LoggingMixin, APIView):
def post(self, request):
return Response({'post': 'response'})


class MockValidationErrorLoggingView(LoggingMixin, APIView):
def get(self, request):
raise serializers.ValidationError('bad input')
Expand Down