demarches-normaliennes/spec/services/instructeurs_import_service_spec.rb
kara Diaby 6ebd57640c tests
2021-06-15 19:36:46 +02:00

129 lines
4 KiB
Ruby
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

describe InstructeursImportService do
describe '#import' do
let(:procedure) { create(:procedure) }
let(:procedure_groupes) do
procedure
.groupe_instructeurs
.map { |gi| [gi.label, gi.instructeurs.map(&:email)] }
.to_h
end
subject { described_class.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.keys).to contain_exactly("Auvergne Rhone-Alpes", "Occitanie", "défaut")
expect(procedure_groupes["Auvergne Rhone-Alpes"]).to contain_exactly("john@lennon.fr")
expect(procedure_groupes["Occitanie"]).to contain_exactly("paul@mccartney.uk", "ringo@starr.uk")
expect(procedure_groupes["défaut"]).to be_empty
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.keys).to contain_exactly("Occitanie", "défaut")
expect(procedure_groupes["Occitanie"]).to contain_exactly("george@harisson.uk", "ringo@starr.uk")
expect(procedure_groupes["défaut"]).to be_empty
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.keys).to contain_exactly("Occitanie", "défaut")
expect(procedure_groupes["Occitanie"]).to contain_exactly("paul@mccartney.uk", "ringo@starr.uk")
expect(procedure_groupes["défaut"]).to be_empty
expect(errors).to contain_exactly("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.keys).to contain_exactly("Occitanie", "défaut")
expect(procedure_groupes["Occitanie"]).to contain_exactly(instructeur.email, "ringo@starr.uk")
expect(procedure_groupes["défaut"]).to be_empty
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.keys).to contain_exactly("Occitanie", "défaut")
expect(procedure_groupes["Occitanie"]).to contain_exactly("ringo@starr.uk")
expect(procedure_groupes["défaut"]).to be_empty
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.keys).to contain_exactly("défaut")
expect(procedure_groupes["défaut"]).to be_empty
expect(errors).to contain_exactly("ringo@starr.uk", "paul@starr.uk")
end
end
end
end