diff --git a/app/controllers/activities_controller.rb b/app/controllers/activities_controller.rb index a4cd94269..eb0b1c09b 100644 --- a/app/controllers/activities_controller.rb +++ b/app/controllers/activities_controller.rb @@ -65,7 +65,7 @@ def index "page[cursor]" => page[:cursor] ? make_cursor(results) : nil, "page[number]" => page[:cursor].nil? && page[:number].present? ? page[:number] + 1 : nil, "page[size]" => page[:size], - sort: params[:sort] }.compact.to_query + sort: params[:sort] }.compact.to_query, }.compact options[:include] = @include options[:is_collection] = true @@ -103,6 +103,6 @@ def set_include def set_activity response = Activity.find_by_id(params[:id]) @activity = response.results.first - fail ActiveRecord::RecordNotFound unless @activity.present? + fail ActiveRecord::RecordNotFound if @activity.blank? end end diff --git a/app/models/activity.rb b/app/models/activity.rb index 4976a868c..46f2839cc 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -204,7 +204,7 @@ def self.import_by_ids(options={}) end def self.import_by_id(options={}) - return nil unless options[:id].present? + return nil if options[:id].blank? id = options[:id].to_i index = Rails.env.test? ? "activities-test" : self.inactive_index @@ -228,7 +228,7 @@ def self.import_by_id(options={}) if errors > 1 Rails.logger.error "[Elasticsearch] #{errors} errors importing #{count} activities with IDs #{id} - #{(id + 499)}." - elsif count > 0 + elsif count.positive? Rails.logger.info "[Elasticsearch] Imported #{count} activities with IDs #{id} - #{(id + 499)}." end @@ -291,19 +291,18 @@ def self.convert_affiliation_by_id(options={}) contributors = Array.wrap(audited_changes["contributors"]).map do |c| # c is an array if there are changes return [] if c.blank? + c = c.last if c.is_a?(Array) if c["affiliation"].nil? c["affiliation"] = [] - should_update = true elsif c["affiliation"].is_a?(String) c["affiliation"] = [{ "name" => c["affiliation"] }] - should_update = true else c["affiliation"].is_a?(Hash) c["affiliation"] = Array.wrap(c["affiliation"]) - should_update = true end + should_update = true c end @@ -314,7 +313,7 @@ def self.convert_affiliation_by_id(options={}) count += 1 end end - + Rails.logger.info "[Elasticsearch] Converted affiliations for #{count} activities with IDs #{id} - #{(id + 499)}." if count > 0 count diff --git a/app/serializers/activity_serializer.rb b/app/serializers/activity_serializer.rb index a5f9fc02d..90c6ebaed 100644 --- a/app/serializers/activity_serializer.rb +++ b/app/serializers/activity_serializer.rb @@ -14,10 +14,10 @@ class ActivitySerializer end attribute "prov:wasAttributedTo" do |object| - return nil if object.username.blank? - - url = Rails.env.production? ? "https://api.datacite.org" : "https://api.test.datacite.org" - object.username.include?(".") ? url + "/clients/" + object.username : url + "/providers/" + object.username + if object.username.present? + url = Rails.env.production? ? "https://api.datacite.org" : "https://api.test.datacite.org" + object.username.include?(".") ? url + "/clients/" + object.username : url + "/providers/" + object.username + end end attribute "prov:wasGeneratedBy" do |object| diff --git a/spec/models/activity_spec.rb b/spec/models/activity_spec.rb new file mode 100644 index 000000000..6b9464786 --- /dev/null +++ b/spec/models/activity_spec.rb @@ -0,0 +1,34 @@ +require "rails_helper" + +describe Activity, type: :model do + context "create doi" do + let(:client) { create(:client) } + let(:doi) { create(:doi, client: client) } + + it "activity exists" do + expect(doi.activities.length).to eq(1) + activity = doi.activities.first + expect(activity.uid).to eq(doi.uid) + # expect(activity.username).to eq(2) + expect(activity.request_uuid).to be_present + expect(activity.changes["aasm_state"]).to eq("draft") + expect(activity.changes["types"]).to eq("bibtex"=>"misc", "citeproc"=>"dataset", "resourceType"=>"DataPackage", "resourceTypeGeneral"=>"Dataset", "ris"=>"DATA", "schemaOrg"=>"Dataset") + end + end + + context "update doi" do + let(:client) { create(:client) } + let(:doi) { create(:doi, client: client) } + + it "activity exists" do + doi.update(event: "publish") + + expect(doi.activities.length).to eq(2) + activity = doi.activities.last + expect(activity.uid).to eq(doi.uid) + # expect(activity.username).to eq(2) + expect(activity.request_uuid).to be_present + expect(activity.changes).to eq("aasm_state"=>["draft", "findable"]) + end + end +end diff --git a/spec/requests/activities_spec.rb b/spec/requests/activities_spec.rb new file mode 100644 index 000000000..5766cce89 --- /dev/null +++ b/spec/requests/activities_spec.rb @@ -0,0 +1,34 @@ +require "rails_helper" + +describe "activities", type: :request do + let(:provider) { create(:provider, symbol: "DATACITE") } + let(:client) { create(:client, provider: provider, symbol: ENV['MDS_USERNAME'], password: ENV['MDS_PASSWORD']) } + let(:doi) { create(:doi, client: client) } + let(:bearer) { Client.generate_token(role_id: "client_admin", uid: client.symbol, provider_id: provider.symbol.downcase, client_id: client.symbol.downcase, password: client.password) } + let(:headers) { { "HTTP_ACCEPT"=>"application/vnd.api+json", "HTTP_AUTHORIZATION" => "Bearer " + bearer }} + + describe "activities for doi", elasticsearch: true do + let!(:doi) { create(:doi, client: client) } + + before do + Doi.import + Activity.import + sleep 1 + end + + context "without username" do + it "returns the activities" do + get "/activities", nil, headers + + expect(last_response.status).to eq(200) + expect(json.dig("data", 0, "attributes", "action")).to eq("create") + expect(json.dig("data", 0, "attributes", "changes", "aasm_state")).to eq("draft") + + expect(json.dig("data", 0, "attributes", "prov:wasAttributedTo")).to be_nil + expect(json.dig("data", 0, "attributes", "prov:wasGeneratedBy")).to be_present + expect(json.dig("data", 0, "attributes", "prov:generatedAtTime")).to be_present + expect(json.dig("data", 0, "attributes", "prov:wasDerivedFrom")).to be_present + end + end + end +end