def test_url_name_port_exclusion(url): """Special case of url name generation that doesn't display ports""" assume(not url.endswith('/')) assume(url.count(':') == 0) server = Server(url, None, 'carl', 'password', 'http') print(url, server.url_name(), server.__dict__, u'{}://{}'.format('http', url)) assert server.url_name() == u'{}://{}'.format('http', url)
def test_url_name(url, port, protocol): """Test to make sure url name generation behaves as expected""" assume(not url.endswith('/')) assume(url.count(':') == 0) server = Server(url, port, 'carl', 'password', protocol) assert server.url_name() == u'{}://{}:{}'.format(protocol, url, port)
def test_server_ssl_setting(): server = Server("code42.com", protocol="https") assert server.verify_ssl == True server = Server("code42.com", protocol="https", verify_ssl=False) assert server.verify_ssl == False try: server.verify_ssl = True assert False except AttributeError: pass
def build_node_server(): """ Common code for building a provider or storage node server. :return: An auth'd server object """ # if we failed to login/obtain auth token, return None so that we move onto the next # possible location try: url, login_token = url_and_storage_login_token() result_server = Server(url.hostname, url.port, protocol=url.scheme, verify_ssl=authority.verify_ssl) auth_token = _request_auth_token(result_server, login_token) except requests.HTTPError: LOG.debug("Failed to auth with location") return None result_server.authorization = auth_token return result_server
def build_node_server(): """ Common code for building a provider or storage node server. :return: An auth'd server object """ # if we failed to login/obtain auth token, return None so that we move onto the next # possible location try: url, login_token = url_and_storage_login_token() result_server = Server(url.hostname, url.port, protocol=url.scheme, verify_ssl=authority.verify_ssl) auth_token = _request_auth_token(result_server, login_token) except requests.RequestException as e: LOG.debug("Failed to auth with location. %e", str(e)) return None result_server.authorization = auth_token return result_server
def test_init(server_address, port, username, password, protocol): """Test to make sure basic initialization works""" assume(not server_address.endswith('/')) assume(server_address.count(':') == 0) server = Server(server_address, port, username, password, protocol) assert server assert server.server_address == server_address assert server.port == port assert server.username == username assert server.password == password assert server.protocol == protocol
def test_get(): """Test to make sure get rest call behaves as expected""" params = {'param_one': 'value_one', 'param_two': 'value_two'} def request_callback(_, uri, headers): """Callback function that asserts get request has proper paramters""" for param, value in params.items(): assert '{}={}'.format(param, value) in uri return 200, headers, json.dumps({'result': 'test'}) httpretty.register_uri(httpretty.GET, 'http://test.com:7777/api/Test', body=request_callback, content_type='application/json') resp = basic_server().get('Test', params) assert Server.json_from_response(resp)['result'] == 'test'
def test_address_parsing(): # Implied protocol, no port server = Server("code42.com") assert server.url_name() == "https://code42.com" # Implied protocol, secure port server = Server("code42.com:4285") assert server.url_name() == "https://code42.com:4285" # Implied protocol, non-secure port server = Server("code42.com:4280") assert server.url_name() == "http://code42.com:4280" # Explicit protocol, no port server = Server("https://code42.com") assert server.url_name() == "https://code42.com" # Explicit protocol, secure port server = Server("https://code42.com:4285") assert server.url_name() == "https://code42.com:4285" # Explicit protocol, non-secure port server = Server("https://code42.com:4280") assert server.url_name() == "https://code42.com:4280" # Explicit protocol, secure port, trailing slash server = Server("https://code42.com:4280/") assert server.url_name() == "https://code42.com:4280" # Explicit protocol, invalid port server = Server("https://code42.com:428a0") assert server.url_name() == "https://code42.com" # Invalid protocol, non-secure port server = Server("htttps://code42.com:4280") assert server.url_name() == "htttps://code42.com:4280" # Implied protocol, secure port argument server = Server("code42.com", port=4285) assert server.url_name() == "https://code42.com:4285" # Implied protocol, non-secure port argument server = Server("code42.com", port=4280) assert server.url_name() == "http://code42.com:4280" # Explicit protocol, secure port argument server = Server("https://code42.com", port=4285) assert server.url_name() == "https://code42.com:4285" # Explicit protocol, non-secure port argument server = Server("https://code42.com", port=4280) assert server.url_name() == "https://code42.com:4280" # Explicit protocol, port, override port argument server = Server("https://code42.com:4285", port=4280) assert server.url_name() == "https://code42.com:4280" # Explicit protocol, port, override port argument server = Server("http://code42.com:4280", port=4285) assert server.url_name() == "http://code42.com:4285" # Protocol, port, override protocol argument server = Server("http://code42.com:4280", protocol="https") assert server.url_name() == "https://code42.com:4280" # Protocol, port, override protocol argument, override port argument server = Server("http://code42.com:4280", protocol="https", port=4285) assert server.url_name() == "https://code42.com:4285"
def basic_server(): """ :return: A general purpose server object """ return Server('test.com', '7777', 'testname', 'testword')
def test_json_from_response(): """Test to make sure json gets parsed correctly from bytes""" response = FakeResponse(b'{"thing": "result"}') json_resp = Server.json_from_response(response) assert json_resp['thing'] == 'result'
# pylint: disable=import-error,invalid-name,protected-access import os import datetime import json import httpretty import hypothesis from c42api import backup_metadata_delta from c42api.common import resources from c42api.common.server import Server from c42api_test import test_lib SERVER = Server('backup_metadata_delta.test', '8378') def _api_url(resource): """Get a URL using the test SERVER variable for an API resource""" _, url = SERVER._prep_request(resource, None) return url with open( os.path.join(os.path.dirname(__file__), "data", "test_backup_metadata_delta.json")) as f: RESPONSE_DATA = json.load(f) @httpretty.activate