diff --git a/app/models/concerns/indexable.rb b/app/models/concerns/indexable.rb index 87e496529..2f6533e14 100644 --- a/app/models/concerns/indexable.rb +++ b/app/models/concerns/indexable.rb @@ -20,17 +20,11 @@ module Indexable end end - if ( - instance_of?(DataciteDoi) || instance_of?(OtherDoi) || - instance_of?(Doi) - ) && - ( - saved_change_to_attribute?("related_identifiers") || - saved_change_to_attribute?("creators") || - saved_change_to_attribute?("funding_references") - ) - if aasm_state == "findable" && !Rails.env.test? - send_import_message(to_jsonapi) + if instance_of?(DataciteDoi) || instance_of?(OtherDoi) || instance_of?(Doi) + if aasm_state == "findable" + changed_attributes = saved_changes + relevant_changes = changed_attributes.keys & %w[related_identifiers creators funding_references aasm_state] + send_import_message(to_jsonapi) if relevant_changes.any? end elsif instance_of?(Event) OtherDoiJob.perform_later(dois_to_import) diff --git a/spec/models/doi_spec.rb b/spec/models/doi_spec.rb index 515357dd5..fee782152 100644 --- a/spec/models/doi_spec.rb +++ b/spec/models/doi_spec.rb @@ -31,6 +31,85 @@ end end + describe "after_commit" do + let(:doi) { create(:doi, aasm_state: "findable") } + let(:sqs_client) { instance_double(Aws::SQS::Client) } + + before do + allow_any_instance_of(DataciteDoi).to receive(:send_message) + allow(Aws::SQS::Client).to receive(:new).and_return(sqs_client) + allow(IndexJob).to receive(:perform_later) + allow(DataciteDoi).to receive_message_chain(:__elasticsearch__, :index_document) + end + + context "On Update event" do + it "sends import message if relevant attributes are modified" do + travel_to(Time.zone.local(2023, 12, 14, 10, 7, 40)) do + expect(doi).to receive(:send_import_message).with(doi.to_jsonapi) + + doi.update(related_identifiers: [{ "relatedIdentifier" => "new_identifier", "relatedIdentifierType" => "DOI", "relationType" => "IsPartOf" }]) + end + end + + it "sends import message if creators are modified" do + travel_to(Time.zone.local(2023, 12, 14, 10, 7, 40)) do + expect(doi).to receive(:send_import_message).with(doi.to_jsonapi) + + doi.update(creators: [{ "nameType" => "Personal", "name" => "New Creator" }]) + end + end + + it "sends import message if funding_references are modified" do + travel_to(Time.zone.local(2023, 12, 14, 10, 7, 40)) do + expect(doi).to receive(:send_import_message).with(doi.to_jsonapi) + + doi.update(funding_references: [{ "funder" => "New Funder", "title" => "New Title" }]) + end + end + + it "does not send import message if no relevant attributes are modified" do + expect(doi).not_to receive(:send_import_message) + + doi.update(titles: "New Title") + end + + it "does not send import message if aasm_state is not 'findable'" do + expect(doi).not_to receive(:send_import_message) + + doi.update(aasm_state: "draft") + end + + it "does not send import message after create if aasm_state is not 'findable'" do + new_doi = create(:doi, aasm_state: "draft") + + expect(new_doi).not_to receive(:send_import_message) + end + end + + context "On Create event" do + it "sends import message after create if aasm_state is 'findable'" do + travel_to(Time.zone.local(2023, 12, 14, 10, 7, 40)) do + new_doi = build(:doi, aasm_state: "findable") + allow(new_doi).to receive(:send_import_message) + new_doi.save + + # Sleep for a short duration to ensure the asynchronous after_commit has completed + sleep 1 + + expect(new_doi).to have_received(:send_import_message).with(new_doi.to_jsonapi) + end + end + + it "does not send import message after create if aasm_state is not 'findable'" do + travel_to(Time.zone.local(2023, 12, 14, 10, 7, 40)) do + new_doi = create(:doi, aasm_state: "draft") + + expect(new_doi).not_to receive(:send_import_message) + end + end + end + end + describe "validate agency" do it "DataCite" do subject = build(:doi, agency: "DataCite") diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 613de2f07..68955c532 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -46,6 +46,7 @@ config.include RSpec::Benchmark::Matchers config.include Rack::Test::Methods, type: :request config.include RSpec::GraphqlMatchers::TypesHelper + config.include ActiveSupport::Testing::TimeHelpers # don't use transactions, use database_clear gem via support file config.use_transactional_fixtures = false diff --git a/spec/requests/events_spec.rb b/spec/requests/events_spec.rb index d98f568d4..1db45f046 100644 --- a/spec/requests/events_spec.rb +++ b/spec/requests/events_spec.rb @@ -925,7 +925,10 @@ end context "show" do - let(:doi) { create(:doi, client: client, aasm_state: "findable") } + let(:doi) do + allow_any_instance_of(DataciteDoi).to receive(:send_import_message) + create(:doi, client: client, aasm_state: "findable") + end let(:source_doi) { create(:doi, client: client, aasm_state: "findable") } let!(:event) do create( diff --git a/spec/requests/old_events_spec.rb b/spec/requests/old_events_spec.rb index beb704324..298432161 100644 --- a/spec/requests/old_events_spec.rb +++ b/spec/requests/old_events_spec.rb @@ -915,7 +915,10 @@ end context "show" do - let(:doi) { create(:doi, client: client, aasm_state: "findable") } + let(:doi) do + allow_any_instance_of(DataciteDoi).to receive(:send_import_message) + create(:doi, client: client, aasm_state: "findable") + end let(:source_doi) { create(:doi, client: client, aasm_state: "findable") } let!(:event) do create(