2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-19 08:17:31 +08:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe DirectoryItemsController do
|
2019-05-07 11:12:20 +08: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-19 08:17:31 +08:00
|
|
|
|
|
|
|
it "requires a `period` param" do
|
2018-01-12 11:15:10 +08:00
|
|
|
get '/directory_items.json'
|
|
|
|
expect(response.status).to eq(400)
|
2017-11-19 08:17:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "requires a proper `period` param" do
|
|
|
|
get '/directory_items.json', params: { period: 'eviltrout' }
|
2018-06-05 15:29:17 +08:00
|
|
|
expect(response).not_to be_successful
|
2017-11-19 08:17:31 +08: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 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-11-19 08:17:31 +08: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 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 23:04:12 +08:00
|
|
|
json = response.parsed_body
|
2017-11-19 08:17:31 +08:00
|
|
|
|
|
|
|
expect(json).to be_present
|
|
|
|
expect(json['directory_items']).to be_present
|
2020-04-03 10:02:50 +08:00
|
|
|
expect(json['meta']['total_rows_directory_items']).to be_present
|
|
|
|
expect(json['meta']['load_more_directory_items']).to be_present
|
2020-03-31 02:27:49 +08:00
|
|
|
expect(json['meta']['last_updated_at']).to be_present
|
2017-11-19 08:17:31 +08:00
|
|
|
|
|
|
|
expect(json['directory_items'].length).to eq(4)
|
2020-04-03 10:02:50 +08:00
|
|
|
expect(json['meta']['total_rows_directory_items']).to eq(4)
|
2017-11-19 08:17:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "fails when the directory is disabled" do
|
|
|
|
SiteSetting.enable_user_directory = false
|
|
|
|
|
|
|
|
get '/directory_items.json', params: { period: 'all' }
|
2018-06-05 15:29:17 +08:00
|
|
|
expect(response).not_to be_successful
|
2017-11-19 08:17:31 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "finds user by name" do
|
|
|
|
get '/directory_items.json', params: { period: 'all', name: 'eviltrout' }
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-11-19 08:17:31 +08:00
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
json = response.parsed_body
|
2017-11-19 08:17:31 +08:00
|
|
|
expect(json).to be_present
|
|
|
|
expect(json['directory_items'].length).to eq(1)
|
2020-04-03 10:02:50 +08:00
|
|
|
expect(json['meta']['total_rows_directory_items']).to eq(1)
|
2017-11-19 08:17:31 +08: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 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2017-11-19 08:17:31 +08:00
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
json = response.parsed_body
|
2017-11-19 08:17:31 +08:00
|
|
|
expect(json).to be_present
|
|
|
|
expect(json['directory_items'].length).to eq(1)
|
2020-04-03 10:02:50 +08:00
|
|
|
expect(json['meta']['total_rows_directory_items']).to eq(1)
|
2017-11-19 08:17:31 +08:00
|
|
|
expect(json['directory_items'][0]['user']['username']).to eq('stage_user')
|
|
|
|
end
|
2018-05-16 22:20:17 +08:00
|
|
|
|
|
|
|
it "excludes users by username" do
|
|
|
|
get '/directory_items.json', params: { period: 'all', exclude_usernames: "stage_user,eviltrout" }
|
2018-06-07 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2018-05-16 22:20:17 +08:00
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
json = response.parsed_body
|
2018-05-16 22:20:17 +08:00
|
|
|
expect(json).to be_present
|
|
|
|
expect(json['directory_items'].length).to eq(2)
|
2020-04-03 10:02:50 +08:00
|
|
|
expect(json['meta']['total_rows_directory_items']).to eq(2)
|
2018-05-16 22:20:17 +08: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 16:11:09 +08:00
|
|
|
expect(response.status).to eq(200)
|
2018-05-16 22:20:17 +08:00
|
|
|
|
2020-05-07 23:04:12 +08:00
|
|
|
json = response.parsed_body
|
2018-05-16 22:20:17 +08:00
|
|
|
expect(json).to be_present
|
|
|
|
expect(json['directory_items'].length).to eq(2)
|
2020-04-03 10:02:50 +08:00
|
|
|
expect(json['meta']['total_rows_directory_items']).to eq(2)
|
2018-05-16 22:20:17 +08: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-10 04:04:05 +08: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
|
2017-11-19 08:17:31 +08:00
|
|
|
end
|
|
|
|
end
|