From 2d16f71cd830472f9b46f23f5925b538b77bb702 Mon Sep 17 00:00:00 2001 From: Kristian Garza Date: Fri, 12 Apr 2019 18:37:57 +0200 Subject: [PATCH] validating doi name only on new dois removes unpermitted characters from DOI names when making Metadata validation fix datacite/bracco#189 when using `/dois/validate` DOI name is not being validated. Not check the DOI format when you are validating a DOI that you want to update. --- app/models/doi.rb | 2 +- spec/requests/dois_spec.rb | 57 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/app/models/doi.rb b/app/models/doi.rb index 00bde26bd..1830c1f36 100644 --- a/app/models/doi.rb +++ b/app/models/doi.rb @@ -77,7 +77,7 @@ class Doi < ActiveRecord::Base # validates_presence_of :url, if: :is_registered_or_findable? # from https://www.crossref.org/blog/dois-and-matching-regular-expressions/ but using uppercase - validates_format_of :doi, :with => /\A10\.\d{4,5}\/[-\._;()\/:a-zA-Z0-9\*~\$\=]+\z/, :on => :create + validates_format_of :doi, :with => /\A10\.\d{4,5}\/[-\._;()\/:a-zA-Z0-9\*~\$\=]+\z/, :on => :create, unless: :only_validate validates_format_of :url, :with => /\A(ftp|http|https):\/\/[\S]+/ , if: :url?, message: "URL is not valid" validates_uniqueness_of :doi, message: "This DOI has already been taken", unless: :only_validate validates :last_landing_page_status, numericality: { only_integer: true }, if: :last_landing_page_status? diff --git a/spec/requests/dois_spec.rb b/spec/requests/dois_spec.rb index a1f27eafd..14f25f473 100644 --- a/spec/requests/dois_spec.rb +++ b/spec/requests/dois_spec.rb @@ -1435,6 +1435,36 @@ end end + context 'when doi has unpermitted characters' do + let(:xml) { Base64.strict_encode64(file_fixture('datacite.xml').read) } + let(:valid_attributes) do + { + "data" => { + "type" => "dois", + "attributes" => { + "doi" => "10.14454/107+03", + "url" => "http://www.bl.uk/pdf/patspec.pdf", + "xml" => xml, + "source" => "test", + "event" => "publish" + } + } + } + end + + before { post '/dois', params: valid_attributes.to_json, headers: headers } + + it 'returns validation error' do + expect(json.dig('errors')).to eq([{"source"=>"doi", "title"=>"Is invalid"}]) + end + + + it 'returns status code 422' do + expect(response).to have_http_status(422) + end + + end + context 'creators no xml' do let(:creators) { [{ "name"=>"Ollomi, Benjamin" }, { "name"=>"Duran, Patrick" }] } let(:valid_attributes) do @@ -1600,6 +1630,33 @@ end end + context 'validates but not DOIname' do + let(:xml) { ::Base64.strict_encode64(File.read(file_fixture('datacite.xml'))) } + let(:params) do + { + "data" => { + "type" => "dois", + "attributes" => { + "doi" => "10.14454/107+03", + "xml" => xml, + } + } + } + end + + before { post '/dois/validate', params: params.to_json, headers: headers } + + it 'validates a Doi' do + expect(json.dig('data', 'attributes', 'doi')).to eq("10.14454/107+03") + expect(json.dig('data', 'attributes', 'titles')).to eq([{"title"=>"Eating your own Dog Food"}]) + expect(json.dig('data', 'attributes', 'dates')).to eq([{"date"=>"2016-12-20", "dateType"=>"Created"}, {"date"=>"2016-12-20", "dateType"=>"Issued"}, {"date"=>"2016-12-20", "dateType"=>"Updated"}]) + end + + it 'returns status code 200' do + expect(response).to have_http_status(200) + end + end + context 'validates schema 3' do let(:xml) { ::Base64.strict_encode64(File.read(file_fixture('datacite_schema_3.xml'))) } let(:params) do