From a2b91c8ec6496bdb2d53e680b15734812363251b Mon Sep 17 00:00:00 2001 From: Pierre de La Morinerie Date: Mon, 25 Oct 2021 13:50:48 +0000 Subject: [PATCH] factories: cleanup Administrateur, Instructeur and Expert factories This changes: - avoids relying on application code to create mock objects, - allows to build Administrateur or Instructeurs without saving them. --- spec/controllers/manager/users_controller_spec.rb | 2 +- spec/controllers/webhook_controller_spec.rb | 2 +- spec/factories/administrateur.rb | 9 +++++++-- spec/factories/expert.rb | 6 ++---- spec/factories/instructeur.rb | 6 ++---- spec/models/administrateur_spec.rb | 2 +- spec/models/commentaire_spec.rb | 2 +- spec/models/user_spec.rb | 2 +- spec/policies/champ_policy_spec.rb | 2 +- 9 files changed, 17 insertions(+), 16 deletions(-) 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/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/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/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