demarches-normaliennes/spec/controllers/support_controller_spec.rb

153 lines
4.5 KiB
Ruby
Raw Normal View History

2018-08-29 11:44:12 +02:00
describe SupportController, type: :controller do
render_views
context 'signed in' do
before do
sign_in user
end
2018-08-29 11:44:12 +02:00
let(:user) { create(:user) }
2018-08-30 15:08:26 +02:00
it 'should not have email field' do
get :index
expect(response.status).to eq(200)
expect(response.body).not_to have_content("Email *")
end
2018-08-29 11:44:12 +02:00
describe "with dossier" do
let(:user) { dossier.user }
let(:dossier) { create(:dossier) }
it 'should fill dossier_id' do
get :index, params: { dossier_id: dossier.id }
expect(response.status).to eq(200)
2018-10-01 14:03:05 +02:00
expect(response.body).to include((dossier.id).to_s)
2018-08-29 11:44:12 +02:00
end
end
2018-08-30 15:08:26 +02:00
describe "with tag" do
2018-08-29 11:44:12 +02:00
let(:tag) { 'yolo' }
it 'should fill tags' do
get :index, params: { tags: [tag] }
expect(response.status).to eq(200)
expect(response.body).to include(tag)
end
end
describe "with multiple tags" do
let(:tags) { ['yolo', 'toto'] }
it 'should fill tags' do
get :index, params: { tags: tags }
expect(response.status).to eq(200)
expect(response.body).to include(tags.join(','))
end
end
2018-08-30 16:12:40 +02:00
describe "send form" do
subject do
post :create, params: params
end
context "when invisible captcha is ignored" do
let(:params) { { subject: 'bonjour', text: 'un message' } }
it 'creates a conversation on HelpScout' do
expect_any_instance_of(Helpscout::FormAdapter).to receive(:send_form).and_return(true)
expect { subject }.to change(Commentaire, :count).by(0)
2018-08-30 16:12:40 +02:00
expect(flash[:notice]).to match('Votre message a été envoyé.')
expect(response).to redirect_to root_path(formulaire_contact_general_submitted: true)
2018-08-30 16:12:40 +02:00
end
context 'when a drafted dossier is mentionned' do
let(:dossier) { create(:dossier) }
let(:user) { dossier.user }
subject do
post :create, params: {
dossier_id: dossier.id,
type: Helpscout::FormAdapter::TYPE_INSTRUCTION,
subject: 'bonjour',
text: 'un message'
}
end
it 'creates a conversation on HelpScout' do
expect_any_instance_of(Helpscout::FormAdapter).to receive(:send_form).and_return(true)
expect { subject }.to change(Commentaire, :count).by(0)
expect(flash[:notice]).to match('Votre message a été envoyé.')
expect(response).to redirect_to root_path(formulaire_contact_general_submitted: true)
end
end
2018-08-30 16:12:40 +02:00
context 'when a submitted dossier is mentionned' do
let(:dossier) { create(:dossier, :en_construction) }
let(:user) { dossier.user }
subject do
post :create, params: {
dossier_id: dossier.id,
type: Helpscout::FormAdapter::TYPE_INSTRUCTION,
subject: 'bonjour',
text: 'un message'
}
end
it 'posts the message to the dossier messagerie' do
expect_any_instance_of(Helpscout::FormAdapter).not_to receive(:send_form)
2018-08-30 16:12:40 +02:00
expect { subject }.to change(Commentaire, :count).by(1)
2018-08-30 16:12:40 +02:00
expect(Commentaire.last.email).to eq(user.email)
expect(Commentaire.last.dossier).to eq(dossier)
expect(Commentaire.last.body).to include('[bonjour]')
expect(Commentaire.last.body).to include('un message')
expect(flash[:notice]).to match('Votre message a été envoyé sur la messagerie de votre dossier.')
expect(response).to redirect_to messagerie_dossier_path(dossier)
end
end
end
2018-08-30 16:12:40 +02:00
context "when invisible captcha is filled" do
let(:params) { { subject: 'bonjour', text: 'un message', InvisibleCaptcha.honeypots.sample => 'boom' } }
it 'does not create a conversation on HelpScout' do
expect { subject }.not_to change(Commentaire, :count)
expect(flash[:alert]).to eq(I18n.t('invisible_captcha.sentence_for_humans'))
2018-08-30 16:12:40 +02:00
end
end
end
2018-08-29 11:44:12 +02:00
end
context 'signed out' do
describe "with dossier" do
it 'should have email field' do
get :index
expect(response.status).to eq(200)
expect(response.body).to have_text("Email")
2018-08-29 11:44:12 +02:00
end
end
describe "with dossier" do
let(:tag) { 'yolo' }
it 'should fill tags' do
get :index, params: { tags: [tag] }
expect(response.status).to eq(200)
expect(response.body).to include(tag)
end
end
end
end