demarches-normaliennes/spec/controllers/dossiers_controller_spec.rb

116 lines
3.9 KiB
Ruby
Raw Normal View History

2015-08-10 11:05:06 +02:00
require 'spec_helper'
RSpec.describe DossiersController, type: :controller do
let(:dossier) { create(:dossier, :with_entreprise, :with_procedure) }
2015-08-20 16:25:54 +02:00
let(:dossier_id) { dossier.id }
2015-08-20 16:52:36 +02:00
let(:siret_not_found) { 999_999_999_999 }
2015-08-10 11:05:06 +02:00
2015-08-20 16:25:54 +02:00
let(:siren) { dossier.siren }
let(:siret) { dossier.siret }
2015-08-20 16:52:36 +02:00
let(:bad_siret) { 1 }
2015-08-10 11:05:06 +02:00
describe 'GET #show' do
2015-08-20 16:52:36 +02:00
it 'returns http success with dossier_id valid' do
get :show, id: dossier_id
2015-08-10 11:05:06 +02:00
expect(response).to have_http_status(:success)
end
it 'redirection vers start si mauvais dossier ID' do
2015-08-20 16:52:36 +02:00
get :show, id: siret_not_found
2015-08-10 11:05:06 +02:00
expect(response).to redirect_to('/start/error_dossier')
end
end
describe 'POST #create' do
before do
2015-08-20 16:52:36 +02:00
stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/etablissements/#{siret_not_found}?token=#{SIADETOKEN}")
.to_return(status: 404, body: 'fake body')
2015-08-10 11:05:06 +02:00
2015-08-20 16:52:36 +02:00
stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/etablissements/#{siret}?token=#{SIADETOKEN}")
.to_return(status: 200, body: File.read('spec/support/files/etablissement.json'))
2015-08-10 11:05:06 +02:00
2015-08-20 16:52:36 +02:00
stub_request(:get, "https://api-dev.apientreprise.fr/api/v1/entreprises/#{siren}?token=#{SIADETOKEN}")
.to_return(status: 200, body: File.read('spec/support/files/entreprise.json'))
2015-08-10 11:05:06 +02:00
end
describe 'professionnel fills form' do
context 'when pro_dossier_id is empty' do
context 'with valid siret ' do
before do
post :create, siret: siret, pro_dossier_id: '', procedure_id: Procedure.last
end
2015-08-10 11:05:06 +02:00
it 'create a dossier' do
2015-08-20 16:52:36 +02:00
expect { post :create, siret: siret, pro_dossier_id: '' }.to change { Dossier.count }.by(1)
end
it 'creates entreprise' do
expect { post :create, siret: siret, pro_dossier_id: '', procedure_id: Procedure.last }.to change { Entreprise.count }.by(1)
end
it 'links entreprise to dossier' do
expect(Entreprise.last.dossier).to eq(Dossier.last)
end
it 'creates etablissement for dossier' do
expect { post :create, siret: siret, pro_dossier_id: '', procedure_id: Procedure.last }.to change { Etablissement.count }.by(1)
end
2015-08-10 11:05:06 +02:00
it 'links etablissement to dossier' do
expect(Etablissement.last.dossier).to eq(Dossier.last)
end
it 'links etablissement to entreprise' do
expect(Etablissement.last.entreprise).to eq(Entreprise.last)
end
it 'links procedure to dossier' do
expect(Dossier.last.procedure).to eq(Procedure.last)
end
end
context 'with non existant siret' do
let(:siret_not_found) { '11111111111111' }
subject { post :create, siret: siret_not_found, pro_dossier_id: '' }
it 'does not create new dossier' do
2015-08-20 16:52:36 +02:00
expect { subject }.not_to change { Dossier.count }
end
it 'redirects to show' do
expect(subject).to redirect_to(controller: :start, action: :error_siret)
end
end
2015-08-10 11:05:06 +02:00
end
end
end
describe 'PUT #update' do
2015-08-17 16:27:21 +02:00
before do
2015-08-20 16:52:36 +02:00
put :update, id: dossier_id, dossier: { autorisation_donnees: autorisation_donnees }
2015-08-17 16:27:21 +02:00
end
context 'when Checkbox is checked' do
2015-08-17 16:27:21 +02:00
let(:autorisation_donnees) { '1' }
it 'redirects to demande' do
expect(response).to redirect_to(controller: :description, action: :show, dossier_id: dossier.id)
2015-08-10 11:05:06 +02:00
end
it 'update dossier' do
2015-08-17 16:27:21 +02:00
dossier.reload
expect(dossier.autorisation_donnees).to be_truthy
2015-08-10 11:05:06 +02:00
end
end
2015-08-10 11:05:06 +02:00
context 'when Checkbox is not checked' do
2015-08-17 16:27:21 +02:00
let(:autorisation_donnees) { '0' }
it 'uses flash alert to display message' do
2015-08-10 11:05:06 +02:00
expect(flash[:alert]).to have_content('Les conditions sont obligatoires.')
end
it "doesn't update dossier autorisation_donnees" do
2015-08-17 16:27:21 +02:00
dossier.reload
expect(dossier.autorisation_donnees).to be_falsy
end
2015-08-10 11:05:06 +02:00
end
end
end