diff --git a/README.md b/README.md index 7975e3ae4..26774a212 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,7 @@ client_secret: '' AutoArchiveProcedureJob.set(cron: "* * * * *").perform_later WeeklyOverviewJob.set(cron: "0 8 * * 0").perform_later AutoReceiveDossiersForProcedureJob.set(cron: "* * * * *").perform_later(procedure_declaratoire_id, "received") + FindDubiousProcedureJob.set(cron: "0 0 * * *").perform_later ## Exécution des tests (RSpec) diff --git a/app/jobs/find_dubious_procedures_job.rb b/app/jobs/find_dubious_procedures_job.rb new file mode 100644 index 000000000..100ee02f4 --- /dev/null +++ b/app/jobs/find_dubious_procedures_job.rb @@ -0,0 +1,29 @@ +class FindDubiousProceduresJob < ApplicationJob + queue_as :cron + + FORBIDDEN_KEYWORDS = ['IBAN', 'NIR', 'NIRPP', 'race', 'religion', + 'carte bancaire', 'carte bleue', 'sécurité sociale'] + + def perform(*args) + # \\y is a word boundary + forbidden_regexp = FORBIDDEN_KEYWORDS + .map { |keyword| '\\y' + keyword + '\\y' } + .join('|') + + # ~* -> case insensitive regexp match + # https://www.postgresql.org/docs/current/static/functions-matching.html#FUNCTIONS-POSIX-REGEXP + forbidden_tdcs = TypeDeChamp + .joins(:procedure) + .where("types_de_champ.libelle ~* '#{forbidden_regexp}'") + .where(type_champ: %w(text textarea)) + .where(procedures: { archived_at: nil }) + + dubious_procedures_and_tdcs = forbidden_tdcs + .group_by(&:procedure_id) + .map { |_procedure_id, tdcs| [tdcs[0].procedure, tdcs] } + + if dubious_procedures_and_tdcs.present? + AdministrationMailer.dubious_procedures(dubious_procedures_and_tdcs).deliver_now + end + end +end diff --git a/app/mailers/administration_mailer.rb b/app/mailers/administration_mailer.rb index 2d8f934cd..6e5b54505 100644 --- a/app/mailers/administration_mailer.rb +++ b/app/mailers/administration_mailer.rb @@ -1,4 +1,6 @@ class AdministrationMailer < ApplicationMailer + layout 'mailers/layout' + def new_admin_email admin, administration @admin = admin @administration = administration @@ -6,4 +8,10 @@ class AdministrationMailer < ApplicationMailer mail(to: 'tech@tps.apientreprise.fr', subject: "Création d'un compte Admin TPS") end + + def dubious_procedures(procedures_and_type_de_champs) + @procedures_and_type_de_champs = procedures_and_type_de_champs + mail(to: 'tech@tps.apientreprise.fr', + subject: "[RGS] De nouvelles procédures comportent des champs interdits") + end end diff --git a/app/views/administration_mailer/dubious_procedures.html.haml b/app/views/administration_mailer/dubious_procedures.html.haml new file mode 100644 index 000000000..3f0d0d179 --- /dev/null +++ b/app/views/administration_mailer/dubious_procedures.html.haml @@ -0,0 +1,7 @@ +- content_for(:title, 'Liste de procédures douteuses') + +%ul + - @procedures_and_type_de_champs.each do |procedure, type_de_champs| + %li + Nº #{procedure.id}, #{procedure.libelle} : + %b= type_de_champs.map(&:libelle).join(', ') diff --git a/spec/jobs/find_dubious_procedures_job_spec.rb b/spec/jobs/find_dubious_procedures_job_spec.rb new file mode 100644 index 000000000..40222c2d4 --- /dev/null +++ b/spec/jobs/find_dubious_procedures_job_spec.rb @@ -0,0 +1,43 @@ +require 'rails_helper' + +RSpec.describe FindDubiousProceduresJob, type: :job do + describe 'perform' do + let(:mailer_double) { double('mailer', deliver_now: true) } + let(:procedure) { create(:procedure) } + let(:allowed_tdc) { create(:type_de_champ_public, libelle: 'fournir') } + + before do + allow(AdministrationMailer).to receive(:dubious_procedures) + .and_return(mailer_double) + + procedure.types_de_champ << tdcs + FindDubiousProceduresJob.new.perform + end + + context 'with suspicious champs' do + let(:forbidden_tdcs) do + [create(:type_de_champ_public, libelle: 'donne ton iban, stp'), + create(:type_de_champ_public, libelle: "t'aurais une carte bancaire ?")] + end + + let(:tdcs) { forbidden_tdcs + [allowed_tdc] } + + it 'mails tech about the dubious procedure' do + expect(AdministrationMailer).to have_received(:dubious_procedures) + .with([[procedure, forbidden_tdcs]]) + end + + context 'and a archived procedure' do + let(:procedure) { create(:procedure, archived_at: DateTime.now) } + + it { expect(AdministrationMailer).not_to have_received(:dubious_procedures) } + end + end + + context 'with no suspicious champs' do + let(:tdcs) { [allowed_tdc] } + + it { expect(AdministrationMailer).not_to receive(:dubious_procedures) } + end + end +end diff --git a/spec/mailers/previews/administration_mailer_preview.rb b/spec/mailers/previews/administration_mailer_preview.rb new file mode 100644 index 000000000..d0a6e2554 --- /dev/null +++ b/spec/mailers/previews/administration_mailer_preview.rb @@ -0,0 +1,9 @@ +class AdministrationMailerPreview < ActionMailer::Preview + def dubious_procedures + procedures_and_champs = [ + [Procedure.first, [TypeDeChamp.new(libelle: 'iban'), TypeDeChamp.new(libelle: 'religion')]], + [Procedure.last, [TypeDeChamp.new(libelle: 'iban'), TypeDeChamp.new(libelle: 'numéro de carte bleu')]] + ] + AdministrationMailer.dubious_procedures(procedures_and_champs) + end +end