-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Python: keep attachments close #7038
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Evan Mattson (moonbox3)
merged 2 commits into
microsoft:main
from
moonbox3:codex/keep-attachments-close
Jul 10, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
python/tests/samples/end_to_end/test_chatkit_attachment_store.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| """Tests for the ChatKit integration sample attachment store.""" | ||
|
|
||
| import importlib.util | ||
| import json | ||
| from io import BytesIO | ||
| from pathlib import Path | ||
| from types import ModuleType | ||
|
|
||
| import agent_framework | ||
| import pytest | ||
|
|
||
| _ATTACHMENT_STORE_PATH = ( | ||
| Path(__file__).parents[3] / "samples" / "05-end-to-end" / "chatkit-integration" / "attachment_store.py" | ||
| ) | ||
|
|
||
|
|
||
| def _load_attachment_store_module() -> ModuleType: | ||
| spec = importlib.util.spec_from_file_location("chatkit_attachment_store", _ATTACHMENT_STORE_PATH) | ||
| assert spec is not None | ||
| assert spec.loader is not None | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| return module | ||
|
|
||
|
|
||
| attachment_store_module = _load_attachment_store_module() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def chatkit_app_module(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> ModuleType: | ||
| sample_dir = _ATTACHMENT_STORE_PATH.parent | ||
| monkeypatch.chdir(tmp_path) | ||
| monkeypatch.setenv("FOUNDRY_MODEL", "test-model") | ||
| monkeypatch.setenv("FOUNDRY_PROJECT_ENDPOINT", "https://example.com") | ||
| monkeypatch.setattr(agent_framework, "FunctionResultContent", object, raising=False) | ||
| monkeypatch.syspath_prepend(str(sample_dir)) | ||
|
|
||
| spec = importlib.util.spec_from_file_location("chatkit_integration_app", sample_dir / "app.py") | ||
| assert spec is not None | ||
| assert spec.loader is not None | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| return module | ||
|
|
||
|
|
||
| def test_get_file_path_returns_direct_child(tmp_path: Path) -> None: | ||
| store = attachment_store_module.FileBasedAttachmentStore(uploads_dir=str(tmp_path)) | ||
|
|
||
| assert store.get_file_path("attachment-123") == tmp_path / "attachment-123" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "attachment_id", | ||
| [ | ||
| "../outside", | ||
| "nested/attachment-123", | ||
| "nested/../attachment-123", | ||
| r"nested\attachment-123", | ||
| r"nested\..\attachment-123", | ||
| "/tmp/attachment-123", | ||
| "", | ||
| ".", | ||
| "..", | ||
| ], | ||
| ) | ||
| def test_get_file_path_rejects_non_filename_ids(tmp_path: Path, attachment_id: str) -> None: | ||
| store = attachment_store_module.FileBasedAttachmentStore(uploads_dir=str(tmp_path)) | ||
|
|
||
| with pytest.raises(ValueError, match="Invalid attachment ID"): | ||
| store.get_file_path(attachment_id) | ||
|
|
||
|
|
||
| async def test_attachment_routes_return_bad_request_for_invalid_id(chatkit_app_module: ModuleType) -> None: | ||
| upload = chatkit_app_module.UploadFile(file=BytesIO(b"contents"), filename="attachment.txt") | ||
|
|
||
| upload_response = await chatkit_app_module.upload_file(".", upload) | ||
| preview_response = await chatkit_app_module.preview_image(".") | ||
|
|
||
| assert upload_response.status_code == 400 | ||
| assert json.loads(upload_response.body) == {"error": "Invalid attachment ID."} | ||
| assert preview_response.status_code == 400 | ||
| assert json.loads(preview_response.body) == {"error": "Invalid attachment ID."} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.