summaryrefslogtreecommitdiff
path: root/macaroonbakery/tests/test_macaroon.py
diff options
context:
space:
mode:
authorColin Watson <cjwatson@debian.org>2017-10-31 10:34:41 +0000
committerColin Watson <cjwatson@debian.org>2017-10-31 10:34:41 +0000
commit79ff2842fa477ee0693ea167c0a74cd7cf080d27 (patch)
treed0eb436672c1a73088fce42f939bdf4cf6f4427b /macaroonbakery/tests/test_macaroon.py
Import py-macaroon-bakery_0.0.3.orig.tar.gz
Diffstat (limited to 'macaroonbakery/tests/test_macaroon.py')
-rw-r--r--macaroonbakery/tests/test_macaroon.py64
1 files changed, 64 insertions, 0 deletions
diff --git a/macaroonbakery/tests/test_macaroon.py b/macaroonbakery/tests/test_macaroon.py
new file mode 100644
index 0000000..afc7d52
--- /dev/null
+++ b/macaroonbakery/tests/test_macaroon.py
@@ -0,0 +1,64 @@
+# Copyright 2017 Canonical Ltd.
+# Licensed under the LGPLv3, see LICENCE file for details.
+
+from unittest import TestCase
+
+import six
+
+import nacl.utils
+
+from macaroonbakery import bakery, macaroon, checkers, codec
+
+
+class TestMacaroon(TestCase):
+ def test_new_macaroon(self):
+ m = macaroon.Macaroon(b'rootkey',
+ b'some id',
+ 'here',
+ bakery.LATEST_BAKERY_VERSION)
+ self.assertIsNotNone(m)
+ self.assertEquals(m._macaroon.identifier, 'some id')
+ self.assertEquals(m._macaroon.location, 'here')
+ self.assertEquals(m.version, macaroon.macaroon_version(
+ bakery.LATEST_BAKERY_VERSION))
+
+ def test_add_first_party_caveat(self):
+ m = macaroon.Macaroon('rootkey',
+ 'some id',
+ 'here',
+ bakery.LATEST_BAKERY_VERSION)
+ m = m.add_caveat(checkers.Caveat('test_condition'))
+ caveats = m.first_party_caveats()
+ self.assertEquals(len(caveats), 1)
+ self.assertEquals(caveats[0].caveat_id, 'test_condition')
+
+ def test_add_third_party_caveat(self):
+ m = macaroon.Macaroon('rootkey',
+ 'some id',
+ 'here',
+ bakery.LATEST_BAKERY_VERSION)
+ loc = macaroon.ThirdPartyLocator()
+ fp_key = nacl.public.PrivateKey.generate()
+ tp_key = nacl.public.PrivateKey.generate()
+
+ loc.add_info('test_location',
+ bakery.ThirdPartyInfo(
+ bakery.BAKERY_V1,
+ tp_key.public_key))
+ m = m.add_caveat(checkers.Caveat(condition='test_condition',
+ location='test_location'),
+ fp_key, loc)
+
+ tp_cav = m.third_party_caveats()
+ self.assertEquals(len(tp_cav), 1)
+ self.assertEquals(tp_cav[0].location, 'test_location')
+ cav = codec.decode_caveat(tp_key, six.b(tp_cav[0].caveat_id))
+ self.assertEquals(cav, macaroon.ThirdPartyCaveatInfo(
+ condition='test_condition',
+ first_party_public_key=fp_key.public_key,
+ third_party_key_pair=tp_key,
+ root_key='random',
+ caveat=six.b(tp_cav[0].caveat_id),
+ version=bakery.BAKERY_V1,
+ ns=macaroon.legacy_namespace()
+ ))