Skip to content

Commit

Permalink
Add test_download_submission_autoresume_fail, which makes sure that i…
Browse files Browse the repository at this point in the history
…n the event of a timeout that doesn't auto-resume, a RequestTimeoutError is ultimately thrown.

I added `__reduce__` methods to the custom exceptions because otherwise an exception was thrown when VCR tried to deepcopy them.
  • Loading branch information
micahflee committed May 20, 2024
1 parent c5b81b1 commit 8ba6242
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions client/securedrop_client/sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class RequestTimeoutError(Exception):
def __init__(self) -> None:
super().__init__("The request timed out.")

def __reduce__(self):
return (self.__class__, ())


class ServerConnectionError(Exception):
"""
Expand All @@ -53,6 +56,9 @@ class ServerConnectionError(Exception):
def __init__(self) -> None:
super().__init__("Cannot connect to the server.")

def __reduce__(self):
return (self.__class__, ())


@dataclass(frozen=True)
class StreamedResponse:
Expand Down
30 changes: 30 additions & 0 deletions client/tests/sdk/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,36 @@ def wait(self):
# Restore Popen
subprocess.Popen = OriginalPopen

@VCRAPI.use_cassette
def test_download_submission_autoresume_fail(self):
# Get a submision
submissions = self.api.get_all_submissions()
for s in submissions:
if s.is_file():
submission = s
break

# Stub subprocess.Popen to raise a subprocess.TimeoutExpired every time
class StubbedPopen(subprocess.Popen):
def __init__(self, *args, content="", **kwargs):
super().__init__(*args, **kwargs)

# Only stub if the command contains "securedrop.Proxy" or "securedrop-proxy"
cmd = shlex.join(args[0])
if "securedrop.Proxy" in cmd or "securedrop-proxy" in cmd:
raise subprocess.TimeoutExpired(
cmd=None, timeout=None, output=None, stderr=None
)

OriginalPopen = subprocess.Popen
subprocess.Popen = StubbedPopen

# Download with the stubbed Popen
with pytest.raises(RequestTimeoutError):
self.api.download_submission(submission, "")

subprocess.Popen = OriginalPopen

# ORDER MATTERS: The following tests add or delete data, and should
# not be run before other tests, which may rely on the original fixture
# state.
Expand Down

0 comments on commit 8ba6242

Please sign in to comment.