diff --git a/spec/controllers/manager/users_controller_spec.rb b/spec/controllers/manager/users_controller_spec.rb index 22d77bd2c..241e59fa0 100644 --- a/spec/controllers/manager/users_controller_spec.rb +++ b/spec/controllers/manager/users_controller_spec.rb @@ -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 diff --git a/spec/controllers/webhook_controller_spec.rb b/spec/controllers/webhook_controller_spec.rb index b5548e224..c2e2ed89e 100644 --- a/spec/controllers/webhook_controller_spec.rb +++ b/spec/controllers/webhook_controller_spec.rb @@ -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') diff --git a/spec/factories/administrateur.rb b/spec/factories/administrateur.rb index b665f9249..479ee824c 100644 --- a/spec/factories/administrateur.rb +++ b/spec/factories/administrateur.rb @@ -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 diff --git a/spec/factories/expert.rb b/spec/factories/expert.rb index 7e1b5863e..13fc41410 100644 --- a/spec/factories/expert.rb +++ b/spec/factories/expert.rb @@ -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 diff --git a/spec/factories/instructeur.rb b/spec/factories/instructeur.rb index 7b73a5f9c..11f28a5d5 100644 --- a/spec/factories/instructeur.rb +++ b/spec/factories/instructeur.rb @@ -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 diff --git a/spec/factories/procedure.rb b/spec/factories/procedure.rb index 47a85de6a..eb874dfa6 100644 --- a/spec/factories/procedure.rb +++ b/spec/factories/procedure.rb @@ -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) diff --git a/spec/models/administrateur_spec.rb b/spec/models/administrateur_spec.rb index a00173c4d..55e6c4d32 100644 --- a/spec/models/administrateur_spec.rb +++ b/spec/models/administrateur_spec.rb @@ -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 diff --git a/spec/models/champs/phone_champ_spec.rb b/spec/models/champs/phone_champ_spec.rb index e69356e41..4d1f12f6e 100644 --- a/spec/models/champs/phone_champ_spec.rb +++ b/spec/models/champs/phone_champ_spec.rb @@ -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 diff --git a/spec/models/commentaire_spec.rb b/spec/models/commentaire_spec.rb index ac7a2cab8..33fe318fe 100644 --- a/spec/models/commentaire_spec.rb +++ b/spec/models/commentaire_spec.rb @@ -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) } diff --git a/spec/models/geo_area_spec.rb b/spec/models/geo_area_spec.rb index dfb5aa10e..e0c0dfc52 100644 --- a/spec/models/geo_area_spec.rb +++ b/spec/models/geo_area_spec.rb @@ -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 } - end + context 'polygon' do + 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 } - end + context 'hourglass_polygon' do + 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 } - end + context 'line_string' do + 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 } - end + context 'point' do + 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 } + context 'invalid_right_hand_rule_polygon' do + 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 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 8d4780268..6989089d7 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -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) } diff --git a/spec/policies/champ_policy_spec.rb b/spec/policies/champ_policy_spec.rb index 3a49c5b08..a7e02747f 100644 --- a/spec/policies/champ_policy_spec.rb +++ b/spec/policies/champ_policy_spec.rb @@ -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