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

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

177 lines
5.9 KiB
Ruby
Raw Normal View History

2019-12-21 20:55:43 +05:30
# frozen_string_literal: true
2017-09-10 17:25:29 +05:30
require 'spec_helper'
2020-06-23 00:09:42 +05:30
RSpec.describe UsersFinder do
2017-09-10 17:25:29 +05:30
describe '#execute' do
2019-07-07 11:18:12 +05:30
include_context 'UsersFinder#execute filter by project context'
2017-09-10 17:25:29 +05:30
2022-06-21 17:19:12 +05:30
let_it_be(:project_bot) { create(:user, :project_bot) }
2023-03-04 22:38:38 +05:30
shared_examples 'executes users finder as normal user' do
2022-06-21 17:19:12 +05:30
it 'returns searchable users' do
2017-09-10 17:25:29 +05:30
users = described_class.new(user).execute
2022-06-21 17:19:12 +05:30
expect(users).to contain_exactly(user, normal_user, external_user, unconfirmed_user, omniauth_user, internal_user, admin_user, project_bot)
2017-09-10 17:25:29 +05:30
end
it 'filters by username' do
users = described_class.new(user, username: 'johndoe').execute
2019-07-07 11:18:12 +05:30
expect(users).to contain_exactly(normal_user)
2017-09-10 17:25:29 +05:30
end
2020-06-23 00:09:42 +05:30
it 'filters by id' do
users = described_class.new(user, id: normal_user.id).execute
expect(users).to contain_exactly(normal_user)
end
2018-12-13 13:39:08 +05:30
it 'filters by username (case insensitive)' do
users = described_class.new(user, username: 'joHNdoE').execute
2019-07-07 11:18:12 +05:30
expect(users).to contain_exactly(normal_user)
2018-12-13 13:39:08 +05:30
end
2017-09-10 17:25:29 +05:30
it 'filters by search' do
2022-06-21 17:19:12 +05:30
users = described_class.new(user, search: 'ohndo').execute
2017-09-10 17:25:29 +05:30
2022-06-21 17:19:12 +05:30
expect(users).to contain_exactly(normal_user)
2017-09-10 17:25:29 +05:30
end
2022-02-05 19:09:49 +05:30
it 'does not filter by private emails search' do
users = described_class.new(user, search: normal_user.email).execute
expect(users).to be_empty
end
2021-04-17 20:07:23 +05:30
it 'filters by external users' do
2017-09-10 17:25:29 +05:30
users = described_class.new(user, external: true).execute
2021-04-17 20:07:23 +05:30
expect(users).to contain_exactly(external_user)
end
it 'filters by non external users' do
users = described_class.new(user, non_external: true).execute
2022-06-21 17:19:12 +05:30
expect(users).to contain_exactly(user, normal_user, unconfirmed_user, omniauth_user, internal_user, admin_user, project_bot)
2017-09-10 17:25:29 +05:30
end
it 'filters by created_at' do
filtered_user_before = create(:user, created_at: 3.days.ago)
filtered_user_after = create(:user, created_at: Time.now + 3.days)
2023-06-20 00:43:36 +05:30
users = described_class.new(
user,
created_after: 2.days.ago,
created_before: Time.now + 2.days
).execute
2017-09-10 17:25:29 +05:30
expect(users.map(&:username)).not_to include([filtered_user_before.username, filtered_user_after.username])
end
2018-03-17 18:26:18 +05:30
2020-11-24 15:15:51 +05:30
it 'filters by non internal users' do
users = described_class.new(user, non_internal: true).execute
2022-06-21 17:19:12 +05:30
expect(users).to contain_exactly(user, normal_user, unconfirmed_user, external_user, omniauth_user, admin_user, project_bot)
2020-11-24 15:15:51 +05:30
end
2018-03-17 18:26:18 +05:30
it 'does not filter by custom attributes' do
users = described_class.new(
user,
custom_attributes: { foo: 'bar' }
).execute
2022-06-21 17:19:12 +05:30
expect(users).to contain_exactly(user, normal_user, external_user, unconfirmed_user, omniauth_user, internal_user, admin_user, project_bot)
2018-03-17 18:26:18 +05:30
end
2020-06-23 00:09:42 +05:30
it 'orders returned results' do
users = described_class.new(user, sort: 'id_asc').execute
2022-06-21 17:19:12 +05:30
expect(users).to eq([normal_user, admin_user, external_user, unconfirmed_user, omniauth_user, internal_user, project_bot, user])
2021-03-08 18:12:59 +05:30
end
it 'does not filter by admins' do
users = described_class.new(user, admins: true).execute
2022-06-21 17:19:12 +05:30
expect(users).to contain_exactly(user, normal_user, external_user, admin_user, unconfirmed_user, omniauth_user, internal_user, project_bot)
2020-06-23 00:09:42 +05:30
end
2017-09-10 17:25:29 +05:30
end
2023-03-04 22:38:38 +05:30
shared_examples 'executes users finder as admin' do
2017-09-10 17:25:29 +05:30
it 'filters by external users' do
2023-03-04 22:38:38 +05:30
users = described_class.new(user, external: true).execute
2017-09-10 17:25:29 +05:30
expect(users).to contain_exactly(external_user)
end
it 'returns all users' do
2023-03-04 22:38:38 +05:30
users = described_class.new(user).execute
2017-09-10 17:25:29 +05:30
2023-03-04 22:38:38 +05:30
expect(users).to contain_exactly(user, normal_user, blocked_user, unconfirmed_user, banned_user, external_user, omniauth_user, internal_user, admin_user, project_bot)
2022-06-21 17:19:12 +05:30
end
it 'filters by blocked users' do
2023-03-04 22:38:38 +05:30
users = described_class.new(user, blocked: true).execute
2022-06-21 17:19:12 +05:30
expect(users).to contain_exactly(blocked_user)
end
it 'filters by active users' do
2023-03-04 22:38:38 +05:30
users = described_class.new(user, active: true).execute
2022-06-21 17:19:12 +05:30
2023-03-04 22:38:38 +05:30
expect(users).to contain_exactly(user, normal_user, unconfirmed_user, external_user, omniauth_user, admin_user, project_bot)
2021-03-08 18:12:59 +05:30
end
it 'returns only admins' do
2023-03-04 22:38:38 +05:30
users = described_class.new(user, admins: true).execute
2021-03-08 18:12:59 +05:30
2023-03-04 22:38:38 +05:30
expect(users).to contain_exactly(user, admin_user)
2017-09-10 17:25:29 +05:30
end
2018-03-17 18:26:18 +05:30
it 'filters by custom attributes' do
2019-07-07 11:18:12 +05:30
create :user_custom_attribute, user: normal_user, key: 'foo', value: 'foo'
create :user_custom_attribute, user: normal_user, key: 'bar', value: 'bar'
create :user_custom_attribute, user: blocked_user, key: 'foo', value: 'foo'
2020-11-24 15:15:51 +05:30
create :user_custom_attribute, user: internal_user, key: 'foo', value: 'foo'
2018-03-17 18:26:18 +05:30
users = described_class.new(
2023-03-04 22:38:38 +05:30
user,
2018-03-17 18:26:18 +05:30
custom_attributes: { foo: 'foo', bar: 'bar' }
).execute
2019-07-07 11:18:12 +05:30
expect(users).to contain_exactly(normal_user)
2018-03-17 18:26:18 +05:30
end
2022-02-05 19:09:49 +05:30
it 'filters by private emails search' do
2023-03-04 22:38:38 +05:30
users = described_class.new(user, search: normal_user.email).execute
2022-02-05 19:09:49 +05:30
expect(users).to contain_exactly(normal_user)
end
2017-09-10 17:25:29 +05:30
end
2023-03-04 22:38:38 +05:30
context 'with a normal user' do
let_it_be(:user) { create(:user) }
it_behaves_like 'executes users finder as normal user'
end
context 'with an admin user' do
let_it_be(:user) { create(:admin) }
context 'when admin mode setting is disabled', :do_not_mock_admin_mode_setting do
it_behaves_like 'executes users finder as admin'
end
context 'when admin mode setting is enabled' do
context 'when in admin mode', :enable_admin_mode do
it_behaves_like 'executes users finder as admin'
end
context 'when not in admin mode' do
it_behaves_like 'executes users finder as normal user'
end
end
end
2017-09-10 17:25:29 +05:30
end
end