diff options
Diffstat (limited to 'macaroonbakery/tests')
-rw-r--r-- | macaroonbakery/tests/test_client.py | 35 | ||||
-rw-r--r-- | macaroonbakery/tests/test_utils.py | 22 |
2 files changed, 57 insertions, 0 deletions
diff --git a/macaroonbakery/tests/test_client.py b/macaroonbakery/tests/test_client.py index bfc7807..4061a8a 100644 --- a/macaroonbakery/tests/test_client.py +++ b/macaroonbakery/tests/test_client.py @@ -55,6 +55,41 @@ class TestClient(TestCase): finally: httpd.shutdown() + def test_cookie_domain_host_not_fqdn(self): + # See + # https://github.com/go-macaroon-bakery/py-macaroon-bakery/issues/53 + + b = new_bakery('loc', None, None) + + def handler(*args): + GetHandler(b, None, None, None, None, AGES, *args) + try: + httpd = HTTPServer(('', 0), handler) + thread = threading.Thread(target=httpd.serve_forever) + thread.start() + srv_macaroon = b.oven.macaroon( + version=bakery.LATEST_VERSION, expiry=AGES, + caveats=None, ops=[TEST_OP]) + self.assertEquals(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 + # code. + resp = requests.get( + url='http://localhost:' + str(httpd.server_address[1]), + cookies=client.cookies, auth=client.auth()) + resp.raise_for_status() + self.assertEquals(resp.text, 'done') + except httpbakery.BakeryException: + pass # interacion required exception is expected + finally: + httpd.shutdown() + + # the cookie has the .local domain appended + [cookie] = client.cookies + self.assertEqual(cookie.name, 'macaroon-test') + self.assertEqual(cookie.domain, 'localhost.local') + def test_single_party_with_header(self): b = new_bakery('loc', None, None) diff --git a/macaroonbakery/tests/test_utils.py b/macaroonbakery/tests/test_utils.py index 65edeb4..4ed3e81 100644 --- a/macaroonbakery/tests/test_utils.py +++ b/macaroonbakery/tests/test_utils.py @@ -1,3 +1,5 @@ +# -*- coding: utf-8 -*- + # Copyright 2017 Canonical Ltd. # Licensed under the LGPLv3, see LICENCE file for details. @@ -26,6 +28,26 @@ class CookieTest(TestCase): ValueError, cookie, 'http://example.com', 'test', 'value', expires=timestamp) + def test_cookie_with_hostname_not_fqdn(self): + c = cookie('http://myhost', 'test', 'value') + self.assertEqual(c.domain, 'myhost.local') + + def test_cookie_with_hostname_ipv4(self): + c = cookie('http://1.2.3.4', 'test', 'value') + self.assertEqual(c.domain, '1.2.3.4') + + def test_cookie_with_hostname_ipv6(self): + c = cookie('http://[dead::beef]', 'test', 'value') + self.assertEqual(c.domain, 'dead::beef') + + def test_cookie_with_hostname_like_ipv4(self): + c = cookie('http://1.2.3.4.com', 'test', 'value') + self.assertEqual(c.domain, '1.2.3.4.com') + + def test_cookie_with_hostname_not_ascii(self): + c = cookie('http://κουλουράκι', 'test', 'value') + self.assertEqual(c.domain, 'κουλουράκι.local') + class TestB64Decode(TestCase): def test_decode(self): |