From eaec2c589d1195ef0c94d8a4fd4bfc5727a3cc21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Negr=C3=B3n?= Date: Fri, 23 Nov 2018 19:19:40 -0400 Subject: [PATCH] Change data decoding errors from strict to replace, add tests --- rest_framework_tracking/base_mixins.py | 4 +--- tests/test_mixins.py | 18 ++++++++++++++++++ tests/urls.py | 1 + tests/views.py | 5 +++++ 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/rest_framework_tracking/base_mixins.py b/rest_framework_tracking/base_mixins.py index 9220a3b..7a712d0 100644 --- a/rest_framework_tracking/base_mixins.py +++ b/rest_framework_tracking/base_mixins.py @@ -5,8 +5,6 @@ from django.db import connection from django.utils.timezone import now -logger = logging.getLogger(__name__) - logger = logging.getLogger(__name__) @@ -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] diff --git a/tests/test_mixins.py b/tests/test_mixins.py index 63cad57..1a8510f 100644 --- a/tests/test_mixins.py +++ b/tests/test_mixins.py @@ -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 @@ -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() diff --git a/tests/urls.py b/tests/urls.py index 3536f8e..5ee0280 100644 --- a/tests/urls.py +++ b/tests/urls.py @@ -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()), diff --git a/tests/views.py b/tests/views.py index 020bdfc..ff41983 100644 --- a/tests/views.py +++ b/tests/views.py @@ -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')