added error handling in create too

pull/6327/head
Markus Kahl 7 years ago
parent f2919d3cb6
commit 6126b4311a
  1. 5
      lib/api/v3/work_packages/create_work_packages.rb
  2. 57
      spec/requests/api/v3/work_packages/dependent_errors_spec.rb

@ -51,13 +51,14 @@ module API
private
def represent_create_result(result, current_user)
if result.success?
work_package = result.result
if result.success?
WorkPackages::WorkPackageRepresenter.create(work_package.reload,
current_user: current_user,
embed_links: true)
else
fail ::API::Errors::ErrorBase.create_and_merge_errors(result.errors)
handle_work_package_errors work_package, result
end
end

@ -87,8 +87,8 @@ describe 'API v3 Work package resource', type: :request, content_type: :json do
end
let(:dependent_error_result) do
proc do |instance|
result = ServiceResult.new(success: true, result: instance.work_package)
proc do |instance, _attributes, work_package|
result = ServiceResult.new(success: true, result: instance.work_package || work_package)
dep = parent
dep.errors.add :base, "invalid", message: "invalid"
@ -146,4 +146,57 @@ describe 'API v3 Work package resource', type: :request, content_type: :json do
end
end
end
describe '#post' do
let(:current_user) { FactoryGirl.create :admin }
let(:path) { api_v3_paths.work_packages }
let(:valid_params) do
{
_type: 'WorkPackage',
lockVersion: 0,
_links: {
author: { href: "/api/v3/users/#{current_user.id}" },
project: { href: "/api/v3/projects/#{project.id}" },
status: { href: "/api/v3/statuses/#{status.id}" },
priority: { href: "/api/v3/priorities/#{work_package.priority.id}" }
}
}
end
subject(:response) { last_response }
shared_context 'post request' do
before(:each) do
post path, params.to_json, 'CONTENT_TYPE' => 'application/json'
end
end
before do
allow_any_instance_of(WorkPackages::CreateService).to receive(:create, &dependent_error_result)
end
context 'request' do
let(:params) { valid_params.merge(subject: "Test Subject") }
include_context 'post request'
it { expect(response.status).to eq(422) }
it 'should respond with an error' do
expected_error = {
"_type": "Error",
"errorIdentifier": "urn:openproject-org:api:v3:errors:PropertyConstraintViolation",
"message": "Error in dependent work package ##{parent.id} #{parent.subject}: invalid",
"_embedded": {
"details": {
"attribute": "base"
}
}
}
expect(subject.body).to be_json_eql(expected_error.to_json)
end
end
end
end

Loading…
Cancel
Save