DEV: Fix specs for directory items controller (#30160)

The directory items controller specs that have a search param were not
matching how things worked in production. In a non-test environment the
UserSearch class depends on the `user_search_data` table being
populated, so the tests I corrected now use this table as well to match
reality.

Also added a new test to match the 20 user limit for search results that
currently exists. This 20 user limit is on the line between a bug and a
feature but it is how it is currently working so we should document
that. We have plans to increase this limit and it has been documented
here: https://meta.discourse.org/t/296485

This PR is a no-op and only changes the tests.

Co-authored-by: brrusselburg <25828824+brrusselburg@users.noreply.github.com>
This commit is contained in:
Blake Erickson 2024-12-10 08:55:29 -07:00 committed by GitHub
parent e5a2ed596c
commit ed13ae7787
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 85 additions and 5 deletions

View File

@ -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

View File

@ -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])