Repository: box/box-python-sdk
Pull request #166 (2016-09-07 15:51:37)

Don't log file content when logging Box API responses.


Patch:
diff --git a/boxsdk/network/logging_network.py b/boxsdk/network/logging_network.py
index 3d29f2c..b1ea383 100644
--- a/boxsdk/network/logging_network.py
+++ b/boxsdk/network/logging_network.py
@@ -14,6 +14,7 @@ class LoggingNetwork(DefaultNetwork):
     REQUEST_FORMAT = '\x1b[36m%s %s %s\x1b[0m'
     SUCCESSFUL_RESPONSE_FORMAT = '\x1b[32m%s\x1b[0m'
     ERROR_RESPONSE_FORMAT = '\x1b[31m%s\n%s\n%s\n\x1b[0m'
+    STREAM_CONTENT_NOT_LOGGED = '<File download contents unavailable for logging>'
 
     def __init__(self, logger=None):
         """
@@ -56,7 +57,10 @@ def _log_response(self, response):
         :param response: The Box API response.
         """
         if response.ok:
-            self._logger.info(self.SUCCESSFUL_RESPONSE_FORMAT, response.content)
+            if response.request_response.raw is not None:
+                self._logger.info(self.SUCCESSFUL_RESPONSE_FORMAT, self.STREAM_CONTENT_NOT_LOGGED)
+            else:
+                self._logger.info(self.SUCCESSFUL_RESPONSE_FORMAT, response.content)
         else:
             self._logger.warning(
                 self.ERROR_RESPONSE_FORMAT,


Test patch:
diff --git a/test/conftest.py b/test/conftest.py
index a5958da..e1f3b0c 100644
--- a/test/conftest.py
+++ b/test/conftest.py
@@ -7,13 +7,15 @@
 from boxsdk.network.default_network import DefaultNetworkResponse
 
 
-@pytest.fixture(scope='session')
+@pytest.fixture()
 def generic_successful_response():
     mock_network_response = Mock(DefaultNetworkResponse)
     mock_network_response.content = b'{"message": "success"}'
     mock_network_response.status_code = 200
     mock_network_response.ok = True
     mock_network_response.raw = Mock()
+    mock_network_response.request_response = Mock()
+    mock_network_response.request_response.raw = None
     return mock_network_response
 
 
diff --git a/test/functional/test_object_clone.py b/test/functional/test_object_clone.py
index edd7fda..b97cc98 100644
--- a/test/functional/test_object_clone.py
+++ b/test/functional/test_object_clone.py
@@ -1,8 +1,8 @@
 # coding: utf-8
 
 from __future__ import unicode_literals, absolute_import
-from boxsdk.object.folder import FolderSyncState
 import pytest
+from boxsdk.object.folder import FolderSyncState
 
 
 @pytest.fixture()
diff --git a/test/unit/network/test_logging_network.py b/test/unit/network/test_logging_network.py
index 58a8c61..33a3764 100644
--- a/test/unit/network/test_logging_network.py
+++ b/test/unit/network/test_logging_network.py
@@ -54,6 +54,17 @@ def test_logging_network_logs_successful_responses(http_verb, test_url, access_t
     logger.info.assert_called_with(network.SUCCESSFUL_RESPONSE_FORMAT, generic_successful_response.content)
 
 
+def test_logging_network_does_not_log_download_content(http_verb, test_url, access_token, generic_successful_response):
+    logger = Mock(Logger)
+    network = LoggingNetwork(logger)
+    generic_successful_response.request_response.raw = Mock()
+    with patch.object(default_network.DefaultNetwork, 'request') as super_request:
+        super_request.return_value = generic_successful_response
+        network.request(http_verb, test_url, access_token)
+        super_request.assert_called_once_with(http_verb, test_url, access_token)
+    logger.info.assert_called_with(network.SUCCESSFUL_RESPONSE_FORMAT, network.STREAM_CONTENT_NOT_LOGGED)
+
+
 def test_logging_network_logs_non_successful_responses(http_verb, test_url, access_token, server_error_response):
     logger = Mock(Logger)
     network = LoggingNetwork(logger)