-
Notifications
You must be signed in to change notification settings - Fork 0
/
jar_test.go
74 lines (63 loc) · 2.04 KB
/
jar_test.go
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
65
66
67
68
69
70
71
72
73
74
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charmstore
import (
"net/url"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/httpbakery"
"github.com/juju/charm/v9"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/macaroon.v2"
)
var _ = gc.Suite(&MacaroonJarSuite{})
type MacaroonJarSuite struct {
testing.IsolationSuite
}
func (MacaroonJarSuite) TestActivate(c *gc.C) {
cache := fakeCache{}
u, err := url.Parse("http://charmstore.com")
c.Assert(err, jc.ErrorIsNil)
jar, err := newMacaroonJar(cache, u)
c.Assert(err, jc.ErrorIsNil)
ch := charm.MustParseURL("cs:mysql")
err = jar.Activate(ch)
c.Assert(err, jc.ErrorIsNil)
m, err := macaroon.New([]byte("key"), []byte("id"), "loc", macaroon.LatestVersion)
c.Assert(err, jc.ErrorIsNil)
ms := macaroon.Slice{m}
httpbakery.SetCookie(jar, u, MacaroonNamespace, ms)
// c.Assert(cache[ch], gc.DeepEquals, ms)
MacaroonEquals(c, cache[ch][0], ms[0])
}
func (MacaroonJarSuite) TestDeactivate(c *gc.C) {
cache := fakeCache{}
u, err := url.Parse("http://charmstore.com")
c.Assert(err, jc.ErrorIsNil)
jar, err := newMacaroonJar(cache, u)
c.Assert(err, jc.ErrorIsNil)
ch := charm.MustParseURL("cs:mysql")
err = jar.Activate(ch)
c.Assert(err, jc.ErrorIsNil)
m, err := macaroon.New([]byte("key"), []byte("id"), "loc", macaroon.LatestVersion)
c.Assert(err, jc.ErrorIsNil)
ms := macaroon.Slice{m}
err = jar.Deactivate()
c.Assert(err, jc.ErrorIsNil)
httpbakery.SetCookie(jar, u, MacaroonNamespace, ms)
c.Assert(cache, gc.HasLen, 0)
c.Assert(jar.Cookies(u), gc.HasLen, 1)
}
type fakeCache map[*charm.URL]macaroon.Slice
func (f fakeCache) Set(u *charm.URL, m macaroon.Slice) error {
f[u] = m
return nil
}
func (f fakeCache) Get(u *charm.URL) (macaroon.Slice, error) {
return f[u], nil
}
func MacaroonEquals(c *gc.C, m1, m2 *macaroon.Macaroon) {
c.Assert(m1.Id(), jc.DeepEquals, m2.Id())
c.Assert(m1.Signature(), jc.DeepEquals, m2.Signature())
c.Assert(m1.Location(), jc.DeepEquals, m2.Location())
}