From 30654f8d7d22dd0d44c27aede4066a03e42d60c3 Mon Sep 17 00:00:00 2001 From: Romit Choudhary Date: Fri, 2 Jul 2021 07:11:05 +0000 Subject: [PATCH] Fixes #3050 - newly created users are only shown in the admin interface after reload --- .../javascripts/app/controllers/user.coffee | 14 ++++++++- app/models/user/search.rb | 19 +++++++----- spec/requests/user_spec.rb | 31 +++++++++++++++++++ spec/system/manage/users_spec.rb | 23 ++++++++++++++ 4 files changed, 79 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/app/controllers/user.coffee b/app/assets/javascripts/app/controllers/user.coffee index 7d4501bc4..0242b590e 100644 --- a/app/assets/javascripts/app/controllers/user.coffee +++ b/app/assets/javascripts/app/controllers/user.coffee @@ -179,9 +179,21 @@ class User extends App.ControllerSubContent navupdate: '#users' genericObject: 'User' container: @el.closest('.content') - callback: @search + callback: @newUserAddedCallback ) + # GitHub Issue #3050 + # resets search input value to empty after new user added + # resets any active role tab + newUserAddedCallback: => + @searchInput.val('') + @query = '' + @resetActiveTabs() + @search() + + resetActiveTabs: -> + @$('.tab.active').removeClass('active') + import: (e) -> e.preventDefault() new App.Import( diff --git a/app/models/user/search.rb b/app/models/user/search.rb index 9cd136fca..b9280b1fd 100644 --- a/app/models/user/search.rb +++ b/app/models/user/search.rb @@ -91,6 +91,8 @@ returns # enable search only for agents and admins return [] if !search_preferences(current_user) + is_query = query.present? && query != '*' + # lookup for roles of permission if params[:permissions].present? params[:role_ids] ||= [] @@ -99,7 +101,7 @@ returns end # try search index backend - if SearchIndexBackend.enabled? + if SearchIndexBackend.enabled? && is_query query_extension = {} if params[:role_ids].present? query_extension['bool'] ||= {} @@ -162,12 +164,15 @@ returns end end - statement.where( - '(users.firstname LIKE ? OR users.lastname LIKE ? OR users.email LIKE ? OR users.login LIKE ?) AND users.id != 1', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%" - ) - .order(Arel.sql(order_sql)) - .offset(offset) - .limit(limit) + if is_query + statement = statement.where( + '(users.firstname LIKE ? OR users.lastname LIKE ? OR users.email LIKE ? OR users.login LIKE ?) AND users.id != 1', "%#{query}%", "%#{query}%", "%#{query}%", "%#{query}%" + ) + end + + statement.order(Arel.sql(order_sql)) + .offset(offset) + .limit(limit) end end end diff --git a/spec/requests/user_spec.rb b/spec/requests/user_spec.rb index 479f6e894..efcd776e2 100644 --- a/spec/requests/user_spec.rb +++ b/spec/requests/user_spec.rb @@ -1434,6 +1434,37 @@ RSpec.describe 'User', type: :request do end end + describe 'GET /api/v1/users/search, checks ES Usage', searchindex: true, authenticated_as: :agent do + let!(:agent) { create(:agent) } + + def make_request(params) + get '/api/v1/users/search', params: params, as: :json + end + + before do + # create some users that can be found + create(:agent, firstname: 'Test-Agent1') + create(:agent, firstname: 'Test-Agent2') + + configure_elasticsearch(rebuild: true) + end + + it 'uses elasticsearch when query is non empty' do + # Check if ES is used + allow(SearchIndexBackend).to receive(:search) + + make_request(query: 'Test') + expect(SearchIndexBackend).to have_received(:search) + end + + it 'does not uses elasticsearch when query is empty' do + allow(SearchIndexBackend).to receive(:search) + + make_request(query: '') + expect(SearchIndexBackend).not_to have_received(:search) + end + end + describe 'POST /api/v1/users/avatar', authenticated_as: :user do let(:user) { create(:user) } let(:base64) { 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==' } diff --git a/spec/system/manage/users_spec.rb b/spec/system/manage/users_spec.rb index 6c5b111ff..d5f715239 100644 --- a/spec/system/manage/users_spec.rb +++ b/spec/system/manage/users_spec.rb @@ -50,4 +50,27 @@ RSpec.describe 'Manage > Users', type: :system do expect(page).to have_text("Zammad looks like this for \"#{user.firstname} #{user.lastname}\"", wait: 10) end end + + # Fixes GitHub Issue #3050 - Newly created users are only shown in the admin interface after reload + describe 'adding a new user', authenticated_as: -> { user } do + let(:user) { create(:admin) } + + it 'newly added user is visible in the user list' do + visit '#manage/users' + + within(:active_content) do + find('[data-type=new]').click + + find('[name=firstname]').fill_in with: 'NewTestUserFirstName' + find('[name=lastname]').fill_in with: 'User' + find('span.label-text', text: 'Customer').first(:xpath, './/..').click + + click '.js-submit' + + expect(page).to have_css('table.user-list td', text: 'NewTestUserFirstName') + end + + end + + end end