require 'spec_helper' describe NewGestionnaire::DossiersController, type: :controller do render_views let(:gestionnaire) { create(:gestionnaire) } let(:procedure) { create(:procedure, :published, gestionnaires: [gestionnaire]) } let(:dossier) { create(:dossier, :initiated, procedure: procedure) } before { sign_in(gestionnaire) } describe '#attestation' do context 'when a dossier has an attestation' do let(:fake_pdf) { double(read: 'pdf content') } let!(:dossier) { create(:dossier, :initiated, attestation: Attestation.new, procedure: procedure) } let!(:procedure) { create(:procedure, :published, gestionnaires: [gestionnaire]) } let!(:dossier) { create(:dossier, :initiated, attestation: Attestation.new, procedure: procedure) } it 'returns the attestation pdf' do allow_any_instance_of(Attestation).to receive(:pdf).and_return(fake_pdf) expect(controller).to receive(:send_data) .with('pdf content', filename: 'attestation.pdf', type: 'application/pdf') do controller.render nothing: true end get :attestation, params: { procedure_id: procedure.id, dossier_id: dossier.id } expect(response).to have_http_status(:success) end end end describe '#follow' do before do expect_any_instance_of(Dossier).to receive(:next_step!).with('gestionnaire', 'follow') patch :follow, params: { procedure_id: procedure.id, dossier_id: dossier.id } end it { expect(gestionnaire.followed_dossiers).to match([dossier]) } it { expect(flash.notice).to eq('Dossier suivi') } it { expect(response).to redirect_to(procedures_url) } end describe '#unfollow' do before do gestionnaire.followed_dossiers << dossier patch :unfollow, params: { procedure_id: procedure.id, dossier_id: dossier.id } gestionnaire.reload end it { expect(gestionnaire.followed_dossiers).to match([]) } it { expect(flash.notice).to eq("Vous ne suivez plus le dossier nº #{dossier.id}") } it { expect(response).to redirect_to(procedures_url) } end describe '#archive' do before do gestionnaire.follow(dossier) patch :archive, params: { procedure_id: procedure.id, dossier_id: dossier.id } dossier.reload gestionnaire.reload end it { expect(dossier.archived).to be true } it { expect(response).to redirect_to(procedures_url) } it { expect(gestionnaire.followed_dossiers).not_to include(dossier) } end describe '#unarchive' do before do dossier.update_attributes(archived: true) patch :unarchive, params: { procedure_id: procedure.id, dossier_id: dossier.id } dossier.reload end it { expect(dossier.archived).to be false } it { expect(response).to redirect_to(procedures_url) } end describe '#passer_en_instruction' do before do dossier.initiated! sign_in gestionnaire post :passer_en_instruction, params: { procedure_id: procedure.id, dossier_id: dossier.id } dossier.reload end it { expect(dossier.state).to eq('received') } it { is_expected.to redirect_to dossier_path(procedure, dossier) } it { expect(gestionnaire.follow?(dossier)).to be true } end describe '#repasser_en_construction' do before do dossier.received! sign_in gestionnaire end subject { post :repasser_en_construction, params: { procedure_id: procedure.id, dossier_id: dossier.id} } it 'change state to initiated' do subject dossier.reload expect(dossier.state).to eq('initiated') end it { is_expected.to redirect_to dossier_path(procedure, dossier) } end describe '#show #messagerie #annotations_privees #avis' do before do dossier.notifications = %w(champs annotations_privees avis commentaire).map{ |type| Notification.create!(type_notif: type) } get method, params: { procedure_id: procedure.id, dossier_id: dossier.id } dossier.notifications.each(&:reload) end context '#show' do let(:method) { :show } it { expect(dossier.notifications.map(&:already_read)).to match([true, false, false, false]) } it { expect(response).to have_http_status(:success) } end context '#annotations_privees' do let(:method) { :annotations_privees } it { expect(dossier.notifications.map(&:already_read)).to match([false, true, false, false]) } it { expect(response).to have_http_status(:success) } end context '#avis' do let(:method) { :avis } it { expect(dossier.notifications.map(&:already_read)).to match([false, false, true, false]) } it { expect(response).to have_http_status(:success) } end context '#messagerie' do let(:method) { :messagerie } it { expect(dossier.notifications.map(&:already_read)).to match([false, false, false, true]) } it { expect(response).to have_http_status(:success) } end end describe "#create_commentaire" do let(:saved_commentaire) { dossier.commentaires.first } let(:body) { "avant\napres" } let(:file) { nil } let(:scan_result) { true } subject { post :create_commentaire, params: { procedure_id: procedure.id, dossier_id: dossier.id, commentaire: { body: body, file: file } } } before do allow(ClamavService).to receive(:safe_file?).and_return(scan_result) end it do subject expect(saved_commentaire.body).to eq("
avant\n
apres