Skip to content

Commit 3f2e40a

Browse files
fix: Update VCR configuration to mask sensitive data in test recordings
1 parent d8a48f0 commit 3f2e40a

File tree

3 files changed

+66
-43
lines changed

3 files changed

+66
-43
lines changed

src/tests/openai/cassettes/test_embeddings_base_url.yaml

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -13,26 +13,8 @@ interactions:
1313
- '87'
1414
content-type:
1515
- application/json
16-
host:
17-
- api.openai.com
18-
user-agent:
19-
- OpenAI/Python 1.54.4
20-
x-stainless-arch:
21-
- x64
22-
x-stainless-async:
23-
- 'false'
24-
x-stainless-lang:
25-
- python
26-
x-stainless-os:
27-
- Linux
28-
x-stainless-package-version:
29-
- 1.54.4
3016
x-stainless-retry-count:
3117
- '0'
32-
x-stainless-runtime:
33-
- CPython
34-
x-stainless-runtime-version:
35-
- 3.12.5
3618
method: POST
3719
uri: https://api.openai.com/v1/embeddings
3820
response:
@@ -151,23 +133,19 @@ interactions:
151133
CF-Cache-Status:
152134
- DYNAMIC
153135
CF-RAY:
154-
- 8ef92722e9cb8389-SEA
136+
- 8f371cddc8d7eb3b-SEA
155137
Connection:
156138
- keep-alive
157139
Content-Encoding:
158140
- gzip
159141
Content-Type:
160142
- application/json
161143
Date:
162-
- Tue, 10 Dec 2024 00:35:05 GMT
144+
- Tue, 17 Dec 2024 13:03:21 GMT
163145
Server:
164146
- cloudflare
165147
Set-Cookie:
166-
- __cf_bm=Bc46FmH66u.HvNfo3T4q3vwT9_TGhtm3wFFMbxQcZ0U-1733790905-1.0.1.1-mXshj7zBxHMWpnIh3c8EmaqnQsDr8FCF6kOqoQvhFoytI6b7MXLbqkZNRfXRP0pd_OraJnyZhUzOl8KaZdq9qg;
167-
path=/; expires=Tue, 10-Dec-24 01:05:05 GMT; domain=.api.openai.com; HttpOnly;
168-
Secure; SameSite=None
169-
- _cfuvid=pdqpFgJ05BGtT4Cs8llmvgLkp1kOJXZNJgkv3mk5xnA-1733790905081-0.0.1.1-604800000;
170-
path=/; domain=.api.openai.com; HttpOnly; Secure; SameSite=None
148+
- '[FILTERED]'
171149
Transfer-Encoding:
172150
- chunked
173151
X-Content-Type-Options:
@@ -181,27 +159,27 @@ interactions:
181159
openai-model:
182160
- text-embedding-ada-002
183161
openai-organization:
184-
- scale3-1
162+
- '[FILTERED]'
185163
openai-processing-ms:
186-
- '49'
164+
- '[FILTERED]'
187165
openai-version:
188166
- '2020-10-01'
189167
strict-transport-security:
190168
- max-age=31536000; includeSubDomains; preload
191169
x-ratelimit-limit-requests:
192-
- '10000'
170+
- '0'
193171
x-ratelimit-limit-tokens:
194-
- '10000000'
172+
- '0'
195173
x-ratelimit-remaining-requests:
196-
- '9999'
174+
- '0'
197175
x-ratelimit-remaining-tokens:
198-
- '9999998'
176+
- '0'
199177
x-ratelimit-reset-requests:
200-
- 6ms
178+
- '0'
201179
x-ratelimit-reset-tokens:
202-
- 0s
180+
- '0'
203181
x-request-id:
204-
- req_0ce98b0d1723b6734062d2cd711aa85e
182+
- '[FILTERED]'
205183
status:
206184
code: 200
207185
message: OK

src/tests/openai/conftest.py

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,72 @@
1111
@pytest.fixture(autouse=True)
1212
def environment():
1313
if not os.getenv("OPENAI_API_KEY"):
14-
os.environ["OPENAI_API_KEY"] = "test_api_key"
14+
os.environ["OPENAI_API_KEY"] = "${openai_api_key}"
1515

1616

1717
@pytest.fixture
1818
def openai_client():
19-
return OpenAI()
19+
return OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
2020

2121

2222
@pytest.fixture
2323
def async_openai_client():
24-
return AsyncOpenAI()
24+
return AsyncOpenAI(api_key=os.getenv("OPENAI_API_KEY"))
2525

2626

2727
@pytest.fixture(scope="module")
2828
def vcr_config():
29-
return {"filter_headers": ["authorization", "api-key"]}
29+
def before_record_response(response):
30+
# Mask rate limit information and sensitive headers
31+
headers = response["headers"]
32+
sensitive_headers = {
33+
"x-request-id", "x-Request-id", "X-Request-ID",
34+
"openai-organization", "openai-Organization", "openai-ORGANIZATION",
35+
"set-cookie", "Set-Cookie",
36+
"_cfuvid", "__cf_bm",
37+
"openai-processing-ms"
38+
}
39+
40+
for key in list(headers.keys()):
41+
# Case insensitive header matching
42+
if any(sensitive.lower() == key.lower() for sensitive in sensitive_headers):
43+
headers[key] = ["[FILTERED]"]
44+
elif key.lower().startswith(("x-ratelimit-", "openai-processing")):
45+
headers[key] = ["0"]
46+
47+
# Keep response structure but mask actual values
48+
if "body" in response and "string" in response["body"]:
49+
try:
50+
import json
51+
import base64
52+
decoded = json.loads(base64.b64decode(response["body"]["string"]))
53+
if "data" in decoded and isinstance(decoded["data"], list):
54+
for item in decoded["data"]:
55+
if "embedding" in item:
56+
item["embedding"] = [0.1] * len(item["embedding"])
57+
response["body"]["string"] = base64.b64encode(json.dumps(decoded).encode()).decode()
58+
except:
59+
pass
60+
return response
61+
62+
return {
63+
"filter_headers": [
64+
"authorization",
65+
"api-key",
66+
"user-agent",
67+
"x-stainless-arch",
68+
"x-stainless-async",
69+
"x-stainless-lang",
70+
"x-stainless-os",
71+
"x-stainless-package-version",
72+
"x-stainless-runtime",
73+
"x-stainless-runtime-version",
74+
"host",
75+
"cf-ray",
76+
],
77+
"filter_query_parameters": ["api-version"],
78+
"before_record_response": before_record_response,
79+
}
3080

3181

3282
@pytest.fixture(scope="session", autouse=True)

src/tests/openai/test_embeddings.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@ def mock_send(self, request, **kwargs):
6565
base_url="https://your-resource.azure.openai.com/v1",
6666
)
6767

68-
# Debug prints
69-
print(f"Debug - Azure client type: {type(azure_client)}")
70-
print(f"Debug - Azure client base_url: {azure_client.base_url}")
71-
print(f"Debug - Azure client _client._base_url: {azure_client._client._base_url if hasattr(azure_client, '_client') else 'No _client'}")
72-
7368
# Patch the HTTP client's send method
7469
monkeypatch.setattr(httpx.Client, "send", mock_send)
7570

0 commit comments

Comments
 (0)