2019-10-16 05:07:51 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-05-15 09:53:12 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-16 14:09:01 -04:00
|
|
|
RSpec.describe UsersFinder do
|
2017-05-15 09:53:12 -04:00
|
|
|
describe '#execute' do
|
2019-03-20 05:33:24 -04:00
|
|
|
include_context 'UsersFinder#execute filter by project context'
|
2017-05-15 09:53:12 -04:00
|
|
|
|
|
|
|
context 'with a normal user' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it 'returns all users' do
|
|
|
|
users = described_class.new(user).execute
|
|
|
|
|
2021-02-24 10:11:10 -05:00
|
|
|
expect(users).to contain_exactly(user, normal_user, blocked_user, external_user, omniauth_user, internal_user, admin_user)
|
2017-05-15 09:53:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters by username' do
|
|
|
|
users = described_class.new(user, username: 'johndoe').execute
|
|
|
|
|
2019-03-20 05:33:24 -04:00
|
|
|
expect(users).to contain_exactly(normal_user)
|
2017-05-15 09:53:12 -04:00
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
it 'filters by id' do
|
|
|
|
users = described_class.new(user, id: normal_user.id).execute
|
|
|
|
|
|
|
|
expect(users).to contain_exactly(normal_user)
|
|
|
|
end
|
|
|
|
|
2018-10-18 05:06:44 -04:00
|
|
|
it 'filters by username (case insensitive)' do
|
|
|
|
users = described_class.new(user, username: 'joHNdoE').execute
|
|
|
|
|
2019-03-20 05:33:24 -04:00
|
|
|
expect(users).to contain_exactly(normal_user)
|
2018-10-18 05:06:44 -04:00
|
|
|
end
|
|
|
|
|
2017-05-15 09:53:12 -04:00
|
|
|
it 'filters by search' do
|
|
|
|
users = described_class.new(user, search: 'orando').execute
|
|
|
|
|
2019-03-20 05:33:24 -04:00
|
|
|
expect(users).to contain_exactly(blocked_user)
|
2017-05-15 09:53:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters by blocked users' do
|
|
|
|
users = described_class.new(user, blocked: true).execute
|
|
|
|
|
2019-03-20 05:33:24 -04:00
|
|
|
expect(users).to contain_exactly(blocked_user)
|
2017-05-15 09:53:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters by active users' do
|
|
|
|
users = described_class.new(user, active: true).execute
|
|
|
|
|
2021-02-24 10:11:10 -05:00
|
|
|
expect(users).to contain_exactly(user, normal_user, external_user, omniauth_user, admin_user)
|
2017-05-15 09:53:12 -04:00
|
|
|
end
|
|
|
|
|
2021-02-24 10:11:10 -05:00
|
|
|
it 'filters by external users' do
|
2017-05-15 09:53:12 -04:00
|
|
|
users = described_class.new(user, external: true).execute
|
|
|
|
|
2021-02-24 10:11:10 -05:00
|
|
|
expect(users).to contain_exactly(external_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters by non external users' do
|
|
|
|
users = described_class.new(user, non_external: true).execute
|
|
|
|
|
2021-01-18 07:10:41 -05:00
|
|
|
expect(users).to contain_exactly(user, normal_user, blocked_user, omniauth_user, internal_user, admin_user)
|
2017-05-15 09:53:12 -04:00
|
|
|
end
|
2017-07-06 10:59:40 -04:00
|
|
|
|
|
|
|
it 'filters by created_at' do
|
2017-07-07 04:29:38 -04:00
|
|
|
filtered_user_before = create(:user, created_at: 3.days.ago)
|
|
|
|
filtered_user_after = create(:user, created_at: Time.now + 3.days)
|
|
|
|
|
2017-07-07 03:29:00 -04:00
|
|
|
users = described_class.new(user,
|
|
|
|
created_after: 2.days.ago,
|
|
|
|
created_before: Time.now + 2.days).execute
|
2017-07-06 10:59:40 -04:00
|
|
|
|
2017-07-07 04:29:38 -04:00
|
|
|
expect(users.map(&:username)).not_to include([filtered_user_before.username, filtered_user_after.username])
|
2017-07-06 10:59:40 -04:00
|
|
|
end
|
2017-09-28 12:49:42 -04:00
|
|
|
|
2020-08-31 11:10:41 -04:00
|
|
|
it 'filters by non internal users' do
|
|
|
|
users = described_class.new(user, non_internal: true).execute
|
|
|
|
|
2021-02-24 10:11:10 -05:00
|
|
|
expect(users).to contain_exactly(user, normal_user, external_user, blocked_user, omniauth_user, admin_user)
|
2020-08-31 11:10:41 -04:00
|
|
|
end
|
|
|
|
|
2017-09-28 12:49:42 -04:00
|
|
|
it 'does not filter by custom attributes' do
|
|
|
|
users = described_class.new(
|
|
|
|
user,
|
|
|
|
custom_attributes: { foo: 'bar' }
|
|
|
|
).execute
|
|
|
|
|
2021-02-24 10:11:10 -05:00
|
|
|
expect(users).to contain_exactly(user, normal_user, blocked_user, external_user, omniauth_user, internal_user, admin_user)
|
2017-09-28 12:49:42 -04:00
|
|
|
end
|
2020-06-08 20:08:47 -04:00
|
|
|
|
|
|
|
it 'orders returned results' do
|
|
|
|
users = described_class.new(user, sort: 'id_asc').execute
|
|
|
|
|
2021-02-24 10:11:10 -05:00
|
|
|
expect(users).to eq([normal_user, admin_user, blocked_user, external_user, omniauth_user, internal_user, user])
|
2021-01-18 07:10:41 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not filter by admins' do
|
|
|
|
users = described_class.new(user, admins: true).execute
|
2021-02-24 10:11:10 -05:00
|
|
|
expect(users).to contain_exactly(user, normal_user, external_user, admin_user, blocked_user, omniauth_user, internal_user)
|
2020-06-08 20:08:47 -04:00
|
|
|
end
|
2017-05-15 09:53:12 -04:00
|
|
|
end
|
|
|
|
|
2021-01-08 10:10:26 -05:00
|
|
|
context 'with an admin user', :enable_admin_mode do
|
2017-05-15 09:53:12 -04:00
|
|
|
let(:admin) { create(:admin) }
|
|
|
|
|
|
|
|
it 'filters by external users' do
|
|
|
|
users = described_class.new(admin, external: true).execute
|
|
|
|
|
|
|
|
expect(users).to contain_exactly(external_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all users' do
|
|
|
|
users = described_class.new(admin).execute
|
|
|
|
|
2021-01-18 07:10:41 -05:00
|
|
|
expect(users).to contain_exactly(admin, normal_user, blocked_user, external_user, omniauth_user, internal_user, admin_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns only admins' do
|
|
|
|
users = described_class.new(admin, admins: true).execute
|
|
|
|
|
|
|
|
expect(users).to contain_exactly(admin, admin_user)
|
2017-05-15 09:53:12 -04:00
|
|
|
end
|
2017-09-28 12:49:42 -04:00
|
|
|
|
|
|
|
it 'filters by custom attributes' do
|
2019-03-20 05:33:24 -04:00
|
|
|
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-08-31 11:10:41 -04:00
|
|
|
create :user_custom_attribute, user: internal_user, key: 'foo', value: 'foo'
|
2017-09-28 12:49:42 -04:00
|
|
|
|
|
|
|
users = described_class.new(
|
|
|
|
admin,
|
|
|
|
custom_attributes: { foo: 'foo', bar: 'bar' }
|
|
|
|
).execute
|
|
|
|
|
2019-03-20 05:33:24 -04:00
|
|
|
expect(users).to contain_exactly(normal_user)
|
2017-09-28 12:49:42 -04:00
|
|
|
end
|
2017-05-15 09:53:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|