2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
require "rails_helper"
|
2013-02-07 06:50:59 -05:00
|
|
|
|
|
|
|
describe UserSearch do
|
|
|
|
|
2019-10-24 07:27:35 -04:00
|
|
|
before_all { SearchIndexer.enable } # Enable for prefabrication
|
2019-10-24 06:58:33 -04:00
|
|
|
before { SearchIndexer.enable } # Enable for each test
|
|
|
|
|
|
|
|
fab!(:topic) { Fabricate :topic }
|
|
|
|
fab!(:topic2) { Fabricate :topic }
|
|
|
|
fab!(:topic3) { Fabricate :topic }
|
|
|
|
fab!(:topic4) { Fabricate :topic }
|
2021-01-25 14:33:11 -05:00
|
|
|
fab!(:mr_b) { Fabricate :user, username: "mrb", name: "Michael Madsen", last_seen_at: 10.days.ago }
|
|
|
|
fab!(:mr_blue) { Fabricate :user, username: "mrblue", name: "Eddie Code", last_seen_at: 9.days.ago }
|
|
|
|
fab!(:mr_orange) { Fabricate :user, username: "mrorange", name: "Tim Roth", last_seen_at: 8.days.ago }
|
|
|
|
fab!(:mr_pink) { Fabricate :user, username: "mrpink", name: "Steve Buscemi", last_seen_at: 7.days.ago }
|
|
|
|
fab!(:mr_brown) { Fabricate :user, username: "mrbrown", name: "Quentin Tarantino", last_seen_at: 6.days.ago }
|
|
|
|
fab!(:mr_white) { Fabricate :user, username: "mrwhite", name: "Harvey Keitel", last_seen_at: 5.days.ago }
|
|
|
|
fab!(:inactive) { Fabricate :user, username: "Ghost", active: false }
|
2019-10-24 06:58:33 -04:00
|
|
|
fab!(:admin) { Fabricate :admin, username: "theadmin" }
|
|
|
|
fab!(:moderator) { Fabricate :moderator, username: "themod" }
|
|
|
|
fab!(:staged) { Fabricate :staged }
|
2013-02-07 06:50:59 -05:00
|
|
|
|
2013-10-30 15:45:13 -04:00
|
|
|
def search_for(*args)
|
2021-01-25 14:33:11 -05:00
|
|
|
# mapping "username" so it's easier to debug
|
|
|
|
UserSearch.new(*args).search.map(&:username)
|
2013-10-30 15:45:13 -04:00
|
|
|
end
|
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
context "with a secure category" do
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:searching_user) { Fabricate(:user) }
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
|
|
|
fab!(:category) { Fabricate(:category, read_restricted: true, user: user) }
|
|
|
|
|
2019-10-24 08:23:19 -04:00
|
|
|
before_all do
|
2021-01-25 14:33:11 -05:00
|
|
|
Fabricate(:category_group, category: category, group: group)
|
|
|
|
|
2019-10-24 08:23:19 -04:00
|
|
|
group.add(user)
|
|
|
|
group.add(searching_user)
|
|
|
|
group.save
|
|
|
|
end
|
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
it "autocompletes with people in the category" do
|
2019-10-24 08:23:19 -04:00
|
|
|
results = search_for("", searching_user: searching_user, category_id: category.id)
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to eq [user.username]
|
2019-10-24 08:23:19 -04:00
|
|
|
end
|
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
it "will lookup the category from the topic id" do
|
2019-10-24 08:23:19 -04:00
|
|
|
topic = Fabricate(:topic, category: category)
|
2021-01-25 14:33:11 -05:00
|
|
|
Fabricate(:post, user: topic.user, topic: topic)
|
2019-08-06 03:57:45 -04:00
|
|
|
|
2019-10-24 08:23:19 -04:00
|
|
|
results = search_for("", searching_user: searching_user, topic_id: topic.id)
|
2019-08-06 03:57:45 -04:00
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to eq [topic.user, user].map(&:username)
|
2019-10-24 08:23:19 -04:00
|
|
|
end
|
2019-08-06 03:57:45 -04:00
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
it "will raise an error if the user cannot see the category" do
|
2019-10-24 08:23:19 -04:00
|
|
|
expect do
|
|
|
|
search_for("", searching_user: Fabricate(:user), category_id: category.id)
|
|
|
|
end.to raise_error(Discourse::InvalidAccess)
|
|
|
|
end
|
2019-08-06 03:57:45 -04:00
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
it "will respect the group member visibility setting" do
|
2019-10-24 08:23:19 -04:00
|
|
|
group.update(members_visibility_level: Group.visibility_levels[:owners])
|
|
|
|
results = search_for("", searching_user: searching_user, category_id: category.id)
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to be_blank
|
2019-08-06 03:57:45 -04:00
|
|
|
|
2019-10-24 08:23:19 -04:00
|
|
|
group.add_owner(searching_user)
|
|
|
|
results = search_for("", searching_user: searching_user, category_id: category.id)
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to eq [user.username]
|
2019-10-24 08:23:19 -04:00
|
|
|
end
|
2019-08-06 03:57:45 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
it "allows for correct underscore searching" do
|
|
|
|
Fabricate(:user, username: "undertaker")
|
|
|
|
under_score = Fabricate(:user, username: "Under_Score")
|
2015-11-30 01:03:47 -05:00
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(search_for("under_sc")).to eq [under_score.username]
|
|
|
|
expect(search_for("under_")).to eq [under_score.username]
|
2015-11-30 01:03:47 -05:00
|
|
|
end
|
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
it "allows filtering by group" do
|
|
|
|
sam = Fabricate(:user, username: "sam")
|
|
|
|
Fabricate(:user, username: "samantha")
|
|
|
|
|
2017-02-09 18:45:39 -05:00
|
|
|
group = Fabricate(:group)
|
|
|
|
group.add(sam)
|
|
|
|
|
2019-05-10 11:35:36 -04:00
|
|
|
results = search_for("sam", groups: [group])
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to eq [sam.username]
|
2017-02-09 18:45:39 -05:00
|
|
|
end
|
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
it "allows filtering by multiple groups" do
|
|
|
|
sam = Fabricate(:user, username: "sam")
|
|
|
|
samantha = Fabricate(:user, username: "samantha")
|
|
|
|
|
2019-05-10 11:35:36 -04:00
|
|
|
group_1 = Fabricate(:group)
|
|
|
|
group_1.add(sam)
|
2021-01-25 14:33:11 -05:00
|
|
|
|
|
|
|
group_2 = Fabricate(:group)
|
2019-05-10 11:35:36 -04:00
|
|
|
group_2.add(samantha)
|
|
|
|
|
|
|
|
results = search_for("sam", groups: [group_1, group_2])
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to eq [sam, samantha].map(&:username)
|
2019-05-10 11:35:36 -04:00
|
|
|
end
|
|
|
|
|
2019-08-06 03:57:45 -04:00
|
|
|
context "with seed data" do
|
2021-01-25 14:33:11 -05:00
|
|
|
fab!(:post1) { Fabricate :post, user: mr_b, topic: topic }
|
|
|
|
fab!(:post2) { Fabricate :post, user: mr_blue, topic: topic2 }
|
|
|
|
fab!(:post3) { Fabricate :post, user: mr_orange, topic: topic }
|
|
|
|
fab!(:post4) { Fabricate :post, user: mr_pink, topic: topic }
|
|
|
|
fab!(:post5) { Fabricate :post, user: mr_brown, topic: topic3 }
|
|
|
|
fab!(:post6) { Fabricate :post, user: mr_white, topic: topic }
|
2019-10-24 06:58:33 -04:00
|
|
|
fab!(:post7) { Fabricate :post, user: staged, topic: topic4 }
|
2013-04-21 21:05:54 -04:00
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
before { mr_white.update(suspended_at: 1.day.ago, suspended_till: 1.year.from_now) }
|
2013-04-21 21:05:54 -04:00
|
|
|
|
2019-10-24 06:58:33 -04:00
|
|
|
it "can search by name and username" do
|
2019-08-06 03:57:45 -04:00
|
|
|
# normal search
|
2021-01-25 14:33:11 -05:00
|
|
|
results = search_for(mr_b.name.split.first)
|
|
|
|
expect(results).to eq [mr_b.username]
|
2014-05-13 11:44:06 -04:00
|
|
|
|
2019-08-06 03:57:45 -04:00
|
|
|
# lower case
|
2021-01-25 14:33:11 -05:00
|
|
|
results = search_for(mr_b.name.split.first.downcase)
|
|
|
|
expect(results).to eq [mr_b.username]
|
2013-04-21 21:05:54 -04:00
|
|
|
|
2019-08-06 03:57:45 -04:00
|
|
|
# username
|
2021-01-25 14:33:11 -05:00
|
|
|
results = search_for(mr_pink.username)
|
|
|
|
expect(results).to eq [mr_pink.username]
|
2013-04-21 21:05:54 -04:00
|
|
|
|
2019-08-06 03:57:45 -04:00
|
|
|
# case insensitive
|
2021-01-25 14:33:11 -05:00
|
|
|
results = search_for(mr_pink.username.upcase)
|
|
|
|
expect(results).to eq [mr_pink.username]
|
2019-10-24 06:58:33 -04:00
|
|
|
end
|
2013-04-21 21:05:54 -04:00
|
|
|
|
2019-10-24 06:58:33 -04:00
|
|
|
it "handles substring search correctly" do
|
2019-08-06 03:57:45 -04:00
|
|
|
results = search_for("mr")
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to eq [mr_brown, mr_pink, mr_orange, mr_blue, mr_b].map(&:username)
|
|
|
|
|
|
|
|
results = search_for("mr", searching_user: mr_b)
|
|
|
|
expect(results).to eq [mr_brown, mr_pink, mr_orange, mr_blue, mr_b].map(&:username)
|
|
|
|
|
|
|
|
# only staff members see suspended users in results
|
|
|
|
results = search_for("mr", searching_user: moderator)
|
|
|
|
expect(results).to eq [mr_white, mr_brown, mr_pink, mr_orange, mr_blue, mr_b].map(&:username)
|
2013-04-21 21:05:54 -04:00
|
|
|
|
2019-08-06 03:57:45 -04:00
|
|
|
results = search_for("mr", searching_user: admin)
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to eq [mr_white, mr_brown, mr_pink, mr_orange, mr_blue, mr_b].map(&:username)
|
2013-04-21 21:05:54 -04:00
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
results = search_for(mr_b.username, searching_user: admin)
|
|
|
|
expect(results).to eq [mr_b, mr_brown, mr_blue].map(&:username)
|
2013-02-07 06:50:59 -05:00
|
|
|
|
2019-08-06 03:57:45 -04:00
|
|
|
results = search_for("MR", searching_user: admin)
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to eq [mr_white, mr_brown, mr_pink, mr_orange, mr_blue, mr_b].map(&:username)
|
2013-10-30 15:45:13 -04:00
|
|
|
|
2019-08-06 03:57:45 -04:00
|
|
|
results = search_for("MRB", searching_user: admin, limit: 2)
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to eq [mr_b, mr_brown].map(&:username)
|
2019-10-24 06:58:33 -04:00
|
|
|
end
|
2013-10-30 15:45:13 -04:00
|
|
|
|
2019-10-24 06:58:33 -04:00
|
|
|
it "prioritises topic participants" do
|
2021-01-25 14:33:11 -05:00
|
|
|
results = search_for(mr_b.username, topic_id: topic.id)
|
|
|
|
expect(results).to eq [mr_b, mr_brown, mr_blue].map(&:username)
|
2014-07-04 19:11:41 -04:00
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
results = search_for(mr_b.username, topic_id: topic2.id)
|
|
|
|
expect(results).to eq [mr_b, mr_blue, mr_brown].map(&:username)
|
2014-07-04 19:11:41 -04:00
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
results = search_for(mr_b.username, topic_id: topic3.id)
|
|
|
|
expect(results).to eq [mr_b, mr_brown, mr_blue].map(&:username)
|
2019-10-24 06:58:33 -04:00
|
|
|
end
|
2013-10-30 15:45:13 -04:00
|
|
|
|
2019-10-24 08:23:19 -04:00
|
|
|
it "only reveals topic participants to people with permission" do
|
|
|
|
pm_topic = Fabricate(:private_message_post).topic
|
|
|
|
|
|
|
|
# Anonymous, does not have access
|
|
|
|
expect do
|
|
|
|
search_for("", topic_id: pm_topic.id)
|
|
|
|
end.to raise_error(Discourse::InvalidAccess)
|
|
|
|
|
|
|
|
# Random user, does not have access
|
|
|
|
expect do
|
2021-01-25 14:33:11 -05:00
|
|
|
search_for("", topic_id: pm_topic.id, searching_user: mr_b)
|
2019-10-24 08:23:19 -04:00
|
|
|
end.to raise_error(Discourse::InvalidAccess)
|
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
pm_topic.invite(pm_topic.user, mr_b.username)
|
|
|
|
|
|
|
|
results = search_for("", topic_id: pm_topic.id, searching_user: mr_b)
|
|
|
|
expect(results).to eq [pm_topic.user.username]
|
2019-10-24 08:23:19 -04:00
|
|
|
end
|
|
|
|
|
2019-10-24 06:58:33 -04:00
|
|
|
it "only searches by name when enabled" do
|
2019-08-06 03:57:45 -04:00
|
|
|
# When searching by name is enabled, it returns the record
|
|
|
|
SiteSetting.enable_names = true
|
|
|
|
results = search_for("Tarantino")
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to eq [mr_brown.username]
|
2014-05-14 22:22:35 -04:00
|
|
|
|
2019-08-06 03:57:45 -04:00
|
|
|
results = search_for("coding")
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to be_blank
|
2015-11-18 14:57:49 -05:00
|
|
|
|
2019-08-06 03:57:45 -04:00
|
|
|
results = search_for("z")
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to be_blank
|
2017-11-18 19:17:31 -05:00
|
|
|
|
2019-08-06 03:57:45 -04:00
|
|
|
# When searching by name is disabled, it will not return the record
|
|
|
|
SiteSetting.enable_names = false
|
|
|
|
results = search_for("Tarantino")
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to be_blank
|
2019-10-24 06:58:33 -04:00
|
|
|
end
|
2019-02-19 21:33:56 -05:00
|
|
|
|
2019-10-24 06:58:33 -04:00
|
|
|
it "prioritises exact matches" do
|
2019-08-06 03:57:45 -04:00
|
|
|
results = search_for("mrB")
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to eq [mr_b, mr_brown, mr_blue].map(&:username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't prioritises exact matches for users who haven't been seen in more than 1 year" do
|
|
|
|
abcdef = Fabricate(:user, username: "abcdef", last_seen_at: 2.days.ago)
|
|
|
|
abcde = Fabricate(:user, username: "abcde", last_seen_at: 2.weeks.ago)
|
|
|
|
abcd = Fabricate(:user, username: "abcd", last_seen_at: 2.months.ago)
|
|
|
|
abc = Fabricate(:user, username: "abc", last_seen_at: 2.years.ago)
|
|
|
|
|
|
|
|
results = search_for("abc")
|
|
|
|
expect(results).to eq [abcdef, abcde, abcd, abc].map(&:username)
|
2019-10-24 06:58:33 -04:00
|
|
|
end
|
2019-02-19 21:33:56 -05:00
|
|
|
|
2019-10-24 06:58:33 -04:00
|
|
|
it "does not include self, staged or inactive" do
|
2019-08-06 03:57:45 -04:00
|
|
|
# don't return inactive users
|
|
|
|
results = search_for(inactive.username)
|
|
|
|
expect(results).to be_blank
|
|
|
|
|
|
|
|
# don't return staged users
|
|
|
|
results = search_for(staged.username)
|
|
|
|
expect(results).to be_blank
|
2013-02-07 06:50:59 -05:00
|
|
|
|
2019-08-06 03:57:45 -04:00
|
|
|
results = search_for(staged.username, include_staged_users: true)
|
2021-01-25 14:33:11 -05:00
|
|
|
expect(results).to eq [staged.username]
|
2019-08-06 03:57:45 -04:00
|
|
|
|
2021-01-25 14:33:11 -05:00
|
|
|
# mrb is omitted since they're the searching user
|
|
|
|
results = search_for("", topic_id: topic.id, searching_user: mr_b)
|
|
|
|
expect(results).to eq [mr_pink, mr_orange].map(&:username)
|
2019-08-06 03:57:45 -04:00
|
|
|
end
|
|
|
|
end
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|