Skip to content

Commit

Permalink
Adding code to only update DOIs that are part of our prefix (#416)
Browse files Browse the repository at this point in the history
Also added test to make sure Arks are updated and Honeybadger is notified if the DOI can not be updated and there is no ARK

fixes #388
  • Loading branch information
carolyncole authored Sep 16, 2022
1 parent d37437e commit 1c23b1c
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 7 deletions.
4 changes: 3 additions & 1 deletion app/models/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -519,12 +519,14 @@ def validate_creators
def publish_doi(user)
if Rails.env.development? && Rails.configuration.datacite.user.blank?
Rails.logger.info "Publishing hard-coded test DOI during development."
else
elsif doi.starts_with?(Rails.configuration.datacite.prefix)
result = data_cite_connection.update(id: doi, attributes: doi_attributes)
if result.failure?
message = "@#{curator_or_current_uid(user)} Error publishing DOI. #{result.failure.status} / #{result.failure.reason_phrase}"
WorkActivity.add_system_activity(id, message, user.id, activity_type: "DATACITE_ERROR")
end
elsif ark.blank? # we can not update the url anywhere
Honeybadger.notify("Publishing for a DOI we do not own and no ARK is present: #{doi}")
end
end

Expand Down
2 changes: 1 addition & 1 deletion config/datacite.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
default: &default
prefix: <%= ENV["DATACITE_PREFIX"] || "10.0000" %>
prefix: '<%= ENV["DATACITE_PREFIX"] || 10.0000 %>'
user: <%= ENV["DATACITE_USER"] || "" %>
password: <%= ENV["DATACITE_PASSWORD"] || "" %>
host: <%= ENV["DATACITE_HOST"] || "api.datacite.org" %>
Expand Down
5 changes: 3 additions & 2 deletions spec/factories/work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@

factory :completed_work do
transient do
doi { "https://doi.org/10.34770/123-abc" }
doi { "10.34770/123-abc" }
ark { nil }
end
collection { Collection.research_data }
state { "awaiting_approval" }
created_by_user_id { FactoryBot.create(:user).id }
resource { FactoryBot.build :resource, doi: doi }
resource { FactoryBot.build :resource, doi: doi, ark: ark }
end

factory :shakespeare_and_company_work do
Expand Down
50 changes: 47 additions & 3 deletions spec/system/migrate_submission_spec.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require "rails_helper"

RSpec.describe "Form submission for a legacy dataset", type: :system, mock_ezid_api: true do
RSpec.describe "Form submission for a legacy dataset", type: :system, mock_ezid_api: true, js: true do
let(:user) { FactoryBot.create(:princeton_submitter) }
let(:doi) { "10.34770/123-abc" }
let(:title) { "Sowing the Seeds for More Usable Web Archives: A Usability Study of Archive-It" }
Expand Down Expand Up @@ -38,7 +38,7 @@
stub_request(:get, "https://handle.stage.datacite.org/10.34770/123-abc").to_return(status: 200, body: "", headers: {})
end

it "produces and saves a valid datacite record", js: true do
it "produces and saves a valid datacite record" do
# Make the screen larger so the save button is alway on screen. This avoids random `Element is not clickable` errors
page.driver.browser.manage.window.resize_to(2000, 2000)
sign_in user
Expand Down Expand Up @@ -67,7 +67,7 @@
stub_request(:get, "https://handle.stage.datacite.org/10.34770/123-ab").to_return(status: 404, body: "", headers: {})
end

it "returns the user to the new page so they can recover from an error", js: true do
it "returns the user to the new page so they can recover from an error" do
# Make the screen larger so the save button is alway on screen. This avoids random `Element is not clickable` errors
page.driver.browser.manage.window.resize_to(2000, 2000)
sign_in user
Expand Down Expand Up @@ -99,4 +99,48 @@
expect(page).to have_content "awaiting_approval"
end
end

context "DOI and Ark updates" do
let(:curator) { FactoryBot.create(:research_data_moderator) }
let(:datacite_stub) { stub_datacite_doi }
let(:identifier) { @identifier } # from the mock_ezid_api
before do
datacite_stub # make sure the stub is created before we start the test
Rails.configuration.update_ark_url = true
allow(Honeybadger).to receive(:notify)
sign_in curator
visit work_path(work)
click_on "Approve"
end

context "Approving a work with a DOI we own" do
let(:work) { FactoryBot.create :completed_work, doi: "#{Rails.configuration.datacite.prefix}/abc-123", ark: "ark:/88435/dsp01d791sj97j" }

it "updates the DOI and ARK url when approved" do
expect(datacite_stub).to have_received("update")
expect(identifier).to have_received("target=")
expect(identifier).to have_received("save!")
end
end

context "Approving a work with a DOI we own do not own, but also has an ARK" do
let(:work) { FactoryBot.create :completed_work, doi: "10.99999/abc-123", ark: "ark:/88435/dsp01d791sj97j" }

it "updates the ARK url when approved" do
expect(datacite_stub).not_to have_received("update")
expect(Honeybadger).not_to have_received(:notify)
expect(identifier).to have_received("target=")
expect(identifier).to have_received("save!")
end
end

context "Approving a work with a DOI we own do not own, but does not have an ARK" do
let(:work) { FactoryBot.create :completed_work, doi: "10.99999/abc-123", ark: nil }
it "updates the ARK url when approved" do
expect(datacite_stub).not_to have_received("update")
expect(identifier).not_to have_received("target=")
expect(Honeybadger).to have_received(:notify)
end
end
end
end

0 comments on commit 1c23b1c

Please sign in to comment.