Commit c366d8fa 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 2d5c86dd
...@@ -43,11 +43,10 @@ RSpec.describe Mutations::Boards::Issues::IssueMoveList do ...@@ -43,11 +43,10 @@ RSpec.describe Mutations::Boards::Issues::IssueMoveList do
context "when we only pass #{arg_name}" do context "when we only pass #{arg_name}" do
let(:move_params) { { arg_name => list1.id } } let(:move_params) { { arg_name => list1.id } }
it 'raises an error' do it 'generates an error' do
expect { subject }.to raise_error( expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError, 'Both fromListId and toListId must be present') do
Gitlab::Graphql::Errors::ArgumentError, subject
'Both fromListId and toListId must be present' end
)
end end
end end
end end
...@@ -55,11 +54,10 @@ RSpec.describe Mutations::Boards::Issues::IssueMoveList do ...@@ -55,11 +54,10 @@ RSpec.describe Mutations::Boards::Issues::IssueMoveList do
context 'when required arguments are missing' do context 'when required arguments are missing' do
let(:move_params) { {} } let(:move_params) { {} }
it 'raises an error' do it 'generates an error' do
expect { subject }.to raise_error( expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError, 'At least one of the arguments fromListId, toListId, afterId or beforeId is required') do
Gitlab::Graphql::Errors::ArgumentError, subject
"At least one of the arguments fromListId, toListId, afterId or beforeId is required" end
)
end end
end end
......
...@@ -22,8 +22,10 @@ RSpec.describe Mutations::Ci::Runner::Delete do ...@@ -22,8 +22,10 @@ RSpec.describe Mutations::Ci::Runner::Delete do
end end
context 'when the user cannot admin the runner' do context 'when the user cannot admin the runner' do
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
......
...@@ -26,8 +26,10 @@ RSpec.describe Mutations::Ci::Runner::Update do ...@@ -26,8 +26,10 @@ RSpec.describe Mutations::Ci::Runner::Update do
end end
context 'when the user cannot admin the runner' do context 'when the user cannot admin the runner' do
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
......
...@@ -63,7 +63,9 @@ RSpec.describe Mutations::ReleaseAssetLinks::Create do ...@@ -63,7 +63,9 @@ RSpec.describe Mutations::ReleaseAssetLinks::Create do
let!(:protected_tag) { create(:protected_tag, :maintainers_can_create, name: '*', project: project) } let!(:protected_tag) { create(:protected_tag, :maintainers_can_create, name: '*', project: project) }
it 'has an access error' do it 'has an access 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
...@@ -71,16 +73,20 @@ RSpec.describe Mutations::ReleaseAssetLinks::Create do ...@@ -71,16 +73,20 @@ RSpec.describe Mutations::ReleaseAssetLinks::Create do
context "when the user doesn't have access to the project" do context "when the user doesn't have access to the project" do
let(:current_user) { reporter } let(:current_user) { reporter }
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
context "when the project doesn't exist" do context "when the project doesn't exist" do
let(:project_path) { 'project/that/does/not/exist' } let(:project_path) { 'project/that/does/not/exist' }
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
......
...@@ -54,10 +54,8 @@ RSpec.describe Resolvers::Ci::JobTokenScopeResolver do ...@@ -54,10 +54,8 @@ RSpec.describe Resolvers::Ci::JobTokenScopeResolver do
project.add_user(current_user, :developer) project.add_user(current_user, :developer)
end end
it 'raises error' do it 'generates an error' do
expect do expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) { resolve_scope }
resolve_scope
end.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable)
end end
end end
end end
......
...@@ -101,38 +101,38 @@ RSpec.describe Resolvers::GroupMilestonesResolver do ...@@ -101,38 +101,38 @@ RSpec.describe Resolvers::GroupMilestonesResolver do
context 'by timeframe' do context 'by timeframe' do
context 'when start_date and end_date are present' do context 'when start_date and end_date are present' do
context 'when start date is after end_date' do context 'when start date is after end_date' do
it 'raises error' do it 'generates an error' do
expect do expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError, "startDate is after endDate") do
resolve_group_milestones(start_date: now, end_date: now - 2.days) resolve_group_milestones(start_date: now, end_date: now - 2.days)
end.to raise_error(Gitlab::Graphql::Errors::ArgumentError, "startDate is after endDate") end
end end
end end
end end
context 'when only start_date is present' do context 'when only start_date is present' do
it 'raises error' do it 'generates an error' do
expect do expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError, /Both startDate and endDate/) do
resolve_group_milestones(start_date: now) resolve_group_milestones(start_date: now)
end.to raise_error(Gitlab::Graphql::Errors::ArgumentError, /Both startDate and endDate/) end
end end
end end
context 'when only end_date is present' do context 'when only end_date is present' do
it 'raises error' do it 'generates an error' do
expect do expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ArgumentError, /Both startDate and endDate/) do
resolve_group_milestones(end_date: now) resolve_group_milestones(end_date: now)
end.to raise_error(Gitlab::Graphql::Errors::ArgumentError, /Both startDate and endDate/) end
end end
end end
end end
context 'when user cannot read milestones' do context 'when user cannot read milestones' do
it 'raises error' do it 'generates an error' do
unauthorized_user = create(:user) unauthorized_user = create(:user)
expect do expect_graphql_error_to_be_created(Gitlab::Graphql::Errors::ResourceNotAvailable) do
resolve_group_milestones({}, { current_user: unauthorized_user }) resolve_group_milestones({}, { current_user: unauthorized_user })
end.to raise_error(Gitlab::Graphql::Errors::ResourceNotAvailable) end
end end
end end
......
...@@ -552,6 +552,12 @@ module GraphqlHelpers ...@@ -552,6 +552,12 @@ module GraphqlHelpers
expect(flattened_errors).to be_empty expect(flattened_errors).to be_empty
end end
# Helps migrate to the new GraphQL interpreter,
# https://gitlab.com/gitlab-org/gitlab/-/issues/210556
def expect_graphql_error_to_be_created(error_class, match_message = nil)
expect { yield }.to raise_error(error_class, match_message)
end
def flattened_errors def flattened_errors
Array.wrap(graphql_errors).flatten.compact Array.wrap(graphql_errors).flatten.compact
end end
......
...@@ -29,8 +29,10 @@ RSpec.shared_examples_for 'graphql mutations security ci configuration' do ...@@ -29,8 +29,10 @@ RSpec.shared_examples_for 'graphql mutations security ci configuration' do
describe '#resolve' do describe '#resolve' do
let(:result) { subject } let(:result) { subject }
it 'raises an error if the resource is not accessible to the user' do it 'generates an error if the resource is not accessible to the user' 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
context 'when user does not have enough permissions' do context 'when user does not have enough permissions' do
...@@ -38,8 +40,10 @@ RSpec.shared_examples_for 'graphql mutations security ci configuration' do ...@@ -38,8 +40,10 @@ RSpec.shared_examples_for 'graphql mutations security ci configuration' do
project.add_guest(user) project.add_guest(user)
end end
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
...@@ -48,8 +52,10 @@ RSpec.shared_examples_for 'graphql mutations security ci configuration' do ...@@ -48,8 +52,10 @@ RSpec.shared_examples_for 'graphql mutations security ci configuration' do
create(:project_empty_repo).add_maintainer(user) create(:project_empty_repo).add_maintainer(user)
end end
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
......
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