Skip to content

Commit

Permalink
Fix lint errors.
Browse files Browse the repository at this point in the history
In test_download_submission_autoresume_fail and test_download_submission_timeout, download to a tempfile directory because CI has issues downloading to the default directory.
  • Loading branch information
micahflee committed May 20, 2024
1 parent d6611b0 commit d6f2fd1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
33 changes: 17 additions & 16 deletions client/securedrop_client/sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class RequestTimeoutError(Exception):
def __init__(self) -> None:
super().__init__("The request timed out.")

def __reduce__(self):
def __reduce__(self) -> tuple[type, tuple]:
return (self.__class__, ())


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

def __reduce__(self):
def __reduce__(self) -> tuple[type, tuple]:
return (self.__class__, ())


Expand Down Expand Up @@ -172,9 +172,9 @@ def _send_json_request(
if stream:
fobj = tempfile.TemporaryFile("w+b")

content_length = None
filename = None
sha256sum = None
content_length = 0
filename = ""
sha256sum = ""

retry = 0
bytes_written = 0
Expand All @@ -196,18 +196,17 @@ def _send_json_request(
data["headers"]["Range"] = f"bytes={bytes_written}-"

data_str = json.dumps(data).encode()
proc.stdin.write(data_str)
proc.stdin.close()
if proc.stdin is not None:
proc.stdin.write(data_str)
proc.stdin.close()

# Save the response headers from the first request
if retry == 0:
try:
stderr = json.loads(proc.stderr.read().decode())
if sha256sum is None:
if proc.stderr is not None:
stderr = json.loads(proc.stderr.read().decode())
sha256sum = stderr["headers"]["etag"]
if filename is None:
filename = stderr["headers"]["content-disposition"]
if content_length is None:
content_length = int(stderr["headers"]["content-length"])
except (json.decoder.JSONDecodeError, KeyError) as err:
raise BaseError(
Expand All @@ -217,10 +216,11 @@ def _send_json_request(
# Write the contents to disk
chunk_size = 64
while not download_finished:
chunk = proc.stdout.read(chunk_size)
if not chunk:
download_finished = True
break
if proc.stdout is not None:
chunk = proc.stdout.read(chunk_size)
if not chunk:
download_finished = True
break

fobj.write(chunk)
bytes_written += len(chunk)
Expand All @@ -238,7 +238,8 @@ def _send_json_request(
# Check for errors
if returncode != 0:
try:
error = json.loads(proc.stderr.read().decode())
if proc.stderr is not None:
error = json.loads(proc.stderr.read().decode())
except json.decoder.JSONDecodeError as err:
raise BaseError("Unable to parse stderr JSON") from err
raise BaseError(
Expand Down
12 changes: 8 additions & 4 deletions client/tests/sdk/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ def read(self, n=-1):
{
"headers": {
"content-type": "application/pgp-encrypted",
"etag": "sha256:3aa5ec3fe60b235a76bfc2c3a5c5d525687f04c1670060632a21b6a77c131f65",
"content-disposition": "attachment; filename=3-assessable_firmness-doc.gz.gpg",
"etag": "sha256:3aa5ec3fe60b235a76bfc2c3a5c5d525687f04c1670060632a21b6a77c131f65", # noqa: E501
"content-disposition": "attachment; filename=3-assessable_firmness-doc.gz.gpg", # noqa: E501
"content-length": "651",
"accept-ranges": "bytes",
}
Expand Down Expand Up @@ -313,8 +313,10 @@ def __init__(self, *args, content="", **kwargs):
subprocess.Popen = StubbedPopen

# Download with the stubbed Popen
tmpdir = tempfile.TemporaryDirectory()
with pytest.raises(RequestTimeoutError):
self.api.download_submission(submission, "")
self.api.download_submission(submission, tmpdir.name)
tmpdir.cleanup()

subprocess.Popen = OriginalPopen

Expand Down Expand Up @@ -386,6 +388,7 @@ def test_download_reply_timeout(mocker):
def test_download_submission_timeout(mocker):
api = API("mock", "mock", "mock", "mock", proxy=False)
mocker.patch("securedrop_client.sdk.API._send_json_request", side_effect=RequestTimeoutError)
tmpdir = tempfile.TemporaryDirectory()
s = Submission(
uuid="climateproblem",
filename="secret.txt",
Expand All @@ -397,7 +400,8 @@ def test_download_submission_timeout(mocker):
seen_by=False,
)
with pytest.raises(RequestTimeoutError):
api.download_submission(s)
api.download_submission(s, tmpdir.name)
tmpdir.cleanup()

# Delete secret.txt, if it exists
try:
Expand Down

0 comments on commit d6f2fd1

Please sign in to comment.