Skip to content

Commit

Permalink
Merge pull request #9946 from colinux/fix-procedure-external-url
Browse files Browse the repository at this point in the history
Tech: fix le job de vérification des liens externes des procédures lorsqu'un autre attribut est en erreur
  • Loading branch information
colinux authored Jan 29, 2024
2 parents 6203b1f + 981b3fa commit df1dd19
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 20 deletions.
38 changes: 18 additions & 20 deletions app/jobs/procedure_external_url_check_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,28 @@ def perform(procedure)

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
4 changes: 4 additions & 0 deletions config/initializers/transition_to_sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ class VirusScannerJob
class DossierRebaseJob < ApplicationJob
self.queue_adapter = :sidekiq
end

class ProcedureExternalURLCheckJob < ApplicationJob
self.queue_adapter = :sidekiq
end
end
end
81 changes: 81 additions & 0 deletions spec/jobs/procedure_external_url_check_job_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit df1dd19

Please sign in to comment.