2020-06-08 20:08:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-26 05:08:59 -04:00
|
|
|
RSpec.describe Resolvers::UsersResolver do
|
2020-06-08 20:08:47 -04:00
|
|
|
include GraphqlHelpers
|
|
|
|
|
2020-11-03 13:09:22 -05:00
|
|
|
let_it_be(:user1) { create(:user, name: "SomePerson") }
|
|
|
|
let_it_be(:user2) { create(:user, username: "someone123784") }
|
2022-02-25 19:14:54 -05:00
|
|
|
let_it_be(:current_user) { create(:user) }
|
2020-06-08 20:08:47 -04:00
|
|
|
|
2020-11-09 19:08:52 -05:00
|
|
|
specify do
|
|
|
|
expect(described_class).to have_nullable_graphql_type(Types::UserType.connection_type)
|
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
describe '#resolve' do
|
2022-03-14 05:08:52 -04:00
|
|
|
it 'generates an error when read_users_list is not authorized' do
|
2022-02-25 19:14:54 -05:00
|
|
|
expect(Ability).to receive(:allowed?).with(current_user, :read_users_list).and_return(false)
|
2020-06-08 20:08:47 -04:00
|
|
|
|
2022-03-14 05:08:52 -04:00
|
|
|
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
|
|
|
|
resolve_users
|
|
|
|
end
|
2020-06-08 20:08:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when no arguments are passed' do
|
|
|
|
it 'returns all users' do
|
2022-02-25 19:14:54 -05:00
|
|
|
expect(resolve_users).to contain_exactly(user1, user2, current_user)
|
2020-06-08 20:08:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when both ids and usernames are passed ' do
|
2022-03-14 05:08:52 -04:00
|
|
|
it 'generates an error' do
|
|
|
|
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError) do
|
|
|
|
resolve_users( args: { ids: [user1.to_global_id.to_s], usernames: [user1.username] } )
|
|
|
|
end
|
2020-06-08 20:08:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a set of IDs is passed' do
|
|
|
|
it 'returns those users' do
|
|
|
|
expect(
|
2021-01-18 07:10:41 -05:00
|
|
|
resolve_users( args: { ids: [user1.to_global_id.to_s, user2.to_global_id.to_s] } )
|
2020-06-08 20:08:47 -04:00
|
|
|
).to contain_exactly(user1, user2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when a set of usernames is passed' do
|
|
|
|
it 'returns those users' do
|
|
|
|
expect(
|
2021-01-18 07:10:41 -05:00
|
|
|
resolve_users( args: { usernames: [user1.username, user2.username] } )
|
2020-06-08 20:08:47 -04:00
|
|
|
).to contain_exactly(user1, user2)
|
|
|
|
end
|
|
|
|
end
|
2020-11-03 13:09:22 -05:00
|
|
|
|
2021-01-18 07:10:41 -05:00
|
|
|
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
|
|
|
|
|
2020-11-03 13:09:22 -05:00
|
|
|
context 'when a search term is passed' do
|
|
|
|
it 'returns all users who match', :aggregate_failures do
|
2021-01-18 07:10:41 -05:00
|
|
|
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)
|
2020-11-03 13:09:22 -05:00
|
|
|
end
|
|
|
|
end
|
2022-02-25 19:14:54 -05:00
|
|
|
|
|
|
|
context 'with anonymous access' do
|
|
|
|
let_it_be(:current_user) { nil }
|
|
|
|
|
|
|
|
it 'prohibits search without usernames passed' do
|
|
|
|
expect { resolve_users }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'allows to search by username' do
|
|
|
|
expect(resolve_users(args: { usernames: [user1.username] })).to contain_exactly(user1)
|
|
|
|
end
|
|
|
|
end
|
2020-06-08 20:08:47 -04:00
|
|
|
end
|
|
|
|
|
2021-01-18 07:10:41 -05:00
|
|
|
def resolve_users(args: {}, ctx: {})
|
2022-02-25 19:14:54 -05:00
|
|
|
resolve(described_class, args: args, ctx: { current_user: current_user }.merge(ctx))
|
2020-06-08 20:08:47 -04:00
|
|
|
end
|
|
|
|
end
|