2017-09-10 17:25:29 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe UsersFinder do
|
|
|
|
describe '#execute' do
|
2019-05-30 16:15:17 +05:30
|
|
|
let!(:user1) { create(:user, username: 'johndoe') }
|
|
|
|
let!(:user2) { create(:user, :blocked, username: 'notsorandom') }
|
|
|
|
let!(:external_user) { create(:user, :external) }
|
|
|
|
let!(:omniauth_user) { create(:omniauth_user, provider: 'twitter', extern_uid: '123456') }
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
context 'with a normal user' do
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
|
|
|
|
it 'returns all users' do
|
|
|
|
users = described_class.new(user).execute
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
expect(users).to contain_exactly(user, user1, user2, omniauth_user)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters by username' do
|
|
|
|
users = described_class.new(user, username: 'johndoe').execute
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
expect(users).to contain_exactly(user1)
|
2017-09-10 17:25:29 +05:30
|
|
|
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-05-30 16:15:17 +05:30
|
|
|
expect(users).to contain_exactly(user1)
|
2018-12-13 13:39:08 +05:30
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
it 'filters by search' do
|
|
|
|
users = described_class.new(user, search: 'orando').execute
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
expect(users).to contain_exactly(user2)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters by blocked users' do
|
|
|
|
users = described_class.new(user, blocked: true).execute
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
expect(users).to contain_exactly(user2)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'filters by active users' do
|
|
|
|
users = described_class.new(user, active: true).execute
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
expect(users).to contain_exactly(user, user1, omniauth_user)
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns no external users' do
|
|
|
|
users = described_class.new(user, external: true).execute
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
expect(users).to contain_exactly(user, user1, user2, omniauth_user)
|
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)
|
|
|
|
|
|
|
|
users = described_class.new(user,
|
|
|
|
created_after: 2.days.ago,
|
|
|
|
created_before: Time.now + 2.days).execute
|
|
|
|
|
|
|
|
expect(users.map(&:username)).not_to include([filtered_user_before.username, filtered_user_after.username])
|
|
|
|
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
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
expect(users).to contain_exactly(user, user1, user2, omniauth_user)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'with an admin user' do
|
|
|
|
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
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
expect(users).to contain_exactly(admin, user1, user2, external_user, omniauth_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-05-30 16:15:17 +05:30
|
|
|
create :user_custom_attribute, user: user1, key: 'foo', value: 'foo'
|
|
|
|
create :user_custom_attribute, user: user1, key: 'bar', value: 'bar'
|
|
|
|
create :user_custom_attribute, user: user2, key: 'foo', value: 'foo'
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
users = described_class.new(
|
|
|
|
admin,
|
|
|
|
custom_attributes: { foo: 'foo', bar: 'bar' }
|
|
|
|
).execute
|
|
|
|
|
2019-05-30 16:15:17 +05:30
|
|
|
expect(users).to contain_exactly(user1)
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|