diff --git a/spec/fabricators/user_fabricator.rb b/spec/fabricators/user_fabricator.rb index 486bfd933f2..5fa84b401c0 100644 --- a/spec/fabricators/user_fabricator.rb +++ b/spec/fabricators/user_fabricator.rb @@ -5,6 +5,7 @@ Fabricator(:user_stat) {} Fabricator(:user, class_name: :user) do transient refresh_auto_groups: false transient trust_level: nil + transient search_index: false name "Bruce Wayne" username { sequence(:username) { |i| "bruce#{i}" } } @@ -19,7 +20,10 @@ Fabricator(:user, class_name: :user) do if transients[:refresh_auto_groups] || transients[:trust_level] Group.user_trust_level_change!(user.id, user.trust_level) end + SearchIndexer.disable if transients[:search_index] end + + before_create { |user, transients| SearchIndexer.enable if transients[:search_index] } end Fabricator(:user_with_secondary_email, from: :user) do diff --git a/spec/requests/directory_items_controller_spec.rb b/spec/requests/directory_items_controller_spec.rb index 425edffdd39..ab11ca58351 100644 --- a/spec/requests/directory_items_controller_spec.rb +++ b/spec/requests/directory_items_controller_spec.rb @@ -2,9 +2,11 @@ RSpec.describe DirectoryItemsController do fab!(:user) - fab!(:evil_trout) - fab!(:walter_white) - fab!(:stage_user) { Fabricate(:staged, username: "stage_user") } + fab!(:evil_trout) { Fabricate(:evil_trout, search_index: true) } + fab!(:walter_white) { Fabricate(:walter_white, search_index: true) } + fab!(:stage_user) do + Fabricate(:staged, username: "stage_user", name: "Stage User", search_index: true) + end fab!(:group) { Fabricate(:group, users: [evil_trout, stage_user]) } it "requires a `period` param" do @@ -157,7 +159,7 @@ RSpec.describe DirectoryItemsController do end it "finds user by name" do - get "/directory_items.json", params: { period: "all", name: "eviltrout" } + get "/directory_items.json", params: { period: "all", name: evil_trout.name } expect(response.status).to eq(200) json = response.parsed_body @@ -168,7 +170,7 @@ RSpec.describe DirectoryItemsController do end it "finds staged user by name" do - get "/directory_items.json", params: { period: "all", name: "stage_user" } + get "/directory_items.json", params: { period: "all", name: stage_user.name } expect(response.status).to eq(200) json = response.parsed_body @@ -254,6 +256,80 @@ RSpec.describe DirectoryItemsController do end end + it "searches users by user field value" do + field1 = Fabricate(:user_field, searchable: true) + field2 = Fabricate(:user_field, searchable: true) + + user_fields = [ + { user: walter_white, field: field1, value: "Yellow", order: 1 }, + { user: stage_user, field: field1, value: "Apple", order: 0 }, + { user: evil_trout, field: field2, value: "Moon", order: 2 }, + ] + + user_fields.each do |data| + UserCustomField.create!( + user_id: data[:user].id, + name: "user_field_#{data[:field].id}", + value: data[:value], + ) + end + + # When the users are fabricated their custom user fields + # aren't added to the index so we can index them here. + SearchIndexer.with_indexing do + [walter_white, stage_user, evil_trout].each { |u| SearchIndexer.index(u, force: true) } + end + + get "/directory_items.json", + params: { + period: "all", + order: field1.name, + name: "Moon", + user_field_ids: "#{field1.id}|#{field2.id}", + asc: true, + } + expect(response.status).to eq(200) + + json = response.parsed_body + expect(json).to be_present + items = json["directory_items"] + expect(items.length).to eq(1) + expect(json["meta"]["total_rows_directory_items"]).to eq(1) + expect(items[0]["user"]["username"]).to eq("eviltrout") + end + + it "filters users by user field value" do + field = Fabricate(:user_field, searchable: true) + + users = Fabricate.times(30, :user) + users.each do |user| + UserCustomField.create!(user_id: user.id, name: "user_field_#{field.id}", value: "blue") + end + + DirectoryItem.refresh! + + # When the users are fabricated their custom user fields + # aren't added to the index so we can index them here. + SearchIndexer.with_indexing { users.each { |u| SearchIndexer.index(u, force: true) } } + + get "/directory_items.json", + params: { + period: "all", + order: field.name, + name: "blue", + user_field_ids: "#{field.id}", + asc: true, + } + expect(response.status).to eq(200) + + json = response.parsed_body + expect(json).to be_present + items = json["directory_items"] + # Internal reference: /t/139545 + expect(items.length).to eq(20) + expect(json["meta"]["total_rows_directory_items"]).to eq(20) + end + it "checks group permissions" do group.update!(visibility_level: Group.visibility_levels[:members])