Skip to content

Commit

Permalink
Merge pull request #7161 from freedomofpress/add-random-test-data
Browse files Browse the repository at this point in the history
Adds option to create large random files as test submissions
  • Loading branch information
eloquence authored May 24, 2024
2 parents b48231d + 7a04452 commit fa5ecc7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
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 \
--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

0 comments on commit fa5ecc7

Please sign in to comment.