Merge pull request #6583 from betagouv/improve-some-factories

This commit is contained in:
Pierre de La Morinerie 2021-10-26 12:12:11 +02:00 committed by GitHub
commit 21e0c41b5d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 84 additions and 74 deletions

View file

@ -61,7 +61,7 @@ describe Manager::UsersController, type: :controller do
context 'and the old account belongs to an instructeur, expert and administrateur' do
let!(:instructeur) { create(:instructeur, user: user) }
let!(:expert) { create(:expert, user: user) }
let!(:administrateur) { create(:administrateur, user: user) }
let!(:administrateur) { create(:administrateur, user: user, instructeur: instructeur) }
it 'transfers instructeur account' do
subject

View file

@ -40,7 +40,7 @@ describe WebhookController, type: :controller do
context 'when there are an associated Instructeur and Administrateur' do
let!(:instructeur) { create(:instructeur, user: user) }
let!(:admin) { create(:administrateur, user: user) }
let!(:admin) { create(:administrateur, user: user, instructeur: instructeur) }
it 'returns a link to the Instructeur profile in the Manager' do
expect(payload).to have_key('html')

View file

@ -1,13 +1,18 @@
FactoryBot.define do
sequence(:administrateur_email) { |n| "admin#{n}@admin.com" }
factory :administrateur do
user { association :user, email: email, password: password }
transient do
email { generate(:administrateur_email) }
password { 'Mon [hien 4im3 {es banane$' }
instructeur { build(:instructeur, user: user) }
end
initialize_with do
User.create_or_promote_to_administrateur(email, password).administrateur
after(:build) do |administrateur, evaluator|
if administrateur.user
administrateur.user.instructeur = evaluator.instructeur
end
end
end

View file

@ -2,13 +2,11 @@ FactoryBot.define do
sequence(:create_expert_email) { |n| "expert#{n}@expert.com" }
factory :expert do
user { association :user, email: email, password: password }
transient do
email { generate(:expert_email) }
password { 'somethingverycomplated!' }
end
initialize_with do
User.create_or_promote_to_expert(email, password).expert
end
end
end

View file

@ -2,13 +2,11 @@ FactoryBot.define do
sequence(:instructeur_email) { |n| "inst#{n}@inst.com" }
factory :instructeur do
user { association :user, email: email, password: password }
transient do
email { generate(:instructeur_email) }
password { 'somethingverycomplated!' }
end
initialize_with do
User.create_or_promote_to_instructeur(email, password).instructeur
end
end
end

View file

@ -24,7 +24,7 @@ FactoryBot.define do
if evaluator.administrateur
procedure.administrateurs = [evaluator.administrateur]
elsif procedure.administrateurs.empty?
procedure.administrateurs = [create(:administrateur)]
procedure.administrateurs = [build(:administrateur)]
end
procedure.draft_revision = build(:procedure_revision, procedure: procedure)

View file

@ -1,7 +1,7 @@
describe Administrateur, type: :model do
let(:administration) { create(:administration) }
describe 'assocations' do
describe 'associations' do
it { is_expected.to have_and_belong_to_many(:instructeurs) }
it { is_expected.to have_many(:procedures) }
end

View file

@ -1,38 +1,44 @@
describe Champs::PhoneChamp do
let(:phone_champ) { build(:champ_phone) }
describe '#valid?' do
it do
expect(build(:champ_phone, value: nil)).to be_valid
expect(build(:champ_phone, value: "0123456789 0123456789")).to_not be_valid
expect(build(:champ_phone, value: "01.23.45.67.89 01.23.45.67.89")).to_not be_valid
expect(build(:champ_phone, value: "3646")).to be_valid
expect(build(:champ_phone, value: "0123456789")).to be_valid
expect(build(:champ_phone, value: "01.23.45.67.89")).to be_valid
expect(build(:champ_phone, value: "0123 45.67.89")).to be_valid
expect(build(:champ_phone, value: "0033 123-456-789")).to be_valid
expect(build(:champ_phone, value: "0033 123-456-789")).to be_valid
expect(build(:champ_phone, value: "0033(0)123456789")).to be_valid
expect(build(:champ_phone, value: "+33-1.23.45.67.89")).to be_valid
expect(build(:champ_phone, value: "+33 - 123 456 789")).to be_valid
expect(build(:champ_phone, value: "+33(0) 123 456 789")).to be_valid
expect(build(:champ_phone, value: "+33 (0)123 45 67 89")).to be_valid
expect(build(:champ_phone, value: "+33 (0)1 2345-6789")).to be_valid
expect(build(:champ_phone, value: "+33(0) - 123456789")).to be_valid
expect(build(:champ_phone, value: "+1(0) - 123456789")).to be_valid
expect(build(:champ_phone, value: "+49 2109 87654321")).to be_valid
expect(build(:champ_phone, value: "012345678")).to be_valid
expect(champ_with_value(nil)).to be_valid
expect(champ_with_value("0123456789 0123456789")).to_not be_valid
expect(champ_with_value("01.23.45.67.89 01.23.45.67.89")).to_not be_valid
expect(champ_with_value("3646")).to be_valid
expect(champ_with_value("0123456789")).to be_valid
expect(champ_with_value("01.23.45.67.89")).to be_valid
expect(champ_with_value("0123 45.67.89")).to be_valid
expect(champ_with_value("0033 123-456-789")).to be_valid
expect(champ_with_value("0033 123-456-789")).to be_valid
expect(champ_with_value("0033(0)123456789")).to be_valid
expect(champ_with_value("+33-1.23.45.67.89")).to be_valid
expect(champ_with_value("+33 - 123 456 789")).to be_valid
expect(champ_with_value("+33(0) 123 456 789")).to be_valid
expect(champ_with_value("+33 (0)123 45 67 89")).to be_valid
expect(champ_with_value("+33 (0)1 2345-6789")).to be_valid
expect(champ_with_value("+33(0) - 123456789")).to be_valid
expect(champ_with_value("+1(0) - 123456789")).to be_valid
expect(champ_with_value("+49 2109 87654321")).to be_valid
expect(champ_with_value("012345678")).to be_valid
# polynesian numbers should not return errors in any way
## landline numbers start with 40 or 45
expect(build(:champ_phone, value: "45187272")).to be_valid
expect(build(:champ_phone, value: "40 473 500")).to be_valid
expect(build(:champ_phone, value: "40473500")).to be_valid
expect(build(:champ_phone, value: "45473500")).to be_valid
expect(champ_with_value("45187272")).to be_valid
expect(champ_with_value("40 473 500")).to be_valid
expect(champ_with_value("40473500")).to be_valid
expect(champ_with_value("45473500")).to be_valid
## +689 is the international indicator
expect(build(:champ_phone, value: "+689 45473500")).to be_valid
expect(build(:champ_phone, value: "0145473500")).to be_valid
expect(champ_with_value("+689 45473500")).to be_valid
expect(champ_with_value("0145473500")).to be_valid
## polynesian mobile numbers start with 87, 88, 89
expect(build(:champ_phone, value: "87473500")).to be_valid
expect(build(:champ_phone, value: "88473500")).to be_valid
expect(build(:champ_phone, value: "89473500")).to be_valid
expect(champ_with_value("87473500")).to be_valid
expect(champ_with_value("88473500")).to be_valid
expect(champ_with_value("89473500")).to be_valid
end
def champ_with_value(number)
phone_champ.tap { |c| c.value = number }
end
end
end

View file

@ -47,8 +47,8 @@ describe Commentaire do
let(:dossier) { create(:dossier, procedure: procedure) }
context 'with a commentaire created by a instructeur' do
let(:instructeur) { create :instructeur, email: 'some_user@exemple.fr' }
let(:commentaire) { build :commentaire, instructeur: instructeur, dossier: dossier }
let(:instructeur) { build :instructeur, email: 'some_user@exemple.fr' }
context 'when the procedure shows instructeurs email' do
before { Flipper.disable(:hide_instructeur_email, procedure) }

View file

@ -1,30 +1,30 @@
RSpec.describe GeoArea, type: :model do
describe '#area' do
let(:geo_area) { build(:geo_area, :polygon) }
let(:geo_area) { build(:geo_area, :polygon, champ: nil) }
it { expect(geo_area.area).to eq(103.6) }
end
describe '#area (hourglass polygon)' do
let(:geo_area) { build(:geo_area, :hourglass_polygon) }
let(:geo_area) { build(:geo_area, :hourglass_polygon, champ: nil) }
it { expect(geo_area.area).to eq(32.4) }
end
describe '#length' do
let(:geo_area) { build(:geo_area, :line_string) }
let(:geo_area) { build(:geo_area, :line_string, champ: nil) }
it { expect(geo_area.length).to eq(21.2) }
end
describe '#location' do
let(:geo_area) { build(:geo_area, :point) }
let(:geo_area) { build(:geo_area, :point, champ: nil) }
it { expect(geo_area.location).to eq("46°32'19\"N 2°25'42\"E") }
end
describe '#rgeo_geometry' do
let(:geo_area) { build(:geo_area, :polygon) }
let(:geo_area) { build(:geo_area, :polygon, champ: nil) }
let(:polygon) do
{
"type" => "Polygon",
@ -46,44 +46,47 @@ RSpec.describe GeoArea, type: :model do
it { expect(geo_area.geometry).to eq(polygon) }
context 'polygon_with_extra_coordinate' do
let(:geo_area) { build(:geo_area, :polygon_with_extra_coordinate) }
let(:geo_area) { build(:geo_area, :polygon_with_extra_coordinate, champ: nil) }
it { expect(geo_area.geometry).not_to eq(polygon) }
it { expect(geo_area.safe_geometry).to eq(polygon) }
end
end
describe '#valid?' do
let(:geo_area) { build(:geo_area, :polygon) }
describe 'validations' do
context 'geometry' do
subject! { geo_area.validate }
context 'polygon' do
it { expect(geo_area.valid?).to be_truthy }
let(:geo_area) { build(:geo_area, :polygon, champ: nil) }
it { expect(geo_area.errors).not_to have_key(:geometry) }
end
context 'hourglass_polygon' do
let(:geo_area) { build(:geo_area, :hourglass_polygon) }
it { expect(geo_area.valid?).to be_falsey }
let(:geo_area) { build(:geo_area, :hourglass_polygon, champ: nil) }
it { expect(geo_area.errors).to have_key(:geometry) }
end
context 'line_string' do
let(:geo_area) { build(:geo_area, :line_string) }
it { expect(geo_area.valid?).to be_truthy }
let(:geo_area) { build(:geo_area, :line_string, champ: nil) }
it { expect(geo_area.errors).not_to have_key(:geometry) }
end
context 'point' do
let(:geo_area) { build(:geo_area, :point) }
it { expect(geo_area.valid?).to be_truthy }
let(:geo_area) { build(:geo_area, :point, champ: nil) }
it { expect(geo_area.errors).not_to have_key(:geometry) }
end
context 'invalid_right_hand_rule_polygon' do
let(:geo_area) { build(:geo_area, :invalid_right_hand_rule_polygon) }
it { expect(geo_area.valid?).to be_falsey }
let(:geo_area) { build(:geo_area, :invalid_right_hand_rule_polygon, champ: nil) }
it { expect(geo_area.errors).to have_key(:geometry) }
end
end
end
describe "cadastre properties" do
let(:geo_area) { build(:geo_area, :cadastre) }
let(:legacy_geo_area) { build(:geo_area, :legacy_cadastre) }
let(:geo_area) { build(:geo_area, :cadastre, champ: nil) }
let(:legacy_geo_area) { build(:geo_area, :legacy_cadastre, champ: nil) }
it "should be backward compatible" do
expect("#{geo_area.code_dep}#{geo_area.code_com}").to eq(geo_area.commune)
@ -103,7 +106,7 @@ RSpec.describe GeoArea, type: :model do
describe 'description' do
context 'when properties is nil' do
let(:geo_area) { build(:geo_area, properties: nil) }
let(:geo_area) { build(:geo_area, properties: nil, champ: nil) }
it { expect(geo_area.description).to be_nil }
end

View file

@ -372,7 +372,7 @@ describe User, type: :model do
end
context 'for administrateurs' do
let(:user) { build(:user, email: 'admin@exemple.fr', password: password, administrateur: build(:administrateur)) }
let(:user) { build(:user, email: 'admin@exemple.fr', password: password, administrateur: create(:administrateur, user: nil)) }
context 'when the password is too short' do
let(:password) { 's' * (PASSWORD_MIN_LENGTH - 1) }

View file

@ -52,7 +52,7 @@ describe ChampPolicy do
end
context 'when the user also has instruction rights' do
let(:instructeur) { create(:instructeur, email: signed_in_user.email, password: signed_in_user.password) }
let(:instructeur) { create(:instructeur, user: signed_in_user) }
let(:account) { { user: signed_in_user, instructeur: instructeur } }
context 'as the dossier instructeur and owner' do