205
211
s.store = s.srv.Pool().Store()
214
func (s *commonSuite) addPublicCharmFromRepo(c *gc.C, charmName string, rurl *router.ResolvedURL) (*router.ResolvedURL, charm.Charm) {
215
return s.addPublicCharm(c, storetesting.Charms.CharmDir(charmName), rurl)
218
func (s *commonSuite) addPublicCharm(c *gc.C, ch charm.Charm, rurl *router.ResolvedURL) (*router.ResolvedURL, charm.Charm) {
219
err := s.store.AddCharmWithArchive(rurl, ch)
220
c.Assert(err, gc.IsNil)
225
func (s *commonSuite) setPublic(c *gc.C, rurl *router.ResolvedURL) {
226
err := s.store.SetPerms(&rurl.URL, "stable.read", params.Everyone)
227
c.Assert(err, gc.IsNil)
228
err = s.store.Publish(rurl, params.StableChannel)
229
c.Assert(err, gc.IsNil)
232
func (s *commonSuite) addPublicBundleFromRepo(c *gc.C, bundleName string, rurl *router.ResolvedURL, addRequiredCharms bool) (*router.ResolvedURL, charm.Bundle) {
233
return s.addPublicBundle(c, storetesting.Charms.BundleDir(bundleName), rurl, addRequiredCharms)
236
func (s *commonSuite) addPublicBundle(c *gc.C, bundle charm.Bundle, rurl *router.ResolvedURL, addRequiredCharms bool) (*router.ResolvedURL, charm.Bundle) {
237
if addRequiredCharms {
238
s.addRequiredCharms(c, bundle)
240
err := s.store.AddBundleWithArchive(rurl, bundle)
241
c.Assert(err, gc.IsNil)
246
// addCharms adds all the given charms to s.store. The
247
// map key is the id of the charm.
248
func (s *commonSuite) addCharms(c *gc.C, charms map[string]charm.Charm) {
249
for id, ch := range charms {
250
s.addPublicCharm(c, storetesting.NewCharm(ch.Meta()), mustParseResolvedURL(id))
254
// setPerms sets the stable channel read permissions of a set of
255
// entities. The map key is the is the id of each entity; its associated
256
// value is its read ACL.
257
func (s *commonSuite) setPerms(c *gc.C, readACLs map[string][]string) {
258
for url, acl := range readACLs {
259
err := s.store.SetPerms(charm.MustParseURL(url), "stable.read", acl...)
260
c.Assert(err, gc.IsNil)
208
264
// handler returns a request handler that can be
209
265
// used to invoke private methods. The caller
210
266
// is responsible for calling Put on the returned handler.
211
267
func (s *commonSuite) handler(c *gc.C) *v5.ReqHandler {
212
h := v5.New(s.store.Pool(), s.srvParams)
268
h := v5.New(s.store.Pool(), s.srvParams, "")
214
rh, err := h.NewReqHandler()
270
rh, err := h.NewReqHandler(new(http.Request))
215
271
c.Assert(err, gc.IsNil)
216
272
// It would be nice if we could call s.AddCleanup here
217
273
// to call rh.Put when the test has completed, but
223
279
func storeURL(path string) string {
283
func (s *commonSuite) bakeryDoAsUser(c *gc.C, user string) func(*http.Request) (*http.Response, error) {
284
bclient := httpbakery.NewClient()
285
m, err := s.store.Bakery.NewMacaroon("", nil, []checkers.Caveat{
286
checkers.DeclaredCaveat("username", user),
288
c.Assert(err, gc.IsNil)
289
macaroonCookie, err := httpbakery.NewCookie(macaroon.Slice{m})
290
c.Assert(err, gc.IsNil)
291
return func(req *http.Request) (*http.Response, error) {
292
req.AddCookie(macaroonCookie)
294
return bclient.Do(req)
296
body := req.Body.(io.ReadSeeker)
298
return bclient.DoWithBody(req, body)
302
// addRequiredCharms adds any charms required by the given
303
// bundle that are not already in the store.
304
func (s *commonSuite) addRequiredCharms(c *gc.C, bundle charm.Bundle) {
305
for _, svc := range bundle.Data().Services {
306
u := charm.MustParseURL(svc.Charm)
307
if _, err := s.store.FindBestEntity(u, params.StableChannel, nil); err == nil {
310
if u.Revision == -1 {
313
var rurl router.ResolvedURL
315
chDir, err := charm.ReadCharmDir(storetesting.Charms.CharmDirPath(u.Name))
316
ch := charm.Charm(chDir)
318
// The charm doesn't exist in the local charm repo; make one up.
319
ch = storetesting.NewCharm(nil)
321
if len(ch.Meta().Series) == 0 && u.Series == "" {
322
rurl.URL.Series = "trusty"
325
rurl.URL.User = "charmers"
326
rurl.PromulgatedRevision = rurl.URL.Revision
328
rurl.PromulgatedRevision = -1
330
c.Logf("adding charm %v %d required by bundle to fulfil %v", &rurl.URL, rurl.PromulgatedRevision, svc.Charm)
331
s.addPublicCharm(c, ch, &rurl)
335
func (s *commonSuite) assertPut(c *gc.C, url string, val interface{}) {
336
s.assertPut0(c, url, val, false)
339
func (s *commonSuite) assertPutAsAdmin(c *gc.C, url string, val interface{}) {
340
s.assertPut0(c, url, val, true)
343
func (s *commonSuite) assertPut0(c *gc.C, url string, val interface{}, asAdmin bool) {
344
body, err := json.Marshal(val)
345
c.Assert(err, gc.IsNil)
346
p := httptesting.JSONCallParams{
352
"Content-Type": {"application/json"},
354
Body: bytes.NewReader(body),
357
p.Username = testUsername
358
p.Password = testPassword
360
httptesting.AssertJSONCall(c, p)
363
func (s *commonSuite) assertGet(c *gc.C, url string, expectVal interface{}) {
364
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
368
ExpectBody: expectVal,
372
// assertGetIsUnauthorized asserts that a GET to the given URL results
373
// in an ErrUnauthorized response with the given error message.
374
func (s *commonSuite) assertGetIsUnauthorized(c *gc.C, url, expectMessage string) {
375
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
380
ExpectStatus: http.StatusUnauthorized,
381
ExpectBody: params.Error{
382
Code: params.ErrUnauthorized,
383
Message: expectMessage,
388
// assertGetIsUnauthorized asserts that a PUT to the given URL with the
389
// given body value results in an ErrUnauthorized response with the given
391
func (s *commonSuite) assertPutIsUnauthorized(c *gc.C, url string, val interface{}, expectMessage string) {
392
body, err := json.Marshal(val)
393
c.Assert(err, gc.IsNil)
394
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
400
"Content-Type": {"application/json"},
402
Body: bytes.NewReader(body),
403
ExpectStatus: http.StatusUnauthorized,
404
ExpectBody: params.Error{
405
Code: params.ErrUnauthorized,
406
Message: expectMessage,
411
// doAsUser calls the given function, discharging any authorization
412
// request as the given user name.
413
func (s *commonSuite) doAsUser(user string, f func()) {
415
s.discharge = dischargeForUser(user)
227
422
func bakeryDo(client *http.Client) func(*http.Request) (*http.Response, error) {