예제 #1
0
func createUser(w http.ResponseWriter, r *http.Request) error {
	var u auth.User
	err := json.NewDecoder(r.Body).Decode(&u)
	if err != nil {
		return &errors.HTTP{Code: http.StatusBadRequest, Message: err.Error()}
	}
	if !validation.ValidateEmail(u.Email) {
		return &errors.HTTP{Code: http.StatusBadRequest, Message: emailError}
	}
	if !validation.ValidateLength(u.Password, passwordMinLen, passwordMaxLen) {
		return &errors.HTTP{Code: http.StatusBadRequest, Message: passwordError}
	}
	gURL := repository.ServerURL()
	c := gandalf.Client{Endpoint: gURL}
	if _, err := c.NewUser(u.Email, keyToMap(u.Keys)); err != nil {
		return fmt.Errorf("Failed to create user in the git server: %s", err)
	}
	if err := u.Create(); err == nil {
		rec.Log(u.Email, "create-user")
		if limit, err := config.GetUint("quota:apps-per-user"); err == nil {
			quota.Create(u.Email, uint(limit))
		}
		w.WriteHeader(http.StatusCreated)
		return nil
	}
	if _, err = auth.GetUserByEmail(u.Email); err == nil {
		err = &errors.HTTP{Code: http.StatusConflict, Message: "This email is already registered"}
	}
	return err
}
예제 #2
0
func ExampleReserve() {
	err := quota.Create("*****@*****.**", 4)
	if err != nil {
		panic(err)
	}
	quota.Reserve("*****@*****.**", "me/0", "me/1", "me/2") // ok
	quota.Reserve("*****@*****.**", "me/3", "me/4", "me/5") // ErrQuotaExceeded
}
예제 #3
0
func (s *S) TestCreateAppQuotaBackward(c *gocheck.C) {
	app := App{
		Name:     "damned",
		Platform: "django",
	}
	err := quota.Create(app.Name, 1)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete(app.Name)
	createAppQuota.Backward(action.BWContext{FWResult: app.Name})
	err = quota.Reserve(app.Name, "something")
	c.Assert(err, gocheck.Equals, quota.ErrQuotaNotFound)
}
예제 #4
0
func ExampleSet() {
	err := quota.Create("*****@*****.**", 3)
	if err != nil {
		panic(err)
	}
	quota.Reserve("*****@*****.**", "me/0")
	quota.Reserve("*****@*****.**", "me/1")
	quota.Reserve("*****@*****.**", "me/2")
	quota.Set("*****@*****.**", 2)
	quota.Reserve("*****@*****.**", "me/3") // ErrQuotaExceeded
	quota.Release("*****@*****.**", "me/2")
	quota.Reserve("*****@*****.**", "me/3") // ErrQuotaExceeded
	quota.Release("*****@*****.**", "me/1")
	quota.Reserve("*****@*****.**", "me/3") // Everything is ok now
}
예제 #5
0
func (s *S) TestReserveUserAppForwardQuotaExceeded(c *gocheck.C) {
	user := auth.User{Email: "*****@*****.**"}
	err := quota.Create(user.Email, 1)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete(user.Email)
	err = quota.Reserve(user.Email, "anything")
	c.Assert(err, gocheck.IsNil)
	app := App{
		Name:     "clap",
		Platform: "django",
	}
	previous, err := reserveUserApp.Forward(action.FWContext{Params: []interface{}{app, user}})
	c.Assert(previous, gocheck.IsNil)
	_, ok := err.(*quota.QuotaExceededError)
	c.Assert(ok, gocheck.Equals, true)
}
예제 #6
0
func (s *S) TestReserveUnitsToAddBackwardNoPointer(c *gocheck.C) {
	app := App{
		Name:     "visions",
		Platform: "django",
	}
	err := quota.Create(app.Name, 5)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete(app.Name)
	ids := []string{"visions-0", "visions-1", "visions-2", "visions-3"}
	err = quota.Reserve(app.Name, ids...)
	c.Assert(err, gocheck.IsNil)
	reserveUnitsToAdd.Backward(action.BWContext{Params: []interface{}{app, 3}, FWResult: ids})
	items, avail, err := quota.Items(app.Name)
	c.Assert(err, gocheck.IsNil)
	c.Assert(avail, gocheck.Equals, uint(5))
	c.Assert(items, gocheck.HasLen, 0)
}
예제 #7
0
파일: quota_test.go 프로젝트: nemx/tsuru
func (QuotaSuite) TestChangeQuota(c *gocheck.C) {
	err := quota.Create("*****@*****.**", 3)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete("*****@*****.**")
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("PUT", "/quota/[email protected]?:[email protected]", strings.NewReader("quota=1"))
	c.Assert(err, gocheck.IsNil)
	request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	err = changeQuota(recorder, request, nil)
	c.Assert(err, gocheck.IsNil)
	_, qtd, err := quota.Items("*****@*****.**")
	c.Assert(err, gocheck.IsNil)
	c.Assert(int(qtd), gocheck.Equals, 1)
	c.Assert(recorder.Code, gocheck.Equals, http.StatusOK)
	body, err := ioutil.ReadAll(recorder.Body)
	c.Assert(err, gocheck.IsNil)
	c.Assert(string(body), gocheck.Equals, "Quota changed sucessfully.")
}
예제 #8
0
func (QuotaSuite) TestQuotaByOwner(c *gocheck.C) {
	err := quota.Create("*****@*****.**", 3)
	c.Assert(err, gocheck.IsNil)
	err = quota.Reserve("*****@*****.**", "tank/1")
	c.Assert(err, gocheck.IsNil)
	recorder := httptest.NewRecorder()
	request, err := http.NewRequest("GET", "/quota/[email protected]?:[email protected]", nil)
	c.Assert(err, gocheck.IsNil)
	err = quotaByOwner(recorder, request, nil)
	c.Assert(err, gocheck.IsNil)
	c.Assert(recorder.Code, gocheck.Equals, http.StatusOK)
	body, err := ioutil.ReadAll(recorder.Body)
	c.Assert(err, gocheck.IsNil)
	result := map[string]interface{}{}
	err = json.Unmarshal(body, &result)
	c.Assert(err, gocheck.IsNil)
	c.Assert(result["available"], gocheck.Equals, float64(2))
	c.Assert(result["items"], gocheck.DeepEquals, []interface{}{"tank/1"})
}
예제 #9
0
func (s *S) TestReserveUnitsToAddForwardNoPointer(c *gocheck.C) {
	app := App{
		Name:     "visions",
		Platform: "django",
	}
	s.conn.Apps().Insert(app)
	defer s.conn.Apps().Remove(bson.M{"name": app.Name})
	err := quota.Create(app.Name, 5)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete(app.Name)
	result, err := reserveUnitsToAdd.Forward(action.FWContext{Params: []interface{}{app, 3}})
	c.Assert(err, gocheck.IsNil)
	ids, ok := result.([]string)
	c.Assert(ok, gocheck.Equals, true)
	c.Assert(ids, gocheck.DeepEquals, []string{"visions-0", "visions-1", "visions-2"})
	items, avail, err := quota.Items(app.Name)
	c.Assert(err, gocheck.IsNil)
	c.Assert(avail, gocheck.Equals, uint(2))
	c.Assert(items, gocheck.DeepEquals, []string{"visions-0", "visions-1", "visions-2"})
}
예제 #10
0
func (s *S) TestReserveUserAppBackward(c *gocheck.C) {
	user := auth.User{Email: "*****@*****.**"}
	err := quota.Create(user.Email, 1)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete(user.Email)
	app := App{
		Name:     "clap",
		Platform: "django",
	}
	err = quota.Reserve(user.Email, app.Name)
	c.Assert(err, gocheck.IsNil)
	ctx := action.BWContext{
		FWResult: map[string]string{
			"app":  app.Name,
			"user": user.Email,
		},
	}
	reserveUserApp.Backward(ctx)
	err = quota.Reserve(user.Email, app.Name)
	c.Assert(err, gocheck.IsNil)
}
예제 #11
0
func (s *S) TestReserveUserAppForwardAppNotPointer(c *gocheck.C) {
	user := auth.User{Email: "*****@*****.**"}
	err := quota.Create(user.Email, 1)
	c.Assert(err, gocheck.IsNil)
	defer quota.Delete(user.Email)
	app := App{
		Name:     "clap",
		Platform: "django",
	}
	expected := map[string]string{"user": user.Email, "app": app.Name}
	previous, err := reserveUserApp.Forward(action.FWContext{Params: []interface{}{app, user}})
	c.Assert(err, gocheck.IsNil)
	c.Assert(previous, gocheck.DeepEquals, expected)
	err = quota.Reserve(user.Email, "another-app")
	_, ok := err.(*quota.QuotaExceededError)
	c.Assert(ok, gocheck.Equals, true)
	err = quota.Release(user.Email, app.Name)
	c.Assert(err, gocheck.IsNil)
	err = quota.Reserve(user.Email, "another-app")
	c.Assert(err, gocheck.IsNil)
}
예제 #12
0
	Name: "create-app-quota",
	Forward: func(ctx action.FWContext) (action.Result, error) {
		var app App
		switch ctx.Params[0].(type) {
		case App:
			app = ctx.Params[0].(App)
		case *App:
			app = *ctx.Params[0].(*App)
		default:
			return nil, errors.New("First parameter must be App or *App.")
		}
		if limit, err := config.GetUint("quota:units-per-app"); err == nil {
			if limit == 0 {
				return nil, errors.New("app creation is disallowed")
			}
			quota.Create(app.Name, uint(limit))
			quota.Reserve(app.Name, app.Name+"-0")
		}
		return app.Name, nil
	},
	Backward: func(ctx action.BWContext) {
		quota.Delete(ctx.FWResult.(string))
	},
	MinParams: 1,
}

// insertApp is an action that inserts an app in the database in Forward and
// removes it in the Backward.
//
// The first argument in the context must be an App or a pointer to an App.
var insertApp = action.Action{