2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-07 06:50:59 -05:00
|
|
|
|
|
|
|
describe UserSearch do
|
|
|
|
|
2014-05-13 11:44:06 -04:00
|
|
|
let(:topic) { Fabricate :topic }
|
|
|
|
let(:topic2) { Fabricate :topic }
|
|
|
|
let(:topic3) { Fabricate :topic }
|
2015-11-18 14:57:49 -05:00
|
|
|
let(:topic4) { Fabricate :topic }
|
2014-05-14 22:22:35 -04:00
|
|
|
let(:user1) { Fabricate :user, username: "mrb", name: "Michael Madsen", last_seen_at: 10.days.ago }
|
2015-11-18 14:57:49 -05:00
|
|
|
let(:user2) { Fabricate :user, username: "mrblue", name: "Eddie Code", last_seen_at: 9.days.ago }
|
2014-05-14 22:22:35 -04:00
|
|
|
let(:user3) { Fabricate :user, username: "mrorange", name: "Tim Roth", last_seen_at: 8.days.ago }
|
|
|
|
let(:user4) { Fabricate :user, username: "mrpink", name: "Steve Buscemi", last_seen_at: 7.days.ago }
|
|
|
|
let(:user5) { Fabricate :user, username: "mrbrown", name: "Quentin Tarantino", last_seen_at: 6.days.ago }
|
|
|
|
let(:user6) { Fabricate :user, username: "mrwhite", name: "Harvey Keitel", last_seen_at: 5.days.ago }
|
2015-11-18 14:57:49 -05:00
|
|
|
let!(:inactive) { Fabricate :user, username: "Ghost", active: false }
|
2014-05-13 11:44:06 -04:00
|
|
|
let(:admin) { Fabricate :admin, username: "theadmin" }
|
|
|
|
let(:moderator) { Fabricate :moderator, username: "themod" }
|
2015-11-18 14:57:49 -05:00
|
|
|
let(:staged) { Fabricate :staged }
|
2013-02-07 06:50:59 -05:00
|
|
|
|
|
|
|
before do
|
2016-12-21 21:13:14 -05:00
|
|
|
SearchIndexer.enable
|
2014-07-04 19:11:41 -04:00
|
|
|
|
2013-02-07 06:50:59 -05:00
|
|
|
Fabricate :post, user: user1, topic: topic
|
|
|
|
Fabricate :post, user: user2, topic: topic2
|
|
|
|
Fabricate :post, user: user3, topic: topic
|
|
|
|
Fabricate :post, user: user4, topic: topic
|
|
|
|
Fabricate :post, user: user5, topic: topic3
|
|
|
|
Fabricate :post, user: user6, topic: topic
|
2015-11-18 14:57:49 -05:00
|
|
|
Fabricate :post, user: staged, topic: topic4
|
|
|
|
|
2019-04-29 03:32:25 -04:00
|
|
|
user6.update(suspended_at: 1.day.ago, suspended_till: 1.year.from_now)
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
|
|
|
|
2013-10-30 15:45:13 -04:00
|
|
|
def search_for(*args)
|
|
|
|
UserSearch.new(*args).search
|
|
|
|
end
|
|
|
|
|
2015-11-30 01:03:47 -05: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 18:45:39 -05:00
|
|
|
it 'allows filtering by group' do
|
|
|
|
group = Fabricate(:group)
|
|
|
|
sam = Fabricate(:user, username: 'sam')
|
|
|
|
_samantha = Fabricate(:user, username: 'samantha')
|
|
|
|
group.add(sam)
|
|
|
|
|
|
|
|
results = search_for("sam", group: group)
|
|
|
|
expect(results.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2015-11-18 14:57:49 -05:00
|
|
|
# this is a seriously expensive integration test,
|
|
|
|
# re-creating this entire test db is too expensive reuse
|
2013-04-21 21:05:54 -04:00
|
|
|
it "operates correctly" do
|
|
|
|
# normal search
|
2013-10-30 15:45:13 -04:00
|
|
|
results = search_for(user1.name.split(" ").first)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.size).to eq(1)
|
2015-05-14 00:38:47 -04:00
|
|
|
expect(results.first.username).to eq(user1.username)
|
2013-04-21 21:05:54 -04:00
|
|
|
|
|
|
|
# lower case
|
2013-10-30 15:45:13 -04:00
|
|
|
results = search_for(user1.name.split(" ").first.downcase)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.size).to eq(1)
|
|
|
|
expect(results.first).to eq(user1)
|
2013-04-21 21:05:54 -04:00
|
|
|
|
2015-11-18 14:57:49 -05:00
|
|
|
# username
|
2013-10-30 15:45:13 -04:00
|
|
|
results = search_for(user4.username)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.size).to eq(1)
|
|
|
|
expect(results.first).to eq(user4)
|
2013-04-21 21:05:54 -04:00
|
|
|
|
|
|
|
# case insensitive
|
2013-10-30 15:45:13 -04:00
|
|
|
results = search_for(user4.username.upcase)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.size).to eq(1)
|
|
|
|
expect(results.first).to eq(user4)
|
2013-04-21 21:05:54 -04:00
|
|
|
|
|
|
|
# substrings
|
2014-05-13 11:44:06 -04:00
|
|
|
# only staff members see suspended users in results
|
2013-10-30 15:45:13 -04:00
|
|
|
results = search_for("mr")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.size).to eq(5)
|
|
|
|
expect(results).not_to include(user6)
|
|
|
|
expect(search_for("mr", searching_user: user1).size).to eq(5)
|
2014-05-13 11:44:06 -04:00
|
|
|
|
|
|
|
results = search_for("mr", searching_user: admin)
|
2014-12-31 09:55:03 -05:00
|
|
|
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 21:05:54 -04:00
|
|
|
|
2015-11-18 15:08:50 -05:00
|
|
|
results = search_for(user1.username, searching_user: admin)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.size).to eq(3)
|
2013-04-21 21:05:54 -04:00
|
|
|
|
2014-05-13 11:44:06 -04:00
|
|
|
results = search_for("MR", searching_user: admin)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.size).to eq(6)
|
2013-04-21 21:05:54 -04:00
|
|
|
|
2014-05-14 22:22:35 -04:00
|
|
|
results = search_for("MRB", searching_user: admin, limit: 2)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.size).to eq(2)
|
2013-04-21 21:05:54 -04:00
|
|
|
|
|
|
|
# topic priority
|
2015-11-18 15:08:50 -05:00
|
|
|
results = search_for(user1.username, topic_id: topic.id)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.first).to eq(user1)
|
2013-04-21 21:05:54 -04:00
|
|
|
|
2015-11-18 15:08:50 -05:00
|
|
|
results = search_for(user1.username, topic_id: topic2.id)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results[1]).to eq(user2)
|
2013-02-07 06:50:59 -05:00
|
|
|
|
2015-11-18 15:08:50 -05:00
|
|
|
results = search_for(user1.username, topic_id: topic3.id)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results[1]).to eq(user5)
|
2013-10-30 15:45:13 -04:00
|
|
|
|
|
|
|
# When searching by name is enabled, it returns the record
|
2014-05-14 22:22:35 -04:00
|
|
|
SiteSetting.enable_names = true
|
2013-10-30 15:45:13 -04:00
|
|
|
results = search_for("Tarantino")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.size).to eq(1)
|
2013-10-30 15:45:13 -04:00
|
|
|
|
2014-07-04 19:11:41 -04:00
|
|
|
results = search_for("coding")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.size).to eq(0)
|
2014-07-04 19:11:41 -04:00
|
|
|
|
|
|
|
results = search_for("z")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.size).to eq(0)
|
2014-07-04 19:11:41 -04:00
|
|
|
|
2013-10-30 15:45:13 -04:00
|
|
|
# When searching by name is disabled, it will not return the record
|
2014-05-14 22:22:35 -04:00
|
|
|
SiteSetting.enable_names = false
|
2013-10-30 15:45:13 -04:00
|
|
|
results = search_for("Tarantino")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results.size).to eq(0)
|
2013-10-30 15:45:13 -04:00
|
|
|
|
2014-05-14 22:22:35 -04:00
|
|
|
# find an exact match first
|
2015-11-18 15:08:50 -05:00
|
|
|
results = search_for("mrB")
|
2015-05-14 00:38:47 -04:00
|
|
|
expect(results.first.username).to eq(user1.username)
|
2014-05-14 22:22:35 -04:00
|
|
|
|
2014-08-13 13:30:25 -04:00
|
|
|
# don't return inactive users
|
2015-11-18 14:57:49 -05:00
|
|
|
results = search_for(inactive.username)
|
|
|
|
expect(results).to be_blank
|
|
|
|
|
|
|
|
# don't return staged users
|
|
|
|
results = search_for(staged.username)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(results).to be_blank
|
2017-11-18 19:17:31 -05:00
|
|
|
|
|
|
|
results = search_for(staged.username, include_staged_users: true)
|
|
|
|
expect(results.first.username).to eq(staged.username)
|
2019-02-19 21:33:56 -05:00
|
|
|
|
|
|
|
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"])
|
2013-02-07 06:50:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|