2020-05-28 11:08:02 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe Resolvers::UserResolver do
|
2020-05-28 11:08:02 -04:00
|
|
|
include GraphqlHelpers
|
|
|
|
|
|
|
|
describe '#resolve' do
|
2022-06-03 11:09:26 -04:00
|
|
|
let_it_be(:current_user) { nil }
|
2020-05-28 11:08:02 -04:00
|
|
|
let_it_be(:user) { create(:user) }
|
|
|
|
|
2022-06-03 11:09:26 -04:00
|
|
|
shared_examples 'queries user' do
|
|
|
|
context 'authenticated access' do
|
|
|
|
let_it_be(:current_user) { create(:user) }
|
|
|
|
|
|
|
|
it 'returns the correct user' do
|
|
|
|
expect(
|
|
|
|
resolve_user(args)
|
|
|
|
).to eq(user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'unauthenticated access' do
|
|
|
|
it 'forbids search' do
|
|
|
|
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
|
|
|
|
resolve_user(args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-28 11:08:02 -04:00
|
|
|
context 'when neither an ID or a username is provided' do
|
2022-03-14 05:08:52 -04:00
|
|
|
it 'generates an ArgumentError' do
|
|
|
|
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError) do
|
|
|
|
resolve_user
|
|
|
|
end
|
2020-05-28 11:08:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-14 05:08:52 -04:00
|
|
|
it 'generates an ArgumentError when both an ID and username are provided' do
|
|
|
|
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError) do
|
|
|
|
resolve_user(id: user.to_global_id, username: user.username)
|
|
|
|
end
|
2020-05-28 11:08:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'by username' do
|
2022-06-03 11:09:26 -04:00
|
|
|
include_examples "queries user" do
|
|
|
|
let(:args) { { username: user.username } }
|
2020-05-28 11:08:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'by ID' do
|
2022-06-03 11:09:26 -04:00
|
|
|
include_examples "queries user" do
|
|
|
|
let(:args) { { id: user.to_global_id } }
|
2020-05-28 11:08:02 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-06-03 11:09:26 -04:00
|
|
|
def resolve_user(args = {}, context = { current_user: current_user })
|
|
|
|
sync(resolve(described_class, args: args, ctx: context))
|
2020-05-28 11:08:02 -04:00
|
|
|
end
|
|
|
|
end
|