FEATURE: user directory returns staged users during search
This commit is contained in:
parent
546b206da0
commit
92a831bae6
|
@ -23,7 +23,7 @@ class DirectoryItemsController < ApplicationController
|
||||||
|
|
||||||
user_ids = nil
|
user_ids = nil
|
||||||
if params[:name].present?
|
if params[:name].present?
|
||||||
user_ids = UserSearch.new(params[:name]).search.pluck(:id)
|
user_ids = UserSearch.new(params[:name], include_staged_users: true).search.pluck(:id)
|
||||||
if user_ids.present?
|
if user_ids.present?
|
||||||
# Add the current user if we have at least one other match
|
# Add the current user if we have at least one other match
|
||||||
if current_user && result.dup.where(user_id: user_ids).exists?
|
if current_user && result.dup.where(user_id: user_ids).exists?
|
||||||
|
|
|
@ -9,6 +9,7 @@ class UserSearch
|
||||||
@topic_id = opts[:topic_id]
|
@topic_id = opts[:topic_id]
|
||||||
@topic_allowed_users = opts[:topic_allowed_users]
|
@topic_allowed_users = opts[:topic_allowed_users]
|
||||||
@searching_user = opts[:searching_user]
|
@searching_user = opts[:searching_user]
|
||||||
|
@include_staged_users = opts[:include_staged_users] || false
|
||||||
@limit = opts[:limit] || 20
|
@limit = opts[:limit] || 20
|
||||||
@group = opts[:group]
|
@group = opts[:group]
|
||||||
@guardian = Guardian.new(@searching_user)
|
@guardian = Guardian.new(@searching_user)
|
||||||
|
@ -16,7 +17,8 @@ class UserSearch
|
||||||
end
|
end
|
||||||
|
|
||||||
def scoped_users
|
def scoped_users
|
||||||
users = User.where(active: true, staged: false)
|
users = User.where(active: true)
|
||||||
|
users = users.where(staged: false) unless @include_staged_users
|
||||||
|
|
||||||
if @group
|
if @group
|
||||||
users = users.where('users.id IN (
|
users = users.where('users.id IN (
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
require 'rails_helper'
|
|
||||||
|
|
||||||
describe DirectoryItemsController do
|
|
||||||
|
|
||||||
it "requires a `period` param" do
|
|
||||||
expect { get :index, format: :json }.to raise_error(ActionController::ParameterMissing)
|
|
||||||
end
|
|
||||||
|
|
||||||
it "requires a proper `period` param" do
|
|
||||||
get :index, params: { period: 'eviltrout' }, format: :json
|
|
||||||
expect(response).not_to be_success
|
|
||||||
end
|
|
||||||
|
|
||||||
context "without data" do
|
|
||||||
|
|
||||||
context "and a logged in user" do
|
|
||||||
let!(:user) { log_in }
|
|
||||||
|
|
||||||
it "succeeds" do
|
|
||||||
get :index, params: { period: 'all' }, format: :json
|
|
||||||
expect(response).to be_success
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
context "with data" do
|
|
||||||
before do
|
|
||||||
Fabricate(:user)
|
|
||||||
DirectoryItem.refresh!
|
|
||||||
end
|
|
||||||
|
|
||||||
it "succeeds with a valid value" do
|
|
||||||
get :index, params: { period: 'all' }, format: :json
|
|
||||||
expect(response).to be_success
|
|
||||||
json = ::JSON.parse(response.body)
|
|
||||||
|
|
||||||
expect(json).to be_present
|
|
||||||
expect(json['directory_items']).to be_present
|
|
||||||
expect(json['total_rows_directory_items']).to be_present
|
|
||||||
expect(json['load_more_directory_items']).to be_present
|
|
||||||
end
|
|
||||||
|
|
||||||
it "fails when the directory is disabled" do
|
|
||||||
SiteSetting.enable_user_directory = false
|
|
||||||
|
|
||||||
get :index, params: { period: 'all' }, format: :json
|
|
||||||
expect(response).not_to be_success
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -134,6 +134,9 @@ describe UserSearch do
|
||||||
# don't return staged users
|
# don't return staged users
|
||||||
results = search_for(staged.username)
|
results = search_for(staged.username)
|
||||||
expect(results).to be_blank
|
expect(results).to be_blank
|
||||||
|
|
||||||
|
results = search_for(staged.username, include_staged_users: true)
|
||||||
|
expect(results.first.username).to eq(staged.username)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
describe DirectoryItemsController do
|
||||||
|
let!(:user) { Fabricate(:user) }
|
||||||
|
|
||||||
|
|
||||||
|
it "requires a `period` param" do
|
||||||
|
expect do
|
||||||
|
get '/directory_items.json'
|
||||||
|
end.to raise_error(ActionController::ParameterMissing)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "requires a proper `period` param" do
|
||||||
|
get '/directory_items.json', params: { period: 'eviltrout' }
|
||||||
|
expect(response).not_to be_success
|
||||||
|
end
|
||||||
|
|
||||||
|
context "without data" do
|
||||||
|
|
||||||
|
context "and a logged in user" do
|
||||||
|
before { sign_in(user) }
|
||||||
|
|
||||||
|
it "succeeds" do
|
||||||
|
get '/directory_items.json', params: { period: 'all' }
|
||||||
|
expect(response).to be_success
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
||||||
|
context "with data" do
|
||||||
|
before do
|
||||||
|
Fabricate(:evil_trout)
|
||||||
|
Fabricate(:walter_white)
|
||||||
|
Fabricate(:staged, username: 'stage_user')
|
||||||
|
|
||||||
|
DirectoryItem.refresh!
|
||||||
|
end
|
||||||
|
|
||||||
|
it "succeeds with a valid value" do
|
||||||
|
get '/directory_items.json', params: { period: 'all' }
|
||||||
|
expect(response).to be_success
|
||||||
|
json = ::JSON.parse(response.body)
|
||||||
|
|
||||||
|
expect(json).to be_present
|
||||||
|
expect(json['directory_items']).to be_present
|
||||||
|
expect(json['total_rows_directory_items']).to be_present
|
||||||
|
expect(json['load_more_directory_items']).to be_present
|
||||||
|
|
||||||
|
expect(json['directory_items'].length).to eq(4)
|
||||||
|
expect(json['total_rows_directory_items']).to eq(4)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "fails when the directory is disabled" do
|
||||||
|
SiteSetting.enable_user_directory = false
|
||||||
|
|
||||||
|
get '/directory_items.json', params: { period: 'all' }
|
||||||
|
expect(response).not_to be_success
|
||||||
|
end
|
||||||
|
|
||||||
|
it "finds user by name" do
|
||||||
|
get '/directory_items.json', params: { period: 'all', name: 'eviltrout' }
|
||||||
|
expect(response).to be_success
|
||||||
|
|
||||||
|
json = ::JSON.parse(response.body)
|
||||||
|
expect(json).to be_present
|
||||||
|
expect(json['directory_items'].length).to eq(1)
|
||||||
|
expect(json['total_rows_directory_items']).to eq(1)
|
||||||
|
expect(json['directory_items'][0]['user']['username']).to eq('eviltrout')
|
||||||
|
end
|
||||||
|
|
||||||
|
it "finds staged user by name" do
|
||||||
|
get '/directory_items.json', params: { period: 'all', name: 'stage_user' }
|
||||||
|
expect(response).to be_success
|
||||||
|
|
||||||
|
json = ::JSON.parse(response.body)
|
||||||
|
expect(json).to be_present
|
||||||
|
expect(json['directory_items'].length).to eq(1)
|
||||||
|
expect(json['total_rows_directory_items']).to eq(1)
|
||||||
|
expect(json['directory_items'][0]['user']['username']).to eq('stage_user')
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue