diff options
author | Antonio Terceiro <antonio.terceiro@linaro.org> | 2013-05-22 10:45:44 -0300 |
---|---|---|
committer | Antonio Terceiro <antonio.terceiro@linaro.org> | 2013-05-22 10:45:44 -0300 |
commit | 3635d12e2fa700b9467bb5876af00a4b947f41b4 (patch) | |
tree | 1a70bdeafdb4b55a029443d0c15a030cef78c534 /lava_tool | |
parent | 6987e702bac88049e97de089ef7e904186e7f0b4 (diff) |
adapt the authtoken tests to the new reality
Diffstat (limited to 'lava_tool')
-rw-r--r-- | lava_tool/authtoken.py | 17 | ||||
-rw-r--r-- | lava_tool/tests/test_authtoken.py | 53 |
2 files changed, 29 insertions, 41 deletions
diff --git a/lava_tool/authtoken.py b/lava_tool/authtoken.py index 70837cd..a556e56 100644 --- a/lava_tool/authtoken.py +++ b/lava_tool/authtoken.py @@ -72,6 +72,15 @@ class XMLRPCTransport(xmlrpclib.Transport): def request(self, host, handler, request_body, verbose=0): self.verbose = verbose + request = self.build_http_request(host, handler, request_body) + try: + response = self._opener.open(request) + except urllib2.HTTPError as e: + raise xmlrpclib.ProtocolError( + host + handler, e.code, e.msg, e.info()) + return self.parse_response(response) + + def build_http_request(self, host, handler, request_body): token = None user = None auth, host = urllib.splituser(host) @@ -88,12 +97,8 @@ class XMLRPCTransport(xmlrpclib.Transport): if token: auth = base64.b64encode(urllib.unquote(user + ':' + token)) request.add_header("Authorization", "Basic " + auth) - try: - response = self._opener.open(request) - except urllib2.HTTPError as e: - raise xmlrpclib.ProtocolError( - host + handler, e.code, e.msg, e.info()) - return self.parse_response(response) + + return request class AuthenticatingServerProxy(xmlrpclib.ServerProxy): diff --git a/lava_tool/tests/test_authtoken.py b/lava_tool/tests/test_authtoken.py index 53127b0..51fcbca 100644 --- a/lava_tool/tests/test_authtoken.py +++ b/lava_tool/tests/test_authtoken.py @@ -31,6 +31,7 @@ from mocker import ARGS, KWARGS, Mocker from lava_tool.authtoken import ( AuthenticatingServerProxy, + XMLRPCTransport, MemoryAuthBackend, ) from lava_tool.interface import LavaCommandError @@ -39,48 +40,30 @@ class TestAuthenticatingServerProxy(TestCase): def auth_headers_for_method_call_on(self, url, auth_backend): parsed = urlparse.urlparse(url) - expected_host = parsed.hostname - if parsed.port: - expected_host += ':' + str(parsed.port) - server_proxy = AuthenticatingServerProxy( - url, auth_backend=auth_backend) + mocker = Mocker() - if url.startswith('https'): - cls_name = 'httplib.HTTPSConnection' - expected_constructor_args = (expected_host, ARGS) - else: - cls_name = 'httplib.HTTPConnection' - expected_constructor_args = (expected_host, ARGS) - mocked_HTTPConnection = mocker.replace(cls_name, passthrough=False) - mocked_connection = mocked_HTTPConnection(*expected_constructor_args) - # nospec() is required because of - # https://bugs.launchpad.net/mocker/+bug/794351 - mocker.nospec() - auth_data = [] - mocked_connection.putrequest(ARGS, KWARGS) + transport = mocker.mock() - def match_header(header, *values): - if header.lower() == 'authorization': - if len(values) != 1: - self.fail( - 'more than one value for ' - 'putheader("Authorization", ...)') - auth_data.append(values[0]) - mocked_connection.putheader(ARGS) - mocker.call(match_header) - mocker.count(1, None) + auth_data = [] - mocked_connection.endheaders(ARGS, KWARGS) + def intercept_request(host, handler, request_body, verbose=0): + actual_transport = XMLRPCTransport(parsed.scheme, auth_backend) + request = actual_transport.build_http_request(host, handler, request_body) + if (request.has_header('Authorization')): + auth_data.append(request.get_header('Authorization')) - mocked_connection.getresponse(ARGS, KWARGS) - s = StringIO.StringIO(xmlrpclib.dumps((1,), methodresponse=True)) - s.status = 200 - mocker.result(s) + response_body = xmlrpclib.dumps((1,), methodresponse=True) + response = StringIO.StringIO(response_body) + response.status = 200 + response.__len__ = lambda: len(response_body) - mocked_connection.close() - mocker.count(0, 1) + transport.request(ARGS, KWARGS) + mocker.call(intercept_request) + mocker.result(response) with mocker: + server_proxy = AuthenticatingServerProxy( + url, auth_backend=auth_backend, transport=transport) server_proxy.method() return auth_data |