From a0a12d89bdc1f969fabda1c754d6a407c2f2a22b Mon Sep 17 00:00:00 2001 From: Ashwini Sukale Date: Tue, 21 Nov 2023 15:03:31 +0530 Subject: [PATCH] Fixed lint errors for event data (#123) * Fixed lint errors * Revert "Fixed lint errors" This reverts commit e7d3c67fc4d8078cc74d328a167b929ff5a1be92. * Fixed lint errors * Fixed lint errors in recently added files --- .rubocop.yml | 2 +- Gemfile | 91 +++++---- Rakefile | 2 +- app/controllers/concerns/facetable.rb | 2 +- app/jobs/report_import_job.rb | 8 +- app/jobs/usage_update_parse_job.rb | 2 +- app/models/affiliation_identifier.rb | 2 +- app/models/base.rb | 8 +- app/models/concerns/importable.rb | 6 +- app/models/orcid_affiliation.rb | 2 +- bin/bundle | 4 +- bin/rails | 10 +- bin/rake | 8 +- bin/setup | 19 +- bin/spring | 8 +- bin/update | 18 +- config.ru | 2 +- config/initializers/constants.rb | 2 +- spec/concerns/importable_spec.rb | 192 +++++++++--------- spec/lib/tasks/funder_identifier_rake_spec.rb | 44 ++-- spec/models/affiliation_identifier_spec.rb | 39 ++-- spec/models/crossref_spec.rb | 7 +- spec/models/name_identifier_spec.rb | 148 +++++++------- spec/models/related_identifier_spec.rb | 185 +++++++++-------- spec/models/related_url_spec.rb | 28 +-- 25 files changed, 424 insertions(+), 415 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 23560c32..bc1c1c14 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -657,7 +657,7 @@ Rails/Validation: Description: 'Use validates :attribute, hash of validations.' Enabled: false -Gemspec/DateAssignment: # (new in 1.10) +Gemspec/DeprecatedAttributeAssignment: # (new in 1.10) Enabled: true Layout/SpaceBeforeBrackets: # (new in 1.7) Enabled: true diff --git a/Gemfile b/Gemfile index 5901f1e4..ba935b31 100644 --- a/Gemfile +++ b/Gemfile @@ -1,63 +1,64 @@ # frozen_string_literal: true -source 'https://rubygems.org' -gem 'rails', '6.0' -gem 'bootsnap', '~> 1.2', '>= 1.2.1' -gem 'dotenv' -gem "oj", ">= 2.8.3" -gem "oj_mimic_json", "~> 1.0", ">= 1.0.1" -gem 'equivalent-xml', '~> 0.6.0' -gem 'nokogiri', '~> 1.13.2' -gem 'iso8601', '~> 0.9.0' -gem 'maremma', '~> 4.9.6' +source "https://rubygems.org" + +gem "active_model_serializers", "~> 0.10.0" +gem "api-pagination" +gem "aws-sdk-sqs", "~> 1.3" +gem "bcrypt", "~> 3.1.7" +gem "bolognese", "~> 1.11.5" +gem "bootsnap", "~> 1.2", ">= 1.2.1" +gem "cancancan", "~> 2.0" +gem "countries", "~> 2.1", ">= 2.1.2" +gem "country_select", "~> 3.1" gem "dalli", "~> 2.7.6" +gem "dotenv" +gem "equivalent-xml", "~> 0.6.0" +gem "facets", require: false +gem "faraday_middleware-aws-sigv4", "~> 0.2.4" +gem "git", "~> 1.5" +gem "iso8601", "~> 0.9.0" +gem "jwt" +gem "kaminari", "~> 1.0", ">= 1.0.1" gem "lograge", "~> 0.11.2" gem "logstash-event", "~> 1.2", ">= 1.2.02" gem "logstash-logger", "~> 0.26.1" -gem 'sentry-raven', '~> 2.9' -gem 'active_model_serializers', '~> 0.10.0' -gem 'jwt' -gem 'bcrypt', '~> 3.1.7' -gem 'simple_command' -gem 'kaminari', '~> 1.0', '>= 1.0.1' -gem 'api-pagination' -gem 'cancancan', '~> 2.0' -gem 'country_select', '~> 3.1' -gem 'countries', '~> 2.1', '>= 2.1.2' -gem "facets", require: false -gem "bolognese", "~> 1.11.5" +gem "maremma", "~> 4.9.6" +gem "nokogiri", "~> 1.13.2" +gem "oj", ">= 2.8.3" +gem "oj_mimic_json", "~> 1.0", ">= 1.0.1" +gem "rack-cors", "~> 1.0", require: "rack/cors" +gem "rack-utf8_sanitizer", "~> 1.6" +gem "rails", "6.0" +gem "sentry-raven", "~> 2.9" gem "shoryuken", "~> 4.0" -gem 'rack-cors', '~> 1.0', :require => 'rack/cors' -gem 'rack-utf8_sanitizer', '~> 1.6' -gem 'slack-notifier', '~> 2.3', '>= 2.3.2' -gem 'aws-sdk-sqs', '~> 1.3' -gem 'faraday_middleware-aws-sigv4', '~> 0.2.4' -gem 'git', '~> 1.5' +gem "simple_command" +gem "slack-notifier", "~> 2.3", ">= 2.3.2" gem "sprockets", "~> 3.7", ">= 3.7.2" group :development, :test do - gem 'rspec-rails', '~> 3.5', '>= 3.5.2' gem "better_errors" - gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] - gem 'rubocop', '~> 0.77.0' - gem 'rubocop-performance', '~> 1.5', '>= 1.5.1' - gem 'rubocop-rails', '~> 2.4' + gem "byebug", platforms: %i[mri mingw x64_mingw] + gem "rspec-rails", "~> 3.5", ">= 3.5.2" + gem "rubocop", "~> 0.77.0" + gem "rubocop-performance", "~> 1.5", ">= 1.5.1" + gem "rubocop-rails", "~> 2.4" end group :development do - gem 'listen', '>= 3.0.5', '< 3.2' - gem 'spring' - gem 'spring-watcher-listen', '~> 2.0.0' + gem "listen", ">= 3.0.5", "< 3.2" + gem "spring" + gem "spring-watcher-listen", "~> 2.0.0" end group :test do - gem 'capybara' - gem 'webmock', '~> 3.1' - gem 'vcr', '~> 5.1' - gem 'codeclimate-test-reporter', '~> 1.0.0' - gem 'simplecov' - gem 'shoulda-matchers', '~> 4.1', '>= 4.1.2' - gem 'rubocop-rspec', '~> 1.28' - gem 'faker' - gem 'factory_bot_rails', '~> 4.8', '>= 4.8.2' + gem "capybara" + gem "codeclimate-test-reporter", "~> 1.0.0" + gem "factory_bot_rails", "~> 4.8", ">= 4.8.2" + gem "faker" + gem "rubocop-rspec", "~> 1.28" + gem "shoulda-matchers", "~> 4.1", ">= 4.1.2" + gem "simplecov" + gem "vcr", "~> 5.1" + gem "webmock", "~> 3.1" end diff --git a/Rakefile b/Rakefile index e85f9139..9a5ea738 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,6 @@ # Add your own tasks in files placed in lib/tasks ending in .rake, # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. -require_relative 'config/application' +require_relative "config/application" Rails.application.load_tasks diff --git a/app/controllers/concerns/facetable.rb b/app/controllers/concerns/facetable.rb index 88c6f645..a6231bbe 100644 --- a/app/controllers/concerns/facetable.rb +++ b/app/controllers/concerns/facetable.rb @@ -12,7 +12,7 @@ def facet_by_year(arr) def facet_by_provider(arr) # generate hash with id and name for each provider in facet - ids = arr.map { |hsh| hsh["key"] }.join(",") + ids = arr.pluck("key").join(",") providers = Provider.find_by(ids: ids).to_a.reduce({}) do |sum, p| sum[p.id] = p.name sum diff --git a/app/jobs/report_import_job.rb b/app/jobs/report_import_job.rb index 441c4310..522eb3d8 100644 --- a/app/jobs/report_import_job.rb +++ b/app/jobs/report_import_job.rb @@ -5,10 +5,7 @@ class ReportImportJob < ApplicationJob def perform(item, options = {}) response = UsageUpdate.get_data(item, options) - if response.status != 200 - Rails.logger.error "[Usage Report Parsing] Report #{item} not found" - {} - else + if response.status == 200 # report = Report.new(response, options) Rails.logger.debug "[Usage Report] Started to parse #{item}." UsageUpdate.redirect(response) @@ -16,6 +13,9 @@ def perform(item, options = {}) # when "normal" then Report.parse_normal_report report # when "compressed" then Report.parse_multi_subset_report report # end + else + Rails.logger.error "[Usage Report Parsing] Report #{item} not found" + {} end end end diff --git a/app/jobs/usage_update_parse_job.rb b/app/jobs/usage_update_parse_job.rb index 9196a478..373d31b5 100644 --- a/app/jobs/usage_update_parse_job.rb +++ b/app/jobs/usage_update_parse_job.rb @@ -23,7 +23,7 @@ def perform(dataset, options) def send_message(data, item, _options = {}) errors = data.select { |hsh| hsh.fetch("errors", nil) } - if data.length.zero? + if data.empty? # options[:level] = "warning" Rails.logger.error "[Usage Report Parsing] Error parsing Report #{item}. Report is empty" elsif !errors.empty? diff --git a/app/models/affiliation_identifier.rb b/app/models/affiliation_identifier.rb index 17bee5fe..58e813a6 100644 --- a/app/models/affiliation_identifier.rb +++ b/app/models/affiliation_identifier.rb @@ -147,7 +147,7 @@ def self.push_item(item) def self.get_ror_metadata(id) return {} if id.blank? - url = "https://api.ror.org/organizations/#{id[8..-1]}" + url = "https://api.ror.org/organizations/#{id[8..]}" response = Maremma.get(url, host: true) return {} if response.status != 200 diff --git a/app/models/base.rb b/app/models/base.rb index 633ad45b..5715cede 100644 --- a/app/models/base.rb +++ b/app/models/base.rb @@ -317,11 +317,9 @@ def self.parse_datacite_metadata(id: nil, response: nil) "name" => attributes["publisher"] } end proxy_identifiers = Array.wrap(attributes["relatedIdentifiers"]).select do |ri| - ["IsVersionOf", "IsIdenticalTo", "IsPartOf", - "IsSupplementTo"].include?(ri["relationType"]) - end.map do |ri| - ri["relatedIdentifier"] - end + ["IsVersionOf", "IsIdenticalTo", "IsPartOf", + "IsSupplementTo"].include?(ri["relationType"]) + end.pluck("relatedIdentifier") resource_type_general = attributes.dig("types", "resourceTypeGeneral") type = Bolognese::Utils::DC_TO_SO_TRANSLATIONS[resource_type_general.to_s.dasherize] # || attributes.dig("types", "schemaOrg") diff --git a/app/models/concerns/importable.rb b/app/models/concerns/importable.rb index 5db0d31f..0be551db 100644 --- a/app/models/concerns/importable.rb +++ b/app/models/concerns/importable.rb @@ -72,7 +72,7 @@ def normalize_arxiv(id) id = id.downcase # turn arXiv into a URL if needed - id = "https://arxiv.org/abs/#{id[6..-1]}" if id.start_with?("arxiv:") + id = "https://arxiv.org/abs/#{id[6..]}" if id.start_with?("arxiv:") # check for valid protocol. uri = Addressable::URI.parse(id) @@ -97,7 +97,7 @@ def normalize_igsn(id) return nil unless uri&.host && %w(http https).include?(uri.scheme) # don't use IGSN resolver as no support for ssl - id = "https://hdl.handle.net/10273/#{id[15..-1]}" if id.start_with?("http://igsn.org") + id = "https://hdl.handle.net/10273/#{id[15..]}" if id.start_with?("http://igsn.org") # clean up URL PostRank::URI.clean(id.downcase) @@ -129,7 +129,7 @@ def normalize_pmid(id) id = id.downcase # strip pmid prefix - id = id[5..-1] if id.start_with?("pmid:") + id = id[5..] if id.start_with?("pmid:") # turn handle into a URL if needed id = "https://identifiers.org/pubmed:#{id}" unless id.start_with?("http") diff --git a/app/models/orcid_affiliation.rb b/app/models/orcid_affiliation.rb index 0cfcb9d0..82ac800d 100644 --- a/app/models/orcid_affiliation.rb +++ b/app/models/orcid_affiliation.rb @@ -155,7 +155,7 @@ def self.push_item(item) def self.get_ror_metadata(id) return {} if id.blank? - url = "https://api.ror.org/organizations/#{id[8..-1]}" + url = "https://api.ror.org/organizations/#{id[8..]}" response = Maremma.get(url, host: true) return {} if response.status != 200 diff --git a/bin/bundle b/bin/bundle index 66e9889e..67efc37f 100755 --- a/bin/bundle +++ b/bin/bundle @@ -1,3 +1,3 @@ #!/usr/bin/env ruby -ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) -load Gem.bin_path('bundler', 'bundle') +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) +load Gem.bin_path("bundler", "bundle") diff --git a/bin/rails b/bin/rails index 5badb2fd..36b01a1c 100755 --- a/bin/rails +++ b/bin/rails @@ -1,9 +1,9 @@ #!/usr/bin/env ruby begin - load File.expand_path('../spring', __FILE__) + load File.expand_path("spring", __dir__) rescue LoadError => e - raise unless e.message.include?('spring') + raise unless e.message.include?("spring") end -APP_PATH = File.expand_path('../config/application', __dir__) -require_relative '../config/boot' -require 'rails/commands' +APP_PATH = File.expand_path("../config/application", __dir__) +require_relative "../config/boot" +require "rails/commands" diff --git a/bin/rake b/bin/rake index d87d5f57..660472a0 100755 --- a/bin/rake +++ b/bin/rake @@ -1,9 +1,9 @@ #!/usr/bin/env ruby begin - load File.expand_path('../spring', __FILE__) + load File.expand_path("spring", __dir__) rescue LoadError => e - raise unless e.message.include?('spring') + raise unless e.message.include?("spring") end -require_relative '../config/boot' -require 'rake' +require_relative "../config/boot" +require "rake" Rake.application.run diff --git a/bin/setup b/bin/setup index 104e40c1..37be32ec 100755 --- a/bin/setup +++ b/bin/setup @@ -1,10 +1,10 @@ #!/usr/bin/env ruby -require 'pathname' -require 'fileutils' +require "pathname" +require "fileutils" include FileUtils # path to your application root. -APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) +APP_ROOT = Pathname.new File.expand_path("..", __dir__) def system!(*args) system(*args) || abort("\n== Command #{args} failed ==") @@ -14,10 +14,9 @@ chdir APP_ROOT do # This script is a starting point to setup your application. # Add necessary setup steps to this file. - puts '== Installing dependencies ==' - system! 'gem install bundler --conservative' - system('bundle check') || system!('bundle install') - + puts "== Installing dependencies ==" + system! "gem install bundler --conservative" + system("bundle check") || system!("bundle install") # puts "\n== Copying sample files ==" # unless File.exist?('config/database.yml') @@ -25,11 +24,11 @@ chdir APP_ROOT do # end puts "\n== Preparing database ==" - system! 'bin/rails db:setup' + system! "bin/rails db:setup" puts "\n== Removing old logs and tempfiles ==" - system! 'bin/rails log:clear tmp:clear' + system! "bin/rails log:clear tmp:clear" puts "\n== Restarting application server ==" - system! 'bin/rails restart' + system! "bin/rails restart" end diff --git a/bin/spring b/bin/spring index fb2ec2eb..b44ad1a7 100755 --- a/bin/spring +++ b/bin/spring @@ -4,14 +4,14 @@ # It gets overwritten when you run the `spring binstub` command. unless defined?(Spring) - require 'rubygems' - require 'bundler' + require "rubygems" + require "bundler" lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read) spring = lockfile.specs.detect { |spec| spec.name == "spring" } if spring Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path - gem 'spring', spring.version - require 'spring/binstub' + gem "spring", spring.version + require "spring/binstub" end end diff --git a/bin/update b/bin/update index a8e4462f..db28935e 100755 --- a/bin/update +++ b/bin/update @@ -1,10 +1,10 @@ #!/usr/bin/env ruby -require 'pathname' -require 'fileutils' +require "pathname" +require "fileutils" include FileUtils # path to your application root. -APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) +APP_ROOT = Pathname.new File.expand_path("..", __dir__) def system!(*args) system(*args) || abort("\n== Command #{args} failed ==") @@ -14,16 +14,16 @@ chdir APP_ROOT do # This script is a way to update your development environment automatically. # Add necessary update steps to this file. - puts '== Installing dependencies ==' - system! 'gem install bundler --conservative' - system('bundle check') || system!('bundle install') + puts "== Installing dependencies ==" + system! "gem install bundler --conservative" + system("bundle check") || system!("bundle install") puts "\n== Updating database ==" - system! 'bin/rails db:migrate' + system! "bin/rails db:migrate" puts "\n== Removing old logs and tempfiles ==" - system! 'bin/rails log:clear tmp:clear' + system! "bin/rails log:clear tmp:clear" puts "\n== Restarting application server ==" - system! 'bin/rails restart' + system! "bin/rails restart" end diff --git a/config.ru b/config.ru index f7ba0b52..441e6ff0 100644 --- a/config.ru +++ b/config.ru @@ -1,5 +1,5 @@ # This file is used by Rack-based servers to start the application. -require_relative 'config/environment' +require_relative "config/environment" run Rails.application diff --git a/config/initializers/constants.rb b/config/initializers/constants.rb index eed7d328..df7fe681 100644 --- a/config/initializers/constants.rb +++ b/config/initializers/constants.rb @@ -13,7 +13,7 @@ DOI_FORMAT = %r(\A10\.\d{4,5}/.+).freeze # Format used for URL validation -URL_FORMAT = %r(\A(http|https|ftp)://[a-z0-9]+([\-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?/.*)?\z).freeze +URL_FORMAT = %r(\A(http|https|ftp)://[a-z0-9]+([-.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?/.*)?\z).freeze # Form queue options QUEUE_OPTIONS = ["high", "default", "low"].freeze diff --git a/spec/concerns/importable_spec.rb b/spec/concerns/importable_spec.rb index 97dbcfd8..858ccaff 100644 --- a/spec/concerns/importable_spec.rb +++ b/spec/concerns/importable_spec.rb @@ -282,16 +282,16 @@ end describe "parse_record" do - let!(:data) {{"id" => "fake_id"}} + let!(:data) { { "id" => "fake_id" } } describe "has related identifiers" do describe "with related identifier type 'DOI'" do it "sends push_item to RelatedIdentifier" do json = { "relatedIdentifiers" => [ - {"relatedIdentifierType" => "DOI"}, - {"relatedIdentifierType" => "FOO"} - ] + { "relatedIdentifierType" => "DOI" }, + { "relatedIdentifierType" => "FOO" }, + ], } allow(RelatedIdentifier).to(receive(:push_item)) @@ -299,12 +299,12 @@ Base.parse_record(data: data) - expect(RelatedIdentifier) - .to(have_received(:push_item).with({ - "id" => "fake_id", - "type" => "dois", - "attributes" => json - }).once) + expect(RelatedIdentifier). + to(have_received(:push_item).with({ + "id" => "fake_id", + "type" => "dois", + "attributes" => json, + }).once) end end @@ -312,9 +312,9 @@ it "does not send push_item to RelatedIdentifier" do json = { "relatedIdentifiers" => [ - {"relatedIdentifierType" => "URL"}, - {"relatedIdentifierType" => "FOO"} - ] + { "relatedIdentifierType" => "URL" }, + { "relatedIdentifierType" => "FOO" }, + ], } allow(RelatedIdentifier).to(receive(:push_item)) @@ -330,9 +330,9 @@ it "sends push_item to RelatedIdentifier" do json = { "relatedIdentifiers" => [ - {"relatedIdentifierType" => "URL"}, - {"relatedIdentifierType" => "FOO"} - ] + { "relatedIdentifierType" => "URL" }, + { "relatedIdentifierType" => "FOO" }, + ], } allow(RelatedUrl).to(receive(:push_item)) @@ -340,22 +340,22 @@ Base.parse_record(data: data) - expect(RelatedUrl) - .to(have_received(:push_item).with({ - "id" => "fake_id", - "type" => "dois", - "attributes" => json - }).once) + expect(RelatedUrl). + to(have_received(:push_item).with({ + "id" => "fake_id", + "type" => "dois", + "attributes" => json, + }).once) end end describe "without related identifier type 'URL'" do it "does not send push_item to RelatedIdentifier" do json = { - "relatedIdentifiers": [ - {"relatedIdentifierType" => "DOI"}, - {"relatedIdentifierType" => "FOO"} - ] + relatedIdentifiers: [ + { "relatedIdentifierType" => "DOI" }, + { "relatedIdentifierType" => "FOO" }, + ], } allow(RelatedUrl).to(receive(:push_item)) @@ -373,9 +373,9 @@ it "sends push_item to FunderIdentifier" do json = { "fundingReferences" => [ - {"funderIdentifierType" => "Foo"}, - {"funderIdentifierType" => "Crossref Funder ID"}, - ] + { "funderIdentifierType" => "Foo" }, + { "funderIdentifierType" => "Crossref Funder ID" }, + ], } allow(FunderIdentifier).to(receive(:push_item)) @@ -383,12 +383,12 @@ Base.parse_record(data: data) - expect(FunderIdentifier) - .to(have_received(:push_item).with({ - "doi" => "fake_id", - "type" => "dois", - "attributes" => json - }).once) + expect(FunderIdentifier). + to(have_received(:push_item).with({ + "doi" => "fake_id", + "type" => "dois", + "attributes" => json, + }).once) end end @@ -396,9 +396,9 @@ it "does not send push_item to FunderIdentifier" do json = { "fundingReferences" => [ - {"funderIdentifierType" => "Foo"}, - {"funderIdentifierType" => "Bar"} - ] + { "funderIdentifierType" => "Foo" }, + { "funderIdentifierType" => "Bar" }, + ], } allow(FunderIdentifier).to(receive(:push_item)) @@ -418,10 +418,11 @@ "creators" => [ { "nameIdentifiers" => [ - {"nameIdentifierScheme" => "ORCID"}, - {"nameIdentifierScheme" => "FOO"} - ]} - ] + { "nameIdentifierScheme" => "ORCID" }, + { "nameIdentifierScheme" => "FOO" }, + ], + }, + ], } allow(NameIdentifier).to(receive(:push_item)) @@ -429,12 +430,12 @@ Base.parse_record(data: data) - expect(NameIdentifier) - .to(have_received(:push_item).with({ - "doi" => "fake_id", - "type" => "dois", - "attributes" => json - }).once) + expect(NameIdentifier). + to(have_received(:push_item).with({ + "doi" => "fake_id", + "type" => "dois", + "attributes" => json, + }).once) end end @@ -444,10 +445,11 @@ "creators" => [ { "nameIdentifiers" => [ - {"nameIdentifierScheme" => "FOO"}, - {"nameIdentifierScheme" => "BAR"} - ]}, - ] + { "nameIdentifierScheme" => "FOO" }, + { "nameIdentifierScheme" => "BAR" }, + ], + }, + ], } allow(NameIdentifier).to(receive(:push_item)) @@ -467,25 +469,26 @@ "creators" => [ { "affiliation" => [ - {"affiliationIdentifierScheme" => "ROR"} + { "affiliationIdentifierScheme" => "ROR" }, ], "nameIdentifiers" => [ - {"nameIdentifierScheme" => "ORCID"} - ] - } - ]} + { "nameIdentifierScheme" => "ORCID" }, + ], + }, + ], + } allow(AffiliationIdentifier).to(receive(:push_item)) allow(Base).to(receive(:get_datacite_json).and_return(json)) Base.parse_record(data: data) - expect(AffiliationIdentifier) - .to(have_received(:push_item).with({ - "doi" => "fake_id", - "type" => "dois", - "attributes" => json - }).once) + expect(AffiliationIdentifier). + to(have_received(:push_item).with({ + "doi" => "fake_id", + "type" => "dois", + "attributes" => json, + }).once) end end @@ -495,13 +498,14 @@ "creators" => [ { "affiliation" => [ - {"affiliationIdentifierScheme" => "ROR"} + { "affiliationIdentifierScheme" => "ROR" }, ], "nameIdentifiers" => [ - {"nameIdentifierScheme" => "FOO"} - ] - } - ]} + { "nameIdentifierScheme" => "FOO" }, + ], + }, + ], + } allow(AffiliationIdentifier).to(receive(:push_item)) allow(Base).to(receive(:get_datacite_json).and_return(json)) @@ -518,13 +522,14 @@ "creators" => [ { "affiliation" => [ - {"affiliationIdentifierScheme" => "FOO"} + { "affiliationIdentifierScheme" => "FOO" }, ], "nameIdentifiers" => [ - {"nameIdentifierScheme" => "ORCID"} - ] - } - ]} + { "nameIdentifierScheme" => "ORCID" }, + ], + }, + ], + } allow(AffiliationIdentifier).to(receive(:push_item)) allow(Base).to(receive(:get_datacite_json).and_return(json)) @@ -541,13 +546,14 @@ "creators" => [ { "affiliation" => [ - {"affiliationIdentifierScheme" => "FOO"} + { "affiliationIdentifierScheme" => "FOO" }, ], "nameIdentifiers" => [ - {"nameIdentifierScheme" => "BAR"} - ] - } - ]} + { "nameIdentifierScheme" => "BAR" }, + ], + }, + ], + } allow(AffiliationIdentifier).to(receive(:push_item)) allow(Base).to(receive(:get_datacite_json).and_return(json)) @@ -566,22 +572,23 @@ "creators" => [ { "affiliation" => [ - {"affiliationIdentifierScheme" => "ROR"} - ] - } - ]} + { "affiliationIdentifierScheme" => "ROR" }, + ], + }, + ], + } allow(OrcidAffiliation).to(receive(:push_item)) allow(Base).to(receive(:get_datacite_json).and_return(json)) Base.parse_record(data: data) - expect(OrcidAffiliation) - .to(have_received(:push_item).with({ - "doi" => "fake_id", - "type" => "dois", - "attributes" => json - }).once) + expect(OrcidAffiliation). + to(have_received(:push_item).with({ + "doi" => "fake_id", + "type" => "dois", + "attributes" => json, + }).once) end end @@ -591,10 +598,11 @@ "creators" => [ { "affiliation" => [ - {"affiliationIdentifierScheme" => "ORCID"} - ] - } - ]} + { "affiliationIdentifierScheme" => "ORCID" }, + ], + }, + ], + } allow(OrcidAffiliation).to(receive(:push_item)) allow(Base).to(receive(:get_datacite_json).and_return(json)) diff --git a/spec/lib/tasks/funder_identifier_rake_spec.rb b/spec/lib/tasks/funder_identifier_rake_spec.rb index ac499f83..111b1d7d 100644 --- a/spec/lib/tasks/funder_identifier_rake_spec.rb +++ b/spec/lib/tasks/funder_identifier_rake_spec.rb @@ -53,7 +53,7 @@ describe "push_item" do it "returns nil when the doi is blank" do - expect(FunderIdentifier.push_item({attributes: {doi: nil}})).to(eq(nil)) + expect(FunderIdentifier.push_item({ attributes: { doi: nil } })).to(eq(nil)) end describe "when STAFF_ADMIN_TOKEN" do @@ -61,8 +61,8 @@ allow(ENV).to(receive(:[]).with("STAFF_ADMIN_TOKEN").and_return("STAFF_ADMIN_TOKEN")) allow(ENV).to(receive(:[]).with("LAGOTTINO_URL").and_return("https://fake.lagattino.com")) allow(ENV).to(receive(:[]).with("DATACITE_FUNDER_SOURCE_TOKEN").and_return("DATACITE_FUNDER_SOURCE_TOKEN")) - allow(Base).to(receive(:cached_datacite_response).and_return({"foo" => "bar"})) - allow(Base).to(receive(:cached_funder_response).and_return({"bar" => "foo"})) + allow(Base).to(receive(:cached_datacite_response).and_return({ "foo" => "bar" })) + allow(Base).to(receive(:cached_funder_response).and_return({ "bar" => "foo" })) allow(Maremma).to(receive(:post).and_return(OpenStruct.new(status: 200))) allow(Time).to(receive_message_chain(:zone, :now, :iso8601).and_return("2023-11-15T12:17:47Z")) end @@ -76,18 +76,18 @@ "fundingReferences" => [ { "funderIdentifier" => "https://doi.org/10.0001/example.one", - "funderIdentifierType" => "Crossref Funder ID" + "funderIdentifierType" => "Crossref Funder ID", }, { "funderIdentifier" => "https://doi.org/10.0001/example.two", - "funderIdentifierType" => "Foo" + "funderIdentifierType" => "Foo", }, { "funderIdentifier" => "https://doi.org/10.0001/example.three", - "funderIdentifierType" => "Crossref Funder ID" + "funderIdentifierType" => "Crossref Funder ID", }, - ] - } + ], + }, } expect(FunderIdentifier.push_item(item)).to(eq(2)) @@ -103,13 +103,13 @@ "fundingReferences" => [ { "funderIdentifier" => "https://doi.org/10.0001/example.one", - "funderIdentifierType" => "Crossref Funder ID" + "funderIdentifierType" => "Crossref Funder ID", }, ], }, } - json_data = ({ + json_data = { "data" => { "type" => "events", "attributes" => { @@ -122,21 +122,21 @@ "occurredAt" => "2023-11-15", "timestamp" => "2023-11-15T12:17:47Z", "license" => "https://creativecommons.org/publicdomain/zero/1.0/", - "subj" => {"foo" => "bar"}, - "obj" => {"bar" => "foo"}, - } - } - }).to_json + "subj" => { "foo" => "bar" }, + "obj" => { "bar" => "foo" }, + }, + }, + }.to_json expect(FunderIdentifier.push_item(item)).to(eq(1)) expect(Maremma).to(have_received(:post).with( - "https://fake.lagattino.com/events", - data: json_data, - bearer: "STAFF_ADMIN_TOKEN", - content_type: "application/vnd.api+json", - accept: "application/vnd.api+json; version=2" - )) + "https://fake.lagattino.com/events", + data: json_data, + bearer: "STAFF_ADMIN_TOKEN", + content_type: "application/vnd.api+json", + accept: "application/vnd.api+json; version=2", + )) end end @@ -151,7 +151,7 @@ "fundingReferences" => [ { "funderIdentifier" => "https://doi.org/10.0001/example.one", - "funderIdentifierType" => "Crossref Funder ID" + "funderIdentifierType" => "Crossref Funder ID", }, ], }, diff --git a/spec/models/affiliation_identifier_spec.rb b/spec/models/affiliation_identifier_spec.rb index 9014edc0..803f58d9 100644 --- a/spec/models/affiliation_identifier_spec.rb +++ b/spec/models/affiliation_identifier_spec.rb @@ -61,7 +61,8 @@ { "attributes" => { "doi" => "10.1234/example-doi", - "relatedIdentifiers" => [{ "relatedIdentifierType" => "IsSupplementTo", "relatedIdentifier" => "10.5678/some-related-doi" }], + "relatedIdentifiers" => [{ "relatedIdentifierType" => "IsSupplementTo", + "relatedIdentifier" => "10.5678/some-related-doi" }], "creators" => [ { "affiliation" => [ @@ -81,7 +82,7 @@ "sourceId" => "datacite_affiliation", } end - + context "when STAFF_ADMIN_TOKEN is present" do before do allow(ENV).to receive(:[]).with("STAFF_ADMIN_TOKEN").and_return("example_admin_token") @@ -91,9 +92,10 @@ allow(ENV).to receive(:[]).with("DATACITE_AFFILIATION_SOURCE_TOKEN").and_return("DATACITE_AFFILIATION_SOURCE_TOKEN") allow(Rails.logger).to receive(:info) end - + it "pushes affiliation identifiers to the Event Data service" do - allow(Maremma).to receive(:post).and_return(OpenStruct.new(status: 200, body: { "data" => { "id" => "example_id" } })) + allow(Maremma).to receive(:post).and_return(OpenStruct.new(status: 200, + body: { "data" => { "id" => "example_id" } })) push_url = "https://fake.lagattino.com/events" expected_data = { "data" => { @@ -113,9 +115,9 @@ }, }, } - + expect(Rails.logger).to receive(:info).with("[Event Data] https://doi.org/10.1234/example-doi is_authored_at https://ror.org/02catss52 pushed to Event Data service.") - + stub_request(:post, push_url). with( body: expected_data.to_json, @@ -126,19 +128,20 @@ }, ). to_return(status: 200, body: { "data" => { "id" => "example_id" } }.to_json, headers: {}) - + AffiliationIdentifier.push_item(item) end - + it "skips pushing if STAFF_ADMIN_TOKEN is not present" do allow(ENV).to receive(:[]).with("STAFF_ADMIN_TOKEN").and_return(nil) expect(Maremma).not_to receive(:post) - + AffiliationIdentifier.push_item(item) end it "returns 409 for already pushed events" do - allow(Maremma).to receive(:post).and_return(OpenStruct.new(status: 409, body: { "data" => { "id" => "example_id" } })) + allow(Maremma).to receive(:post).and_return(OpenStruct.new(status: 409, + body: { "data" => { "id" => "example_id" } })) push_url = "https://fake.lagattino.com/events" expected_data = { "data" => { @@ -158,9 +161,9 @@ }, }, } - + expect(Rails.logger).to receive(:info).with("[Event Data] https://doi.org/10.1234/example-doi is_authored_at https://ror.org/02catss52 already pushed to Event Data service.") - + stub_request(:post, push_url). with( body: expected_data.to_json, @@ -171,12 +174,13 @@ }, ). to_return(status: 200, body: { "data" => { "id" => "example_id" } }.to_json, headers: {}) - + AffiliationIdentifier.push_item(item) end it "returns 500 when there is error while pushing an event" do - allow(Maremma).to receive(:post).and_return(OpenStruct.new(status: 500, body: { "errors" => "An error occurred during the put request." })) + allow(Maremma).to receive(:post).and_return(OpenStruct.new(status: 500, + body: { "errors" => "An error occurred during the put request." })) allow(Rails.logger).to receive(:error) push_url = "https://fake.lagattino.com/events" @@ -198,9 +202,9 @@ }, }, } - + expect(Rails.logger).to receive(:error).with("[Event Data] https://doi.org/10.1234/example-doi is_authored_at https://ror.org/02catss52 had an error: An error occurred during the put request.") - + stub_request(:post, push_url). with( body: expected_data.to_json, @@ -211,7 +215,7 @@ }, ). to_return(status: 200, body: { "data" => { "id" => "example_id" } }.to_json, headers: {}) - + AffiliationIdentifier.push_item(item) end end @@ -232,7 +236,6 @@ response = AffiliationIdentifier.get_ror_metadata(id) expect(response).to eq({}) end - end end end diff --git a/spec/models/crossref_spec.rb b/spec/models/crossref_spec.rb index 37395abc..b6f095e1 100644 --- a/spec/models/crossref_spec.rb +++ b/spec/models/crossref_spec.rb @@ -33,19 +33,18 @@ it "queues jobs for the default date range (yesterday to today)" do # Stub Date.current to return a fixed date allow(Date).to receive(:current).and_return(Date.new(2023, 1, 2)) - + # Use a spy on Date.parse date_spy = spy("Date") allow(Date).to receive(:parse).and_wrap_original do |original_method, *args| date_spy.parse(*args) original_method.call(*args) end - + # Mocking Date.parse response = Crossref.import - - expect(response).to be_a(Integer).and be >= 0 + expect(response).to be_a(Integer).and be >= 0 end end end diff --git a/spec/models/name_identifier_spec.rb b/spec/models/name_identifier_spec.rb index bcd2b476..adb78d9b 100644 --- a/spec/models/name_identifier_spec.rb +++ b/spec/models/name_identifier_spec.rb @@ -56,59 +56,59 @@ end before(:each) do - allow(ENV) - .to(receive(:[]) - .with(staff_admin_token) - .and_return(staff_admin_token)) - - allow(ENV) - .to(receive(:[]) - .with("LAGOTTINO_URL") - .and_return("https://fake.lagattino.com")) - - allow(ENV) - .to(receive(:[]) - .with("DATACITE_ORCID_AUTO_UPDATE_SOURCE_TOKEN") - .and_return("DATACITE_ORCID_AUTO_UPDATE_SOURCE_TOKEN")) - - allow(ENV) - .to(receive(:[]) - .with(staff_profiles_admin_token) - .and_return(staff_profiles_admin_token)) - - allow(ENV) - .to(receive(:[]) - .with("VOLPINO_URL") - .and_return("https://fake.volpino.com")) - - allow(Base) - .to(receive(:cached_datacite_response) - .and_return("foo" => "bar")) - - allow(Base) - .to(receive(:cached_orcid_response) - .and_return("bar" => "foo")) - - allow(Maremma) - .to(receive(:post) - .with("https://fake.lagattino.com/events", - data: lagottino_json, - accept: "application/vnd.api+json; version=2", - content_type: "application/vnd.api+json", - bearer: staff_admin_token) - .and_return(OpenStruct.new(status: 200))) - - allow(Maremma) - .to(receive(:post) - .with("https://fake.volpino.com/claims", - data: volpino_json, - content_type: "application/json", - bearer: staff_profiles_admin_token) - .and_return(OpenStruct.new(status: 202))) - - allow(Time) - .to(receive_message_chain(:zone, :now, :iso8601) - .and_return("2023-11-15T12:17:47Z")) + allow(ENV). + to(receive(:[]). + with(staff_admin_token). + and_return(staff_admin_token)) + + allow(ENV). + to(receive(:[]). + with("LAGOTTINO_URL"). + and_return("https://fake.lagattino.com")) + + allow(ENV). + to(receive(:[]). + with("DATACITE_ORCID_AUTO_UPDATE_SOURCE_TOKEN"). + and_return("DATACITE_ORCID_AUTO_UPDATE_SOURCE_TOKEN")) + + allow(ENV). + to(receive(:[]). + with(staff_profiles_admin_token). + and_return(staff_profiles_admin_token)) + + allow(ENV). + to(receive(:[]). + with("VOLPINO_URL"). + and_return("https://fake.volpino.com")) + + allow(Base). + to(receive(:cached_datacite_response). + and_return("foo" => "bar")) + + allow(Base). + to(receive(:cached_orcid_response). + and_return("bar" => "foo")) + + allow(Maremma). + to(receive(:post). + with("https://fake.lagattino.com/events", + data: lagottino_json, + accept: "application/vnd.api+json; version=2", + content_type: "application/vnd.api+json", + bearer: staff_admin_token). + and_return(OpenStruct.new(status: 200))) + + allow(Maremma). + to(receive(:post). + with("https://fake.volpino.com/claims", + data: volpino_json, + content_type: "application/json", + bearer: staff_profiles_admin_token). + and_return(OpenStruct.new(status: 202))) + + allow(Time). + to(receive_message_chain(:zone, :now, :iso8601). + and_return("2023-11-15T12:17:47Z")) end describe "returns nil" do @@ -209,10 +209,10 @@ "attributes" => { "doi" => "https://doi.org/10.0001/foo.bar", "updated" => "2023-11-15", - "relatedIdentifiers": [ + relatedIdentifiers: [ { "relatedIdentifierType" => "DOI" }, ], - "creators": [], + creators: [], }, } @@ -248,9 +248,9 @@ expect(NameIdentifier.push_item(item)).to(eq(1)) - expect(Maremma) - .to(have_received(:post) - .with( + expect(Maremma). + to(have_received(:post). + with( "https://fake.lagattino.com/events", data: lagottino_json, bearer: staff_admin_token, @@ -282,9 +282,9 @@ expect(NameIdentifier.push_item(item)).to(eq(1)) - expect(Maremma) - .not_to(have_received(:post) - .with( + expect(Maremma). + not_to(have_received(:post). + with( "https://fake.lagattino.com/events", data: lagottino_json, bearer: staff_admin_token, @@ -315,22 +315,23 @@ NameIdentifier.push_item(item) - expect(Maremma) - .to(have_received(:post) - .with( + expect(Maremma). + to(have_received(:post). + with( "https://fake.volpino.com/claims", data: volpino_json, bearer: staff_profiles_admin_token, - content_type: "application/json")) + content_type: "application/json", + )) end end describe "is invalid" do it "does not make request to volpino" do - allow(ENV) - .to(receive(:[]) - .with(staff_profiles_admin_token) - .and_return(nil)) + allow(ENV). + to(receive(:[]). + with(staff_profiles_admin_token). + and_return(nil)) item = { "attributes" => { @@ -347,13 +348,14 @@ }, } - expect(Maremma) - .not_to(have_received(:post) - .with( + expect(Maremma). + not_to(have_received(:post). + with( "https://fake.volpino.com/claims", data: volpino_json, bearer: staff_profiles_admin_token, - content_type: "application/json")) + content_type: "application/json", + )) end end end diff --git a/spec/models/related_identifier_spec.rb b/spec/models/related_identifier_spec.rb index e644bfe0..ff9990d8 100644 --- a/spec/models/related_identifier_spec.rb +++ b/spec/models/related_identifier_spec.rb @@ -1,12 +1,12 @@ -require 'rails_helper' +require "rails_helper" describe RelatedIdentifier, type: :model, vcr: true do - let(:from_date) { '2018-01-04' } - let(:until_date) { '2018-08-05' } - let(:doi) { '10.5061/dryad.j86rt6b' } + let(:from_date) { "2018-01-04" } + let(:until_date) { "2018-08-05" } + let(:doi) { "10.5061/dryad.j86rt6b" } - describe '.import_by_month' do - it 'queues jobs for DOIs created within the specified month range' do + describe ".import_by_month" do + it "queues jobs for DOIs created within the specified month range" do allow(RelatedIdentifierImportByMonthJob).to receive(:perform_later) response = RelatedIdentifier.import_by_month(from_date: from_date, until_date: until_date) expect(RelatedIdentifierImportByMonthJob).to have_received(:perform_later).at_least(:once) @@ -14,37 +14,38 @@ end end - describe '.import' do - it 'queues jobs for DOIs updated within the specified date range' do + describe ".import" do + it "queues jobs for DOIs updated within the specified date range" do allow_any_instance_of(RelatedIdentifier).to receive(:queue_jobs).and_return(97) response = RelatedIdentifier.import(from_date: from_date, until_date: until_date) expect(response).to eq(97) end end - describe '#source_id' do - it 'returns the source_id' do + describe "#source_id" do + it "returns the source_id" do related_identifier = RelatedIdentifier.new - expect(related_identifier.source_id).to eq('datacite_related') + expect(related_identifier.source_id).to eq("datacite_related") end end - describe '#query' do - it 'returns the query string for related identifiers' do + describe "#query" do + it "returns the query string for related identifiers" do related_identifier = RelatedIdentifier.new - expect(related_identifier.query).to eq('relatedIdentifiers.relatedIdentifierType:DOI') + expect(related_identifier.query).to eq("relatedIdentifiers.relatedIdentifierType:DOI") end end - describe '#push_data' do - it 'pushes data and returns the number of items processed' do - result = double(body: { 'data' => [{ 'id' => 'example_id', 'attributes' => { 'doi' => 'example_doi', 'updated' => '2023-01-01' } }] }) - allow_any_instance_of(RelatedIdentifier).to receive(:cached_doi_ra).and_return('DataCite') + describe "#push_data" do + it "pushes data and returns the number of items processed" do + result = double(body: { "data" => [{ "id" => "example_id", + "attributes" => { "doi" => "example_doi", "updated" => "2023-01-01" } }] }) + allow_any_instance_of(RelatedIdentifier).to receive(:cached_doi_ra).and_return("DataCite") allow(Maremma).to receive(:post).and_return(double(status: 201)) allow(Time).to receive(:zone).and_return(double(now: Time.new(2023, 1, 1))) # Create a spy for perform_later method - job_spy = class_double('RelatedIdentifierImportJob', perform_later: true) + job_spy = class_double("RelatedIdentifierImportJob", perform_later: true) allow(RelatedIdentifierImportJob).to receive(:perform_later).and_return(job_spy) response = RelatedIdentifier.new.push_data(result) @@ -52,53 +53,52 @@ expect(RelatedIdentifierImportJob).to have_received(:perform_later).once expect(response).to eq(1) end - - it 'returns errors if present in the API response' do - result = double(body: { 'errors' => 'Example error' }) + it "returns errors if present in the API response" do + result = double(body: { "errors" => "Example error" }) response = RelatedIdentifier.new.push_data(result) - expect(response).to eq('Example error') + expect(response).to eq("Example error") end end - describe '#push_item' do - let(:valid_doi) { 'https://doi.org/10.0001/foo.bar' } - let(:valid_related_identifier) { 'https://doi.org/10.0001/example.one' } + describe "#push_item" do + let(:valid_doi) { "https://doi.org/10.0001/foo.bar" } + let(:valid_related_identifier) { "https://doi.org/10.0001/example.one" } let(:item) do { - 'attributes' => { - 'doi' => 'https://doi.org/10.1234/example', - 'updated' => '2023-11-15', - 'relatedIdentifiers' => [ + "attributes" => { + "doi" => "https://doi.org/10.1234/example", + "updated" => "2023-11-15", + "relatedIdentifiers" => [ { - 'relatedIdentifierType' => 'DOI', - 'relatedIdentifier' => 'https://doi.org/10.5678/related', - 'relationType' => 'example-type' - } - ] - } + "relatedIdentifierType" => "DOI", + "relatedIdentifier" => "https://doi.org/10.5678/related", + "relationType" => "example-type", + }, + ], + }, } end - context 'when the DOI and related identifiers are valid' do + context "when the DOI and related identifiers are valid" do before do - allow(ENV).to receive(:[]).with('STAFF_ADMIN_TOKEN').and_return('STAFF_ADMIN_TOKEN') - allow(ENV).to receive(:[]).with('LAGOTTINO_URL').and_return('https://fake.lagattino.com') - allow(ENV).to receive(:[]).with('DATACITE_RELATED_SOURCE_TOKEN').and_return('DATACITE_RELATED_SOURCE_TOKEN') - allow(ENV).to receive(:[]).with('USER_AGENT').and_return('default_user_agent') + allow(ENV).to receive(:[]).with("STAFF_ADMIN_TOKEN").and_return("STAFF_ADMIN_TOKEN") + allow(ENV).to receive(:[]).with("LAGOTTINO_URL").and_return("https://fake.lagattino.com") + allow(ENV).to receive(:[]).with("DATACITE_RELATED_SOURCE_TOKEN").and_return("DATACITE_RELATED_SOURCE_TOKEN") + allow(ENV).to receive(:[]).with("USER_AGENT").and_return("default_user_agent") allow(ENV).to receive(:[]).with("EVENTDATA_TOKEN").and_return("EVENTDATA_TOKEN") allow(ENV).to receive(:[]).with("EVENTDATA_URL").and_return("https://fake.eventdataurl.com") - allow(Base).to receive(:cached_datacite_response).and_return({ 'foo' => 'bar' }) + allow(Base).to receive(:cached_datacite_response).and_return({ "foo" => "bar" }) allow(Maremma).to receive(:post).and_return(OpenStruct.new(status: 201)) - allow(Time.zone).to receive(:now).and_return(Time.zone.parse('2023-11-15T12:17:47Z')) + allow(Time.zone).to receive(:now).and_return(Time.zone.parse("2023-11-15T12:17:47Z")) end - it 'queues jobs and pushes to Event Data services' do + it "queues jobs and pushes to Event Data services" do related_identifier = RelatedIdentifier.new - allow(related_identifier).to receive(:normalize_doi).with(valid_doi).and_return('normalized_doi') - allow(related_identifier).to receive(:normalize_doi).with(valid_related_identifier).and_return('normalized_related_identifier') - allow(related_identifier).to receive(:validate_prefix).with(valid_related_identifier).and_return('datacite') - allow(RelatedIdentifier).to receive(:cached_doi_ra).and_return('DataCite') + allow(related_identifier).to receive(:normalize_doi).with(valid_doi).and_return("normalized_doi") + allow(related_identifier).to receive(:normalize_doi).with(valid_related_identifier).and_return("normalized_related_identifier") + allow(related_identifier).to receive(:validate_prefix).with(valid_related_identifier).and_return("datacite") + allow(RelatedIdentifier).to receive(:cached_doi_ra).and_return("DataCite") allow(RelatedIdentifier).to receive(:cached_datacite_response).and_return({}) allow(related_identifier).to receive(:set_event_for_bus).and_return({}) allow(Rails.logger).to receive(:info) @@ -111,103 +111,102 @@ end end - context 'when the DOI and related identifiers are valid and already pushed' do + context "when the DOI and related identifiers are valid and already pushed" do before do - allow(ENV).to receive(:[]).with('STAFF_ADMIN_TOKEN').and_return('STAFF_ADMIN_TOKEN') - allow(ENV).to receive(:[]).with('LAGOTTINO_URL').and_return('https://fake.lagattino.com') - allow(ENV).to receive(:[]).with('DATACITE_RELATED_SOURCE_TOKEN').and_return('DATACITE_RELATED_SOURCE_TOKEN') - allow(ENV).to receive(:[]).with('USER_AGENT').and_return('default_user_agent') + allow(ENV).to receive(:[]).with("STAFF_ADMIN_TOKEN").and_return("STAFF_ADMIN_TOKEN") + allow(ENV).to receive(:[]).with("LAGOTTINO_URL").and_return("https://fake.lagattino.com") + allow(ENV).to receive(:[]).with("DATACITE_RELATED_SOURCE_TOKEN").and_return("DATACITE_RELATED_SOURCE_TOKEN") + allow(ENV).to receive(:[]).with("USER_AGENT").and_return("default_user_agent") allow(ENV).to receive(:[]).with("EVENTDATA_TOKEN").and_return("EVENTDATA_TOKEN") allow(ENV).to receive(:[]).with("EVENTDATA_URL").and_return("https://fake.eventdataurl.com") - allow(Base).to receive(:cached_datacite_response).and_return({ 'foo' => 'bar' }) + allow(Base).to receive(:cached_datacite_response).and_return({ "foo" => "bar" }) allow(Maremma).to receive(:post).and_return(OpenStruct.new(status: 409)) - allow(Time.zone).to receive(:now).and_return(Time.zone.parse('2023-11-15T12:17:47Z')) + allow(Time.zone).to receive(:now).and_return(Time.zone.parse("2023-11-15T12:17:47Z")) end - it 'queues jobs and pushes to Event Data services' do + it "queues jobs and pushes to Event Data services" do related_identifier = RelatedIdentifier.new - allow(related_identifier).to receive(:normalize_doi).with(valid_doi).and_return('normalized_doi') - allow(related_identifier).to receive(:normalize_doi).with(valid_related_identifier).and_return('normalized_related_identifier') - allow(related_identifier).to receive(:validate_prefix).with(valid_related_identifier).and_return('datacite') - allow(RelatedIdentifier).to receive(:cached_doi_ra).and_return('DataCite') + allow(related_identifier).to receive(:normalize_doi).with(valid_doi).and_return("normalized_doi") + allow(related_identifier).to receive(:normalize_doi).with(valid_related_identifier).and_return("normalized_related_identifier") + allow(related_identifier).to receive(:validate_prefix).with(valid_related_identifier).and_return("datacite") + allow(RelatedIdentifier).to receive(:cached_doi_ra).and_return("DataCite") allow(RelatedIdentifier).to receive(:cached_datacite_response).and_return({}) allow(related_identifier).to receive(:set_event_for_bus).and_return({}) allow(Rails.logger).to receive(:info) expect(RelatedIdentifier.push_item(item)).to eq(1) expect(Maremma).to have_received(:post).twice - + expect(Rails.logger).to have_received(:info).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related already pushed to Event Data service.") expect(Rails.logger).to have_received(:info).with("[Event Data Bus] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related already pushed to Event Data service.") - end end - context 'when there is error while creating the event' do + context "when there is error while creating the event" do let(:error_message) { "An error occurred during the put request." } before do - allow(ENV).to receive(:[]).with('STAFF_ADMIN_TOKEN').and_return('STAFF_ADMIN_TOKEN') - allow(ENV).to receive(:[]).with('LAGOTTINO_URL').and_return('https://fake.lagattino.com') - allow(ENV).to receive(:[]).with('DATACITE_RELATED_SOURCE_TOKEN').and_return('DATACITE_RELATED_SOURCE_TOKEN') - allow(ENV).to receive(:[]).with('USER_AGENT').and_return('default_user_agent') + allow(ENV).to receive(:[]).with("STAFF_ADMIN_TOKEN").and_return("STAFF_ADMIN_TOKEN") + allow(ENV).to receive(:[]).with("LAGOTTINO_URL").and_return("https://fake.lagattino.com") + allow(ENV).to receive(:[]).with("DATACITE_RELATED_SOURCE_TOKEN").and_return("DATACITE_RELATED_SOURCE_TOKEN") + allow(ENV).to receive(:[]).with("USER_AGENT").and_return("default_user_agent") allow(ENV).to receive(:[]).with("EVENTDATA_TOKEN").and_return("EVENTDATA_TOKEN") allow(ENV).to receive(:[]).with("EVENTDATA_URL").and_return("https://fake.eventdataurl.com") - allow(Base).to receive(:cached_datacite_response).and_return({ 'foo' => 'bar' }) + allow(Base).to receive(:cached_datacite_response).and_return({ "foo" => "bar" }) allow(Maremma).to receive(:post) do |_, _options| OpenStruct.new(status: 500, body: { "errors" => error_message }) end - allow(Time.zone).to receive(:now).and_return(Time.zone.parse('2023-11-15T12:17:47Z')) + allow(Time.zone).to receive(:now).and_return(Time.zone.parse("2023-11-15T12:17:47Z")) end - it 'queues jobs and pushes to Event Data services' do + it "queues jobs and pushes to Event Data services" do related_identifier = RelatedIdentifier.new - allow(related_identifier).to receive(:normalize_doi).with(valid_doi).and_return('normalized_doi') - allow(related_identifier).to receive(:normalize_doi).with(valid_related_identifier).and_return('normalized_related_identifier') - allow(related_identifier).to receive(:validate_prefix).with(valid_related_identifier).and_return('datacite') - allow(RelatedIdentifier).to receive(:cached_doi_ra).and_return('DataCite') + allow(related_identifier).to receive(:normalize_doi).with(valid_doi).and_return("normalized_doi") + allow(related_identifier).to receive(:normalize_doi).with(valid_related_identifier).and_return("normalized_related_identifier") + allow(related_identifier).to receive(:validate_prefix).with(valid_related_identifier).and_return("datacite") + allow(RelatedIdentifier).to receive(:cached_doi_ra).and_return("DataCite") allow(RelatedIdentifier).to receive(:cached_datacite_response).and_return({}) allow(related_identifier).to receive(:set_event_for_bus).and_return({}) allow(Rails.logger).to receive(:error) expect(RelatedIdentifier.push_item(item)).to eq(1) expect(Maremma).to have_received(:post).twice - + expect(Rails.logger).to have_received(:error).with("[Event Data] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related had an error: An error occurred during the put request.") expect(Rails.logger).to have_received(:error).with("[Event Data Bus] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related had an error:") end - it 'Does not sent an event to Event Data Bus.' do + it "Does not sent an event to Event Data Bus." do allow(ENV).to receive(:[]).with("EVENTDATA_TOKEN").and_return(nil) related_identifier = RelatedIdentifier.new - allow(related_identifier).to receive(:normalize_doi).with(valid_doi).and_return('normalized_doi') - allow(related_identifier).to receive(:normalize_doi).with(valid_related_identifier).and_return('normalized_related_identifier') - allow(related_identifier).to receive(:validate_prefix).with(valid_related_identifier).and_return('datacite') - allow(RelatedIdentifier).to receive(:cached_doi_ra).and_return('DataCite') + allow(related_identifier).to receive(:normalize_doi).with(valid_doi).and_return("normalized_doi") + allow(related_identifier).to receive(:normalize_doi).with(valid_related_identifier).and_return("normalized_related_identifier") + allow(related_identifier).to receive(:validate_prefix).with(valid_related_identifier).and_return("datacite") + allow(RelatedIdentifier).to receive(:cached_doi_ra).and_return("DataCite") allow(RelatedIdentifier).to receive(:cached_datacite_response).and_return({}) allow(related_identifier).to receive(:set_event_for_bus).and_return({}) allow(Rails.logger).to receive(:info) expect(RelatedIdentifier.push_item(item)).to eq(1) expect(Maremma).to have_received(:post).once - + expect(Rails.logger).to have_received(:info).with("[Event Data Bus] https://doi.org/10.1234/example example_type https://doi.org/10.5678/related was not sent to Event Data Bus.") end end - - context 'when the DOI is blank' do - it 'returns nil' do + + context "when the DOI is blank" do + it "returns nil" do allow(Maremma).to receive(:post) item = { - 'attributes' => { - 'doi' => nil, - 'relatedIdentifiers' => [ + "attributes" => { + "doi" => nil, + "relatedIdentifiers" => [ { - 'relatedIdentifierType' => 'DOI', - 'relatedIdentifier' => valid_related_identifier, - 'relationType' => 'example-one' - } - ] - } + "relatedIdentifierType" => "DOI", + "relatedIdentifier" => valid_related_identifier, + "relationType" => "example-one", + }, + ], + }, } expect(RelatedIdentifier.push_item(item)).to eq(nil) diff --git a/spec/models/related_url_spec.rb b/spec/models/related_url_spec.rb index 6de62ecf..7c82b8a5 100644 --- a/spec/models/related_url_spec.rb +++ b/spec/models/related_url_spec.rb @@ -45,7 +45,7 @@ allow(ENV).to(receive(:[]).with("STAFF_ADMIN_TOKEN").and_return("STAFF_ADMIN_TOKEN")) allow(ENV).to(receive(:[]).with("LAGOTTINO_URL").and_return("https://fake.lagattino.com")) allow(ENV).to(receive(:[]).with("DATACITE_URL_SOURCE_TOKEN").and_return("DATACITE_URL_SOURCE_TOKEN")) - allow(Base).to(receive(:cached_datacite_response).and_return({"foo" => "bar"})) + allow(Base).to(receive(:cached_datacite_response).and_return({ "foo" => "bar" })) allow(Maremma).to(receive(:post).and_return(OpenStruct.new(status: 200))) allow(Time).to(receive_message_chain(:zone, :now, :iso8601).and_return("2023-11-15T12:17:47Z")) end @@ -91,12 +91,12 @@ "relatedIdentifierType" => "URL", "relatedIdentifier" => "https://doi.org/10.0001/example.one", "relationType" => "example-one", - } + }, ], }, } - json_data = ({ + json_data = { "data" => { "type" => "events", "attributes" => { @@ -109,21 +109,21 @@ "occurredAt" => "2023-11-15", "timestamp" => "2023-11-15T12:17:47Z", "license" => "https://creativecommons.org/publicdomain/zero/1.0/", - "subj" => {"foo" => "bar"}, - "obj" => {} - } - } - }).to_json + "subj" => { "foo" => "bar" }, + "obj" => {}, + }, + }, + }.to_json expect(RelatedUrl.push_item(item)).to(eq(1)) expect(Maremma).to(have_received(:post).with( - "https://fake.lagattino.com/events", - data: json_data, - bearer: "STAFF_ADMIN_TOKEN", - content_type: "application/vnd.api+json", - accept: "application/vnd.api+json; version=2" - )) + "https://fake.lagattino.com/events", + data: json_data, + bearer: "STAFF_ADMIN_TOKEN", + content_type: "application/vnd.api+json", + accept: "application/vnd.api+json; version=2", + )) end end