Skip to content

Commit

Permalink
register in handle system as part of the api call. datacite/datacite#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fenner committed Oct 12, 2020
1 parent 0cae0e5 commit 42f5e4d
Show file tree
Hide file tree
Showing 52 changed files with 2,280 additions and 210 deletions.
3 changes: 2 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def authenticated_user
when "ActionController::UnknownFormat" then 406
when "ActiveRecord::RecordNotUnique" then 409
when "ActiveModel::ForbiddenAttributesError", "ActionController::ParameterMissing", "ActionController::UnpermittedParameters", "ActiveModelSerializers::Adapter::JsonApi::Deserialization::InvalidDocument" then 422
when "ActionController::BadRequest" then 400
when "SocketError" then 500
else 400
end
Expand All @@ -121,7 +122,7 @@ def authenticated_user
message = "The content type is not recognized."
elsif status == 409
message = "The resource already exists."
elsif ["JSON::ParserError", "Nokogiri::XML::SyntaxError", "ActionDispatch::Http::Parameters::ParseError"].include?(exception.class.to_s)
elsif ["JSON::ParserError", "Nokogiri::XML::SyntaxError", "ActionDispatch::Http::Parameters::ParseError", "ActionController::BadRequest"].include?(exception.class.to_s)
message = exception.message
else
Raven.capture_exception(exception)
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/datacite_dois_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,10 @@ def safe_params
p[:subjects], p[:contentUrl], p[:schemaVersion]].compact

# generate random DOI if no DOI is provided
if p[:doi].blank? && p[:prefix].present?
# make random DOI predictable in test
if p[:doi].blank? && p[:prefix].present? && Rails.env.test?
p[:doi] = generate_random_dois(p[:prefix], number: 123456).first
elsif p[:doi].blank? && p[:prefix].present?
p[:doi] = generate_random_dois(p[:prefix]).first
end

Expand Down
11 changes: 4 additions & 7 deletions app/models/concerns/helpable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,19 @@ module Helpable

def register_url
unless url.present?
Rails.logger.error "[Handle] Error updating DOI " + doi + ": url missing."
return OpenStruct.new(body: { "errors" => [{ "title" => "URL missing." }] })
raise ActionController::BadRequest.new(), "[Handle] Error updating DOI " + doi + ": url missing."
end

unless client_id.present?
Rails.logger.error "[Handle] Error updating DOI " + doi + ": client ID missing."
return OpenStruct.new(body: { "errors" => [{ "title" => "Client ID missing." }] })
raise ActionController::BadRequest.new(), "[Handle] Error updating DOI " + doi + ": client ID missing."
end

unless match_url_with_domains(url: url, domains: client.domains)
Rails.logger.error "[Handle] Error updating DOI " + doi + ": URL not allowed by client domains settings."
return OpenStruct.new(body: { "errors" => [{ "title" => "URL not allowed by client domains settings." }] })
raise ActionController::BadRequest.new(), "[Handle] Error updating DOI " + doi + ": URL not allowed by client domains settings."
end

unless is_registered_or_findable?
return OpenStruct.new(body: { "errors" => [{ "title" => "DOI is not registered or findable." }] })
raise ActionController::BadRequest.new(), "DOI is not registered or findable."
end

payload = [
Expand Down
8 changes: 4 additions & 4 deletions app/models/doi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class Doi < ActiveRecord::Base
validate :check_geo_locations, if: :geo_locations?
validate :check_language, if: :language?

after_commit :update_url, on: [:create, :update]
# after_commit :update_url, on: [:create, :update]
after_commit :update_media, on: [:create, :update]

before_validation :update_xml, if: :regenerate
Expand All @@ -132,7 +132,7 @@ class Doi < ActiveRecord::Base
before_validation :update_rights_list, if: :rights_list?
before_validation :update_identifiers
before_validation :update_types
before_save :set_defaults, :save_metadata
before_save :set_defaults, :save_metadata, :update_url
before_create { self.created = Time.zone.now.utc.iso8601 }

scope :q, ->(query) { where("dataset.doi = ?", query) }
Expand Down Expand Up @@ -1773,8 +1773,8 @@ def update_url

if %w(europ).include?(provider_id) || type == "OtherDoi"
UrlJob.perform_later(doi)
else
HandleJob.perform_later(doi)
elsif url_changed?
register_url
end
end

Expand Down
1 change: 1 addition & 0 deletions config/initializers/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class IdentifierError < RuntimeError; end
ActiveRecord::RecordNotUnique,
ActiveRecord::RecordNotFound,
AbstractController::ActionNotFound,
ActionController::BadRequest,
ActionController::UnknownFormat,
ActionController::RoutingError,
ActionController::ParameterMissing,
Expand Down
11 changes: 6 additions & 5 deletions spec/concerns/helpable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,34 +167,35 @@
it 'wrong domain' do
client = create(:client, provider: provider, symbol: ENV['MDS_USERNAME'], password: ENV['MDS_PASSWORD'], domains: "example.org")
subject = build(:doi, doi: "10.5438/mcnv-ga6n", url: "https://blog.datacite.org/", client: client, aasm_state: "findable")
expect(subject.register_url.body).to eq("errors"=>[{"title"=>"URL not allowed by client domains settings."}])
expect { subject.register_url }.to raise_error(ActionController::BadRequest, "[Handle] Error updating DOI 10.5438/MCNV-GA6N: URL not allowed by client domains settings.")
end

it 'wrong subdomain' do
client = create(:client, provider: provider, symbol: ENV['MDS_USERNAME'], password: ENV['MDS_PASSWORD'], domains: "datacite.org")
subject = build(:doi, doi: "10.5438/mcnv-ga6n", url: "https://blog.datacite.org/", client: client, aasm_state: "findable")
expect(subject.register_url.body).to eq("errors"=>[{"title"=>"URL not allowed by client domains settings."}])
expect { subject.register_url }.to raise_error(ActionController::BadRequest, "[Handle] Error updating DOI 10.5438/MCNV-GA6N: URL not allowed by client domains settings.")
end

it 'wildcard for subdomain but using naked domain' do
client = create(:client, provider: provider, symbol: ENV['MDS_USERNAME'], password: ENV['MDS_PASSWORD'], domains: "*.datacite.org")
subject = build(:doi, doi: "10.5438/mcnv-ga6n", url: "https://datacite.org/", client: client, aasm_state: "findable")
expect(subject.register_url.body).to eq("errors"=>[{"title"=>"URL not allowed by client domains settings."}])
expect { subject.register_url }.to raise_error(ActionController::BadRequest, "[Handle] Error updating DOI 10.5438/MCNV-GA6N: URL not allowed by client domains settings.")
end

it 'draft doi' do
subject = build(:doi, doi: "10.5438/mcnv-ga6n", url: "https://blog.datacite.org/", client: client, aasm_state: "draft")
expect(subject.register_url.body).to eq("errors"=>[{"title"=>"DOI is not registered or findable."}])
expect { subject.register_url }.to raise_error(ActionController::BadRequest, "DOI is not registered or findable.")
end

it 'missing username' do
subject = build(:doi, doi: "10.5438/mcnv-ga6n", url: "https://blog.datacite.org/re3data-science-europe/", client: nil, aasm_state: "findable")
expect(subject.register_url.body).to eq("errors"=>[{"title"=>"Client ID missing."}])
expect { subject.register_url }.to raise_error(ActionController::BadRequest, "[Handle] Error updating DOI 10.5438/MCNV-GA6N: client ID missing.")
end

it 'server not responsible' do
subject = build(:doi, doi: "10.1371/journal.pbio.2001414", url: "https://journals.plos.org/plosbiology/article?id=10.1371/journal.pbio.2001414", client: client, aasm_state: "findable")
expect(subject.register_url.body).to eq("errors"=>[{"status"=>400, "title"=>{"responseCode"=>301, "message"=>"That prefix doesn't live here", "handle"=>"10.1371/JOURNAL.PBIO.2001414"}}])
# expect { subject.register_url }.to raise_error(ActionController::BadRequest, "No valid prefix found")
end
end

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 42f5e4d

Please sign in to comment.