demarches-normaliennes/spec/features/instructeurs/expert_spec.rb
Pierre de La Morinerie 4cb747fdb6 specs: always require rails_helper
Test helpers are separated between two files: spec_helper and
rails_helper. This separation is meant to allow tests that do not
require Rails (like testing standalone libs) to boot faster.

The spec_helper file is always loaded, through `--require spec_helper`
in the `.rspec` config file. When needed, the rails_helper file is
expected to be required manually.

This is fine, but:
- Many test files have a redundant `require 'spec_helper'` line;
- Many test files should require `rails_helper`, but don't.

Not requiring `rails_helper` will cause the Rails-concerned section of
the test environment not to be configured–which may cause subtle bugs
(like the test database not being properly initialized).

Moreover, Spring loads all the Rails files on preloading anyway. So the
gains from using only `spec_helper` are thin.

To streamline this process, this commit:
- Configures `.rspec` to require `rails_helper` by default;
- Remove all manual requires to spec_helper or rails_helper.

Reference: https://stackoverflow.com/questions/24145329/how-is-spec-rails-helper-rb-different-from-spec-spec-helper-rb-do-i-need-it
2020-03-31 12:48:32 +02:00

133 lines
4.9 KiB
Ruby
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

feature 'Inviting an expert:' do
include ActiveJob::TestHelper
include ActionView::Helpers
let(:instructeur) { create(:instructeur, password: 'démarches-simplifiées-pwd') }
let(:expert) { create(:instructeur, password: expert_password) }
let(:expert_password) { 'mot de passe dexpert' }
let(:procedure) { create(:procedure, :published, instructeurs: [instructeur]) }
let(:dossier) { create(:dossier, :en_construction, :with_dossier_link, procedure: procedure) }
let(:linked_dossier) { Dossier.find_by(id: dossier.reload.champs.filter(&:dossier_link?).map(&:value).compact) }
context 'as an Instructeur' do
scenario 'I can invite an expert' do
# assign instructeur to linked dossier
instructeur.assign_to_procedure(linked_dossier.procedure)
login_as instructeur.user, scope: :user
visit instructeur_dossier_path(procedure, dossier)
click_on 'Avis externes'
expect(page).to have_current_path(avis_instructeur_dossier_path(procedure, dossier))
fill_in 'avis_emails', with: 'expert1@exemple.fr, expert2@exemple.fr'
fill_in 'avis_introduction', with: 'Bonjour, merci de me donner votre avis sur ce dossier.'
check 'avis_invite_linked_dossiers'
page.select 'confidentiel', from: 'avis_confidentiel'
perform_enqueued_jobs do
click_on 'Demander un avis'
end
expect(page).to have_content('Une demande d\'avis a été envoyée')
expect(page).to have_content('Avis des invités')
within('.list-avis') do
expect(page).to have_content('expert1@exemple.fr')
expect(page).to have_content('expert2@exemple.fr')
expect(page).to have_content('Bonjour, merci de me donner votre avis sur ce dossier.')
end
expect(Avis.count).to eq(4)
expect(all_emails.size).to eq(2)
invitation_email = open_email('expert2@exemple.fr')
avis = Avis.find_by(email: 'expert2@exemple.fr', dossier: dossier)
sign_up_link = sign_up_instructeur_avis_path(avis.id, avis.email)
expect(invitation_email.body).to include(sign_up_link)
end
context 'when experts submitted their answer' do
let!(:answered_avis) { create(:avis, :with_answer, dossier: dossier, claimant: instructeur, email: expert.email) }
scenario 'I can read the expert answer' do
login_as instructeur.user, scope: :user
visit instructeur_dossier_path(procedure, dossier)
click_on 'Avis externes'
expect(page).to have_content(expert.email)
answered_avis.answer.split("\n").each do |answer_line|
expect(page).to have_content(answer_line)
end
end
end
end
context 'as an invited Expert' do
let(:avis_email) { expert.email }
let(:avis) { create(:avis, dossier: dossier, claimant: instructeur, email: avis_email, confidentiel: true) }
context 'when I dont already have an account' do
let(:avis_email) { 'not-signed-up-expert@exemple.fr' }
scenario 'I can sign up' do
visit sign_up_instructeur_avis_path(avis.id, avis_email)
expect(page).to have_field('Email', with: avis_email, disabled: true)
fill_in 'Mot de passe', with: 'This is a very complicated password !'
click_on 'Créer un compte'
expect(page).to have_current_path(instructeur_avis_index_path)
expect(page).to have_text('avis à donner 1')
end
end
context 'when I already have an existing account' do
let(:avis_email) { expert.email }
scenario 'I can sign in' do
visit sign_up_instructeur_avis_path(avis.id, avis_email)
expect(page).to have_current_path(new_user_session_path)
sign_in_with(expert.email, expert_password)
expect(page).to have_current_path(instructeur_avis_index_path)
expect(page).to have_text('avis à donner 1')
end
end
scenario 'I can give an answer' do
avis # create avis
login_as expert.user, scope: :user
visit instructeur_avis_index_path
expect(page).to have_text('avis à donner 1')
expect(page).to have_text('avis donnés 0')
click_on avis.dossier.user.email
within('.tabs') { click_on 'Avis' }
expect(page).to have_text("Demandeur : #{instructeur.email}")
expect(page).to have_text('Cet avis est confidentiel')
fill_in 'avis_answer', with: 'Ma réponse dexpert : cest un oui.'
find('.attachment input[name="avis[piece_justificative_file]"]').attach_file(Rails.root + 'spec/fixtures/files/RIB.pdf')
click_on 'Envoyer votre avis'
expect(page).to have_content('Votre réponse est enregistrée')
expect(page).to have_content('Ma réponse dexpert : cest un oui.')
expect(page).to have_content('RIB.pdf')
within('.new-header') { click_on 'Avis' }
expect(page).to have_text('avis à donner 0')
expect(page).to have_text('avis donné 1')
end
# TODO
# scenario 'I can read other experts advices' do
# end
# scenario 'I can invite other experts' do
# end
end
end