Skip to content

Commit

Permalink
test: refactor mocked tests against API._send_json_request()
Browse files Browse the repository at this point in the history
_send_json_request() raises different exceptions for connection errors
than does requests.
  • Loading branch information
cfm committed Feb 6, 2024
1 parent 42e8c6b commit 4ccc325
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions client/tests/sdk/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import pyotp
import pytest
import vcr
from requests.exceptions import ConnectTimeout, ReadTimeout
from test_shared import TestShared

from securedrop_client.sdk import API, RequestTimeoutError
from securedrop_client.sdk import API, RequestTimeoutError, ServerConnectionError
from securedrop_client.sdk.sdlocalobjects import AuthError, Reply, Submission

NUM_REPLIES_PER_SOURCE = 2
Expand Down Expand Up @@ -247,29 +246,29 @@ def test_zlogout(self):

def test_request_connect_timeout(mocker):
api = API("mock", "mock", "mock", "mock", proxy=False)
mocker.patch("securedrop_client.sdk.requests.request", side_effect=ConnectTimeout)
with pytest.raises(RequestTimeoutError):
mocker.patch("securedrop_client.sdk.API._send_json_request", side_effect=ServerConnectionError)
with pytest.raises(ServerConnectionError):
api.authenticate()


def test_request_read_timeout(mocker):
api = API("mock", "mock", "mock", "mock", proxy=False)
mocker.patch("securedrop_client.sdk.requests.request", side_effect=ReadTimeout)
mocker.patch("securedrop_client.sdk.API._send_json_request", side_effect=RequestTimeoutError)
with pytest.raises(RequestTimeoutError):
api.authenticate()


def test_download_reply_timeout(mocker):
api = API("mock", "mock", "mock", "mock", proxy=False)
mocker.patch("securedrop_client.sdk.requests.request", side_effect=RequestTimeoutError)
mocker.patch("securedrop_client.sdk.API._send_json_request", side_effect=RequestTimeoutError)
with pytest.raises(RequestTimeoutError):
r = Reply(uuid="humanproblem", filename="secret.txt")
api.download_reply(r)


def test_download_submission_timeout(mocker):
api = API("mock", "mock", "mock", "mock", proxy=False)
mocker.patch("securedrop_client.sdk.requests.request", side_effect=RequestTimeoutError)
mocker.patch("securedrop_client.sdk.API._send_json_request", side_effect=RequestTimeoutError)
with pytest.raises(RequestTimeoutError):
s = Submission(uuid="climateproblem")
api.download_submission(s)

0 comments on commit 4ccc325

Please sign in to comment.