Merge pull request #5849 from betagouv/dev

2021-01-20-01
This commit is contained in:
LeSim 2021-01-20 16:09:07 +01:00 committed by GitHub
commit 2220b24536
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 239 additions and 7 deletions

View file

@ -2,13 +2,25 @@ class Cron::AutoArchiveProcedureJob < Cron::CronJob
self.schedule_expression = "every 1 minute" self.schedule_expression = "every 1 minute"
def perform(*args) def perform(*args)
Procedure.publiees.where("auto_archive_on <= ?", Time.zone.today).each do |procedure| procedures_to_close.each do |procedure|
# A buggy procedure should NEVER prevent the closing of another procedure
# we therefore exceptionally add a `begin resue` block.
begin
procedure procedure
.dossiers .dossiers
.state_en_construction .state_en_construction
.find_each(&:passer_automatiquement_en_instruction!) .find_each(&:passer_automatiquement_en_instruction!)
procedure.close! procedure.close!
rescue StandardError => e
Raven.capture_exception(e, extra: { procedure_id: procedure.id })
end end
end end
end
def procedures_to_close
Procedure
.publiees
.where("auto_archive_on <= ?", Time.zone.today)
end
end end

View file

@ -0,0 +1,39 @@
class InstructeursImportService
def import(procedure, groupes_emails)
admins = procedure.administrateurs
errors = []
groupes_emails.each do |groupe_emails|
groupe = groupe_emails["groupe"].strip
instructeur_email = groupe_emails["email"].strip.downcase
if groupe.present? && Devise.email_regexp.match?(instructeur_email)
gi = procedure.groupe_instructeurs.find_or_create_by!(label: groupe)
instructeur = Instructeur.by_email(instructeur_email) || create_instructeur(admins, instructeur_email)
if !gi.instructeurs.include?(instructeur)
gi.instructeurs << instructeur
end
else
errors << instructeur_email
end
end
errors
end
private
def create_instructeur(administrateurs, email)
user = User.create_or_promote_to_instructeur(
email,
SecureRandom.hex,
administrateurs: administrateurs
)
user.invite!
user.instructeur
end
end

View file

@ -0,0 +1,25 @@
require Rails.root.join("lib", "tasks", "task_helper")
namespace :instructeurs do
desc <<~EOD
Import several instructeurs for a procedure
rails instructeurs:import\[procedure_id,csv_path\]
EOD
task :import, [:procedure_id, :csv] => :environment do |_t, args|
procedure_id = args[:procedure_id]
csv = args[:csv]
lines = CSV.readlines(csv, headers: true)
rake_puts "Import en cours..."
errors =
InstructeursImportService.new.import(Procedure.find(procedure_id), lines)
if errors.present?
rake_puts "Ces instructeurs n'ont pas pu être importés :"
rake_puts errors
end
rake_puts "Import terminé"
end
end

View file

@ -3,8 +3,9 @@ RSpec.describe Cron::AutoArchiveProcedureJob, type: :job do
let!(:procedure_hier) { create(:procedure, :published, :with_instructeur, auto_archive_on: 1.day.ago.to_date) } let!(:procedure_hier) { create(:procedure, :published, :with_instructeur, auto_archive_on: 1.day.ago.to_date) }
let!(:procedure_aujourdhui) { create(:procedure, :published, :with_instructeur, auto_archive_on: Time.zone.today) } let!(:procedure_aujourdhui) { create(:procedure, :published, :with_instructeur, auto_archive_on: Time.zone.today) }
let!(:procedure_demain) { create(:procedure, :published, :with_instructeur, auto_archive_on: 1.day.from_now.to_date) } let!(:procedure_demain) { create(:procedure, :published, :with_instructeur, auto_archive_on: 1.day.from_now.to_date) }
let!(:job) { Cron::AutoArchiveProcedureJob.new }
subject { Cron::AutoArchiveProcedureJob.new.perform } subject { job.perform }
context "when procedures have no auto_archive_on" do context "when procedures have no auto_archive_on" do
before do before do
@ -63,4 +64,21 @@ RSpec.describe Cron::AutoArchiveProcedureJob, type: :job do
it { expect(procedure_demain.close?).to eq false } it { expect(procedure_demain.close?).to eq false }
end end
context 'when an error occurs' do
let!(:buggy_procedure) { create(:procedure, :published, :with_instructeur, auto_archive_on: 1.day.ago.to_date) }
before do
error = StandardError.new('nop')
expect(buggy_procedure).to receive(:close!).and_raise(error)
expect(job).to receive(:procedures_to_close).and_return([buggy_procedure, procedure_hier])
expect(Raven).to receive(:capture_exception).with(error, extra: { procedure_id: buggy_procedure.id })
subject
end
it "should close all the procedure" do
expect(procedure_hier.reload.close?).to eq true
end
end
end end

View file

@ -0,0 +1,138 @@
describe InstructeursImportService do
describe '#import' do
let(:service) { InstructeursImportService.new }
let(:procedure) { create(:procedure) }
let(:procedure_groupes) do
procedure
.groupe_instructeurs
.map { |gi| [gi.label, gi.instructeurs.map(&:email)] }
end
subject { service.import(procedure, lines) }
context 'nominal case' do
let(:lines) do
[
{ "groupe" => "Auvergne Rhone-Alpes", "email" => "john@lennon.fr" },
{ "groupe" => " Occitanie ", "email" => "paul@mccartney.uk" },
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
]
end
it 'imports' do
errors = subject
expect(procedure_groupes).to match_array([
["Auvergne Rhone-Alpes", ["john@lennon.fr"]],
["Occitanie", ["paul@mccartney.uk", "ringo@starr.uk"]],
["défaut", []]
])
expect(errors).to match_array([])
end
end
context 'when group already exists' do
let!(:gi) { create(:groupe_instructeur, label: 'Occitanie', procedure: procedure) }
let(:lines) do
[
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
]
end
before do
gi.instructeurs << create(:instructeur, email: 'george@harisson.uk')
end
it 'adds instructeur to existing groupe' do
subject
expect(procedure_groupes).to match_array([
["Occitanie", ["george@harisson.uk", "ringo@starr.uk"]],
["défaut", []]
])
end
end
context 'when an email is malformed' do
let(:lines) do
[
{ "groupe" => "Occitanie", "email" => "paul" },
{ "groupe" => "Occitanie", "email" => "  Paul@mccartney.uk " },
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
]
end
it 'ignores or corrects' do
errors = subject
expect(procedure_groupes).to match_array([
["Occitanie", ["paul@mccartney.uk", "ringo@starr.uk"]],
["défaut", []]
])
expect(errors).to match_array(['paul'])
end
end
context 'when an instructeur already exists' do
let!(:instructeur) { create(:instructeur) }
let(:lines) do
[
{ "groupe" => "Occitanie", "email" => instructeur.email },
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
]
end
it 'reuses instructeur' do
subject
expect(procedure_groupes).to match_array([
["Occitanie", [instructeur.email, "ringo@starr.uk"]],
["défaut", []]
])
end
end
context 'when there are 2 emails of same instructeur to be imported' do
let(:lines) do
[
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" },
{ "groupe" => "Occitanie", "email" => "ringo@starr.uk" }
]
end
it 'ignores duplicated instructeur' do
subject
expect(procedure_groupes).to match_array([
["Occitanie", ["ringo@starr.uk"]],
["défaut", []]
])
end
end
context 'when label of group is empty' do
let(:lines) do
[
{ "groupe" => "", "email" => "ringo@starr.uk" },
{ "groupe" => " ", "email" => "paul@starr.uk" }
]
end
it 'ignores instructeur' do
errors = subject
expect(procedure_groupes).to match_array([
["défaut", []]
])
expect(errors).to match_array([
'ringo@starr.uk',
'paul@starr.uk'
])
end
end
end
end