summaryrefslogtreecommitdiff
path: root/macaroonbakery/tests/test_macaroon.py
blob: afc7d52558cf2d022d5b40d5fedc13374f760ceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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()
        ))