2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 22:27:38 -04:00
|
|
|
RSpec.describe DirectoryItemsController do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:evil_trout) { Fabricate(:evil_trout) }
|
|
|
|
fab!(:walter_white) { Fabricate(:walter_white) }
|
|
|
|
fab!(:stage_user) { Fabricate(:staged, username: 'stage_user') }
|
|
|
|
fab!(:group) { Fabricate(:group, users: [evil_trout, stage_user]) }
|
2017-11-18 19:17:31 -05:00
|
|
|
|
|
|
|
it "requires a `period` param" do
|
2018-01-11 22:15:10 -05:00
|
|
|
get '/directory_items.json'
|
|
|
|
expect(response.status).to eq(400)
|
2017-11-18 19:17:31 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "requires a proper `period` param" do
|
|
|
|
get '/directory_items.json', params: { period: 'eviltrout' }
|
2018-06-05 03:29:17 -04:00
|
|
|
expect(response).not_to be_successful
|
2017-11-18 19:17:31 -05:00
|
|
|
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' }
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-11-18 19:17:31 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
context "with data" do
|
|
|
|
before do
|
|
|
|
DirectoryItem.refresh!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "succeeds with a valid value" do
|
|
|
|
get '/directory_items.json', params: { period: 'all' }
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2017-11-18 19:17:31 -05:00
|
|
|
|
|
|
|
expect(json).to be_present
|
|
|
|
expect(json['directory_items']).to be_present
|
2020-04-02 22:02:50 -04:00
|
|
|
expect(json['meta']['total_rows_directory_items']).to be_present
|
|
|
|
expect(json['meta']['load_more_directory_items']).to be_present
|
2020-03-30 14:27:49 -04:00
|
|
|
expect(json['meta']['last_updated_at']).to be_present
|
2017-11-18 19:17:31 -05:00
|
|
|
|
|
|
|
expect(json['directory_items'].length).to eq(4)
|
2020-04-02 22:02:50 -04:00
|
|
|
expect(json['meta']['total_rows_directory_items']).to eq(4)
|
2020-08-11 14:35:23 -04:00
|
|
|
expect(json['meta']['load_more_directory_items']).to include('.json')
|
2017-11-18 19:17:31 -05:00
|
|
|
end
|
|
|
|
|
2021-06-23 11:21:53 -04:00
|
|
|
it "respects more_params in load_more_directory_items" do
|
2021-06-29 15:43:38 -04:00
|
|
|
get '/directory_items.json', params: { period: 'all', order: "likes_given", group: group.name, user_field_ids: "1|2" }
|
2021-06-23 11:21:53 -04:00
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
expect(json['meta']['load_more_directory_items']).to include("group=#{group.name}")
|
2021-06-29 15:43:38 -04:00
|
|
|
expect(json['meta']['load_more_directory_items']).to include("user_field_ids=#{CGI.escape('1|2')}")
|
2021-06-23 11:21:53 -04:00
|
|
|
expect(json['meta']['load_more_directory_items']).to include("order=likes_given")
|
|
|
|
expect(json['meta']['load_more_directory_items']).to include("period=all")
|
|
|
|
end
|
|
|
|
|
2017-11-18 19:17:31 -05:00
|
|
|
it "fails when the directory is disabled" do
|
|
|
|
SiteSetting.enable_user_directory = false
|
|
|
|
|
|
|
|
get '/directory_items.json', params: { period: 'all' }
|
2018-06-05 03:29:17 -04:00
|
|
|
expect(response).not_to be_successful
|
2017-11-18 19:17:31 -05:00
|
|
|
end
|
|
|
|
|
2020-08-26 10:14:20 -04:00
|
|
|
it "sort username with asc as a parameter" do
|
|
|
|
get '/directory_items.json', params: { asc: true, order: 'username', period: 'all' }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
names = json['directory_items'].map { |item| item['user']['username'] }
|
|
|
|
expect(names).to eq(names.sort)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sort username without asc as a parameter" do
|
|
|
|
get '/directory_items.json', params: { order: 'username', period: 'all' }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
json = response.parsed_body
|
|
|
|
|
|
|
|
names = json['directory_items'].map { |item| item['user']['username'] }
|
|
|
|
|
|
|
|
expect(names).to eq(names.sort.reverse)
|
|
|
|
end
|
|
|
|
|
2017-11-18 19:17:31 -05:00
|
|
|
it "finds user by name" do
|
|
|
|
get '/directory_items.json', params: { period: 'all', name: 'eviltrout' }
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-11-18 19:17:31 -05:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2017-11-18 19:17:31 -05:00
|
|
|
expect(json).to be_present
|
|
|
|
expect(json['directory_items'].length).to eq(1)
|
2020-04-02 22:02:50 -04:00
|
|
|
expect(json['meta']['total_rows_directory_items']).to eq(1)
|
2017-11-18 19:17:31 -05:00
|
|
|
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' }
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2017-11-18 19:17:31 -05:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2017-11-18 19:17:31 -05:00
|
|
|
expect(json).to be_present
|
|
|
|
expect(json['directory_items'].length).to eq(1)
|
2020-04-02 22:02:50 -04:00
|
|
|
expect(json['meta']['total_rows_directory_items']).to eq(1)
|
2017-11-18 19:17:31 -05:00
|
|
|
expect(json['directory_items'][0]['user']['username']).to eq('stage_user')
|
|
|
|
end
|
2018-05-16 10:20:17 -04:00
|
|
|
|
|
|
|
it "excludes users by username" do
|
|
|
|
get '/directory_items.json', params: { period: 'all', exclude_usernames: "stage_user,eviltrout" }
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2018-05-16 10:20:17 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2018-05-16 10:20:17 -04:00
|
|
|
expect(json).to be_present
|
|
|
|
expect(json['directory_items'].length).to eq(2)
|
2020-04-02 22:02:50 -04:00
|
|
|
expect(json['meta']['total_rows_directory_items']).to eq(2)
|
2018-05-16 10:20:17 -04:00
|
|
|
expect(json['directory_items'][0]['user']['username']).to eq(walter_white.username) | eq(user.username)
|
|
|
|
expect(json['directory_items'][1]['user']['username']).to eq(walter_white.username) | eq(user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "filters users by group" do
|
|
|
|
get '/directory_items.json', params: { period: 'all', group: group.name }
|
2018-06-07 04:11:09 -04:00
|
|
|
expect(response.status).to eq(200)
|
2018-05-16 10:20:17 -04:00
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2018-05-16 10:20:17 -04:00
|
|
|
expect(json).to be_present
|
|
|
|
expect(json['directory_items'].length).to eq(2)
|
2020-04-02 22:02:50 -04:00
|
|
|
expect(json['meta']['total_rows_directory_items']).to eq(2)
|
2018-05-16 10:20:17 -04:00
|
|
|
expect(json['directory_items'][0]['user']['username']).to eq(evil_trout.username) | eq(stage_user.username)
|
|
|
|
expect(json['directory_items'][1]['user']['username']).to eq(evil_trout.username) | eq(stage_user.username)
|
|
|
|
end
|
2020-03-09 16:04:05 -04:00
|
|
|
|
2022-02-15 21:27:35 -05:00
|
|
|
it "orders users by user fields" do
|
|
|
|
group.add(walter_white)
|
|
|
|
field1 = Fabricate(:user_field, searchable: true)
|
|
|
|
field2 = Fabricate(:user_field, searchable: true)
|
|
|
|
|
|
|
|
user_fields = [
|
|
|
|
{ user: walter_white, field: field1, value: "Yellow", order: 1 },
|
|
|
|
{ user: stage_user, field: field1, value: "Apple", order: 0 },
|
|
|
|
{ user: evil_trout, field: field2, value: "Moon", order: 2 }
|
|
|
|
]
|
|
|
|
|
|
|
|
user_fields.each do |data|
|
|
|
|
UserCustomField.create!(
|
|
|
|
user_id: data[:user].id,
|
|
|
|
name: "user_field_#{data[:field].id}",
|
|
|
|
value: data[:value]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
get '/directory_items.json', params: { period: 'all', group: group.name, order: field1.name, user_field_ids: "#{field1.id}|#{field2.id}", asc: true }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
json = response.parsed_body
|
|
|
|
expect(json).to be_present
|
|
|
|
items = json['directory_items']
|
|
|
|
expect(items.length).to eq(3)
|
|
|
|
expect(json['meta']['total_rows_directory_items']).to eq(3)
|
|
|
|
|
|
|
|
user_fields.each do |data|
|
|
|
|
user = items[data[:order]]['user']
|
|
|
|
expect(user['username']).to eq(data[:user].username)
|
|
|
|
expect(user['user_fields']).to eq({ data[:field].id.to_s => data[:value] })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-09 16:04:05 -04:00
|
|
|
it "checks group permissions" do
|
|
|
|
group.update!(visibility_level: Group.visibility_levels[:members])
|
|
|
|
|
|
|
|
sign_in(evil_trout)
|
|
|
|
get '/directory_items.json', params: { period: 'all', group: group.name }
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
|
|
|
get '/directory_items.json', params: { period: 'all', group: 'not a group' }
|
|
|
|
expect(response.status).to eq(400)
|
|
|
|
|
|
|
|
sign_in(user)
|
|
|
|
get '/directory_items.json', params: { period: 'all', group: group.name }
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
2020-11-22 18:22:14 -05:00
|
|
|
|
|
|
|
it "does not force-include self in group-filtered results" do
|
|
|
|
me = Fabricate(:user)
|
|
|
|
DirectoryItem.refresh!
|
|
|
|
sign_in(me)
|
|
|
|
|
|
|
|
get '/directory_items.json', params: { period: 'all', group: group.name }
|
|
|
|
expect(response.parsed_body['directory_items'].length).to eq(2)
|
|
|
|
end
|
2017-11-18 19:17:31 -05:00
|
|
|
end
|
|
|
|
end
|