Skip to content

Commit

Permalink
fix(instanceUser): fix email search flaky test
Browse files Browse the repository at this point in the history
- include only human users inside the Instance Admin Table
- scope InstanceUser inside test to only include human_users
- split flaky email search in system admin user management
  • Loading branch information
bivanalhar authored and cysjonathan committed Dec 16, 2024
1 parent ff920c8 commit 0b9fafe
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/controllers/system/admin/instance/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def destroy
private

def load_instance_users
@instance_users = @instance.instance_users.includes(user: [:emails, :courses]).
@instance_users = @instance.instance_users.human_users.includes(user: [:emails, :courses]).
search_and_ordered_by_username(search_param)
@instance_users = @instance_users.active_in_past_7_days if ActiveRecord::Type::Boolean.new.cast(params[:active])
@instance_users = @instance_users.where(role: params[:role]) \
Expand Down
1 change: 1 addition & 0 deletions app/models/instance_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class InstanceUser < ApplicationRecord
belongs_to :user, inverse_of: :instance_users

scope :ordered_by_username, -> { joins(:user).merge(User.order(name: :asc)) }
scope :human_users, -> { where.not(user_id: [User::SYSTEM_USER_ID, User::DELETED_USER_ID]) }
scope :active_in_past_7_days, -> { where('last_active_at > ?', 7.days.ago) }

def self.search_and_ordered_by_username(keyword)
Expand Down
16 changes: 7 additions & 9 deletions spec/features/system/admin/instance/user_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

with_tenant(:instance) do
let(:instance_admin) { create(:instance_user, role: :administrator).user }
let!(:prefix) { "testadm-#{rand(36**12).to_s(36)}-usr-" }
let!(:prefix) { "testadm-#{SecureRandom.hex}-usr-" }
let!(:instance_users) do
(1..2).map do |i|
create(:instance_user, user_name: "#{prefix}#{i}")
Expand Down Expand Up @@ -72,25 +72,23 @@ def search_for_users(query, click: true)

# Generate new users to search so it doesn't conflict with above scenarios
scenario 'I can search users by name' do
search_prefix = "testadm-search-#{rand(36**12).to_s(36)}-usr-"
instance_users_to_search = (1..2).map do |i|
create(:instance_user, user_name: "#{search_prefix}#{i}")
end
user_name = SecureRandom.hex
instance_users_to_search = create_list(:instance_user, 2, user_name: user_name)

# Search by username
search_for_users(search_prefix)
search_for_users(user_name)

instance_users_to_search.each do |instance_user|
expect(page).to have_text(instance_user.user.name)
expect(page).to have_selector('p.user_email', text: instance_user.user.email)
end
expect(page).to have_selector('.instance_user', count: 2)
end

scenario 'I can search users by email' do
random_instance_user = InstanceUser.order('RANDOM()').first
random_instance_user = InstanceUser.human_users.order('RANDOM()').first
search_for_users(random_instance_user.user.email)

expect(page).to have_text(random_instance_user.user.name)
expect(page).to have_selector('p.user_email', text: random_instance_user.user.email)
expect(page).to have_selector('.instance_user', count: 1)
end
end
Expand Down
14 changes: 9 additions & 5 deletions spec/features/system/admin/user_management_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,30 @@
expect_toastify('User was deleted.')
end

scenario 'I can search users' do
scenario 'I can search users by name' do
user_name = SecureRandom.hex
users_to_search = create_list(:user, 2, name: user_name)

# Search by username
find_button('Search').click
find('div[aria-label="Search"]').find('input').set(user_name)

wait_for_field_debouncing # timeout for search debouncing
users_to_search.each do |user|
expect(page).to have_selector('div.user_name', text: user.name)
expect(page).to have_selector('p.user_email', text: user.email)
end
expect(all('.system_user').count).to eq(2)
end

# Search by email
scenario 'I can search users by email' do
user_name = SecureRandom.hex
users_to_search = create_list(:user, 2, name: user_name)

find_button('Search').click
random_user = users_to_search.sample
find('div[aria-label="Search"]').find('input').set(random_user.email)
wait_for_field_debouncing # timeout for search debouncing

expect(page).to have_selector('div.user_name', text: random_user.name)
expect(page).to have_selector('p.user_email', text: random_user.email)
expect(all('.system_user').count).to eq(1)
end
end
Expand Down

0 comments on commit 0b9fafe

Please sign in to comment.