Commit 114230e3 authored by Alex Kalderimis's avatar Alex Kalderimis

Improvements to dast site GQL resolvers

This makes the following changes:

- uses more direct project resolution in mutations
- catch errors appropriately in mutations
- test clean-up

This was work done as part of
https://gitlab.com/gitlab-org/gitlab/-/merge_requests/40088
which introduces stricter
requirements on resolvers, but it can be cleanly extracted.
parent 83c4d68b
......@@ -3,8 +3,6 @@
module Mutations
module DastSiteProfiles
class Delete < BaseMutation
include AuthorizesProject
graphql_name 'DastSiteProfileDelete'
argument :full_path, GraphQL::ID_TYPE,
......@@ -18,7 +16,7 @@ module Mutations
authorize :create_on_demand_dast_scan
def resolve(full_path:, id:)
project = authorized_find_project!(full_path: full_path)
project = authorized_find!(full_path)
# TODO: remove explicit coercion once compatibility layer is removed
# See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
id = ::Types::GlobalIDType[::DastSiteProfile].coerce_isolated_input(id)
......@@ -28,10 +26,16 @@ module Mutations
return { errors: dast_site_profile.errors.full_messages } unless dast_site_profile.destroy
{ errors: [] }
rescue ActiveRecord::RecordNotFound
raise_resource_not_available_error!
end
private
def find_object(full_path)
Project.find_by_full_path(full_path)
end
def find_dast_site_profile(project:, global_id:)
project.dast_site_profiles.find(global_id.model_id)
rescue ActiveRecord::RecordNotFound
......
......@@ -5,14 +5,12 @@ require 'spec_helper'
RSpec.describe Resolvers::DastSiteProfileResolver do
include GraphqlHelpers
let_it_be(:current_user) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:developer) { create(:user, developer_projects: [project] ) }
let_it_be(:dast_site_profile1) { create(:dast_site_profile, project: project) }
let_it_be(:dast_site_profile2) { create(:dast_site_profile, project: project) }
before do
project.add_maintainer(current_user)
end
let(:current_user) { developer }
specify do
expect(described_class).to have_nullable_graphql_type(Types::DastSiteProfileType.connection_type)
......@@ -21,7 +19,7 @@ RSpec.describe Resolvers::DastSiteProfileResolver do
context 'when resolving a single DAST site profile' do
subject { sync(single_dast_site_profile(id: dast_site_profile1.to_global_id)) }
it { is_expected.to contain_exactly(dast_site_profile1) }
it { is_expected.to eq dast_site_profile1 }
end
context 'when resolving multiple DAST site profiles' do
......@@ -32,11 +30,11 @@ RSpec.describe Resolvers::DastSiteProfileResolver do
private
def dast_site_profiles(args = {}, context = { current_user: current_user })
resolve(described_class, obj: project, args: args, ctx: context)
def dast_site_profiles
resolve(described_class, obj: project, ctx: { current_user: current_user })
end
def single_dast_site_profile(args = {}, context = { current_user: current_user })
resolve(described_class, obj: project, args: args, ctx: context)
def single_dast_site_profile(**args)
resolve(described_class.single, obj: project, args: args, ctx: { current_user: current_user })
end
end
......@@ -17,17 +17,17 @@ RSpec.describe Resolvers::DastSiteValidationResolver do
before do
project.add_maintainer(current_user)
stub_licensed_features(security_on_demand_scans: true)
end
specify do
expect(described_class).to have_nullable_graphql_type(Types::DastSiteValidationType.connection_type)
end
subject { sync(resolver) }
context 'when resolving multiple DAST site validations' do
subject { dast_site_validations(**args) }
let(:args) { {} }
let(:resolver) { dast_site_validations(args) }
it { is_expected.to contain_exactly(dast_site_validation3, dast_site_validation2, dast_site_validation1) }
......@@ -52,7 +52,8 @@ RSpec.describe Resolvers::DastSiteValidationResolver do
private
def dast_site_validations(args = {}, context = { current_user: current_user })
def dast_site_validations(**args)
context = { current_user: current_user }
resolve(described_class, obj: project, args: args, ctx: context)
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