github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/charmstore/jar_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package charmstore
     5  
     6  import (
     7  	"net/url"
     8  
     9  	"github.com/juju/testing"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  	"gopkg.in/juju/charm.v6-unstable"
    13  	"gopkg.in/macaroon-bakery.v1/httpbakery"
    14  	"gopkg.in/macaroon.v1"
    15  )
    16  
    17  var _ = gc.Suite(&MacaroonJarSuite{})
    18  
    19  type MacaroonJarSuite struct {
    20  	testing.IsolationSuite
    21  }
    22  
    23  func (MacaroonJarSuite) TestActivate(c *gc.C) {
    24  	cache := fakeCache{}
    25  	u, err := url.Parse("http://charmstore.com")
    26  	c.Assert(err, jc.ErrorIsNil)
    27  	jar, err := newMacaroonJar(cache, u)
    28  	c.Assert(err, jc.ErrorIsNil)
    29  	ch := charm.MustParseURL("cs:mysql")
    30  	err = jar.Activate(ch)
    31  	c.Assert(err, jc.ErrorIsNil)
    32  	m, err := macaroon.New([]byte("key"), "id", "loc")
    33  	c.Assert(err, jc.ErrorIsNil)
    34  	ms := macaroon.Slice{m}
    35  	httpbakery.SetCookie(jar, u, ms)
    36  	c.Assert(cache[ch], gc.DeepEquals, ms)
    37  }
    38  
    39  func (MacaroonJarSuite) TestDeactivate(c *gc.C) {
    40  	cache := fakeCache{}
    41  	u, err := url.Parse("http://charmstore.com")
    42  	c.Assert(err, jc.ErrorIsNil)
    43  	jar, err := newMacaroonJar(cache, u)
    44  	c.Assert(err, jc.ErrorIsNil)
    45  	ch := charm.MustParseURL("cs:mysql")
    46  	err = jar.Activate(ch)
    47  	c.Assert(err, jc.ErrorIsNil)
    48  	m, err := macaroon.New([]byte("key"), "id", "loc")
    49  	c.Assert(err, jc.ErrorIsNil)
    50  	ms := macaroon.Slice{m}
    51  	err = jar.Deactivate()
    52  	c.Assert(err, jc.ErrorIsNil)
    53  	httpbakery.SetCookie(jar, u, ms)
    54  	c.Assert(cache, gc.HasLen, 0)
    55  	c.Assert(jar.Cookies(u), gc.HasLen, 1)
    56  }
    57  
    58  type fakeCache map[*charm.URL]macaroon.Slice
    59  
    60  func (f fakeCache) Set(u *charm.URL, m macaroon.Slice) error {
    61  	f[u] = m
    62  	return nil
    63  }
    64  
    65  func (f fakeCache) Get(u *charm.URL) (macaroon.Slice, error) {
    66  	return f[u], nil
    67  }