From 48a2c2a037a21e428588da75c01b9c8765de1d09 Mon Sep 17 00:00:00 2001 From: Wendel Fabian Chinsamy Date: Thu, 16 Nov 2023 13:46:59 +0200 Subject: [PATCH 1/2] add specs for funder identifier --- config/application.rb | 10 +- ...ose_related_identifiers_with_type_URL_.yml | 69 +++++++++++ .../will_not_make_request_to_lagottino.yml | 69 +++++++++++ spec/lib/tasks/funder_identifier_rake_spec.rb | 114 ++++++++++++++++++ spec/models/related_url_spec.rb | 4 +- 5 files changed, 259 insertions(+), 7 deletions(-) create mode 100644 spec/fixtures/vcr_cassettes/RelatedUrl/class_methods/push_item/when_STAFF_ADMIN_TOKEN/is_invalid/is_not_set_will_only_request_logottino_for_those_related_identifiers_with_type_URL_.yml create mode 100644 spec/fixtures/vcr_cassettes/RelatedUrl/class_methods/push_item/when_STAFF_ADMIN_TOKEN/is_invalid/will_not_make_request_to_lagottino.yml diff --git a/config/application.rb b/config/application.rb index dffa9c28..d47a93f1 100644 --- a/config/application.rb +++ b/config/application.rb @@ -21,11 +21,11 @@ # load ENV variables from container environment if json file exists # see https://github.com/phusion/baseimage-docker#envvar_dumps -env_json_file = "/etc/container_environment.json" -if File.exist?(env_json_file) - env_vars = JSON.parse(File.read(env_json_file)) - env_vars.each { |k, v| ENV[k] = v } -end +# env_json_file = "/etc/container_environment.json" +# if File.exist?(env_json_file) +# env_vars = JSON.parse(File.read(env_json_file)) +# env_vars.each { |k, v| ENV[k] = v } +# end # default values for some ENV variables ENV["APPLICATION"] ||= "levriero" diff --git a/spec/fixtures/vcr_cassettes/RelatedUrl/class_methods/push_item/when_STAFF_ADMIN_TOKEN/is_invalid/is_not_set_will_only_request_logottino_for_those_related_identifiers_with_type_URL_.yml b/spec/fixtures/vcr_cassettes/RelatedUrl/class_methods/push_item/when_STAFF_ADMIN_TOKEN/is_invalid/is_not_set_will_only_request_logottino_for_those_related_identifiers_with_type_URL_.yml new file mode 100644 index 00000000..39359111 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/RelatedUrl/class_methods/push_item/when_STAFF_ADMIN_TOKEN/is_invalid/is_not_set_will_only_request_logottino_for_those_related_identifiers_with_type_URL_.yml @@ -0,0 +1,69 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.stage.datacite.org/dois/10.0001/foo.bar + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Mozilla/5.0 (compatible; Maremma/4.9.8; mailto:info@datacite.org) + Accept: + - text/html,application/json,application/xml;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5 + Accept-Encoding: + - gzip,deflate + response: + status: + code: 404 + message: Not Found + headers: + Date: + - Thu, 16 Nov 2023 09:49:41 GMT + Content-Type: + - application/json; charset=utf-8 + Connection: + - keep-alive + Status: + - 404 Not Found + Cache-Control: + - no-cache + Vary: + - Accept-Encoding + Content-Encoding: + - gzip + Referrer-Policy: + - strict-origin-when-cross-origin + X-Permitted-Cross-Domain-Policies: + - none + X-Xss-Protection: + - 1; mode=block + X-Request-Id: + - 7117c4d8-8b01-4c9a-8c60-24d19df01c5e + X-Download-Options: + - noopen + X-Runtime: + - '0.012978' + X-Frame-Options: + - SAMEORIGIN + X-Content-Type-Options: + - nosniff + X-Powered-By: + - Phusion Passenger(R) 6.0.18 + Server: + - nginx/1.18.0 + Phusion Passenger(R) 6.0.18 + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Methods: + - GET, POST, PUT, PATCH, DELETE, OPTIONS + Access-Control-Allow-Headers: + - Accept,Access-Control-Allow-Origin,Access-Control-Expose-Headers,Access-Control-Allow-Methods,Access-Control-Allow-Headers,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With + Access-Control-Expose-Headers: + - Authorization + body: + encoding: ASCII-8BIT + string: !binary |- + H4sIALXlVWUAAwzHMQqAMAwF0KuELC4iDp08h5s4FP1qsTSQpKCId9ftvYehKmo8TA+bR68/OfSBW/bkGf/GA6QwqbqAbqkUFZRFzlR22kRpFVhpnHAl847f+f0AAAD//wMArpOW4VcAAAA= + http_version: null + recorded_at: Thu, 16 Nov 2023 09:49:41 GMT +recorded_with: VCR 5.1.0 diff --git a/spec/fixtures/vcr_cassettes/RelatedUrl/class_methods/push_item/when_STAFF_ADMIN_TOKEN/is_invalid/will_not_make_request_to_lagottino.yml b/spec/fixtures/vcr_cassettes/RelatedUrl/class_methods/push_item/when_STAFF_ADMIN_TOKEN/is_invalid/will_not_make_request_to_lagottino.yml new file mode 100644 index 00000000..8bbcd336 --- /dev/null +++ b/spec/fixtures/vcr_cassettes/RelatedUrl/class_methods/push_item/when_STAFF_ADMIN_TOKEN/is_invalid/will_not_make_request_to_lagottino.yml @@ -0,0 +1,69 @@ +--- +http_interactions: +- request: + method: get + uri: https://api.stage.datacite.org/dois/10.0001/foo.bar + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Mozilla/5.0 (compatible; Maremma/4.9.8; mailto:info@datacite.org) + Accept: + - text/html,application/json,application/xml;q=0.9, text/plain;q=0.8,image/png,*/*;q=0.5 + Accept-Encoding: + - gzip,deflate + response: + status: + code: 404 + message: Not Found + headers: + Date: + - Thu, 16 Nov 2023 11:09:44 GMT + Content-Type: + - application/json; charset=utf-8 + Connection: + - keep-alive + Status: + - 404 Not Found + Cache-Control: + - no-cache + Vary: + - Accept-Encoding + Content-Encoding: + - gzip + Referrer-Policy: + - strict-origin-when-cross-origin + X-Permitted-Cross-Domain-Policies: + - none + X-Xss-Protection: + - 1; mode=block + X-Request-Id: + - 70080989-8273-4aac-9f2a-18a0f02ef24f + X-Download-Options: + - noopen + X-Runtime: + - '0.013436' + X-Frame-Options: + - SAMEORIGIN + X-Content-Type-Options: + - nosniff + X-Powered-By: + - Phusion Passenger(R) 6.0.18 + Server: + - nginx/1.18.0 + Phusion Passenger(R) 6.0.18 + Access-Control-Allow-Credentials: + - 'true' + Access-Control-Allow-Methods: + - GET, POST, PUT, PATCH, DELETE, OPTIONS + Access-Control-Allow-Headers: + - Accept,Access-Control-Allow-Origin,Access-Control-Expose-Headers,Access-Control-Allow-Methods,Access-Control-Allow-Headers,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Mx-ReqToken,X-Requested-With + Access-Control-Expose-Headers: + - Authorization + body: + encoding: ASCII-8BIT + string: !binary |- + H4sIAHj4VWUAAwzHMQqAMAwF0KuELC4iDp08h5s4FP1qsTSQpKCId9ftvYehKmo8TA+bR68/OfSBW/bkGf/GA6QwqbqAbqkUFZRFzlR22kRpFVhpnHAl847f+f0AAAD//wMArpOW4VcAAAA= + http_version: null + recorded_at: Thu, 16 Nov 2023 11:09:44 GMT +recorded_with: VCR 5.1.0 diff --git a/spec/lib/tasks/funder_identifier_rake_spec.rb b/spec/lib/tasks/funder_identifier_rake_spec.rb index 2edead47..ac499f83 100644 --- a/spec/lib/tasks/funder_identifier_rake_spec.rb +++ b/spec/lib/tasks/funder_identifier_rake_spec.rb @@ -50,3 +50,117 @@ expect(enqueued_jobs.last[:job]).to be(FunderIdentifierImportJob) end end + +describe "push_item" do + it "returns nil when the doi is blank" do + expect(FunderIdentifier.push_item({attributes: {doi: nil}})).to(eq(nil)) + end + + describe "when STAFF_ADMIN_TOKEN" do + 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_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(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 + + describe "is valid" do + it "makes request to lagottino for those funder identifiers with funder identifier type 'Crossref Funder Id'" do + item = { + "attributes" => { + "doi" => "https://doi.org/10.0001/foo.bar", + "updated" => "2023-11-15", + "fundingReferences" => [ + { + "funderIdentifier" => "https://doi.org/10.0001/example.one", + "funderIdentifierType" => "Crossref Funder ID" + }, + { + "funderIdentifier" => "https://doi.org/10.0001/example.two", + "funderIdentifierType" => "Foo" + }, + { + "funderIdentifier" => "https://doi.org/10.0001/example.three", + "funderIdentifierType" => "Crossref Funder ID" + }, + ] + } + } + + expect(FunderIdentifier.push_item(item)).to(eq(2)) + + expect(Maremma).to(have_received(:post).twice) + end + + it "passes the expected values to lagottino" do + item = { + "attributes" => { + "doi" => "https://doi.org/10.0001/foo.bar", + "updated" => "2023-11-15", + "fundingReferences" => [ + { + "funderIdentifier" => "https://doi.org/10.0001/example.one", + "funderIdentifierType" => "Crossref Funder ID" + }, + ], + }, + } + + json_data = ({ + "data" => { + "type" => "events", + "attributes" => { + "messageAction" => "create", + "subjId" => "https://doi.org/10.0001/foo.bar", + "objId" => "https://doi.org/10.0001/example.one", + "relationTypeId" => "is-funded-by", + "sourceId" => "datacite-funder", + "sourceToken" => "DATACITE_FUNDER_SOURCE_TOKEN", + "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 + + 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" + )) + end + end + + describe "is invalid" do + it "will not make request to lagottino" do + allow(ENV).to(receive(:[]).with("STAFF_ADMIN_TOKEN").and_return(nil)) + + item = { + "attributes" => { + "doi" => "https://doi.org/10.0001/foo.bar", + "updated" => "2023-11-15", + "fundingReferences" => [ + { + "funderIdentifier" => "https://doi.org/10.0001/example.one", + "funderIdentifierType" => "Crossref Funder ID" + }, + ], + }, + } + + expect(FunderIdentifier.push_item(item)).to(eq(1)) + + expect(Maremma).not_to(have_received(:post)) + end + end + end +end diff --git a/spec/models/related_url_spec.rb b/spec/models/related_url_spec.rb index 4af12966..faa7c232 100644 --- a/spec/models/related_url_spec.rb +++ b/spec/models/related_url_spec.rb @@ -51,7 +51,7 @@ allow(Time).to(receive_message_chain(:zone, :now, :iso8601).and_return("2023-11-15T12:17:47Z")) end - it "make request to lagottino for those related identifiers with type 'URL'" do + it "makes request to lagottino for those related identifiers with type 'URL'" do item = { "attributes" => { "doi" => "https://doi.org/10.0001/foo.bar", @@ -128,7 +128,7 @@ end describe "is invalid" do - it "is not set will only request logottino for those related identifiers with type 'URL'" do + it "will not make request to lagottino" do allow(Maremma).to(receive(:post)) item = { From af78bc950090c4249e5d84efcebc007a79bc87cc Mon Sep 17 00:00:00 2001 From: Wendel Fabian Chinsamy Date: Thu, 16 Nov 2023 13:48:57 +0200 Subject: [PATCH 2/2] remove commented out code in the application.rb --- config/application.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/config/application.rb b/config/application.rb index d47a93f1..dffa9c28 100644 --- a/config/application.rb +++ b/config/application.rb @@ -21,11 +21,11 @@ # load ENV variables from container environment if json file exists # see https://github.com/phusion/baseimage-docker#envvar_dumps -# env_json_file = "/etc/container_environment.json" -# if File.exist?(env_json_file) -# env_vars = JSON.parse(File.read(env_json_file)) -# env_vars.each { |k, v| ENV[k] = v } -# end +env_json_file = "/etc/container_environment.json" +if File.exist?(env_json_file) + env_vars = JSON.parse(File.read(env_json_file)) + env_vars.each { |k, v| ENV[k] = v } +end # default values for some ENV variables ENV["APPLICATION"] ||= "levriero"