github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/api/client_macaroon_test.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package api_test
     5  
     6  import (
     7  	"fmt"
     8  
     9  	jc "github.com/juju/testing/checkers"
    10  	gc "gopkg.in/check.v1"
    11  	"gopkg.in/juju/charm.v6-unstable"
    12  
    13  	"github.com/juju/juju/api"
    14  	apitesting "github.com/juju/juju/api/testing"
    15  	"github.com/juju/juju/testcharms"
    16  )
    17  
    18  var _ = gc.Suite(&clientMacaroonSuite{})
    19  
    20  // clientMacaroonSuite tests that Client endpoints that are
    21  // independent of the RPC-based API work with
    22  // macaroon authentication.
    23  type clientMacaroonSuite struct {
    24  	apitesting.MacaroonSuite
    25  	client    *api.Client
    26  	cookieJar *apitesting.ClearableCookieJar
    27  }
    28  
    29  func (s *clientMacaroonSuite) SetUpTest(c *gc.C) {
    30  	s.MacaroonSuite.SetUpTest(c)
    31  	s.AddModelUser(c, "testuser@somewhere")
    32  	s.cookieJar = apitesting.NewClearableCookieJar()
    33  	s.DischargerLogin = func() string { return "testuser@somewhere" }
    34  	s.client = s.OpenAPI(c, nil, s.cookieJar).Client()
    35  
    36  	// Even though we've logged into the API, we want
    37  	// the tests below to exercise the discharging logic
    38  	// so we clear the cookies.
    39  	s.cookieJar.Clear()
    40  }
    41  
    42  func (s *clientMacaroonSuite) TearDownTest(c *gc.C) {
    43  	s.client.Close()
    44  	s.MacaroonSuite.TearDownTest(c)
    45  }
    46  
    47  func (s *clientMacaroonSuite) TestAddLocalCharmWithFailedDischarge(c *gc.C) {
    48  	s.DischargerLogin = func() string { return "" }
    49  	charmArchive := testcharms.Repo.CharmArchive(c.MkDir(), "dummy")
    50  	curl := charm.MustParseURL(
    51  		fmt.Sprintf("local:quantal/%s-%d", charmArchive.Meta().Name, charmArchive.Revision()),
    52  	)
    53  	savedURL, err := s.client.AddLocalCharm(curl, charmArchive)
    54  	c.Assert(err, gc.ErrorMatches, `POST https://.*/model/deadbeef-0bad-400d-8000-4b1d0d06f00d/charms\?series=quantal: cannot get discharge from "https://.*": third party refused discharge: cannot discharge: login denied by discharger`)
    55  	c.Assert(savedURL, gc.IsNil)
    56  }
    57  
    58  func (s *clientMacaroonSuite) TestAddLocalCharmSuccess(c *gc.C) {
    59  	c.Skip("dimitern: disabled as flaky - see http://pad.lv/1560511 as possible root cause")
    60  
    61  	charmArchive := testcharms.Repo.CharmArchive(c.MkDir(), "dummy")
    62  	curl := charm.MustParseURL(
    63  		fmt.Sprintf("local:quantal/%s-%d", charmArchive.Meta().Name, charmArchive.Revision()),
    64  	)
    65  	// Upload an archive with its original revision.
    66  	savedURL, err := s.client.AddLocalCharm(curl, charmArchive)
    67  	c.Assert(err, jc.ErrorIsNil)
    68  	c.Assert(savedURL.String(), gc.Equals, curl.String())
    69  }
    70  
    71  func (s *clientMacaroonSuite) TestAddLocalCharmUnauthorized(c *gc.C) {
    72  	s.DischargerLogin = func() string { return "baduser" }
    73  	charmArchive := testcharms.Repo.CharmArchive(c.MkDir(), "dummy")
    74  	curl := charm.MustParseURL(
    75  		fmt.Sprintf("local:quantal/%s-%d", charmArchive.Meta().Name, charmArchive.Revision()),
    76  	)
    77  	// Upload an archive with its original revision.
    78  	_, err := s.client.AddLocalCharm(curl, charmArchive)
    79  	c.Assert(err, gc.ErrorMatches, `POST https://.*/model/deadbeef-0bad-400d-8000-4b1d0d06f00d/charms\?series=quantal: invalid entity name or password`)
    80  }