Commit a9c56677 authored by Stan Hu's avatar Stan Hu

Merge branch 'ajk-globalid-boards' into 'master'

[GraphQL] Boards: use Global-ID scalar

See merge request gitlab-org/gitlab!36113
parents 702ca824 1fb4c850
...@@ -73,22 +73,12 @@ module Mutations ...@@ -73,22 +73,12 @@ module Mutations
end end
def find_list_by_global_id(gid) def find_list_by_global_id(gid)
parsed_gid = GlobalID.parse(gid) return unless gid
id = parsed_gid.model_id.to_i if list_accessible?(parsed_gid)
return unless id
List.find_by_id(id) # TODO: remove this line when the compatibility layer is removed
end # See: https://gitlab.com/gitlab-org/gitlab/-/issues/257883
gid = ::Types::GlobalIDType[::List].coerce_isolated_input(gid)
def list_accessible?(gid) List.find_by_id(gid.model_id)
gid.app == GlobalID.app && list?(gid)
end
def list?(gid)
model_class = gid&.model_class
return unless model_class
model_class <= List
end end
def board def board
......
...@@ -18,7 +18,7 @@ RSpec.describe Mutations::Boards::Lists::UpdateLimitMetrics do ...@@ -18,7 +18,7 @@ RSpec.describe Mutations::Boards::Lists::UpdateLimitMetrics do
group.add_guest(guest) group.add_guest(guest)
end end
subject { mutation.resolve(list_id: list.to_global_id.to_s, **list_update_params) } subject { mutation.resolve(list_id: list.to_global_id, **list_update_params) }
describe '#ready?' do describe '#ready?' do
it 'raises an error if required arguments are missing' do it 'raises an error if required arguments are missing' do
......
...@@ -9,6 +9,8 @@ RSpec.describe 'Update list limit metrics' do ...@@ -9,6 +9,8 @@ RSpec.describe 'Update list limit metrics' do
let_it_be(:board) { create(:board, group: group) } let_it_be(:board) { create(:board, group: group) }
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:list) { create(:list, board: board) } let_it_be(:list) { create(:list, board: board) }
let_it_be(:forbidden_user) { create(:user) }
let(:current_user) { user }
let(:mutation_class) { Mutations::Boards::Lists::UpdateLimitMetrics } let(:mutation_class) { Mutations::Boards::Lists::UpdateLimitMetrics }
let(:mutation_name) { mutation_class.graphql_name } let(:mutation_name) { mutation_class.graphql_name }
...@@ -18,39 +20,48 @@ RSpec.describe 'Update list limit metrics' do ...@@ -18,39 +20,48 @@ RSpec.describe 'Update list limit metrics' do
group.add_maintainer(user) group.add_maintainer(user)
end end
it 'returns an error if the user is not allowed to update the issue' do context 'the current_user is not allowed to update the issue' do
post_graphql_mutation(mutation('all_metrics'), current_user: create(:user)) let(:current_user) { forbidden_user }
expect(graphql_errors).to include(a_hash_including('message' => "The resource that you are attempting to access does " \ it 'returns an error' do
"not exist or you don't have permission to perform this action")) perform_mutation(build_mutation('all_metrics'))
expect(graphql_errors).to include(a_hash_including('message' => "The resource that you are attempting to access does " \
"not exist or you don't have permission to perform this action"))
end
end end
it 'returns an error if the list cannot be found' do it 'returns an error if the list cannot be found' do
list_gid = ::URI::GID.parse("gid://#{GlobalID.app}/List/0") list_gid = GlobalID.new(::Gitlab::GlobalId.build(model_name: 'List', id: 0))
post_graphql_mutation(mutation('all_metrics', list_id: list_gid), current_user: create(:user)) perform_mutation(build_mutation('all_metrics', list_id: list_gid.to_s))
expect(graphql_errors).to include(a_hash_including('message' => "The resource that you are attempting to access does " \ expect(graphql_errors).to include(a_hash_including('message' => "The resource that you are attempting to access does " \
"not exist or you don't have permission to perform this action")) "not exist or you don't have permission to perform this action"))
end end
it 'returns an error if the gid identifies another object' do context 'the list_id is not a valid ListID' do
post_graphql_mutation(mutation('all_metrics', list_id: user.to_global_id.to_s), current_user: create(:user)) let(:mutation) { build_mutation('all_metrics', list_id: user.to_global_id.to_s) }
expect(graphql_errors).to include(a_hash_including('message' => /"#{GitlabSchema.id_from_object(user)}" does not represent an instance of List/)) it_behaves_like 'an invalid argument to the mutation', argument_name: :list_id
end end
%w[all_metrics issue_count issue_weights].each do |metric| %w[all_metrics issue_count issue_weights].each do |metric|
it "updates the list limit metrics for limit metric #{metric}" do it "updates the list limit metrics for limit metric #{metric}" do
post_graphql_mutation(mutation(metric), current_user: user) perform_mutation(build_mutation(metric))
expect(response).to have_gitlab_http_status(:success) expect(response).to have_gitlab_http_status(:success)
response_list = json_response['data'][mutation_result_identifier]['list'] expect(graphql_data).to include(
expect(response_list['id']).to eq(list.to_global_id.to_s) mutation_result_identifier => include(
expect(response_list['limitMetric']).to eq(metric.to_s) 'list' => include(
expect(response_list['maxIssueCount']).to eq(3) 'id' => eq(list.to_global_id.to_s),
expect(response_list['maxIssueWeight']).to eq(42) 'limitMetric' => eq(metric.to_s),
'maxIssueCount' => eq(3),
'maxIssueWeight' => eq(42)
)
)
)
expect_list_update(list, metric: metric, count: 3, weight: 42) expect_list_update(list, metric: metric, count: 3, weight: 42)
end end
...@@ -65,7 +76,11 @@ RSpec.describe 'Update list limit metrics' do ...@@ -65,7 +76,11 @@ RSpec.describe 'Update list limit metrics' do
} }
end end
def mutation(metric, additional_params = {}) def perform_mutation(mutation)
post_graphql_mutation(mutation, current_user: current_user)
end
def build_mutation(metric, additional_params = {})
graphql_mutation(mutation_name, list_update_params(metric).merge(additional_params), graphql_mutation(mutation_name, list_update_params(metric).merge(additional_params),
<<-QL.strip_heredoc <<-QL.strip_heredoc
clientMutationId clientMutationId
......
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