Esempio n. 1
0
func getAnySlackTokenWithGroup(context *models.Context) (string, error) {
	var token string
	groupName := context.GroupName

	users, err := modelhelper.GetAnySlackTokenWithGroup(groupName)
	if err != nil {
		return token, err
	}

	for _, user := range users {
		if user.ForeignAuth.Slack != nil {
			if gName, ok := user.ForeignAuth.Slack[groupName]; ok {
				if gName.Token != "" {
					return gName.Token, nil
				}
			}
		}
	}

	return token, models.ErrTokenIsNotFound
}
Esempio n. 2
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)
	}
}