Commit ef37e090 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre

Merge branch 'improve-lists-update-errors' into 'master'

Improve errors reporting for board lists update

See merge request gitlab-org/gitlab!59549
parents e72e67d1 94258b72
...@@ -33,10 +33,10 @@ module Boards ...@@ -33,10 +33,10 @@ module Boards
service = Boards::Lists::UpdateService.new(board_parent, current_user, update_list_params) service = Boards::Lists::UpdateService.new(board_parent, current_user, update_list_params)
result = service.execute(list) result = service.execute(list)
if result[:status] == :success if result.success?
head :ok head :ok
else else
head result[:http_status] head result.http_status
end end
end end
......
...@@ -20,8 +20,8 @@ module Mutations ...@@ -20,8 +20,8 @@ module Mutations
update_result = update_list(list, args) update_result = update_list(list, args)
{ {
list: update_result[:list], list: update_result.payload[:list],
errors: list.errors.full_messages errors: update_result.errors
} }
end end
......
...@@ -3,16 +3,30 @@ ...@@ -3,16 +3,30 @@
module Boards module Boards
module Lists module Lists
class BaseUpdateService < Boards::BaseService class BaseUpdateService < Boards::BaseService
extend ::Gitlab::Utils::Override
def execute(list) def execute(list)
if execute_by_params(list) if execute_by_params(list)
success(list: list) success(list: list)
else else
error(list.errors.messages, 422) message = list.errors.empty? ? 'The update was not successful.' : list.errors.messages
error(message, { list: list })
end end
end end
private private
override :error
def error(message, pass_back = {})
ServiceResponse.error(message: message, http_status: :unprocessable_entity, payload: pass_back)
end
override :success
def success(pass_back = {})
ServiceResponse.success(payload: pass_back)
end
def execute_by_params(list) def execute_by_params(list)
update_preferences_result = update_preferences(list) if can_read?(list) update_preferences_result = update_preferences(list) if can_read?(list)
update_position_result = update_position(list) if can_admin?(list) update_position_result = update_position(list) if can_admin?(list)
......
---
title: Improve errors reporting for board lists update
merge_request: 59549
author:
type: added
...@@ -46,8 +46,8 @@ module Mutations ...@@ -46,8 +46,8 @@ module Mutations
update_result = update_list(args) update_result = update_list(args)
{ {
list: update_result[:list], list: update_result.payload.fetch(:list),
errors: list.errors.full_messages errors: update_result.errors
} }
end end
......
...@@ -38,6 +38,10 @@ RSpec.describe Mutations::Boards::Lists::UpdateLimitMetrics do ...@@ -38,6 +38,10 @@ RSpec.describe Mutations::Boards::Lists::UpdateLimitMetrics do
expect(reloaded_list.max_issue_count).to eq(10) expect(reloaded_list.max_issue_count).to eq(10)
expect(reloaded_list.max_issue_weight).to eq(50) expect(reloaded_list.max_issue_weight).to eq(50)
end end
it 'returns the correct response' do
expect(subject.keys).to match_array([:list, :errors])
end
end end
context 'without admin rights' do context 'without admin rights' do
......
...@@ -6,7 +6,8 @@ RSpec.describe Boards::EpicLists::UpdateService do ...@@ -6,7 +6,8 @@ RSpec.describe Boards::EpicLists::UpdateService do
let_it_be(:user) { create(:user) } let_it_be(:user) { create(:user) }
let_it_be(:group) { create(:group, :private) } let_it_be(:group) { create(:group, :private) }
let_it_be(:board) { create(:epic_board, group: group) } let_it_be(:board) { create(:epic_board, group: group) }
let_it_be(:list) { create(:epic_list, epic_board: board, position: 0) } let_it_be_with_reload(:list) { create(:epic_list, epic_board: board, position: 0) }
let_it_be_with_reload(:list2) { create(:epic_list, epic_board: board, position: 1) }
before do before do
stub_licensed_features(epics: true) stub_licensed_features(epics: true)
......
...@@ -36,8 +36,8 @@ RSpec.describe 'EE::Boards::Lists::UpdateService' do ...@@ -36,8 +36,8 @@ RSpec.describe 'EE::Boards::Lists::UpdateService' do
service = Boards::Lists::UpdateService.new(board, user, { limit_metric: 'foo' }) service = Boards::Lists::UpdateService.new(board, user, { limit_metric: 'foo' })
result = service.execute(list) result = service.execute(list)
expect(result[:http_status]).to eq(422) expect(result.http_status).to eq(:unprocessable_entity)
expect(result[:status]).to eq(:error) expect(result.status).to eq(:error)
reloaded_list = list.reload reloaded_list = list.reload
expect(reloaded_list.limit_metric).to be_nil expect(reloaded_list.limit_metric).to be_nil
...@@ -200,7 +200,7 @@ RSpec.describe 'EE::Boards::Lists::UpdateService' do ...@@ -200,7 +200,7 @@ RSpec.describe 'EE::Boards::Lists::UpdateService' do
service = Boards::Lists::UpdateService.new(board, user, initialization_params) service = Boards::Lists::UpdateService.new(board, user, initialization_params)
result = service.execute(list) result = service.execute(list)
expect(result[:status]).to eq(expected_service_result) expect(result.status).to eq(expected_service_result)
reloaded_list = list.reload reloaded_list = list.reload
......
...@@ -11,6 +11,7 @@ RSpec.describe 'Update of an existing board list' do ...@@ -11,6 +11,7 @@ RSpec.describe 'Update of an existing board list' do
let_it_be(:list) { create(:list, board: board, position: 0) } let_it_be(:list) { create(:list, board: board, position: 0) }
let_it_be(:list2) { create(:list, board: board) } let_it_be(:list2) { create(:list, board: board) }
let_it_be(:input) { { list_id: list.to_global_id.to_s, position: 1, collapsed: true } } let_it_be(:input) { { list_id: list.to_global_id.to_s, position: 1, collapsed: true } }
let(:mutation) { graphql_mutation(:update_board_list, input) } let(:mutation) { graphql_mutation(:update_board_list, input) }
let(:mutation_response) { graphql_mutation_response(:update_board_list) } let(:mutation_response) { graphql_mutation_response(:update_board_list) }
......
...@@ -3,8 +3,10 @@ ...@@ -3,8 +3,10 @@
require 'spec_helper' require 'spec_helper'
RSpec.describe Boards::Lists::UpdateService do RSpec.describe Boards::Lists::UpdateService do
let(:user) { create(:user) } let_it_be(:user) { create(:user) }
let!(:list) { create(:list, board: board, position: 0) } let!(:list) { create(:list, board: board, position: 0) }
let!(:list2) { create(:list, board: board, position: 1) }
describe '#execute' do describe '#execute' do
let(:service) { described_class.new(board.resource_parent, user, params) } let(:service) { described_class.new(board.resource_parent, user, params) }
......
...@@ -2,15 +2,31 @@ ...@@ -2,15 +2,31 @@
RSpec.shared_examples 'moving list' do RSpec.shared_examples 'moving list' do
context 'when user can admin list' do context 'when user can admin list' do
it 'calls Lists::MoveService to update list position' do before do
board.resource_parent.add_developer(user) board.resource_parent.add_developer(user)
end
context 'when the new position is valid' do
it 'calls Lists::MoveService to update list position' do
expect_next_instance_of(Boards::Lists::MoveService, board.resource_parent, user, params) do |move_service| expect_next_instance_of(Boards::Lists::MoveService, board.resource_parent, user, params) do |move_service|
expect(move_service).to receive(:execute).with(list).and_call_original expect(move_service).to receive(:execute).with(list).and_call_original
end end
service.execute(list) service.execute(list)
end end
it 'returns a success response' do
expect(service.execute(list)).to be_success
end
end
context 'when the new position is invalid' do
let(:params) { { position: 10 } }
it 'returns error response' do
expect(service.execute(list)).to be_error
end
end
end end
context 'when user cannot admin list' do context 'when user cannot admin list' do
...@@ -19,6 +35,10 @@ RSpec.shared_examples 'moving list' do ...@@ -19,6 +35,10 @@ RSpec.shared_examples 'moving list' do
service.execute(list) service.execute(list)
end end
it 'returns an error response' do
expect(service.execute(list)).to be_error
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