185
190
s.store = s.srv.Pool().Store()
193
func (s *commonSuite) addPublicCharmFromRepo(c *gc.C, charmName string, rurl *router.ResolvedURL) (*router.ResolvedURL, charm.Charm) {
194
return s.addPublicCharm(c, storetesting.Charms.CharmDir(charmName), rurl)
197
func (s *commonSuite) addPublicCharm(c *gc.C, ch charm.Charm, rurl *router.ResolvedURL) (*router.ResolvedURL, charm.Charm) {
198
err := s.store.AddCharmWithArchive(rurl, ch)
199
c.Assert(err, gc.IsNil)
204
func (s *commonSuite) setPublic(c *gc.C, rurl *router.ResolvedURL) {
205
err := s.store.SetPerms(&rurl.URL, "stable.read", params.Everyone)
206
c.Assert(err, gc.IsNil)
207
err = s.store.Publish(rurl, params.StableChannel)
208
c.Assert(err, gc.IsNil)
211
func (s *commonSuite) addPublicBundleFromRepo(c *gc.C, bundleName string, rurl *router.ResolvedURL, addRequiredCharms bool) (*router.ResolvedURL, charm.Bundle) {
212
return s.addPublicBundle(c, storetesting.Charms.BundleDir(bundleName), rurl, addRequiredCharms)
215
func (s *commonSuite) addPublicBundle(c *gc.C, bundle charm.Bundle, rurl *router.ResolvedURL, addRequiredCharms bool) (*router.ResolvedURL, charm.Bundle) {
216
if addRequiredCharms {
217
s.addRequiredCharms(c, bundle)
219
err := s.store.AddBundleWithArchive(rurl, bundle)
220
c.Assert(err, gc.IsNil)
225
// addCharms adds all the given charms to s.store. The
226
// map key is the id of the charm.
227
func (s *commonSuite) addCharms(c *gc.C, charms map[string]charm.Charm) {
228
for id, ch := range charms {
229
s.addPublicCharm(c, storetesting.NewCharm(ch.Meta()), mustParseResolvedURL(id))
233
// setPerms sets the stable channel read permissions of a set of
234
// entities. The map key is the is the id of each entity; its associated
235
// value is its read ACL.
236
func (s *commonSuite) setPerms(c *gc.C, readACLs map[string][]string) {
237
for url, acl := range readACLs {
238
err := s.store.SetPerms(charm.MustParseURL(url), "stable.read", acl...)
239
c.Assert(err, gc.IsNil)
188
243
// handler returns a request handler that can be
189
244
// used to invoke private methods. The caller
190
245
// is responsible for calling Put on the returned handler.
191
246
func (s *commonSuite) handler(c *gc.C) v4.ReqHandler {
192
h := v4.New(s.store.Pool(), s.srvParams)
247
h := v4.New(s.store.Pool(), s.srvParams, "")
194
rh, err := h.NewReqHandler()
249
rh, err := h.NewReqHandler(new(http.Request))
195
250
c.Assert(err, gc.IsNil)
196
251
// It would be nice if we could call s.AddCleanup here
197
252
// to call rh.Put when the test has completed, but
204
259
return "/v4/" + path
262
// addRequiredCharms adds any charms required by the given
263
// bundle that are not already in the store.
264
func (s *commonSuite) addRequiredCharms(c *gc.C, bundle charm.Bundle) {
265
for _, svc := range bundle.Data().Services {
266
u := charm.MustParseURL(svc.Charm)
267
if _, err := s.store.FindBestEntity(u, params.StableChannel, nil); err == nil {
270
if u.Revision == -1 {
273
var rurl router.ResolvedURL
275
chDir, err := charm.ReadCharmDir(storetesting.Charms.CharmDirPath(u.Name))
276
ch := charm.Charm(chDir)
278
// The charm doesn't exist in the local charm repo; make one up.
279
ch = storetesting.NewCharm(nil)
281
if len(ch.Meta().Series) == 0 && u.Series == "" {
282
rurl.URL.Series = "trusty"
285
rurl.URL.User = "charmers"
286
rurl.PromulgatedRevision = rurl.URL.Revision
288
rurl.PromulgatedRevision = -1
290
c.Logf("adding charm %v %d required by bundle to fulfil %v", &rurl.URL, rurl.PromulgatedRevision, svc.Charm)
291
s.addPublicCharm(c, ch, &rurl)
295
func (s *commonSuite) assertPut(c *gc.C, url string, val interface{}) {
296
s.assertPut0(c, url, val, false)
299
func (s *commonSuite) assertPutAsAdmin(c *gc.C, url string, val interface{}) {
300
s.assertPut0(c, url, val, true)
303
func (s *commonSuite) assertPut0(c *gc.C, url string, val interface{}, asAdmin bool) {
304
body, err := json.Marshal(val)
305
c.Assert(err, gc.IsNil)
306
p := httptesting.JSONCallParams{
312
"Content-Type": {"application/json"},
314
Body: bytes.NewReader(body),
317
p.Username = testUsername
318
p.Password = testPassword
320
httptesting.AssertJSONCall(c, p)
323
func (s *commonSuite) assertGet(c *gc.C, url string, expectVal interface{}) {
324
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
328
ExpectBody: expectVal,
332
// assertGetIsUnauthorized asserts that a GET to the given URL results
333
// in an ErrUnauthorized response with the given error message.
334
func (s *commonSuite) assertGetIsUnauthorized(c *gc.C, url, expectMessage string) {
335
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
340
ExpectStatus: http.StatusUnauthorized,
341
ExpectBody: params.Error{
342
Code: params.ErrUnauthorized,
343
Message: expectMessage,
348
// assertGetIsUnauthorized asserts that a PUT to the given URL with the
349
// given body value results in an ErrUnauthorized response with the given
351
func (s *commonSuite) assertPutIsUnauthorized(c *gc.C, url string, val interface{}, expectMessage string) {
352
body, err := json.Marshal(val)
353
c.Assert(err, gc.IsNil)
354
httptesting.AssertJSONCall(c, httptesting.JSONCallParams{
360
"Content-Type": {"application/json"},
362
Body: bytes.NewReader(body),
363
ExpectStatus: http.StatusUnauthorized,
364
ExpectBody: params.Error{
365
Code: params.ErrUnauthorized,
366
Message: expectMessage,
371
// doAsUser calls the given function, discharging any authorization
372
// request as the given user name.
373
func (s *commonSuite) doAsUser(user string, f func()) {
375
s.discharge = dischargeForUser(user)
207
382
func bakeryDo(client *http.Client) func(*http.Request) (*http.Response, error) {
208
383
if client == nil {
209
384
client = httpbakery.NewHTTPClient()