From 34f8a35ed4c137a6fd8b4b01cac6f94504eb8551 Mon Sep 17 00:00:00 2001 From: Tyagiquamar Date: Sun, 6 Sep 2026 16:23:12 +0530 Subject: [PATCH] fix(auth): apply request normalization and status filter in bulk delete query resolution --- pkg/auth/user/RoleGroupService.go | 4 + pkg/auth/user/UserService.go | 9 ++ .../helper/UserRepositoryQueryBuilder_test.go | 85 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 pkg/auth/user/repository/helper/UserRepositoryQueryBuilder_test.go diff --git a/pkg/auth/user/RoleGroupService.go b/pkg/auth/user/RoleGroupService.go index 12e08e70c5..fe65de342b 100644 --- a/pkg/auth/user/RoleGroupService.go +++ b/pkg/auth/user/RoleGroupService.go @@ -843,6 +843,9 @@ func (impl RoleGroupServiceImpl) BulkDeleteRoleGroups(request *bean2.BulkDeleteR // setting the filtered user ids here for further processing request.Ids = filteredGroupIds } + if len(request.Ids) == 0 { + return true, nil + } err := impl.deleteRoleGroupsByIds(request) if err != nil { @@ -854,6 +857,7 @@ func (impl RoleGroupServiceImpl) BulkDeleteRoleGroups(request *bean2.BulkDeleteR // getGroupIdsHonoringFilters get the filtered group ids according to the request filters and returns groupIds and error(not nil) if any exception is caught. func (impl *RoleGroupServiceImpl) getGroupIdsHonoringFilters(request *bean2.ListingRequest) ([]int32, error) { + impl.userCommonService.SetDefaultValuesIfNotPresent(request, true) //query to get particular models respecting filters query, queryParams := helper.GetQueryForGroupListingWithFilters(request) models, err := impl.roleGroupRepository.GetAllExecutingQuery(query, queryParams) diff --git a/pkg/auth/user/UserService.go b/pkg/auth/user/UserService.go index 21320edd23..ec401c9eb1 100644 --- a/pkg/auth/user/UserService.go +++ b/pkg/auth/user/UserService.go @@ -1470,6 +1470,9 @@ func (impl *UserServiceImpl) BulkDeleteUsers(request *userBean.BulkDeleteRequest // setting the filtered user ids here for further processing request.Ids = filteredUserIds } + if len(request.Ids) == 0 { + return true, nil + } err := impl.deleteUsersByIds(request) if err != nil { impl.logger.Errorw("error in BulkDeleteUsers", "err", err) @@ -1480,6 +1483,12 @@ func (impl *UserServiceImpl) BulkDeleteUsers(request *userBean.BulkDeleteRequest // getUserIdsHonoringFilters get the filtered user ids according to the request filters and returns userIds and error(not nil) if any exception is caught. func (impl *UserServiceImpl) getUserIdsHonoringFilters(request *userBean.ListingRequest) ([]int32, error) { + // default values will be used if not provided + impl.userCommonService.SetDefaultValuesIfNotPresent(request, false) + // setting filter status type + setStatusFilterType(request) + // Recording time here for overall consistency + setCurrentTimeInUserInfo(request) //query to get particular models respecting filters query, queryParams := helper.GetQueryForUserListingWithFilters(request) models, err := impl.userRepository.GetAllExecutingQuery(query, queryParams) diff --git a/pkg/auth/user/repository/helper/UserRepositoryQueryBuilder_test.go b/pkg/auth/user/repository/helper/UserRepositoryQueryBuilder_test.go new file mode 100644 index 0000000000..24b6181aa6 --- /dev/null +++ b/pkg/auth/user/repository/helper/UserRepositoryQueryBuilder_test.go @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2024. Devtron Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package helper + +import ( + "testing" + + bean2 "github.com/devtron-labs/devtron/pkg/auth/user/bean" + "github.com/stretchr/testify/assert" +) + +func TestGetQueryForUserListingWithFilters(t *testing.T) { + t.Run("QueryWithSearchKeyAndPagination", func(t *testing.T) { + req := &bean2.ListingRequest{ + SearchKey: "devtron", + SortBy: bean2.Email, + SortOrder: bean2.Asc, + Size: 20, + Offset: 0, + CountCheck: false, + } + + query, params := GetQueryForUserListingWithFilters(req) + assert.Contains(t, query, "where active = true") + assert.Contains(t, query, "email_id ilike ?") + assert.Contains(t, query, "order by email_id") + assert.Contains(t, query, "limit ? offset ?") + assert.Equal(t, 3, len(params)) + assert.Equal(t, "%devtron%", params[0]) + assert.Equal(t, 20, params[1]) + assert.Equal(t, 0, params[2]) + }) + + t.Run("CountCheckQuery", func(t *testing.T) { + req := &bean2.ListingRequest{ + SearchKey: "test", + CountCheck: true, + } + + query, params := GetQueryForUserListingWithFilters(req) + assert.Contains(t, query, "select count(*)") + assert.Contains(t, query, "email_id ilike ?") + assert.NotContains(t, query, "limit ? offset ?") + assert.Equal(t, 1, len(params)) + assert.Equal(t, "%test%", params[0]) + }) +} + +func TestGetQueryForGroupListingWithFilters(t *testing.T) { + t.Run("GroupListingWithSearchKeyAndSorting", func(t *testing.T) { + req := &bean2.ListingRequest{ + SearchKey: "admin-group", + SortBy: bean2.GroupName, + SortOrder: bean2.Desc, + Size: 10, + Offset: 5, + CountCheck: false, + } + + query, params := GetQueryForGroupListingWithFilters(req) + assert.Contains(t, query, "where active = ?") + assert.Contains(t, query, "name ilike ?") + assert.Contains(t, query, "order by name DESC") + assert.Contains(t, query, "limit ? offset ?") + assert.Equal(t, 4, len(params)) + assert.Equal(t, true, params[0]) + assert.Equal(t, "%admin-group%", params[1]) + assert.Equal(t, 10, params[2]) + assert.Equal(t, 5, params[3]) + }) +}