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