Commit 777a2726 authored by Alex Pooley's avatar Alex Pooley

Merge branch 'bw-graphql-encapsulate-error-expects-2' into 'master'

Encapsulate checking for GraphQL error (part 2)

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