Commit d228214a authored by Brett Walker's avatar Brett Walker

Encapsulate checking for GraphQL error

Helps migrate to the new GraphQL interpreter,
https://gitlab.com/gitlab-org/gitlab/-/issues/210556
parent 3e060e01
......@@ -31,16 +31,20 @@ RSpec.describe Resolvers::Admin::Analytics::UsageTrends::MeasurementsResolver do
context 'as a non-admin user' do
let(:current_user) { user }
it 'raises ResourceNotAvailable error' do
expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
it 'generates a ResourceNotAvailable error' do
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
subject
end
end
end
context 'as an unauthenticated user' do
let(:current_user) { nil }
it 'raises ResourceNotAvailable error' do
expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
it 'generates a ResourceNotAvailable error' do
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
subject
end
end
end
......
......@@ -26,8 +26,10 @@ RSpec.describe Resolvers::BlobsResolver do
subject(:resolve_blobs) { resolve(described_class, obj: repository, args: args, ctx: { current_user: user }) }
context 'when unauthorized' do
it 'raises an exception' do
expect { resolve_blobs }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
it 'generates an error' do
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
resolve_blobs
end
end
end
......
......@@ -63,10 +63,10 @@ RSpec.describe Resolvers::BoardListIssuesResolver do
expect(result).to contain_exactly(incident)
end
it 'raises an exception if both assignee_username and assignee_wildcard_id are present' do
expect do
it 'generates an error if both assignee_username and assignee_wildcard_id are present' do
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError) do
resolve_board_list_issues(args: { filters: { assignee_username: ['username'], assignee_wildcard_id: 'NONE' } })
end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
end
end
it 'accepts assignee wildcard id NONE' do
......
......@@ -74,9 +74,10 @@ RSpec.describe Resolvers::BoardListsResolver do
expect(list).to eq List.none
end
it 'raises an argument error if list ID is not valid' do
expect { resolve_board_lists(args: { id: 'test' }) }
.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
it 'generates an error if list ID is not valid' do
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError) do
resolve_board_lists(args: { id: 'test' })
end
end
end
end
......
......@@ -23,9 +23,9 @@ RSpec.describe Resolvers::BoardResolver do
end
it 'requires an ID' do
expect do
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError) do
resolve(described_class, obj: board_parent, args: {}, ctx: { current_user: user })
end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
end
end
context 'when querying for a single board' do
......
......@@ -29,8 +29,10 @@ RSpec.describe Resolvers::DesignManagement::DesignAtVersionResolver do
context 'when the user cannot see designs' do
let(:current_user) { create(:user) }
it 'raises ResourceNotAvailable' do
expect { resolve_design }.to raise_error(resource_not_available)
it 'generates ResourceNotAvailable' do
expect_graphql_error_to_be_created(resource_not_available) do
resolve_design
end
end
end
......@@ -45,8 +47,10 @@ RSpec.describe Resolvers::DesignManagement::DesignAtVersionResolver do
let(:global_id) { global_id_of(other_dav) }
it 'raises ResourceNotAvailable' do
expect { resolve_design }.to raise_error(resource_not_available)
it 'generates ResourceNotAvailable' do
expect_graphql_error_to_be_created(resource_not_available) do
resolve_design
end
end
context 'the current object does not constrain the issue' do
......
......@@ -42,16 +42,20 @@ RSpec.describe Resolvers::DesignManagement::DesignResolver do
context 'when no argument has been passed' do
let(:args) { {} }
it 'raises an error' do
expect { resolve_design }.to raise_error(::Gitlab::Graphql::Errors::ArgumentError, /must/)
it 'generates an error' do
expect_graphql_error_to_be_created(::Gitlab::Graphql::Errors::ArgumentError, /must/) do
resolve_design
end
end
end
context 'when both arguments have been passed' do
let(:args) { { filename: first_design.filename, id: GitlabSchema.id_from_object(first_design).to_s } }
it 'raises an error' do
expect { resolve_design }.to raise_error(::Gitlab::Graphql::Errors::ArgumentError, /may/)
it 'generates an error' do
expect_graphql_error_to_be_created(::Gitlab::Graphql::Errors::ArgumentError, /may/) do
resolve_design
end
end
end
......
......@@ -24,8 +24,10 @@ RSpec.describe Resolvers::DesignManagement::Version::DesignAtVersionResolver do
shared_examples 'a bad argument' do
let(:err_class) { ::Gitlab::Graphql::Errors::ArgumentError }
it 'raises an appropriate error' do
expect { resolve_objects }.to raise_error(err_class)
it 'generates an error' do
expect_graphql_error_to_be_created(err_class) do
resolve_objects
end
end
end
......
......@@ -46,10 +46,10 @@ RSpec.describe Resolvers::EnvironmentsResolver do
expect(resolve_environments(states: ['available'])).to contain_exactly(environment1, environment3)
end
it 'returns error if requested state is invalid' do
expect { resolve_environments(states: ['invalid']) }.to(
raise_error(Gitlab::Graphql::Errors::ArgumentError)
)
it 'generates an error if requested state is invalid' do
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError) do
resolve_environments(states: ['invalid'])
end
end
end
......
......@@ -27,8 +27,10 @@ RSpec.describe Resolvers::GroupLabelsResolver do
describe '#resolve' do
context 'with unauthorized user' do
it 'raises error' do
expect { resolve_labels(subgroup) }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
it 'generates an error' do
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
resolve_labels(subgroup)
end
end
end
......
......@@ -81,12 +81,14 @@ RSpec.describe Resolvers::Projects::SnippetsResolver do
end
context 'when project snippets are disabled' do
it 'raises an error' do
it 'generates an error' do
disabled_snippet_project = create(:project, :snippets_disabled)
disabled_snippet_project.add_developer(current_user)
expect(SnippetsFinder).not_to receive(:new)
expect { resolve_snippets(obj: disabled_snippet_project) }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
resolve_snippets(obj: disabled_snippet_project)
end
end
end
end
......
......@@ -108,15 +108,15 @@ RSpec.describe Resolvers::SnippetsResolver do
end.to raise_error(GraphQL::CoercionError, '"foo" is not a valid Global ID')
end
it 'returns an error if both project and author are provided' do
expect do
it 'generates an error if both project and author are provided' do
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError) do
args = {
author_id: current_user.to_global_id,
project_id: project.to_global_id
}
resolve_snippets(args: args)
end.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
end
end
end
end
......
......@@ -170,27 +170,30 @@ RSpec.describe Resolvers::TimelogResolver do
context 'when start_time and start_date are present' do
let(:args) { { start_time: short_time_ago, start_date: short_time_ago } }
it 'returns correct error' do
expect { timelogs }
.to raise_error(error_class, /Provide either a start date or time, but not both/)
it 'generates an error' do
expect_graphql_error_to_be_created(error_class, /Provide either a start date or time, but not both/) do
timelogs
end
end
end
context 'when end_time and end_date are present' do
let(:args) { { end_time: short_time_ago, end_date: short_time_ago } }
it 'returns correct error' do
expect { timelogs }
.to raise_error(error_class, /Provide either an end date or time, but not both/)
it 'generates an error' do
expect_graphql_error_to_be_created(error_class, /Provide either an end date or time, but not both/) do
timelogs
end
end
end
context 'when start argument is after end argument' do
let(:args) { { start_time: short_time_ago, end_time: medium_time_ago } }
it 'returns correct error' do
expect { timelogs }
.to raise_error(error_class, /Start argument must be before End argument/)
it 'generates an error' do
expect_graphql_error_to_be_created(error_class, /Start argument must be before End argument/) do
timelogs
end
end
end
end
......
......@@ -43,7 +43,9 @@ RSpec.describe Resolvers::UserDiscussionsCountResolver do
subject { batch_sync { resolve_user_discussions_count(private_issue) } }
it 'returns no discussions' do
expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
subject
end
end
end
end
......
......@@ -44,8 +44,10 @@ RSpec.describe Resolvers::UserNotesCountResolver do
context 'when a user does not have permission to view notes' do
subject { batch_sync { resolve_user_notes_count(private_issue) } }
it 'raises an error' do
expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
it 'generates an error' do
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
subject
end
end
end
end
......@@ -80,8 +82,10 @@ RSpec.describe Resolvers::UserNotesCountResolver do
context 'when a user does not have permission to view notes' do
subject { batch_sync { resolve_user_notes_count(private_merge_request) } }
it 'raises an error' do
expect { subject }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
it 'generates an error' do
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
subject
end
end
end
end
......
......@@ -9,15 +9,17 @@ RSpec.describe Resolvers::UserResolver do
let_it_be(:user) { create(:user) }
context 'when neither an ID or a username is provided' do
it 'raises an ArgumentError' do
expect { resolve_user }
.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
it 'generates an ArgumentError' do
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError) do
resolve_user
end
end
end
it 'raises an ArgumentError when both an ID and username are provided' do
expect { resolve_user(id: user.to_global_id, username: user.username) }
.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
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
end
context 'by username' do
......
......@@ -14,10 +14,12 @@ RSpec.describe Resolvers::UsersResolver do
end
describe '#resolve' do
it 'raises an error when read_users_list is not authorized' do
it 'generates an error when read_users_list is not authorized' do
expect(Ability).to receive(:allowed?).with(current_user, :read_users_list).and_return(false)
expect { resolve_users }.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
resolve_users
end
end
context 'when no arguments are passed' do
......@@ -27,9 +29,10 @@ RSpec.describe Resolvers::UsersResolver do
end
context 'when both ids and usernames are passed ' do
it 'raises an error' do
expect { resolve_users( args: { ids: [user1.to_global_id.to_s], usernames: [user1.username] } ) }
.to raise_error(Gitlab::Graphql::Errors::ArgumentError)
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
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment