From 4ccc325b07f24d2aebc6e5dbdd517980ed65e257 Mon Sep 17 00:00:00 2001 From: Cory Francis Myers Date: Thu, 1 Feb 2024 18:28:50 -0800 Subject: [PATCH] test: refactor mocked tests against API._send_json_request() _send_json_request() raises different exceptions for connection errors than does requests. --- client/tests/sdk/test_api.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/client/tests/sdk/test_api.py b/client/tests/sdk/test_api.py index 5424cc6649..c7870aae49 100644 --- a/client/tests/sdk/test_api.py +++ b/client/tests/sdk/test_api.py @@ -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 @@ -247,21 +246,21 @@ 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) @@ -269,7 +268,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.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)