Skip to content
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

Adds option to create large random files as test submissions #7161

Merged
merged 2 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ safety: ## Run `safety check` to check python dependencies for vulnerabilities.
--ignore 66777 \
--ignore 66704 \
--ignore 66710 \
--ignore 67895 \
--ignore 67599 \
eloquence marked this conversation as resolved.
Show resolved Hide resolved
--full-report -r $$req_file \
&& echo -e '\n' \
|| exit 1; \
Expand Down
23 changes: 20 additions & 3 deletions securedrop/loaddata.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,24 @@ def submit_message(source: Source, journalist_who_saw: Optional[Journalist]) ->
db.session.add(seen_message)


def submit_file(source: Source, journalist_who_saw: Optional[Journalist]) -> None:
def submit_file(source: Source, journalist_who_saw: Optional[Journalist], size: int = 0) -> None:
"""
Adds a single file submitted by a source.
"""
record_source_interaction(source)
if not size:
file_bytes = b"This is an example of a plain text file upload"
else:
file_bytes = os.urandom(size * 1024)

fpath = Storage.get_default().save_file_submission(
source.filesystem_id,
source.interaction_count,
source.journalist_filename,
"memo.txt",
io.BytesIO(b"This is an example of a plain text file upload."),
io.BytesIO(file_bytes),
)

submission = Submission(source, fpath, Storage.get_default())
db.session.add(submission)

Expand Down Expand Up @@ -358,7 +364,11 @@ def add_sources(args: argparse.Namespace, journalists: Tuple[Journalist, ...]) -
seen_message_count -= 1

for _ in range(args.files_per_source):
submit_file(source, secrets.choice(journalists) if seen_file_count > 0 else None)
submit_file(
source,
secrets.choice(journalists) if seen_file_count > 0 else None,
args.random_file_size,
)
seen_file_count -= 1

if i <= starred_sources_count:
Expand Down Expand Up @@ -484,6 +494,13 @@ def parse_arguments() -> argparse.Namespace:
action="store_true",
default=False,
)
parser.add_argument(
"--random-file-size",
help="Create random submission files with size specified (in KB)",
type=non_negative_int,
default=0,
)

return parser.parse_args()


Expand Down