Skip to content

Commit

Permalink
feat: enable credentials visualization when in debug or trace logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nestor Acuna-Blanco committed Jun 28, 2024
1 parent 023d3ed commit ba2b7d9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/RequestsLibrary/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def log_request(response):
original_request = request
redirected = ""
safe_headers = dict(original_request.headers)
if AUTHORIZATION in safe_headers:
if logger.LOGLEVEL not in ['TRACE', 'DEBUG'] and AUTHORIZATION in safe_headers:
safe_headers[AUTHORIZATION] = '*****'
logger.info(
"%s Request : " % original_request.method.upper()
Expand Down
27 changes: 26 additions & 1 deletion utests/test_log.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import pytest

from requests import Request

Expand Down Expand Up @@ -79,8 +80,10 @@ def test_log_request_with_headers(mocked_logger):
"body=%s \n " % request.body)


@pytest.mark.parametrize('log_level', ['INFO', 'CONSOLE', 'HTML', 'WARN', 'ERROR'])
@mock.patch('RequestsLibrary.log.logger')
def test_log_request_with_headers_auth(mocked_logger):
def test_log_request_with_headers_auth_with_no_debug_trace_logger(mocked_logger, log_level):
mocked_logger.LOGLEVEL = log_level
headers = {'User-Agent': 'python-requests/2.31.0',
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
Expand All @@ -101,6 +104,28 @@ def test_log_request_with_headers_auth(mocked_logger):
"body=%s \n " % request.body)


@pytest.mark.parametrize('log_level', ['DEBUG', 'TRACE'])
@mock.patch('RequestsLibrary.log.logger')
def test_log_request_with_headers_auth_with_debug_trace_logger(mocked_logger, log_level):
headers = {'User-Agent': 'python-requests/2.31.0',
'Accept-Encoding': 'gzip, deflate',
'Accept': '*/*',
'Connection': 'keep-alive',
'Authorization': 'some_token'}
mocked_logger.LOGLEVEL = log_level
request = Request(method='get', url='http://mock.rulezz', headers=headers)
request = request.prepare()
response = mock.MagicMock()
response.history = []
response.request = request
log_request(response)
assert mocked_logger.info.call_args[0][0] == ("%s Request : " % request.method +
"url=%s \n " % request.url +
"path_url=%s \n " % request.path_url +
"headers=%s \n " % request.headers +
"body=%s \n " % request.body)


@mock.patch('RequestsLibrary.log.logger')
def test_log_request_with_redirect(mocked_logger):
request = Request(method='get', url='http://mock.rulezz/redirected')
Expand Down

0 comments on commit ba2b7d9

Please sign in to comment.