2020-06-23 00:09:42 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Resolvers::UsersResolver do
|
2020-06-23 00:09:42 +05:30
|
|
|
include GraphqlHelpers
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
let_it_be(:user1) { create(:user, name: "SomePerson") }
|
|
|
|
let_it_be(:user2) { create(:user, username: "someone123784") }
|
|
|
|
|
|
|
|
specify do
|
|
|
|
expect(described_class).to have_nullable_graphql_type(Types::UserType.connection_type)
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
describe '#resolve' do
|
|
|
|
it 'raises an error when read_users_list is not authorized' do
|
|
|
|
expect(Ability).to receive(:allowed?).with(nil, :read_users_list).and_return(false)
|
|
|
|
|
|
|
|
expect { resolve_users }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no arguments are passed' do
|
|
|
|
it 'returns all users' do
|
|
|
|
expect(resolve_users).to contain_exactly(user1, user2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when both ids and usernames are passed ' do
|
|
|
|
it 'raises an error' do
|
2021-03-08 18:12:59 +05:30
|
|
|
expect { resolve_users( args: { ids: [user1.to_global_id.to_s], usernames: [user1.username] } ) }
|
2020-06-23 00:09:42 +05:30
|
|
|
.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a set of IDs is passed' do
|
|
|
|
it 'returns those users' do
|
|
|
|
expect(
|
2021-03-08 18:12:59 +05:30
|
|
|
resolve_users( args: { ids: [user1.to_global_id.to_s, user2.to_global_id.to_s] } )
|
2020-06-23 00:09:42 +05:30
|
|
|
).to contain_exactly(user1, user2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a set of usernames is passed' do
|
|
|
|
it 'returns those users' do
|
|
|
|
expect(
|
2021-03-08 18:12:59 +05:30
|
|
|
resolve_users( args: { usernames: [user1.username, user2.username] } )
|
2020-06-23 00:09:42 +05:30
|
|
|
).to contain_exactly(user1, user2)
|
|
|
|
end
|
|
|
|
end
|
2021-01-29 00:20:46 +05:30
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
context 'when admins is true', :enable_admin_mode do
|
|
|
|
let(:admin_user) { create(:user, :admin) }
|
|
|
|
|
|
|
|
it 'returns only admins' do
|
|
|
|
expect(
|
|
|
|
resolve_users( args: { admins: true }, ctx: { current_user: admin_user } )
|
|
|
|
).to contain_exactly(admin_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
context 'when a search term is passed' do
|
|
|
|
it 'returns all users who match', :aggregate_failures do
|
2021-03-08 18:12:59 +05:30
|
|
|
expect(resolve_users( args: { search: "some" } )).to contain_exactly(user1, user2)
|
|
|
|
expect(resolve_users( args: { search: "123784" } )).to contain_exactly(user2)
|
|
|
|
expect(resolve_users( args: { search: "someperson" } )).to contain_exactly(user1)
|
2021-01-29 00:20:46 +05:30
|
|
|
end
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
def resolve_users(args: {}, ctx: {})
|
|
|
|
resolve(described_class, args: args, ctx: ctx)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
|
|
|
end
|