Skip to content
This repository has been archived by the owner on Jan 7, 2024. It is now read-only.

Commit

Permalink
added tests for api/proxy timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
heartsucker committed May 13, 2019
1 parent c890daa commit e3cbaa0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
18 changes: 17 additions & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import datetime
import os
import pytest
import shutil
import tempfile
import time
import unittest

import pyotp
import vcr
from requests.exceptions import ConnectTimeout, ReadTimeout
from utils import load_auth
from utils import save_auth

from sdclientapi import API
from sdclientapi import API, RequestTimeoutError
from sdclientapi.sdlocalobjects import AuthError
from sdclientapi.sdlocalobjects import BaseError
from sdclientapi.sdlocalobjects import Reply
Expand Down Expand Up @@ -318,3 +320,17 @@ def test_delete_reply(self):

# We deleted one, so there must be 1 less reply now
self.assertEqual(len(self.api.get_all_replies()), number_of_replies_before - 1)


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


def test_request_read_timeout(mocker):
api = API("mock", "mock", "mock", "mock", proxy=False)
mocker.patch("sdclientapi.requests.request", side_effect=ReadTimeout)
with pytest.raises(RequestTimeoutError):
api.authenticate()
20 changes: 19 additions & 1 deletion tests/test_apiproxy.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import datetime
import os
import pyotp
import pytest
import time
import shutil
import tempfile
import unittest

from sdclientapi import API
from subprocess import TimeoutExpired

from sdclientapi import API, RequestTimeoutError
from sdclientapi.sdlocalobjects import (
BaseError,
WrongUUIDError,
Expand Down Expand Up @@ -304,3 +307,18 @@ def test_delete_reply(self):

# We deleted one, so there must be 1 less reply now
self.assertEqual(len(self.api.get_all_replies()), number_of_replies_before - 1)


def test_request_timeout(mocker):
class MockedPopen:
def __init__(self, *nargs, **kwargs) -> None:
self.stdin = mocker.MagicMock()

def communicate(self, *nargs, **kwargs) -> None:
raise TimeoutExpired(["mock"], 123)

api = API("mock", "mock", "mock", "mock", proxy=True)
mocker.patch("sdclientapi.Popen", MockedPopen)

with pytest.raises(RequestTimeoutError):
api.authenticate()

0 comments on commit e3cbaa0

Please sign in to comment.