From 2ff7784df39bf34377823c0470468a356cc3105c Mon Sep 17 00:00:00 2001 From: Martin Fenner Date: Tue, 8 Sep 2020 16:56:27 +0200 Subject: [PATCH] validate contributor roles. datacite/bracco#454 --- app/models/doi.rb | 3 ++ spec/requests/datacite_dois_spec.rb | 47 +++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/app/models/doi.rb b/app/models/doi.rb index 776e5a480..9e5f71f9e 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -1825,6 +1825,9 @@ def check_creators def check_contributors Array.wrap(contributors).each do |c| errors.add(:contributors, "Contributor '#{c}' should be an object instead of a string.") unless c.is_a?(Hash) + if schema_version == "http://datacite.org/schema/kernel-4" + errors.add(:contributors, "Contributor type #{c["contributorType"]} is not supported in schema 4.") unless %w(ContactPerson DataCollector DataCurator DataManager Distributor Editor HostingInstitution Other Producer ProjectLeader ProjectManager ProjectMember RegistrationAgency RegistrationAuthority RelatedPerson ResearchGroup RightsHolder Researcher Sponsor Supervisor WorkPackageLeader).include?(c["contributorType"]) + end end end diff --git a/spec/requests/datacite_dois_spec.rb b/spec/requests/datacite_dois_spec.rb index 7324a8759..08a792a8d 100644 --- a/spec/requests/datacite_dois_spec.rb +++ b/spec/requests/datacite_dois_spec.rb @@ -2148,6 +2148,32 @@ end end + context 'when the request creates schema 3 with funder contributor type' do + let(:xml) { Base64.strict_encode64(file_fixture('datacite_schema_3.xml').read) } + let(:valid_attributes) do + { + "data" => { + "type" => "dois", + "attributes" => { + "doi" => "10.14454/10703", + "url" => "http://www.bl.uk/pdf/patspec.pdf", + "xml" => xml, + "contributors" => [{"contributorType"=>"Funder", "name"=>"Wellcome Trust", "nameType"=>"Organizational"}], + "source" => "test", + "event" => "publish" + } + } + } + end + + it 'creates a Doi' do + post '/dois', valid_attributes, headers + + expect(last_response.status).to eq(422) + expect(json.fetch('errors', nil)).to eq([{"source"=>"xml", "title"=>"Schema http://datacite.org/schema/kernel-2.2 is no longer supported"}]) + end + end + context 'when the request has wrong object in nameIDentifiers' do let(:valid_attributes) { JSON.parse(file_fixture('datacite_wrong_nameIdentifiers.json').read) } @@ -2990,6 +3016,27 @@ end end + # Funder has been removed as valid contributor type in schema 4.0 + context 'update contributor type with funder', elasticsearch: true do + let(:update_attributes) do + { + "data" => { + "type" => "dois", + "attributes" => { + "contributors" => [{"contributorType"=>"Funder", "name"=>"Wellcome Trust", "nameType"=>"Organizational"}], + } + } + } + end + + it 'updates the Doi' do + patch "/dois/#{doi.doi}", update_attributes, headers + puts last_response.body + expect(last_response.status).to eq(422) + expect(json["errors"]).to eq([{"source"=>"contributors", "title"=>"Contributor type Funder is not supported in schema 4."}]) + end + end + context 'update rightsList', elasticsearch: true do let(:rights_list) { [{ "rightsIdentifier"=>"CC0-1.0" }] } let(:update_attributes) do