def test_download_reply_timeout(mocker): api = API("mock", "mock", "mock", "mock", proxy=False) mocker.patch("sdclientapi.requests.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("sdclientapi.requests.request", side_effect=RequestTimeoutError) with pytest.raises(RequestTimeoutError): s = Submission(uuid="climateproblem") api.download_submission(s)
def setup_method(self): self.totp = pyotp.TOTP("JHCOGO7VCER3EJ4L") self.username = "******" self.password = "******" self.server = "http://127.0.0.1:8081/" # Because we may be using a TOTP code from a previous run that has since # been invalidated (or that may be invalid because of bad timing), # we retry repeatedly to get the token with a new TOTP code. # # It doesn't matter if these intermittent 403s are captured in the # cassette as we ignore them during playback. auth_result = None with vcr.use_cassette("data/test-setup.yml") as cassette: for i in range(3): totp = self.totp.now() self.api = API(self.server, self.username, self.password, str(totp)) try: auth_result = self.api.authenticate() except AuthError: # Don't sleep on final retry attempt or during playback if i < 2 and cassette.play_count == 0: time.sleep(31) continue # No error, let's move on break if auth_result is None: raise AuthError("Could not obtain API token during test setup.")
def test_download_get_sources_error_request_timeout(mocker): api = API("mock", "mock", "mock", "mock", True) mocker.patch( "sdclientapi.json_query", return_value=(json.dumps({ "body": json.dumps({"error": "wah"}), "status": http.HTTPStatus.GATEWAY_TIMEOUT, "headers": "foo", })), ) with pytest.raises(RequestTimeoutError): api.get_sources()
def test_filename_key_not_in_download_response(mocker): api = API("mock", "mock", "mock", "mock", True) s = Submission(uuid="foobar") mocker.patch( "sdclientapi.json_query", return_value=(json.dumps({ "body": json.dumps({"error": "wah"}), "status": 200, "headers": "foo" })), ) with pytest.raises(BaseError): api.download_submission(s)
def test_download_submission_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): s = Submission(uuid="climateproblem") api.download_submission(s)
def test_download_reply_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): r = Reply(uuid="humanproblem", filename="secret.txt") api.download_reply(r)
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()
def setUp(self): self.totp = pyotp.TOTP("JHCOGO7VCER3EJ4L") self.username = "******" self.password = "******" self.server = "http://localhost:8081/" self.api = API(self.server, self.username, self.password, str(self.totp.now()), proxy=True) for i in range(3): if os.path.exists("testtoken.json"): token = load_auth() self.api.token = token self.api.update_auth_header() break # The following is True when we did a logout if os.path.exists("logout.txt"): os.unlink("logout.txt") time.sleep(31) # Now let us try to login try: self.api = API(self.server, self.username, self.password, str(self.totp.now()), proxy=True) self.api.authenticate() with open("login.txt", "w") as fobj: fobj.write("in") except Exception as err: print(err) time.sleep(31) continue save_auth(self.api.token) break
def setup_method(self): self.totp = pyotp.TOTP("JHCOGO7VCER3EJ4L") self.username = "******" self.password = "******" self.server = "http://localhost:8081/" self.api = API(self.server, self.username, self.password, str(self.totp.now()), proxy=True) try: self.api.authenticate() except BaseError as err: raise AuthError( "Could not obtain API token during test setup. " "TOTP code may have expired or proxy may not be reachable. " "Error was: {}".format(err.msg))
def setUp(self): self.totp = pyotp.TOTP("JHCOGO7VCER3EJ4L") self.username = "******" self.password = "******" self.server = "http://127.0.0.1:8081/" self.api = API(self.server, self.username, self.password, str(self.totp.now())) for i in range(3): try: self.api.authenticate() except BaseError: token = load_auth() if token: self.api.token = token self.api.update_auth_header() break time.sleep(31) save_auth(self.api.token) break
def test_auth_badotp(self): self.api = API(self.server, self.username, self.password, "no") with self.assertRaises(AuthError): self.api.authenticate()
def test_auth_badpassword(self): self.api = API(self.server, self.username, "no", str(self.totp.now())) with self.assertRaises(AuthError): self.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()
def test_auth_baduser(self): self.api = API(self.server, "no", self.password, str(self.totp.now())) with pytest.raises(AuthError): self.api.authenticate()