-
Notifications
You must be signed in to change notification settings - Fork 0
Add oversize submission detection and backward-compatible DB flag #73
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
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
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
157 changes: 157 additions & 0 deletions
157
submit_ce/domain/event/tests/test_oversize_detection.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,157 @@ | ||
| """Tests for oversize detection wired into the file-upload events. | ||
|
|
||
| These exercise ``execute``/``project`` directly with a hand-rolled fake API so | ||
| no Flask app or real file store is needed. | ||
| """ | ||
|
|
||
| from datetime import datetime | ||
| from types import SimpleNamespace | ||
|
|
||
| from pytz import UTC | ||
|
|
||
| from submit_ce.domain import submission as submod, agent | ||
| from submit_ce.domain.meta import Classification | ||
| from submit_ce.domain.event.file import ( | ||
| UploadFiles, | ||
| UploadArchive, | ||
| RemoveFiles, | ||
| RemoveAllFiles, | ||
| ) | ||
| from submit_ce.domain.size_limits import SizeLimits | ||
|
|
||
| MB = 1024 * 1024 | ||
|
|
||
|
|
||
| class _FakeStore: | ||
| def __init__(self, workspace): | ||
| self.workspace = workspace | ||
|
|
||
| def store_source_package(self, sid, content, chunk_size): | ||
| return [] | ||
|
|
||
| def store_source_file(self, sid, content, chunk_size): | ||
| return SimpleNamespace(bytes=0, path=getattr(content, "filename", "f")) | ||
|
|
||
| def delete_source_file(self, sid, name): | ||
| return None | ||
|
|
||
| def delete_all_source_files(self, sid): | ||
| pass | ||
|
|
||
| def get_workspace(self, sid): | ||
| return self.workspace | ||
|
|
||
| def delete_preflight(self, sid): | ||
| pass | ||
|
|
||
| def delete_preview(self, sid): | ||
| pass | ||
|
|
||
|
|
||
| class _FakeApi: | ||
| def __init__(self, workspace, limits=None): | ||
| self._store = _FakeStore(workspace) | ||
| self._limits = limits or SizeLimits.defaults() | ||
|
|
||
| def get_file_store(self): | ||
| return self._store | ||
|
|
||
| def get_size_limits(self): | ||
| return self._limits | ||
|
|
||
|
|
||
| def _ws(total, per_file=None): | ||
| per_file = per_file or {} | ||
| files = [SimpleNamespace(path=p, bytes=b) for p, b in per_file.items()] | ||
| return SimpleNamespace(size=total, files=files) | ||
|
|
||
|
|
||
| def _user(uid="u1"): | ||
| return agent.PublicUser(name="Test User", user_id=uid, | ||
| email=f"{uid}@example.org", endorsements=[]) | ||
|
|
||
|
|
||
| def _submission(category="astro-ph.GA"): | ||
| u = _user() | ||
| return submod.Submission( | ||
| creator=u, owner=u, created=datetime.now(UTC), | ||
| primary_classification=Classification(category=category)) | ||
|
|
||
|
|
||
| def test_submission_defaults_not_oversize(): | ||
| assert _submission().is_oversize is False | ||
|
|
||
|
|
||
| def test_upload_files_flags_oversize(): | ||
| s = _submission() | ||
| api = _FakeApi(_ws(60 * MB, {"huge.pdf": 60 * MB})) | ||
| e = UploadFiles(creator=s.creator, files=[]) | ||
| e.execute(api, s) | ||
| assert e.oversize is True | ||
| s = e.project(s) | ||
| assert s.is_oversize is True | ||
|
|
||
|
|
||
| def test_upload_files_within_limit_not_oversize(): | ||
| s = _submission() | ||
| api = _FakeApi(_ws(10 * MB, {"ok.pdf": 10 * MB})) | ||
| e = UploadFiles(creator=s.creator, files=[]) | ||
| e.execute(api, s) | ||
| s = e.project(s) | ||
| assert s.is_oversize is False | ||
|
|
||
|
|
||
| def test_upload_archive_flags_oversize(): | ||
| s = _submission() | ||
| api = _FakeApi(_ws(80 * MB, {"a.tex": 80 * MB})) | ||
| e = UploadArchive(creator=s.creator, file=None) | ||
| e.execute(api, s) | ||
| s = e.project(s) | ||
| assert s.is_oversize is True | ||
|
|
||
|
|
||
| def test_remove_files_clears_oversize(): | ||
| s = _submission() | ||
| s.is_oversize = True | ||
| api = _FakeApi(_ws(5 * MB, {"small.tex": 5 * MB})) | ||
| e = RemoveFiles(creator=s.creator, files=[]) | ||
| e.execute(api, s) | ||
| s = e.project(s) | ||
| assert s.is_oversize is False | ||
|
|
||
|
|
||
| def test_remove_all_files_clears_oversize(): | ||
| s = _submission() | ||
| s.is_oversize = True | ||
| e = RemoveAllFiles(creator=s.creator) | ||
| s = e.project(s) | ||
| assert s.is_oversize is False | ||
|
|
||
|
|
||
| def test_project_uses_persisted_flag_on_replay(): | ||
| # On replay execute() does not run; the flag comes from the stored event. | ||
| s = _submission() | ||
| e = UploadFiles(creator=s.creator, files=[], oversize=True) | ||
| s = e.project(s) | ||
| assert s.is_oversize is True | ||
|
|
||
|
|
||
| def test_no_workspace_is_not_oversize(): | ||
| s = _submission() | ||
| api = _FakeApi(None) | ||
| e = UploadFiles(creator=s.creator, files=[]) | ||
| e.execute(api, s) | ||
| assert e.oversize is False | ||
|
|
||
|
|
||
| def test_per_archive_limit_used_in_event(): | ||
| s = _submission(category="astro-ph.GA") | ||
| limits = SizeLimits( | ||
| max_uncompressed_total={"default": 100 * MB, "astro-ph": 5 * MB}, | ||
| max_uncompressed_per_file={"default": 100 * MB}, | ||
| max_compressed={"default": 100 * MB}, | ||
| ) | ||
| api = _FakeApi(_ws(10 * MB, {"f": 10 * MB}), limits=limits) | ||
| e = UploadFiles(creator=s.creator, files=[]) | ||
| e.execute(api, s) | ||
| assert e.oversize is True # 10 MB exceeds the 5 MB astro-ph total limit |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So per_file is the sum of file space used in src/
Is per_file the same as workspace.size or is the comment wrong?
or is workspace.size out of date?
class Workspace(BaseModel):
size: Optional[int] = None
"""Size in bytes of the uncompressed upload workspace."""
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The per_file here is a dict of file.path -> file.bytes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
workspace size should still be the uncompressed size of workspace.
The per_file is to detect single files that are over the size limit.