2024-04-29 00:17:15 +02:00
# frozen_string_literal: true
2019-11-04 16:18:09 +01:00
describe WebhookController , type : :controller do
2022-11-03 11:31:18 +01:00
before do
2023-01-11 17:31:31 +01:00
allow ( controller ) . to receive ( :verify_helpscout_signature! ) . and_return ( true )
2022-11-03 11:31:18 +01:00
allow ( controller ) . to receive ( :verify_authenticity_token )
end
describe '#helpscout_support_dev' do
subject ( :response ) { post :helpscout_support_dev , params : payload }
2023-04-19 10:55:16 +02:00
let ( :payload ) { JSON . parse ( Rails . root . join ( 'spec' , 'fixtures' , 'files' , 'helpscout' , 'tagged-dev.json' ) . read ) }
2023-01-11 17:31:31 +01:00
let ( :webhook_url ) { " https://notification_url " }
2022-11-03 11:31:18 +01:00
it 'works' do
2023-01-11 17:31:31 +01:00
allow ( Rails . application . secrets ) . to receive ( :dig ) . with ( :mattermost , :support_webhook_url ) . and_return ( webhook_url )
expect ( controller ) . to receive ( :send_mattermost_notification ) . with ( webhook_url , " \n Nouveau bug taggué # dev : https://secure.helpscout.net/conversation/123456789/123456789?folderId=123456789 \n \n > Bonjour, Je voudrais faire une demande de changement d'adresse et la plateforme m'indique que j'ai plusieurs comptes et que je dois d'abord les fusionner. Cela fait 3 jours que j'essaie de fusio \n \n **personnes impliquées** : anonymous@anon.fr \n **utilisateur en attente depuis** : 11 min ago " )
2022-11-03 11:31:18 +01:00
subject
2020-08-10 17:18:10 +02:00
end
2022-11-03 11:31:18 +01:00
end
2019-11-04 16:18:09 +01:00
2022-11-03 11:31:18 +01:00
describe '#helpscout' do
2019-11-04 16:18:09 +01:00
subject ( :response ) { get :helpscout , params : { customer : { email : customer_email } } }
let ( :payload ) { JSON . parse ( subject . body ) }
2020-08-10 17:18:10 +02:00
let ( :customer_email ) { 'a-user@exemple.fr' }
it " doesn't verify authenticity token " do
subject
expect ( controller ) . not_to have_received ( :verify_authenticity_token )
end
2019-11-04 16:18:09 +01:00
context 'when there is no matching user' do
let ( :customer_email ) { 'not-a-user@exemple.fr' }
it 'returns an empty response' do
expect ( subject . status ) . to eq ( 404 )
expect ( subject . body ) . to be_empty
end
end
context 'when there is a matching user' do
2020-09-18 09:57:26 +02:00
let ( :user ) { create ( :user , :with_strong_password ) }
2019-11-04 16:18:09 +01:00
let ( :customer_email ) { user . email }
it 'returns a 200 response' do
expect ( subject . status ) . to eq ( 200 )
expect ( subject . body ) . to be_present
end
it 'returns a link to the User profile in the Manager' do
expect ( payload ) . to have_key ( 'html' )
expect ( payload [ 'html' ] ) . to have_selector ( " a[href=' #{ manager_user_url ( user ) } '] " )
end
context 'when there are an associated Instructeur and Administrateur' do
let! ( :instructeur ) { create ( :instructeur , user : user ) }
2021-10-25 15:50:48 +02:00
let! ( :admin ) { create ( :administrateur , user : user , instructeur : instructeur ) }
2019-11-04 16:18:09 +01:00
it 'returns a link to the Instructeur profile in the Manager' do
expect ( payload ) . to have_key ( 'html' )
expect ( payload [ 'html' ] ) . to have_selector ( " a[href=' #{ manager_instructeur_url ( instructeur ) } '] " )
end
it 'returns a link to the Administrateur profile in the Manager' do
expect ( payload ) . to have_key ( 'html' )
expect ( payload [ 'html' ] ) . to have_selector ( " a[href=' #{ manager_administrateur_url ( admin ) } '] " )
2024-10-01 10:04:32 +02:00
expect ( payload [ 'html' ] ) . to have_text ( " Notifications activées " )
end
end
context " when notifications are disabled " do
let ( :instructeur ) { create ( :instructeur , user : ) }
let ( :procedure ) { create ( :procedure ) }
before do
create ( :assign_to , instructeur : , procedure : ,
instant_email_dossier_notifications_enabled : false )
end
it 'returns a summary of disabled notifications' do
expect ( payload [ 'html' ] ) . to have_text ( " Notifs désactivées Procedure # #{ procedure . id } " )
2019-11-04 16:18:09 +01:00
end
end
end
end
2023-01-11 17:31:31 +01:00
describe '#sendinblue' do
subject ( :response ) { post :sendinblue , params : payload }
2023-04-19 10:55:16 +02:00
let ( :payload ) { JSON . parse ( Rails . root . join ( 'spec' , 'fixtures' , 'files' , 'sendinblue' , 'incident.json' ) . read ) }
2023-01-11 17:31:31 +01:00
it 'sends notification to mattermost' do
notification_url = " https://notification_url "
allow ( Rails . application . secrets ) . to receive ( :dig ) . with ( :mattermost , :send_in_blue_outage_webhook_url ) . and_return ( notification_url )
expect ( controller ) . to receive ( :send_mattermost_notification ) . with ( notification_url , " Incident sur SIB : Database Issues. \n Etat de SIB: Degraded Performance \n L'Incident a commencé à 2015-04-03T18:27:15+00:00 et est p-e terminé a \n les composant suivants sont affectés : Chat Service, Voice Services, Admin Dashboard " )
subject
end
end
2019-11-04 16:18:09 +01:00
end