dossiers: refactor dossiers specs

- Use login_as instead of signing up in the browser
- Better name factory objects
- Group specs by use-case
This commit is contained in:
Pierre de La Morinerie 2019-02-06 19:12:04 +01:00
parent a6704c4cd6
commit 4bfc258cbd
2 changed files with 58 additions and 53 deletions

View file

@ -798,13 +798,13 @@ describe NewUser::DossiersController, type: :controller do
subject { post :ask_deletion, params: { id: dossier.id } }
shared_examples_for "the dossier can not be deleted" do
it do
it "doesnt notify the deletion" do
expect(DossierMailer).not_to receive(:notify_deletion_to_administration)
expect(DossierMailer).not_to receive(:notify_deletion_to_user)
subject
end
it do
it "doesnt delete the dossier" do
subject
expect(Dossier.find_by(id: dossier.id)).not_to eq(nil)
expect(dossier.procedure.deleted_dossiers.count).to eq(0)
@ -814,13 +814,13 @@ describe NewUser::DossiersController, type: :controller do
context 'when dossier is owned by signed in user' do
let(:dossier) { create(:dossier, :en_construction, user: user, autorisation_donnees: true) }
it do
it "notifies the user and the admin of the deletion" do
expect(DossierMailer).to receive(:notify_deletion_to_administration).with(kind_of(DeletedDossier), dossier.procedure.administrateur.email).and_return(double(deliver_later: nil))
expect(DossierMailer).to receive(:notify_deletion_to_user).with(kind_of(DeletedDossier), dossier.user.email).and_return(double(deliver_later: nil))
subject
end
it do
it "deletes the dossier" do
procedure = dossier.procedure
dossier_id = dossier.id
subject

View file

@ -1,13 +1,13 @@
require 'spec_helper'
describe 'user access to the list of his dossier' do
describe 'user access to the list of their dossiers' do
let(:user) { create(:user) }
let!(:last_updated_dossier) { create(:dossier, :with_entreprise, user: user, state: Dossier.states.fetch(:en_construction)) }
let!(:dossier1) { create(:dossier, :with_entreprise, user: user, state: Dossier.states.fetch(:en_instruction)) }
let!(:dossier2) { create(:dossier, :with_entreprise) }
let!(:dossier_brouillon) { create(:dossier, :with_entreprise, user: user) }
let!(:dossier_archived) { create(:dossier, :with_entreprise, user: user, state: Dossier.states.fetch(:en_construction)) }
let!(:dossier_brouillon) { create(:dossier, user: user) }
let!(:dossier_en_construction) { create(:dossier, :en_construction, user: user) }
let!(:dossier_en_instruction) { create(:dossier, :en_instruction, user: user) }
let!(:dossier_archived) { create(:dossier, :en_instruction, :archived, user: user) }
let(:dossiers_per_page) { 25 }
let(:last_updated_dossier) { dossier_en_construction }
before do
@default_per_page = Dossier.default_per_page
@ -15,12 +15,8 @@ describe 'user access to the list of his dossier' do
last_updated_dossier.update_column(:updated_at, "19/07/2052 15:35".to_time)
visit new_user_session_path
within('#new_user') do
page.find_by_id('user_email').set user.email
page.find_by_id('user_password').set user.password
page.click_on 'Se connecter'
end
login_as user, scope: :user
visit dossiers_path
end
after do
@ -28,52 +24,59 @@ describe 'user access to the list of his dossier' do
end
it 'the list of dossier is displayed' do
expect(page).to have_content(dossier1.procedure.libelle)
expect(page).to have_content('en construction')
end
it 'dossiers belonging to other users are not displayed' do
expect(page).not_to have_content(dossier2.procedure.libelle)
end
it 'the list must be ordered by last updated' do
expect(page.body).to match(/#{last_updated_dossier.procedure.libelle}.*#{dossier1.procedure.libelle}/m)
end
it 'should list archived dossiers' do
expect(page).to have_content(dossier_brouillon.procedure.libelle)
expect(page).to have_content(dossier_en_construction.procedure.libelle)
expect(page).to have_content(dossier_en_instruction.procedure.libelle)
expect(page).to have_content(dossier_archived.procedure.libelle)
end
it 'should have link to only delete brouillon' do
expect(page).to have_link(nil, href: ask_deletion_dossier_path(dossier_brouillon))
expect(page).not_to have_link(nil, href: ask_deletion_dossier_path(dossier1))
it 'the list must be ordered by last updated' do
expect(page.body).to match(/#{last_updated_dossier.procedure.libelle}.*#{dossier_en_instruction.procedure.libelle}/m)
end
context 'when user clicks on delete brouillon', js: true do
scenario 'dossier is deleted' do
page.accept_alert('Confirmer la suppression ?') do
find(:xpath, "//a[@href='#{ask_deletion_dossier_path(dossier_brouillon)}']").click
end
context 'when there are dossiers from other users' do
let!(:dossier_other_user) { create(:dossier) }
expect(page).to have_content('Votre dossier a bien été supprimé.')
end
end
context 'when user clicks on a projet in list', js: true do
before do
page.click_on(dossier1.procedure.libelle)
end
scenario 'user is redirected to dossier page' do
expect(page).to have_current_path(dossier_path(dossier1))
it 'doesnt display dossiers belonging to other users' do
expect(page).not_to have_content(dossier_other_user.procedure.libelle)
end
end
context 'when there is more than one page' do
let(:dossiers_per_page) { 2 }
scenario 'the user can navigate through the other pages', js: true do
scenario 'the user can navigate through the other pages' do
expect(page).not_to have_content(dossier_en_instruction.procedure.libelle)
page.click_link("Suivant")
expect(page).to have_content(dossier_archived.procedure.libelle)
expect(page).to have_content(dossier_en_instruction.procedure.libelle)
end
end
context 'when user clicks on a projet in list' do
before do
page.click_on(dossier_en_construction.procedure.libelle)
end
scenario 'user is redirected to dossier page' do
expect(page).to have_current_path(dossier_path(dossier_en_construction))
end
end
describe 'deletion' do
it 'should have links to delete dossiers' do
expect(page).to have_link(nil, href: ask_deletion_dossier_path(dossier_brouillon))
expect(page).to have_link(nil, href: ask_deletion_dossier_path(dossier_en_construction))
expect(page).not_to have_link(nil, href: ask_deletion_dossier_path(dossier_en_instruction))
end
context 'when user clicks on delete button', js: true do
scenario 'the dossier is deleted' do
page.accept_alert('Confirmer la suppression ?') do
find(:xpath, "//a[@href='#{ask_deletion_dossier_path(dossier_brouillon)}']").click
end
expect(page).to have_content('Votre dossier a bien été supprimé.')
end
end
end
@ -91,25 +94,27 @@ describe 'user access to the list of his dossier' do
end
context "when the dossier does not belong to the user" do
let!(:dossier_other_user) { create(:dossier) }
before do
page.find_by_id('dossier_id').set(dossier2.id)
page.find_by_id('dossier_id').set(dossier_other_user.id)
click_button("Rechercher")
end
it "shows an error message on the dossiers page" do
expect(current_path).to eq(dossiers_path)
expect(page).to have_content("Vous navez pas de dossier avec le nº #{dossier2.id}.")
expect(page).to have_content("Vous navez pas de dossier avec le nº #{dossier_other_user.id}.")
end
end
context "when the dossier belongs to the user" do
before do
page.find_by_id('dossier_id').set(dossier1.id)
page.find_by_id('dossier_id').set(dossier_en_construction.id)
click_button("Rechercher")
end
it "redirects to the dossier page" do
expect(current_path).to eq(dossier_path(dossier1))
expect(current_path).to eq(dossier_path(dossier_en_construction))
end
end
end