Merge pull request #9946 from colinux/fix-procedure-external-url
Tech: fix le job de vérification des liens externes des procédures lorsqu'un autre attribut est en erreur
This commit is contained in:
commit
df1dd19eac
3 changed files with 103 additions and 20 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
81
spec/jobs/procedure_external_url_check_job_spec.rb
Normal file
81
spec/jobs/procedure_external_url_check_job_spec.rb
Normal file
|
@ -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
|
Loading…
Reference in a new issue