Exemplo n.º 1
0
func (c *cleaner) add(v interface{}) {
	var fn func()
	switch model := v.(type) {
	case *models.User:
		fn = func() {
			modelhelper.RemoveUser(model.Name)
		}
	case *models.Account:
		fn = func() {
			modelhelper.RemoveAccount(model.Id)
		}
	case *models.Group:
		fn = func() {
			modelhelper.RemoveGroup(model.Id)
		}
	case *models.Machine:
		fn = func() {
			modelhelper.DeleteMachine(model.ObjectId)
		}
	case *models.ComputeStack:
		fn = func() {
			modelhelper.DeleteComputeStack(model.Id.Hex())
		}
	default:
		panic(fmt.Errorf("cleaner for %T not implemented", v))
	}
	*c = append(*c, fn)
}
Exemplo n.º 2
0
func TestGetMachinesByUsernameAndProvider(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	user := &models.User{Name: "testuser", ObjectId: bson.NewObjectId()}
	defer modelhelper.RemoveUser(user.Name)

	if err := modelhelper.CreateUser(user); err != nil {
		t.Error(err)
	}

	// koding provider machine
	m1 := &models.Machine{
		ObjectId: bson.NewObjectId(),
		Uid:      bson.NewObjectId().Hex(),
		Provider: "koding",
		Users: []models.MachineUser{
			{Id: user.ObjectId, Owner: true},
		},
	}

	if err := modelhelper.CreateMachine(m1); err != nil {
		t.Errorf(err.Error())
	}

	defer modelhelper.DeleteMachine(m1.ObjectId)

	// non koding provider machine
	m2 := &models.Machine{
		ObjectId: bson.NewObjectId(),
		Uid:      bson.NewObjectId().Hex(),
		Provider: "amazon",
		Users: []models.MachineUser{
			{Id: user.ObjectId, Owner: true},
		},
	}

	if err := modelhelper.CreateMachine(m2); err != nil {
		t.Errorf(err.Error())
	}

	defer modelhelper.DeleteMachine(m2.ObjectId)

	// should only get koding provider machine
	machines, err := modelhelper.GetMachinesByUsernameAndProvider(user.Name, m1.Provider)
	if err != nil {
		t.Error(err.Error())
	}

	if len(machines) != 1 {
		t.Errorf("machine count should be 2, got: %d", len(machines))
	}
}
Exemplo n.º 3
0
func deleteUser(rel interface{}) error {
	result := rel.(*models.User)
	if getAccountByNick(result.Name) && result.LastLoginDate.After(deadline) && result.Status != "deleted" {
		return nil
	}

	fmt.Printf("deleting user %q\n", result.Name)
	if !*flagDry {
		return helper.RemoveUser(result.Name)
	}

	return nil
}
Exemplo n.º 4
0
func TestBlockUser(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	username, blockedReason := "testuser", "testing"

	user := &models.User{
		Name: username, ObjectId: bson.NewObjectId(), Status: models.UserBlocked,
	}

	defer func() {
		modelhelper.RemoveUser(username)
	}()

	err := modelhelper.CreateUser(user)
	if err != nil {
		t.Error(err)
	}

	err = modelhelper.BlockUser(username, blockedReason, 1*time.Hour)
	if err != nil {
		t.Error(err)
	}

	user, err = modelhelper.GetUser(username)
	if err != nil {
		t.Error(err)
	}

	if user.Status != models.UserBlocked {
		t.Errorf("User status is not blocked")
	}

	if user.BlockedReason != blockedReason {
		t.Errorf("User blocked reason is not: %s", blockedReason)
	}

	if user.BlockedUntil.IsZero() {
		t.Errorf("User blocked until date is not set")
	}

	id, err := modelhelper.GetUserID(user.Name)
	if err != nil {
		t.Fatalf("GetUserID()=%s", err)
	}

	if id != user.ObjectId {
		t.Fatalf("got %q, want %q", id.Hex(), user.ObjectId.Hex())
	}
}
Exemplo n.º 5
0
func TestRemoveUser(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	username := "******"
	user := &models.User{
		Name: username, ObjectId: bson.NewObjectId(),
	}

	err := modelhelper.CreateUser(user)
	if err != nil {
		t.Error(err)
	}

	err = modelhelper.RemoveUser(username)
	if err != nil {
		t.Error(err)
	}

	user, err = modelhelper.GetUser(username)
	if err == nil {
		t.Errorf("User should've been deleted, but wasn't")
	}
}
Exemplo n.º 6
0
func TestGetAnyUserTokenFromGroup(t *testing.T) {
	db := modeltesthelper.NewMongoDB(t)
	defer db.Close()

	id := bson.NewObjectId()
	username := id.Hex()
	user := &models.User{
		ObjectId: id,
		Name:     username,
		Email:    username + "@" + username + ".com",
	}

	err := modelhelper.CreateUser(user)
	if err != nil {
		t.Error(err)
	}

	groupName := bson.NewObjectId().Hex()

	key := fmt.Sprintf("foreignAuth.slack.%s.token", groupName)
	token := "token-123qwe"
	selector := bson.M{"username": username}
	update := bson.M{key: token}

	if err := modelhelper.UpdateUser(selector, update); err != nil {
		t.Error("Error while updating user")
	}

	id2 := bson.NewObjectId()
	username2 := id2.Hex()
	user2 := &models.User{
		ObjectId: id2,
		Name:     username2,
		Email:    username2 + "@" + username2 + ".com",
	}

	err = modelhelper.CreateUser(user2)
	if err != nil {
		t.Error(err)
	}

	groupName2 := bson.NewObjectId().Hex()
	key2 := fmt.Sprintf("foreignAuth.slack.%s.token", groupName2)
	token2 := "token-123qwe11"
	selector2 := bson.M{"username": username2}
	update2 := bson.M{key2: token2}

	if err := modelhelper.UpdateUser(selector2, update2); err != nil {
		t.Error("Error while updating user")
	}

	users, err := modelhelper.GetAnySlackTokenWithGroup(groupName)
	if err != nil {
		t.Error("Error while getting user token")
	}

	if len(users) != 1 {
		t.Error("Length of user should be 1")
	}

	err = modelhelper.RemoveUser(username)
	if err != nil {
		t.Error(err)
	}
}