From 533ce8d7babb5e271c71c7948fd887923ad1c2bf Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Mon, 29 Jan 2024 15:42:35 +0100 Subject: [PATCH 1/2] fix(procedure-url-check): don't fail when other attribute have an error --- app/jobs/procedure_external_url_check_job.rb | 38 +++++---- .../procedure_external_url_check_job_spec.rb | 81 +++++++++++++++++++ 2 files changed, 99 insertions(+), 20 deletions(-) create mode 100644 spec/jobs/procedure_external_url_check_job_spec.rb diff --git a/app/jobs/procedure_external_url_check_job.rb b/app/jobs/procedure_external_url_check_job.rb index fb4229eb3..c0678e882 100644 --- a/app/jobs/procedure_external_url_check_job.rb +++ b/app/jobs/procedure_external_url_check_job.rb @@ -4,30 +4,28 @@ class ProcedureExternalURLCheckJob < ApplicationJob if procedure.lien_notice.present? error = procedure.errors.find { _1.attribute == :lien_notice } - if error.present? - procedure.update!(lien_notice_error: error.message) - else - response = Typhoeus.get(procedure.lien_notice, followlocation: true) - if response.success? - procedure.update!(lien_notice_error: nil) - else - procedure.update!(lien_notice_error: "#{response.code} #{response.return_message}") - end - end + + procedure.lien_notice_error = check_for_error(error, procedure.lien_notice) + procedure.save!(validate: false) # others errors may prevent save if validate end if procedure.lien_dpo.present? && !procedure.lien_dpo_email? error = procedure.errors.find { _1.attribute == :lien_dpo } - if error.present? - procedure.update!(lien_dpo_error: error.message) - else - response = Typhoeus.get(procedure.lien_dpo, followlocation: true) - if response.success? - procedure.update!(lien_dpo_error: nil) - else - procedure.update!(lien_dpo_error: "#{response.code} #{response.return_message}") - end - end + + procedure.lien_dpo_error = check_for_error(error, procedure.lien_dpo) + procedure.save!(validate: false) end end + + private + + def check_for_error(error, url) + return error.message if error.present? + + response = Typhoeus.get(url, followlocation: true) + + return if response.success? + + "#{response.code} #{response.return_message}" + end end diff --git a/spec/jobs/procedure_external_url_check_job_spec.rb b/spec/jobs/procedure_external_url_check_job_spec.rb new file mode 100644 index 000000000..5a9e6b70a --- /dev/null +++ b/spec/jobs/procedure_external_url_check_job_spec.rb @@ -0,0 +1,81 @@ +describe ProcedureExternalURLCheckJob do + subject(:perform) { described_class.new(procedure).perform_now; procedure.reload } + let(:lien_dpo) { "https://example.com/dpo" } + let(:lien_notice) { "https://example.com/notice" } + let(:lien_dpo_error) { nil } + let(:dpo_code) { 200 } + let(:notice_code) { 200 } + let(:procedure) { create(:procedure, lien_dpo:, lien_dpo_error:, lien_notice:) } + + before do + allow(Typhoeus).to receive(:get).with("https://example.com/dpo", followlocation: true).and_return(Typhoeus::Response.new(code: dpo_code, mock: true)) + allow(Typhoeus).to receive(:get).with("https://example.com/notice", followlocation: true).and_return(Typhoeus::Response.new(code: notice_code, mock: true)) + end + + context 'with valid links' do + it "changes nothing" do + perform + + expect(procedure.lien_dpo).to be_present + expect(procedure.lien_notice).to be_present + expect(procedure.lien_dpo_error).to be_nil + expect(procedure.lien_notice_error).to be_nil + end + end + + context 'with invalid dpo' do + let(:dpo_code) { 404 } + + it "update dpo error" do + perform + expect(procedure.lien_dpo_error).to include("404") + end + end + + context "with invalid lien_dpo attribute" do + before do + procedure.lien_dpo = "http://localhost" + procedure.save!(validate: false) + end + + it "update dpo error" do + perform + + expect(procedure.lien_dpo_error).to include("pas un lien valide") + end + end + + context 'when there was an error before' do + let(:lien_dpo_error) { "old error" } + + it "removes error when link is valid" do + perform + + expect(procedure.lien_dpo_error).to be_nil + end + end + + context 'with invalid notice link' do + let(:notice_code) { 500 } + + it "updates lien_notice_error" do + perform + + expect(procedure.lien_notice_error).to include("500") + end + end + + context 'when there are other errors' do + let(:notice_code) { 404 } + before do + procedure.libelle = nil + procedure.save!(validate: false) + end + + it "does not fail the job" do + perform + + expect(procedure.lien_notice_error).to include("404") + end + end +end From 981b3fa4ffe7120ce769bc8d581788a16114abf3 Mon Sep 17 00:00:00 2001 From: Colin Darie Date: Mon, 29 Jan 2024 15:42:55 +0100 Subject: [PATCH 2/2] chore(sidekiq): ProcedureExternalURLCheckJob on sidekiq --- config/initializers/transition_to_sidekiq.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/config/initializers/transition_to_sidekiq.rb b/config/initializers/transition_to_sidekiq.rb index 633f1834e..a8b74abac 100644 --- a/config/initializers/transition_to_sidekiq.rb +++ b/config/initializers/transition_to_sidekiq.rb @@ -13,5 +13,9 @@ if Rails.env.production? && sidekiq_enabled class DossierRebaseJob < ApplicationJob self.queue_adapter = :sidekiq end + + class ProcedureExternalURLCheckJob < ApplicationJob + self.queue_adapter = :sidekiq + end end end