diff options
-rw-r--r-- | oauthlib/__init__.py | 9 | ||||
-rw-r--r-- | oauthlib/common.py | 3 | ||||
-rw-r--r-- | tests/__init__.py | 3 | ||||
-rw-r--r-- | tests/test_common.py | 16 |
4 files changed, 31 insertions, 0 deletions
diff --git a/oauthlib/__init__.py b/oauthlib/__init__.py index 8eb82a6..8639e88 100644 --- a/oauthlib/__init__.py +++ b/oauthlib/__init__.py @@ -15,3 +15,12 @@ __author__ = 'The OAuthlib Community' __version__ = '3.0.2-dev' logging.getLogger('oauthlib').addHandler(NullHandler()) + +_DEBUG = False + +def set_debug(debug_val): + global _DEBUG + _DEBUG = debug_val + +def get_debug_flag(): + return _DEBUG diff --git a/oauthlib/common.py b/oauthlib/common.py index 96de1f1..ea5bfe7 100644 --- a/oauthlib/common.py +++ b/oauthlib/common.py @@ -14,6 +14,7 @@ import logging import re import sys import time +from . import get_debug_flag try: from secrets import randbits @@ -435,6 +436,8 @@ class Request(object): raise AttributeError(name) def __repr__(self): + if not get_debug_flag(): + return "<oauthlib.Request SANITIZED>" body = self.body headers = self.headers.copy() if body: diff --git a/tests/__init__.py b/tests/__init__.py index e69de29..f33236b 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -0,0 +1,3 @@ +import oauthlib + +oauthlib.set_debug(True) diff --git a/tests/test_common.py b/tests/test_common.py index 20d9f5b..ae2531b 100644 --- a/tests/test_common.py +++ b/tests/test_common.py @@ -1,8 +1,10 @@ # -*- coding: utf-8 -*- from __future__ import absolute_import, unicode_literals +import os import sys +import oauthlib from oauthlib.common import (CaseInsensitiveDict, Request, add_params_to_uri, extract_params, generate_client_id, generate_nonce, generate_timestamp, @@ -214,6 +216,20 @@ class RequestTest(TestCase): self.assertEqual(r.headers['token'], 'foobar') self.assertEqual(r.token, 'banana') + def test_sanitized_request_non_debug_mode(self): + """make sure requests are sanitized when in non debug mode. + For the debug mode, the other tests checking sanitization should prove + that debug mode is working. + """ + try: + oauthlib.set_debug(False) + r = Request(URI, headers={'token': 'foobar'}, body='token=banana') + self.assertNotIn('token', repr(r)) + self.assertIn('SANITIZED', repr(r)) + finally: + # set flag back for other tests + oauthlib.set_debug(True) + class CaseInsensitiveDictTest(TestCase): |