From 0c0b27d4212e452b7001595b5e0337aeb852ccc9 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Fri, 25 Oct 2019 13:55:29 +0200
Subject: [PATCH 1/2] logging: rename LOGGED_REQUESTS_MAX_SIZE parameter
 (#36596)

---
 passerelle/settings.py           | 2 +-
 passerelle/utils/__init__.py     | 2 +-
 passerelle/utils/jsonresponse.py | 2 +-
 passerelle/views.py              | 2 +-
 tests/test_api_access.py         | 2 +-
 tests/test_requests.py           | 2 +-
 6 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/passerelle/settings.py b/passerelle/settings.py
index 16845a27..607719a4 100644
--- a/passerelle/settings.py
+++ b/passerelle/settings.py
@@ -212,7 +212,7 @@ LOGGED_CONTENT_TYPES_MESSAGES = (
 LOGGED_RESPONSES_MAX_SIZE = 4096
 
 # Max size of the request to log
-LOGGED_REQUEST_MAX_SIZE = 5000
+LOGGED_REQUESTS_MAX_SIZE = 5000
 
 # Number of days to keep logs
 LOG_RETENTION_DAYS = 7
diff --git a/passerelle/utils/__init__.py b/passerelle/utils/__init__.py
index 676bf86b..4710a9f3 100644
--- a/passerelle/utils/__init__.py
+++ b/passerelle/utils/__init__.py
@@ -161,7 +161,7 @@ def log_http_request(logger, request, response=None, exception=None, error_log=T
     if logger.level == 10:  # DEBUG
         extra['request_headers'] = dict(request.headers.items())
         if request.body:
-            extra['request_payload'] = repr(request.body[:settings.LOGGED_REQUEST_MAX_SIZE])
+            extra['request_payload'] = repr(request.body[:settings.LOGGED_REQUESTS_MAX_SIZE])
     if response is not None:
         message = message + ' (=> %s)' % response.status_code
         extra['response_status'] = response.status_code
diff --git a/passerelle/utils/jsonresponse.py b/passerelle/utils/jsonresponse.py
index b9b23dce..4e25e932 100644
--- a/passerelle/utils/jsonresponse.py
+++ b/passerelle/utils/jsonresponse.py
@@ -132,7 +132,7 @@ class to_json(object):
         except Exception as e:
             extras = {'method': req.method, 'exception': exception_to_text(e), 'request': req}
             if req.method == 'POST':
-                extras.update({'body': repr(req.body[:settings.LOGGED_REQUEST_MAX_SIZE])})
+                extras.update({'body': repr(req.body[:settings.LOGGED_REQUESTS_MAX_SIZE])})
             if (not isinstance(e, (Http404, PermissionDenied, ObjectDoesNotExist, RequestException))
                     and getattr(e, 'log_error', True)):
                 logger.exception("Error occurred while processing request", extra=extras)
diff --git a/passerelle/views.py b/passerelle/views.py
index 7295afeb..b330e611 100644
--- a/passerelle/views.py
+++ b/passerelle/views.py
@@ -408,7 +408,7 @@ class GenericEndpointView(GenericConnectorMixin, SingleObjectMixin, View):
         connector_name, endpoint_name = kwargs['connector'], kwargs['endpoint']
         connector = self.get_object()
         url = request.get_full_path()
-        payload = request.body[:settings.LOGGED_REQUEST_MAX_SIZE]
+        payload = request.body[:settings.LOGGED_REQUESTS_MAX_SIZE]
         try:
             payload.decode('utf-8')
         except UnicodeDecodeError:
diff --git a/tests/test_api_access.py b/tests/test_api_access.py
index c95a3fd8..bfd73827 100644
--- a/tests/test_api_access.py
+++ b/tests/test_api_access.py
@@ -227,7 +227,7 @@ def test_logged_requests_and_responses_max_size(app, oxyd, settings):
     assert len(ResourceLog.objects.all()[3].extra['body']) == 86
 
     # troncate logs
-    settings.LOGGED_REQUEST_MAX_SIZE = 10
+    settings.LOGGED_REQUESTS_MAX_SIZE = 10
     settings.LOGGED_RESPONSES_MAX_SIZE = 20
     with utils.mock_url(oxyd.URL, response, headers=headers):
         result = app.post_json(endpoint_url, params=payload)
diff --git a/tests/test_requests.py b/tests/test_requests.py
index e6413ea2..b0c91cb4 100644
--- a/tests/test_requests.py
+++ b/tests/test_requests.py
@@ -111,7 +111,7 @@ def test_log_error_request_max_size(caplog, log_level, settings):
     logger = logging.getLogger('requests')
     logger.setLevel(log_level)
 
-    settings.LOGGED_REQUEST_MAX_SIZE = 8
+    settings.LOGGED_REQUESTS_MAX_SIZE = 8
     with HTTMock(http400_mock):
         requests = Request(logger=logger)
         response = requests.post(url, json={'name':'josh'})
-- 
2.23.0

