debian-mirror-gitlab/spec/finders/autocomplete/users_finder_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

199 lines
6.3 KiB
Ruby
Raw Permalink Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
2018-03-17 18:26:18 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe Autocomplete::UsersFinder do
2022-04-04 11:22:00 +05:30
# TODO update when multiple owners are possible in projects
# https://gitlab.com/gitlab-org/gitlab/-/issues/21432
2018-03-17 18:26:18 +05:30
describe '#execute' do
2022-04-04 11:22:00 +05:30
let_it_be(:user1) { create(:user, name: 'zzzzzname', username: 'johndoe') }
2023-01-13 00:05:48 +05:30
let_it_be(:blocked_user) { create(:user, :blocked, username: 'blocked_user') }
let_it_be(:banned_user) { create(:user, :banned, username: 'banned_user') }
2022-04-04 11:22:00 +05:30
let_it_be(:external_user) { create(:user, :external) }
let_it_be(:omniauth_user) { create(:omniauth_user, provider: 'twitter', extern_uid: '123456') }
2018-03-17 18:26:18 +05:30
let(:current_user) { create(:user) }
let(:params) { {} }
2022-04-04 11:22:00 +05:30
let_it_be(:project) { nil }
let_it_be(:group) { nil }
2018-03-17 18:26:18 +05:30
subject { described_class.new(params: params, current_user: current_user, project: project, group: group).execute.to_a }
context 'when current_user not passed or nil' do
let(:current_user) { nil }
it { is_expected.to match_array([]) }
end
context 'when project passed' do
2022-04-04 11:22:00 +05:30
let_it_be(:project) { create(:project) }
2018-03-17 18:26:18 +05:30
2022-04-04 11:22:00 +05:30
it { is_expected.to match_array([project.first_owner]) }
2018-03-17 18:26:18 +05:30
context 'when author_id passed' do
2019-07-31 22:56:46 +05:30
context 'and author is active' do
let(:params) { { author_id: user1.id } }
2018-03-17 18:26:18 +05:30
2022-04-04 11:22:00 +05:30
it { is_expected.to match_array([project.first_owner, user1]) }
2019-07-31 22:56:46 +05:30
end
context 'and author is blocked' do
2023-01-13 00:05:48 +05:30
let(:params) { { author_id: blocked_user.id } }
it { is_expected.to match_array([project.first_owner]) }
end
context 'and author is banned' do
let(:params) { { author_id: banned_user.id } }
2019-07-31 22:56:46 +05:30
2022-04-04 11:22:00 +05:30
it { is_expected.to match_array([project.first_owner]) }
end
end
context 'searching with less than 3 characters' do
let(:params) { { search: 'zz' } }
before do
project.add_guest(user1)
end
it 'allows partial matches' do
expect(subject).to contain_exactly(user1)
2019-07-31 22:56:46 +05:30
end
2018-03-17 18:26:18 +05:30
end
end
context 'when group passed and project not passed' do
2022-04-04 11:22:00 +05:30
let_it_be(:group) { create(:group, :public) }
2018-03-17 18:26:18 +05:30
2022-04-04 11:22:00 +05:30
before_all do
2022-08-13 15:12:31 +05:30
group.add_members([user1], GroupMember::DEVELOPER)
2018-03-17 18:26:18 +05:30
end
it { is_expected.to match_array([user1]) }
2022-04-04 11:22:00 +05:30
context 'searching with less than 3 characters' do
let(:params) { { search: 'zz' } }
it 'allows partial matches' do
expect(subject).to contain_exactly(user1)
end
end
2018-03-17 18:26:18 +05:30
end
2019-10-12 21:52:04 +05:30
context 'when passed a subgroup' do
2018-03-17 18:26:18 +05:30
let(:grandparent) { create(:group, :public) }
let(:parent) { create(:group, :public, parent: grandparent) }
let(:child) { create(:group, :public, parent: parent) }
let(:group) { parent }
let!(:grandparent_user) { create(:group_member, :developer, group: grandparent).user }
let!(:parent_user) { create(:group_member, :developer, group: parent).user }
let!(:child_user) { create(:group_member, :developer, group: child).user }
it 'includes users from parent groups as well' do
expect(subject).to match_array([grandparent_user, parent_user])
end
end
it { is_expected.to match_array([user1, external_user, omniauth_user, current_user]) }
context 'when filtered by search' do
let(:params) { { search: 'johndoe' } }
it { is_expected.to match_array([user1]) }
2022-04-04 11:22:00 +05:30
context 'searching with less than 3 characters' do
let(:params) { { search: 'zz' } }
it 'does not allow partial matches' do
expect(subject).to be_empty
end
end
2018-03-17 18:26:18 +05:30
end
context 'when filtered by skip_users' do
2023-01-13 00:05:48 +05:30
let(:params) { { skip_users: [omniauth_user.id, current_user.id, blocked_user] } }
2018-03-17 18:26:18 +05:30
it { is_expected.to match_array([user1, external_user]) }
end
context 'when todos exist' do
let!(:pending_todo1) { create(:todo, user: current_user, author: user1, state: :pending) }
let!(:pending_todo2) { create(:todo, user: external_user, author: omniauth_user, state: :pending) }
let!(:done_todo1) { create(:todo, user: current_user, author: external_user, state: :done) }
let!(:done_todo2) { create(:todo, user: user1, author: external_user, state: :done) }
context 'when filtered by todo_filter without todo_state_filter' do
let(:params) { { todo_filter: true } }
it { is_expected.to match_array([]) }
end
context 'when filtered by todo_filter with pending todo_state_filter' do
let(:params) { { todo_filter: true, todo_state_filter: 'pending' } }
it { is_expected.to match_array([user1]) }
end
context 'when filtered by todo_filter with done todo_state_filter' do
let(:params) { { todo_filter: true, todo_state_filter: 'done' } }
it { is_expected.to match_array([external_user]) }
end
end
context 'when filtered by current_user' do
2023-01-13 00:05:48 +05:30
let(:current_user) { blocked_user }
2018-03-17 18:26:18 +05:30
let(:params) { { current_user: true } }
2023-01-13 00:05:48 +05:30
it { is_expected.to match_array([blocked_user, user1, external_user, omniauth_user]) }
2018-03-17 18:26:18 +05:30
end
context 'when filtered by author_id' do
2019-07-31 22:56:46 +05:30
let(:params) { { author_id: user1.id } }
2018-03-17 18:26:18 +05:30
2019-07-31 22:56:46 +05:30
it { is_expected.to match_array([user1, external_user, omniauth_user, current_user]) }
2018-03-17 18:26:18 +05:30
end
2021-03-11 19:13:27 +05:30
it 'preloads the status association' do
associations = subject.map { |user| user.association(:status) }
expect(associations).to all(be_loaded)
end
2023-01-13 00:05:48 +05:30
context 'when filtered by state' do
context "searching without states" do
let(:params) { { states: nil } }
it { is_expected.to match_array([user1, external_user, omniauth_user, current_user]) }
end
context "searching with states=active" do
let(:params) { { states: %w[active] } }
it { is_expected.to match_array([user1, external_user, omniauth_user, current_user]) }
end
context "searching with states=blocked" do
let(:params) { { states: %w[blocked] } }
it { is_expected.to match_array([blocked_user]) }
end
context "searching with states=banned" do
let(:params) { { states: %w[banned] } }
it { is_expected.to match_array([banned_user]) }
end
context "searching with states=blocked,banned" do
let(:params) { { states: %w[blocked banned] } }
it { is_expected.to match_array([blocked_user, banned_user]) }
end
end
2018-03-17 18:26:18 +05:30
end
end