Skip to content

Instantly share code, notes, and snippets.

@raphaelfruneaux
Last active January 19, 2022 19:34
Show Gist options
  • Save raphaelfruneaux/a878a2b6ad3d8d98ef98dd71ab1fa3b8 to your computer and use it in GitHub Desktop.
Save raphaelfruneaux/a878a2b6ad3d8d98ef98dd71ab1fa3b8 to your computer and use it in GitHub Desktop.
A Pytest-VCR sensitive data replacer setup
import json
import pytest
REPLACEMENT_VALUE = "fake"
SENSITIVE_FIELDS = (
"client-id",
"client-secret",
"access_token",
"Authorization",
)
def clean_sensitive_data_from_request(response):
content = response["content"]
if isinstance(content, str):
content = json.loads(content)
updated = False
for field in SENSITIVE_FIELDS:
if field in content:
content[field] = REPLACEMENT_VALUE
updated = True
if updated:
response["content"] = json.dumps(content)
return response
@pytest.fixture(scope="module")
def vcr_config():
return {
"filter_headers": [("authorization", REPLACEMENT_VALUE)],
"filter_query_parameters": [("access_token", REPLACEMENT_VALUE)],
"before_record_response": clean_sensitive_data_from_request,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment