debian-mirror-gitlab/spec/graphql/resolvers/users_resolver_spec.rb

90 lines
2.9 KiB
Ruby
Raw Normal View History

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") }
2022-02-27 12:50:16 +05:30
let_it_be(:current_user) { create(:user) }
2021-01-29 00:20:46 +05:30
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
2022-05-07 20:08:51 +05:30
it 'generates an error when read_users_list is not authorized' do
2022-02-27 12:50:16 +05:30
expect(Ability).to receive(:allowed?).with(current_user, :read_users_list).and_return(false)
2020-06-23 00:09:42 +05:30
2022-05-07 20:08:51 +05:30
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
resolve_users
end
2020-06-23 00:09:42 +05:30
end
context 'when no arguments are passed' do
it 'returns all users' do
2022-02-27 12:50:16 +05:30
expect(resolve_users).to contain_exactly(user1, user2, current_user)
2020-06-23 00:09:42 +05:30
end
end
context 'when both ids and usernames are passed ' do
2022-05-07 20:08:51 +05:30
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-23 00:09:42 +05:30
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
2022-02-27 12:50:16 +05:30
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-23 00:09:42 +05:30
end
2021-03-08 18:12:59 +05:30
def resolve_users(args: {}, ctx: {})
2022-02-27 12:50:16 +05:30
resolve(described_class, args: args, ctx: { current_user: current_user }.merge(ctx))
2020-06-23 00:09:42 +05:30
end
end