def test_offline_hello_api_call(self, monkeypatch): class MockSession: def get(self, *args, **kwargs): r = Response() r.status_code = 200 r._content = json.dumps({ 'helloResponse': { 'response': 'Hello World!' } }).encode("utf-8") return r monkeypatch.setattr(requests, "Session", MockSession) cisco_hello_api = CiscoHelloApi() monkeypatch.setattr( cisco_hello_api, "create_temporary_access_token", lambda force_new_token=True: mock_access_token_generation()) with pytest.raises(CiscoApiCallFailed) as exinfo: cisco_hello_api.hello_api_call() assert exinfo.match("Client not ready") cisco_hello_api.load_client_credentials() cisco_hello_api.create_temporary_access_token() json_result = cisco_hello_api.hello_api_call() assert json_result == {'helloResponse': {'response': 'Hello World!'}}
def test_offline_hello_api_call_with_connection_issue(self, monkeypatch): def get_endpoint_result(): raise Exception() monkeypatch.setattr(requests, "get", lambda x, headers: get_endpoint_result()) cisco_hello_api = CiscoHelloApi() monkeypatch.setattr(cisco_hello_api, "create_temporary_access_token", lambda force_new_token=True: mock_access_token_generation()) cisco_hello_api.load_client_credentials() cisco_hello_api.create_temporary_access_token() with pytest.raises(ConnectionFailedException) as exinfo: cisco_hello_api.hello_api_call() assert exinfo.match("cannot contact API endpoint at")
def test_offline_hello_api_call_with_connection_issue(self, monkeypatch): class MockSession: def get(self, *args, **kwargs): raise Exception() monkeypatch.setattr(requests, "Session", MockSession) cisco_hello_api = CiscoHelloApi() monkeypatch.setattr(cisco_hello_api, "create_temporary_access_token", lambda force_new_token=True: mock_access_token_generation()) cisco_hello_api.load_client_credentials() cisco_hello_api.create_temporary_access_token() with pytest.raises(ConnectionFailedException) as exinfo: cisco_hello_api.hello_api_call() assert exinfo.match("cannot contact API endpoint at")
def test_online_hello_api_call(self): cisco_hello_api = CiscoHelloApi() cisco_hello_api.load_client_credentials() cisco_hello_api.create_temporary_access_token() json_result = cisco_hello_api.hello_api_call() assert json_result == {'helloResponse': {'response': 'Hello World!'}}
def test_offline_hello_api_call_with_malformed_result(self, monkeypatch): def get_endpoint_result(): r = Response() r.status_code = 200 r._content = "My invalid JSON string".encode("utf-8") return r monkeypatch.setattr(requests, "get", lambda x, headers: get_endpoint_result()) cisco_hello_api = CiscoHelloApi() monkeypatch.setattr(cisco_hello_api, "create_temporary_access_token", lambda force_new_token=True: mock_access_token_generation()) cisco_hello_api.load_client_credentials() cisco_hello_api.create_temporary_access_token() with pytest.raises(CiscoApiCallFailed) as exinfo: cisco_hello_api.hello_api_call() assert exinfo.match("unexpected content from API endpoint")
def test_online_hello_api_call(self): cisco_hello_api = CiscoHelloApi() cisco_hello_api.load_client_credentials() cisco_hello_api.create_temporary_access_token() json_result = cisco_hello_api.hello_api_call() assert json_result == {'response': 'Hello World'}
def test_online_hello_api_call(self): cisco_hello_api = CiscoHelloApi() cisco_hello_api.load_client_credentials() cisco_hello_api.create_temporary_access_token() json_result = cisco_hello_api.hello_api_call() assert isinstance(json_result, dict)
def test_offline_hello_api_call_with_malformed_result(self, monkeypatch): class MockSession: def get(self, *args, **kwargs): r = Response() r.status_code = 200 r._content = "My invalid JSON string".encode("utf-8") return r monkeypatch.setattr(requests, "Session", MockSession) cisco_hello_api = CiscoHelloApi() monkeypatch.setattr(cisco_hello_api, "create_temporary_access_token", lambda force_new_token=True: mock_access_token_generation()) cisco_hello_api.load_client_credentials() cisco_hello_api.create_temporary_access_token() with pytest.raises(CiscoApiCallFailed) as exinfo: cisco_hello_api.hello_api_call() assert exinfo.match("unexpected content from API endpoint")
def test_cisco_hello_api_access(client_id, client_secret, drop_credentials=True): """ test the Cisco Hello API access """ try: base_api = CiscoHelloApi() base_api.load_client_credentials() if drop_credentials: base_api.drop_cached_token() base_api.client_id = client_id base_api.client_secret = client_secret base_api.hello_api_call() return True except: return False
def test_offline_hello_api_call(self, monkeypatch): def get_endpoint_result(): r = Response() r.status_code = 200 r._content = json.dumps({'response': 'Hello World'}).encode("utf-8") return r monkeypatch.setattr(requests, "get", lambda x, headers: get_endpoint_result()) cisco_hello_api = CiscoHelloApi() monkeypatch.setattr(cisco_hello_api, "create_temporary_access_token", lambda force_new_token=True: mock_access_token_generation()) with pytest.raises(CiscoApiCallFailed) as exinfo: cisco_hello_api.hello_api_call() assert exinfo.match("Client not ready") cisco_hello_api.load_client_credentials() cisco_hello_api.create_temporary_access_token() json_result = cisco_hello_api.hello_api_call() assert json_result == {'response': 'Hello World'}
def check_cisco_hello_api_access(client_id, client_secret, drop_credentials=True): """ test the Cisco Hello API access """ try: base_api = CiscoHelloApi() base_api.load_client_credentials() if drop_credentials: base_api.drop_cached_token() base_api.client_id = client_id base_api.client_secret = client_secret base_api.hello_api_call() return True except Exception: return False
def check_cisco_hello_api_access(client_id, client_secret, drop_credentials=True): """ test the Cisco Hello API access """ try: base_api = CiscoHelloApi() base_api.load_client_credentials() if drop_credentials: base_api.drop_cached_token() base_api.client_id = client_id base_api.client_secret = client_secret base_api.hello_api_call() return True except Exception as ex: logging.error("Cisco Hello API test access failed (%s)" % ex, exc_info=True) return False