diff options
Diffstat (limited to 'macaroonbakery/tests/test_client.py')
-rw-r--r-- | macaroonbakery/tests/test_client.py | 80 |
1 files changed, 66 insertions, 14 deletions
diff --git a/macaroonbakery/tests/test_client.py b/macaroonbakery/tests/test_client.py index 6437a54..4bccb20 100644 --- a/macaroonbakery/tests/test_client.py +++ b/macaroonbakery/tests/test_client.py @@ -3,6 +3,7 @@ import base64 import datetime import json +import platform import threading import macaroonbakery.bakery as bakery @@ -11,6 +12,7 @@ import macaroonbakery.httpbakery as httpbakery import pymacaroons import requests import macaroonbakery._utils as utils +from macaroonbakery.httpbakery._error import DischargeError from fixtures import ( EnvironmentVariable, @@ -49,7 +51,7 @@ class TestClient(TestWithFixtures): srv_macaroon = b.oven.macaroon( version=bakery.LATEST_VERSION, expiry=AGES, caveats=None, ops=[TEST_OP]) - self.assertEquals(srv_macaroon.macaroon.location, 'loc') + self.assertEqual(srv_macaroon.macaroon.location, 'loc') client = httpbakery.Client() client.cookies.set_cookie(requests.cookies.create_cookie( 'macaroon-test', base64.b64encode(json.dumps([ @@ -61,7 +63,7 @@ class TestClient(TestWithFixtures): str(httpd.server_address[1]), cookies=client.cookies, auth=client.auth()) resp.raise_for_status() - self.assertEquals(resp.text, 'done') + self.assertEqual(resp.text, 'done') finally: httpd.shutdown() @@ -108,7 +110,7 @@ class TestClient(TestWithFixtures): cookies=client.cookies, auth=client.auth()) resp.raise_for_status() - self.assertEquals(resp.text, 'done') + self.assertEqual(resp.text, 'done') finally: httpd.shutdown() @@ -155,7 +157,7 @@ class TestClient(TestWithFixtures): cookies=client.cookies, auth=client.auth()) resp.raise_for_status() - self.assertEquals(resp.text, 'done') + self.assertEqual(resp.text, 'done') finally: httpd.shutdown() @@ -201,7 +203,7 @@ class TestClient(TestWithFixtures): cookies=client.cookies, auth=client.auth()) resp.raise_for_status() - self.assertEquals(resp.text, 'done') + self.assertEqual(resp.text, 'done') finally: httpd.shutdown() @@ -220,7 +222,7 @@ class TestClient(TestWithFixtures): srv_macaroon = b.oven.macaroon( version=bakery.LATEST_VERSION, expiry=AGES, caveats=None, ops=[TEST_OP]) - self.assertEquals(srv_macaroon.macaroon.location, 'loc') + self.assertEqual(srv_macaroon.macaroon.location, 'loc') client = httpbakery.Client() # Note: by using "localhost" instead of the presumably numeric address held # in httpd.server_address, we're triggering the no-FQDN logic in the cookie @@ -229,7 +231,7 @@ class TestClient(TestWithFixtures): url='http://localhost:' + str(httpd.server_address[1]), cookies=client.cookies, auth=client.auth()) resp.raise_for_status() - self.assertEquals(resp.text, 'done') + self.assertEqual(resp.text, 'done') except httpbakery.BakeryException: pass # interacion required exception is expected finally: @@ -252,7 +254,7 @@ class TestClient(TestWithFixtures): srv_macaroon = b.oven.macaroon( version=bakery.LATEST_VERSION, expiry=AGES, caveats=None, ops=[TEST_OP]) - self.assertEquals(srv_macaroon.macaroon.location, 'loc') + self.assertEqual(srv_macaroon.macaroon.location, 'loc') headers = { 'Macaroons': base64.b64encode(json.dumps([ srv_macaroon.to_dict().get('m') @@ -263,7 +265,7 @@ class TestClient(TestWithFixtures): str(httpd.server_address[1]), headers=headers) resp.raise_for_status() - self.assertEquals(resp.text, 'done') + self.assertEqual(resp.text, 'done') finally: httpd.shutdown() @@ -315,8 +317,8 @@ class TestClient(TestWithFixtures): base64.b64decode(client.cookies.get('macaroon-test')).decode('utf-8'))[0]) t = checkers.macaroons_expiry_time( checkers.Namespace(), [m.macaroon]) - self.assertEquals(ages, t) - self.assertEquals(resp.text, 'done') + self.assertEqual(ages, t) + self.assertEqual(resp.text, 'done') finally: httpd.shutdown() @@ -516,6 +518,56 @@ class TestClient(TestWithFixtures): finally: httpd.shutdown() + def test_discharge_jsondecodeerror(self): + class _DischargerLocator(bakery.ThirdPartyLocator): + def __init__(self): + self.key = bakery.generate_key() + + def third_party_info(self, loc): + if loc == 'http://1.2.3.4': + return bakery.ThirdPartyInfo( + public_key=self.key.public_key, + version=bakery.LATEST_VERSION, + ) + d = _DischargerLocator() + b = new_bakery('loc', d, None) + + @urlmatch(path='.*/discharge') + def discharge(url, request): + return { + 'status_code': 503, + 'content': 'bad system', + } + + def handler(*args): + GetHandler(b, 'http://1.2.3.4', None, None, None, AGES, *args) + + try: + httpd = HTTPServer(('', 0), handler) + thread = threading.Thread(target=httpd.serve_forever) + thread.start() + + client = httpbakery.Client() + + with HTTMock(discharge): + with self.assertRaises(DischargeError) as discharge_error: + requests.get( + 'http://' + httpd.server_address[0] + ':' + str( + httpd.server_address[1]), + cookies=client.cookies, + auth=client.auth()) + if platform.python_version_tuple()[0] == '2': + self.assertEqual(str(discharge_error.exception), + 'third party refused dischargex: unexpected response: ' + "[503] 'bad system'") + else: + self.assertEqual(str(discharge_error.exception), + 'third party refused dischargex: unexpected response: ' + "[503] b'bad system'") + + finally: + httpd.shutdown() + def test_extract_macaroons_from_request(self): def encode_macaroon(m): macaroons = '[' + utils.macaroon_to_json_string(m) + ']' @@ -539,10 +591,10 @@ class TestClient(TestWithFixtures): jar.add_cookie_header(req) macaroons = httpbakery.extract_macaroons(req) - self.assertEquals(len(macaroons), 2) + self.assertEqual(len(macaroons), 2) macaroons.sort(key=lambda ms: ms[0].identifier) - self.assertEquals(macaroons[0][0].identifier, m1.identifier) - self.assertEquals(macaroons[1][0].identifier, m2.identifier) + self.assertEqual(macaroons[0][0].identifier, m1.identifier) + self.assertEqual(macaroons[1][0].identifier, m2.identifier) def test_handle_error_cookie_path(self): macaroon = bakery.Macaroon( |