2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 04:41:23 -05:00
|
|
|
require 'rails_helper'
|
2013-02-07 05:50:59 -06:00
|
|
|
|
|
|
|
describe UserSearch do
|
|
|
|
|
2019-10-24 06:27:35 -05:00
|
|
|
before_all { SearchIndexer.enable } # Enable for prefabrication
|
2019-10-24 05:58:33 -05:00
|
|
|
before { SearchIndexer.enable } # Enable for each test
|
|
|
|
|
|
|
|
fab!(:topic) { Fabricate :topic }
|
|
|
|
fab!(:topic2) { Fabricate :topic }
|
|
|
|
fab!(:topic3) { Fabricate :topic }
|
|
|
|
fab!(:topic4) { Fabricate :topic }
|
|
|
|
fab!(:user1) { Fabricate :user, username: "mrb", name: "Michael Madsen", last_seen_at: 10.days.ago }
|
|
|
|
fab!(:user2) { Fabricate :user, username: "mrblue", name: "Eddie Code", last_seen_at: 9.days.ago }
|
|
|
|
fab!(:user3) { Fabricate :user, username: "mrorange", name: "Tim Roth", last_seen_at: 8.days.ago }
|
|
|
|
fab!(:user4) { Fabricate :user, username: "mrpink", name: "Steve Buscemi", last_seen_at: 7.days.ago }
|
|
|
|
fab!(:user5) { Fabricate :user, username: "mrbrown", name: "Quentin Tarantino", last_seen_at: 6.days.ago }
|
|
|
|
fab!(:user6) { Fabricate :user, username: "mrwhite", name: "Harvey Keitel", last_seen_at: 5.days.ago }
|
|
|
|
fab!(:inactive) { Fabricate :user, username: "Ghost", active: false }
|
|
|
|
fab!(:admin) { Fabricate :admin, username: "theadmin" }
|
|
|
|
fab!(:moderator) { Fabricate :moderator, username: "themod" }
|
|
|
|
fab!(:staged) { Fabricate :staged }
|
2013-02-07 05:50:59 -06:00
|
|
|
|
2013-10-30 14:45:13 -05:00
|
|
|
def search_for(*args)
|
|
|
|
UserSearch.new(*args).search
|
|
|
|
end
|
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
it 'finds users in secure category' do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
user = Fabricate(:user)
|
|
|
|
group.add(user)
|
|
|
|
group.save
|
|
|
|
|
2019-08-06 05:26:54 -05:00
|
|
|
category =
|
|
|
|
Fabricate(
|
|
|
|
:category,
|
|
|
|
read_restricted: true,
|
|
|
|
user: user
|
|
|
|
)
|
2019-08-06 02:57:45 -05:00
|
|
|
|
|
|
|
Fabricate(:category_group, category: category, group: group)
|
|
|
|
|
|
|
|
results = search_for("", category_id: category.id)
|
|
|
|
|
|
|
|
expect(user.username).to eq(results[0].username)
|
|
|
|
expect(results.length).to eq(1)
|
|
|
|
|
|
|
|
topic = Fabricate(:topic, category: category)
|
|
|
|
_post = Fabricate(:post, user: topic.user, topic: topic)
|
|
|
|
|
|
|
|
results = search_for("", topic_id: topic.id)
|
|
|
|
|
|
|
|
expect(results.length).to eq(2)
|
|
|
|
|
|
|
|
expect(results.map(&:username)).to contain_exactly(
|
|
|
|
user.username, topic.user.username
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2015-11-30 00:03:47 -06:00
|
|
|
it 'allows for correct underscore searching' do
|
|
|
|
Fabricate(:user, username: 'Under_Score')
|
|
|
|
Fabricate(:user, username: 'undertaker')
|
|
|
|
|
|
|
|
expect(search_for("under_sc").length).to eq(1)
|
|
|
|
expect(search_for("under_").length).to eq(1)
|
|
|
|
end
|
|
|
|
|
2017-02-09 17:45:39 -06:00
|
|
|
it 'allows filtering by group' do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
sam = Fabricate(:user, username: 'sam')
|
|
|
|
_samantha = Fabricate(:user, username: 'samantha')
|
|
|
|
group.add(sam)
|
|
|
|
|
2019-05-10 10:35:36 -05:00
|
|
|
results = search_for("sam", groups: [group])
|
2017-02-09 17:45:39 -06:00
|
|
|
expect(results.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2019-05-10 10:35:36 -05:00
|
|
|
it 'allows filtering by multiple groups' do
|
|
|
|
group_1 = Fabricate(:group)
|
|
|
|
sam = Fabricate(:user, username: 'sam')
|
|
|
|
group_2 = Fabricate(:group)
|
|
|
|
samantha = Fabricate(:user, username: 'samantha')
|
|
|
|
group_1.add(sam)
|
|
|
|
group_2.add(samantha)
|
|
|
|
|
|
|
|
results = search_for("sam", groups: [group_1, group_2])
|
|
|
|
expect(results.count).to eq(2)
|
|
|
|
end
|
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
context "with seed data" do
|
2019-10-24 05:58:33 -05:00
|
|
|
fab!(:post1) { Fabricate :post, user: user1, topic: topic }
|
|
|
|
fab!(:post2) { Fabricate :post, user: user2, topic: topic2 }
|
|
|
|
fab!(:post3) { Fabricate :post, user: user3, topic: topic }
|
|
|
|
fab!(:post4) { Fabricate :post, user: user4, topic: topic }
|
|
|
|
fab!(:post5) { Fabricate :post, user: user5, topic: topic3 }
|
|
|
|
fab!(:post6) { Fabricate :post, user: user6, topic: topic }
|
|
|
|
fab!(:post7) { Fabricate :post, user: staged, topic: topic4 }
|
2013-04-21 20:05:54 -05:00
|
|
|
|
2019-10-24 05:58:33 -05:00
|
|
|
before { user6.update(suspended_at: 1.day.ago, suspended_till: 1.year.from_now) }
|
2013-04-21 20:05:54 -05:00
|
|
|
|
2019-10-24 05:58:33 -05:00
|
|
|
it "can search by name and username" do
|
2019-08-06 02:57:45 -05:00
|
|
|
# normal search
|
|
|
|
results = search_for(user1.name.split(" ").first)
|
|
|
|
expect(results.size).to eq(1)
|
|
|
|
expect(results.first.username).to eq(user1.username)
|
2014-05-13 10:44:06 -05:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
# lower case
|
|
|
|
results = search_for(user1.name.split(" ").first.downcase)
|
|
|
|
expect(results.size).to eq(1)
|
|
|
|
expect(results.first).to eq(user1)
|
2013-04-21 20:05:54 -05:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
# username
|
|
|
|
results = search_for(user4.username)
|
|
|
|
expect(results.size).to eq(1)
|
|
|
|
expect(results.first).to eq(user4)
|
2013-04-21 20:05:54 -05:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
# case insensitive
|
|
|
|
results = search_for(user4.username.upcase)
|
|
|
|
expect(results.size).to eq(1)
|
|
|
|
expect(results.first).to eq(user4)
|
2019-10-24 05:58:33 -05:00
|
|
|
end
|
2013-04-21 20:05:54 -05:00
|
|
|
|
2019-10-24 05:58:33 -05:00
|
|
|
it "handles substring search correctly" do
|
2019-08-06 02:57:45 -05:00
|
|
|
# substrings
|
|
|
|
# only staff members see suspended users in results
|
|
|
|
results = search_for("mr")
|
|
|
|
expect(results.size).to eq(5)
|
|
|
|
expect(results).not_to include(user6)
|
|
|
|
expect(search_for("mr", searching_user: user1).size).to eq(5)
|
2013-04-21 20:05:54 -05:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
results = search_for("mr", searching_user: admin)
|
|
|
|
expect(results.size).to eq(6)
|
|
|
|
expect(results).to include(user6)
|
|
|
|
expect(search_for("mr", searching_user: moderator).size).to eq(6)
|
2013-04-21 20:05:54 -05:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
results = search_for(user1.username, searching_user: admin)
|
|
|
|
expect(results.size).to eq(3)
|
2013-02-07 05:50:59 -06:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
results = search_for("MR", searching_user: admin)
|
|
|
|
expect(results.size).to eq(6)
|
2013-10-30 14:45:13 -05:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
results = search_for("MRB", searching_user: admin, limit: 2)
|
|
|
|
expect(results.size).to eq(2)
|
2019-10-24 05:58:33 -05:00
|
|
|
end
|
2013-10-30 14:45:13 -05:00
|
|
|
|
2019-10-24 05:58:33 -05:00
|
|
|
it "prioritises topic participants" do
|
2019-08-06 02:57:45 -05:00
|
|
|
# topic priority
|
|
|
|
results = search_for(user1.username, topic_id: topic.id)
|
|
|
|
expect(results.first).to eq(user1)
|
2014-07-04 18:11:41 -05:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
results = search_for(user1.username, topic_id: topic2.id)
|
|
|
|
expect(results[1]).to eq(user2)
|
2014-07-04 18:11:41 -05:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
results = search_for(user1.username, topic_id: topic3.id)
|
|
|
|
expect(results[1]).to eq(user5)
|
2019-10-24 05:58:33 -05:00
|
|
|
end
|
2013-10-30 14:45:13 -05:00
|
|
|
|
2019-10-24 05:58:33 -05:00
|
|
|
it "only searches by name when enabled" do
|
2019-08-06 02:57:45 -05:00
|
|
|
# When searching by name is enabled, it returns the record
|
|
|
|
SiteSetting.enable_names = true
|
|
|
|
results = search_for("Tarantino")
|
|
|
|
expect(results.size).to eq(1)
|
2014-05-14 21:22:35 -05:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
results = search_for("coding")
|
|
|
|
expect(results.size).to eq(0)
|
2015-11-18 13:57:49 -06:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
results = search_for("z")
|
|
|
|
expect(results.size).to eq(0)
|
2017-11-18 18:17:31 -06:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
# When searching by name is disabled, it will not return the record
|
|
|
|
SiteSetting.enable_names = false
|
|
|
|
results = search_for("Tarantino")
|
|
|
|
expect(results.size).to eq(0)
|
2019-10-24 05:58:33 -05:00
|
|
|
end
|
2019-02-19 20:33:56 -06:00
|
|
|
|
2019-10-24 05:58:33 -05:00
|
|
|
it "prioritises exact matches" do
|
2019-08-06 02:57:45 -05:00
|
|
|
# find an exact match first
|
|
|
|
results = search_for("mrB")
|
|
|
|
expect(results.first.username).to eq(user1.username)
|
2019-10-24 05:58:33 -05:00
|
|
|
end
|
2019-02-19 20:33:56 -06:00
|
|
|
|
2019-10-24 05:58:33 -05:00
|
|
|
it "does not include self, staged or inactive" do
|
2019-08-06 02:57:45 -05: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 05:50:59 -06:00
|
|
|
|
2019-08-06 02:57:45 -05:00
|
|
|
results = search_for(staged.username, include_staged_users: true)
|
|
|
|
expect(results.first.username).to eq(staged.username)
|
|
|
|
|
|
|
|
results = search_for("", topic_id: topic.id, searching_user: user1)
|
|
|
|
|
|
|
|
# mrb is omitted, mrb is current user
|
|
|
|
expect(results.map(&:username)).to eq(["mrpink", "mrorange"])
|
|
|
|
end
|
|
|
|
end
|
2013-02-07 05:50:59 -06:00
|
|
|
end
|