[fix #1097] Add job to monitor procedures

This commit is contained in:
simon lehericey 2018-01-05 11:16:50 +01:00 committed by Simon Lehericey
parent d7d5d7eff4
commit f1b7a03b63
6 changed files with 97 additions and 0 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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(', ')

View file

@ -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

View file

@ -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