RUBYCRITIC
require 'rails_helper'
RSpec.describe Accounts::ContactsController, type: :request do
let!(:account) { create(:account) }
let!(:user) { create(:user) }
describe 'GET /accounts/{account.id}/contacts' do
context 'when it is an unauthenticated user' do -
it 'returns unauthorized' do
get "/accounts/#{account.id}/contacts"
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when it is an authenticated user' do
let!(:contact) { create(:contact) }
before do
sign_in(user)
end
it 'lists contacts' do - describe(GET /accounts/{account.id}/contacts)::context(when it is an authenticated user)::it#lists contacts has a flog score of 35
get "/accounts/#{account.id}/contacts"
expect(response).to have_http_status(200)
doc = Nokogiri::HTML(response.body)
table_body = doc.at_css('tbody#contacts').text
expect(table_body).to include(contact.full_name)
end
context 'when query params are present' do
context 'when query params match with contact full_name' do -
it 'returns contacts on contacts table' do - describe(GET /accounts/{account.id}/contacts)::context(when it is an authenticated user)::context(when query params are present)::context(when query params match with contact full_name)::it#returns contacts on contacts table has a flog score of 93
get "/accounts/#{account.id}/contacts", params: { query: contact.full_name }
expect(response).to have_http_status(200)
expect(response.body).to include('Contacts')
doc = Nokogiri::HTML(response.body)
table_body = doc.at_css('tbody#contacts').text
expect(table_body).to include(ERB::Util.html_escape(contact.full_name))
expect(table_body).to include(contact.email)
expect(table_body).to include(contact.phone)
expect(table_body).to include(contact.id.to_s)
end
end
context 'when query params match with contact email' do -
it 'returns contacts on contacts table' do - describe(GET /accounts/{account.id}/contacts)::context(when it is an authenticated user)::context(when query params are present)::context(when query params match with contact email)::it#returns contacts on contacts table has a flog score of 93
get "/accounts/#{account.id}/contacts", params: { query: contact.email }
expect(response).to have_http_status(200)
expect(response.body).to include('Contacts')
doc = Nokogiri::HTML(response.body)
table_body = doc.at_css('tbody#contacts').text
expect(table_body).to include(ERB::Util.html_escape(contact.full_name))
expect(table_body).to include(contact.email)
expect(table_body).to include(contact.phone)
expect(table_body).to include(contact.id.to_s)
end
end
context 'when query params match with contact phone' do -
it 'returns contacts on contacts table' do - describe(GET /accounts/{account.id}/contacts)::context(when it is an authenticated user)::context(when query params are present)::context(when query params match with contact phone)::it#returns contacts on contacts table has a flog score of 93
get "/accounts/#{account.id}/contacts", params: { query: contact.phone }
expect(response).to have_http_status(200)
expect(response.body).to include('Contacts')
doc = Nokogiri::HTML(response.body)
table_body = doc.at_css('tbody#contacts').text
expect(table_body).to include(ERB::Util.html_escape(contact.full_name))
expect(table_body).to include(contact.email)
expect(table_body).to include(contact.phone)
expect(table_body).to include(contact.id.to_s)
end
end
context 'when query params match partially with contact full_name' do
let(:first_name) { contact.full_name.split.first }
it 'returns contacts with partial match' do - describe(GET /accounts/{account.id}/contacts)::context(when it is an authenticated user)::context(when query params are present)::context(when query params match partially with contact full_name)::it#returns contacts with partial match has a flog score of 95
get "/accounts/#{account.id}/contacts", params: { query: first_name }
expect(response).to have_http_status(200)
expect(response.body).to include('Contacts')
doc = Nokogiri::HTML(response.body)
table_body = doc.at_css('tbody#contacts').text
expect(table_body).to include(ERB::Util.html_escape(contact.full_name))
expect(table_body).to include(contact.email)
expect(table_body).to include(contact.phone)
expect(table_body).to include(contact.id.to_s)
end
end
context 'when query params are case-insensitive' do
it 'returns contacts regardless of case' do - describe(GET /accounts/{account.id}/contacts)::context(when it is an authenticated user)::context(when query params are present)::context(when query params are case-insensitive)::it#returns contacts regardless of case has a flog score of 95
get "/accounts/#{account.id}/contacts", params: { query: contact.full_name.swapcase }
expect(response).to have_http_status(200)
expect(response.body).to include('Contacts')
doc = Nokogiri::HTML(response.body)
table_body = doc.at_css('tbody#contacts').text
expect(table_body).to include(ERB::Util.html_escape(contact.full_name))
expect(table_body).to include(contact.email)
expect(table_body).to include(contact.phone)
expect(table_body).to include(contact.id.to_s)
end
end
context 'when query params do not match any contacts' do
it 'returns an empty contacts table' do - describe(GET /accounts/{account.id}/contacts)::context(when it is an authenticated user)::context(when query params are present)::context(when query params do not match any contacts)::it#returns an empty contacts table has a flog score of 89
get "/accounts/#{account.id}/contacts", params: { query: 'NonexistentContact123' }
expect(response).to have_http_status(200)
expect(response.body).to include('Contacts')
doc = Nokogiri::HTML(response.body)
table_body = doc.at_css('tbody#contacts').text
expect(table_body).not_to include(ERB::Util.html_escape(contact.full_name))
expect(table_body).not_to include(contact.email)
expect(table_body).not_to include(contact.phone)
expect(table_body).not_to include(contact.id.to_s)
end
end
context 'when there are multiple contacts and query does not match any' do
let!(:contact2) do
create(:contact, full_name: 'Jane Smith', email: 'jane.smith@example.com',
phone: '+55226598745699')
end
let!(:contact3) do
create(:contact, full_name: 'Bob Johnson', email: 'bob.johnson@example.com',
phone: '+5541225695285')
end
it 'returns an empty contacts table' do - describe(GET /accounts/{account.id}/contacts)::context(when it is an authenticated user)::context(when query params are present)::context(when there are multiple contacts and query does not match any)::it#returns an empty contacts table has a flog score of 84
get "/accounts/#{account.id}/contacts", params: { query: 'NonexistentContact123' }
expect(response).to have_http_status(200)
expect(response.body).to include('Contacts')
doc = Nokogiri::HTML(response.body)
table_body = doc.at_css('tbody#contacts').text
expect(table_body).not_to include(ERB::Util.html_escape(contact.full_name))
expect(table_body).not_to include(ERB::Util.html_escape(contact2.full_name))
expect(table_body).not_to include(ERB::Util.html_escape(contact3.full_name))
end
end
end
end
end
describe 'GET /accounts/{account.id}/contacts/new' do
context 'when it is an unauthenticated user' do -
it 'returns unauthorized' do
get "/accounts/#{account.id}/contacts/new"
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when it is an authenticated user' do
before do
sign_in(user)
end
it 'renders new contact page' do
get "/accounts/#{account.id}/contacts/new"
expect(response).to have_http_status(200)
end
end
end
describe 'POST /accounts/{account.id}/contacts' do
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
expect { post "/accounts/#{account.id}/contacts", params: {} }.not_to change(Contact, :count)
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when it is an authenticated user' do
let(:params) do
{ contact: { full_name: 'Yukio Arie', email: 'yukioarie@gmail.com', phone: '+5522998813788' } }
end
before do
sign_in(user)
end
it 'creates a contact' do - describe(POST /accounts/{account.id}/contacts)::context(when it is an authenticated user)::it#creates a contact has a flog score of 57
expect do
post "/accounts/#{account.id}/contacts", params:
end.to change(Contact, :count).by(1)
expect(response).to redirect_to(account_contact_path(account, Contact.last))
expect(Contact.last.full_name).to eq('Yukio Arie')
expect(Contact.last.email).to eq('yukioarie@gmail.com')
expect(Contact.last.phone).to eq('+5522998813788')
end
context 'when contact creation fails' do
it 'renders new with unprocessable_entity status' do - describe(POST /accounts/{account.id}/contacts)::context(when it is an authenticated user)::context(when contact creation fails)::it#renders new with unprocessable_entity status has a flog score of 31
params = { contact: { full_name: 'Yukio Arie', email: 'yukioarie@gmail.com', phone: '+552299881378888889' } }
expect do
post "/accounts/#{account.id}/contacts", params:
end.not_to change(Contact, :count)
expect(response).to have_http_status(:unprocessable_entity)
expect(response.body).to include('must be in e164 format')
end
end
end
end
describe 'GET /accounts/{account.id}/contacts/{contact.id}' do
let!(:contact) { create(:contact) }
context 'when it is an unauthenticated user' do -
it 'returns unauthorized' do
get "/accounts/#{account.id}/contacts/#{contact.id}"
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when it is an authenticated user' do
let!(:pipeline) { create(:pipeline) }
let!(:stage) { create(:stage, pipeline:) }
let!(:deal) { create(:deal, stage:, contact:, name: 'Assigned Deal') }
let!(:deal_assignee) { create(:deal_assignee, deal:, user:) }
let!(:unassigned_deal) { create(:deal, stage:, contact:, name: 'Unassigned Deal') }
before do
sign_in(user)
end
it 'gets contact by account' do - describe(GET /accounts/{account.id}/contacts/{contact.id})::context(when it is an authenticated user)::it#gets contact by account has a flog score of 121
get "/accounts/#{account.id}/contacts/#{contact.id}"
expect(response).to have_http_status(200)
expect(response.body).to include(ERB::Util.html_escape(contact.full_name))
expect(response.body).to include(ERB::Util.html_escape(contact.email))
expect(response.body).to include(ERB::Util.html_escape(deal.name))
expect(response.body).to include(ERB::Util.html_escape(unassigned_deal.name))
expect(response.body).not_to include('icon_chatwoot_conversation_link')
expect(response.body).not_to include('chatwoot-contact-section')
expect(flash[:error]).to be_nil
end
context 'when paginating deals' do
let!(:deals) { create_list(:deal, 15, stage:, contact:) }
it 'returns first page of deals by default' do - describe(GET /accounts/{account.id}/contacts/{contact.id})::context(when it is an authenticated user)::context(when paginating deals)::it#returns first page of deals by default has a flog score of 36
get "/accounts/#{account.id}/contacts/#{contact.id}"
expect(response).to have_http_status(200)
doc = Nokogiri::HTML(response.body)
deals_frame = doc.at_css("turbo-frame#contact_#{contact.id}_deals")
expect(deals_frame).to be_present
end
it 'returns second page of deals via turbo_stream' do - describe(GET /accounts/{account.id}/contacts/{contact.id})::context(when it is an authenticated user)::context(when paginating deals)::it#returns second page of deals via turbo_stream has a flog score of 58
get "/accounts/#{account.id}/contacts/#{contact.id}",
params: { deals_page: 2 },
headers: { 'Accept' => 'text/vnd.turbo-stream.html' }
expect(response).to have_http_status(200)
expect(response.media_type).to eq('text/vnd.turbo-stream.html')
expect(response.body).to include("turbo-stream")
expect(response.body).to include("contact_#{contact.id}_deals")
end
end
context 'when there is chatwoot integration' do
let!(:chatwoot) do
create(:apps_chatwoots, :skip_validate, chatwoot_account_id: '456',
chatwoot_endpoint_url: 'https://chatwoot.example.com/')
end
before do
user.reload
contact.update!(additional_attributes: { chatwoot_id: 'contact_chatwoot_id_123456',
chatwoot_identifier: 'chatwoot_identifier_123456' },
chatwoot_conversations_label_list: 'chatwoot_label_1, chatwoot_label_2')
end
it 'shows chatwoot section' do - describe(GET /accounts/{account.id}/contacts/{contact.id})::context(when it is an authenticated user)::context(when there is chatwoot integration)::it#shows chatwoot section has a flog score of 195
get "/accounts/#{account.id}/contacts/#{contact.id}"
expect(response).to have_http_status(:success)
expect(response.body).to include(ERB::Util.html_escape(contact.full_name))
expect(response.body).to include(ERB::Util.html_escape(contact.email))
expect(response.body).to include(ERB::Util.html_escape(deal.name))
expect(response.body).to include(ERB::Util.html_escape(unassigned_deal.name))
expect(response.body).to include('icon_chatwoot_conversation_link')
expect(response.body).to include('text_chatwoot_conversation_link')
expect(response.body).to include('chatwoot-contact-section')
expect(response.body).to include(contact.additional_attributes['chatwoot_id'])
expect(response.body).to include(contact.additional_attributes['chatwoot_identifier'])
expect(response.body).to include(*contact.chatwoot_conversations_labels.pluck(:name))
expect(flash[:error]).to be_nil
end
end
end
end
describe 'PATCH /accounts/{account.id}/contacts/{contact.id}' do
let!(:contact) { create(:contact) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
patch "/accounts/#{account.id}/contacts/#{contact.id}", params: {}
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when it is an authenticated user' do
let(:params) do
{ contact: { full_name: 'Contact Updated Name Test', phone: '+552299881358',
email: 'contact-updated-email@email.com', label_list: 'label_test_1, label_test_2',
custom_attributes: { 'custom_attribute_teste' => '123456' } } }
end
before do
sign_in(user)
end
it 'updates the contact' do - describe(PATCH /accounts/{account.id}/contacts/{contact.id})::context(when it is an authenticated user)::it#updates the contact has a flog score of 74
patch("/accounts/#{account.id}/contacts/#{contact.id}", params:)
expect(response).to redirect_to(account_contact_path(account, contact))
expect(contact.reload.full_name).to eq('Contact Updated Name Test')
expect(contact.phone).to eq('+552299881358')
expect(contact.email).to eq('contact-updated-email@email.com')
expect(contact.label_list).to match_array(%w[label_test_1 label_test_2])
expect(contact.custom_attributes).to eq({ 'custom_attribute_teste' => '123456' })
end
context 'when update fails' do
it 'renders edit with unprocessable_entity status' do - describe(PATCH /accounts/{account.id}/contacts/{contact.id})::context(when it is an authenticated user)::context(when update fails)::it#renders edit with unprocessable_entity status has a flog score of 29
params = { contact: { phone: '+552299881378888889' } }
patch("/accounts/#{account.id}/contacts/#{contact.id}", params:)
expect(response).to have_http_status(:unprocessable_entity)
expect(response.body).to include('must be in e164 format')
end
end
end
end
describe 'DELETE /accounts/{account.id}/contacts/{contact.id}' do
let!(:contact) { create(:contact) }
context 'when it is an unauthenticated user' do -
it 'returns unauthorized' do
delete "/accounts/#{account.id}/contacts/#{contact.id}"
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when it is an authenticated user' do
before do
sign_in(user)
end
it 'deletes the contact' do - describe(DELETE /accounts/{account.id}/contacts/{contact.id})::context(when it is an authenticated user)::it#deletes the contact has a flog score of 31
expect do
delete "/accounts/#{account.id}/contacts/#{contact.id}"
end.to change(Contact, :count).by(-1)
expect(response).to redirect_to(account_contacts_path(account))
end
end
end
describe 'GET /accounts/{account.id}/contacts/{contact.id}/edit_custom_attributes' do
let!(:contact) { create(:contact) }
let!(:custom_attribute_definition) { create(:custom_attribute_definition, :contact_attribute) }
context 'when it is an unauthenticated user' do -
it 'returns unauthorized' do
get "/accounts/#{account.id}/contacts/#{contact.id}/edit_custom_attributes"
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when it is an authenticated user' do
before do
sign_in(user)
end
it 'renders edit custom attributes page' do - describe(GET /accounts/{account.id}/contacts/{contact.id}/edit_custom_attributes)::context(when it is an authenticated user)::it#renders edit custom attributes page has a flog score of 34
get "/accounts/#{account.id}/contacts/#{contact.id}/edit_custom_attributes"
expect(response).to have_http_status(200)
expect(response.body).to include(custom_attribute_definition.attribute_display_name)
end
end
end
describe 'GET /accounts/{account.id}/contacts/select_contact_search?query=query' do
context 'when it is an unauthenticated user' do -
it 'returns unauthorized' do
get "/accounts/#{account.id}/contacts/select_contact_search?query=query"
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when it is an authenticated user' do
let!(:contact) { create(:contact) }
before do
sign_in(user)
end
context 'select contact search component' do
it do
get "/accounts/#{account.id}/contacts/select_contact_search"
expect(response).to have_http_status(200)
end
context 'when there is query parameter' do
it 'should return contact' do - describe(GET /accounts/{account.id}/contacts/select_contact_search?query=query)::context(when it is an authenticated user)::context(select contact search component)::context(when there is query parameter)::it#should return contact has a flog score of 43
get "/accounts/#{account.id}/contacts/select_contact_search?query=#{contact.full_name}"
expect(response).to have_http_status(200)
html = Nokogiri::HTML(response.body)
contact_list_frame = html.at_css('turbo-frame#select_contact_results').text
expect(contact_list_frame).to include(contact.full_name)
end
context 'when query parameter is not found' do
it 'should return 0 contacts' do - describe(GET /accounts/{account.id}/contacts/select_contact_search?query=query)::context(when it is an authenticated user)::context(select contact search component)::context(when there is query parameter)::context(when query parameter is not found)::it#should return 0 contacts has a flog score of 49
get "/accounts/#{account.id}/contacts/select_contact_search?query=teste"
expect(response).to have_http_status(200)
html = Nokogiri::HTML(response.body)
contact_list_frame = html.at_css('turbo-frame#select_contact_results').text
expect(contact_list_frame).not_to include(contact.full_name)
expect(contact_list_frame.strip.empty?).to be_truthy
end
end
end
context 'when there is a form_name parameter' do -
it 'should render form_name as hidden_field_name on html form' do - describe(GET /accounts/{account.id}/contacts/select_contact_search?query=query)::context(when it is an authenticated user)::context(select contact search component)::context(when there is a form_name parameter)::it#should render form_name as hidden_field_name on html form has a flog score of 27
get "/accounts/#{account.id}/contacts/select_contact_search",
params: { form_name: 'deal[contact_id]' }
expect(response).to have_http_status(200)
expect(response.body).to include('deal[contact_id]')
end
end
context 'when there is no form_name parameter' do -
it 'should not render a specific hidden_field_name on html form' do - describe(GET /accounts/{account.id}/contacts/select_contact_search?query=query)::context(when it is an authenticated user)::context(select contact search component)::context(when there is no form_name parameter)::it#should not render a specific hidden_field_name on html form has a flog score of 27
get "/accounts/#{account.id}/contacts/select_contact_search"
expect(response).to have_http_status(200)
expect(response.body).not_to include('deal[contact_id]')
end
end
context 'when there is a content_value parameter' do
it 'should render content_value as selected_model_name on html form' do - describe(GET /accounts/{account.id}/contacts/select_contact_search?query=query)::context(when it is an authenticated user)::context(select contact search component)::context(when there is a content_value parameter)::it#should render content_value as selected_model_name on html form has a flog score of 39
get "/accounts/#{account.id}/contacts/select_contact_search",
params: { content_value: 'contact_name_test' }
expect(response).to have_http_status(200)
expect(response.body).to include('contact_name_test')
expect(response.body).not_to include('Search contact')
end
end
context 'when there is no content_value parameter' do
it 'should render the default search placeholder instead of a selected name' do - describe(GET /accounts/{account.id}/contacts/select_contact_search?query=query)::context(when it is an authenticated user)::context(select contact search component)::context(when there is no content_value parameter)::it#should render the default search placeholder instead of a selected name has a flog score of 39
get "/accounts/#{account.id}/contacts/select_contact_search"
expect(response).to have_http_status(200)
expect(response.body).not_to include('contact_name_test')
expect(response.body).to include('Search contact')
end
end
context 'when there is a form_id parameter' do -
it 'should render form_id as hidden_field_value on html form' do - describe(GET /accounts/{account.id}/contacts/select_contact_search?query=query)::context(when it is an authenticated user)::context(select contact search component)::context(when there is a form_id parameter)::it#should render form_id as hidden_field_value on html form has a flog score of 27
get "/accounts/#{account.id}/contacts/select_contact_search",
params: { form_id: '101563597' }
expect(response).to have_http_status(200)
expect(response.body).to include('value="101563597"')
end
end
context 'when there is no form_id parameter' do -
it 'should not render a specific id in the hidden field' do - describe(GET /accounts/{account.id}/contacts/select_contact_search?query=query)::context(when it is an authenticated user)::context(select contact search component)::context(when there is no form_id parameter)::it#should not render a specific id in the hidden field has a flog score of 27
get "/accounts/#{account.id}/contacts/select_contact_search"
expect(response).to have_http_status(200)
expect(response.body).not_to include('value="101563597"')
end
end
context 'when all parameters are present' do
it 'should render all parameters correctly in the HTML form' do - describe(GET /accounts/{account.id}/contacts/select_contact_search?query=query)::context(when it is an authenticated user)::context(select contact search component)::context(when all parameters are present)::it#should render all parameters correctly in the HTML form has a flog score of 63
get "/accounts/#{account.id}/contacts/select_contact_search",
params: {
form_name: 'deal[contact_id]',
content_value: 'contact_name_test',
form_id: '101'
}
expect(response).to have_http_status(200)
expect(response.body).to include('deal[contact_id]')
expect(response.body).to include('contact_name_test')
expect(response.body).to include('value="101"')
expect(response.body).not_to include('Search contact')
end
end
end
end
end
describe 'GET /accounts/{account.id}/contacts/{contact.id}/chatwoot_conversation_link' do
let!(:contact) { create(:contact) }
context 'when it is an unauthenticated user' do -
it 'returns unauthorized' do
get "/accounts/#{account.id}/contacts/#{contact.id}/chatwoot_conversation_link"
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when it is an authenticated user' do
before do
sign_in(user)
end
context 'get contact conversation link by account' do
context 'when the Chatwoot conversation link is successfully generated' do
let(:link) { 'https://chatwoot.example.com/app/accounts/456/conversations/789' }
before do
allow(Contact::Integrations::Chatwoot::GenerateConversationLink).to receive(:new)
.with(contact)
.and_return(double(call: { ok: link }))
end
context 'when display_format param is "text"' do -
it 'returns the conversation link in text format with no errors' do - describe(GET /accounts/{account.id}/contacts/{contact.id}/chatwoot_conversation_link)::context(when it is an authenticated user)::context(get contact conversation link by account)::context(when the Chatwoot conversation link is successfully generated)::context(when display_format param is "text")::it#returns the conversation link in text format with no errors has a flog score of 60
get "/accounts/#{account.id}/contacts/#{contact.id}/chatwoot_conversation_link", params: { display_format: 'text' }
expect(response).to have_http_status(200)
expect(response.body).to include('text_chatwoot_conversation_link')
expect(response.body).to include('Go to the last conversation')
expect(response.body).to include(link)
end
end
context 'when display_format param is "icon"' do -
it 'returns the conversation link in icon format with no errors' do - describe(GET /accounts/{account.id}/contacts/{contact.id}/chatwoot_conversation_link)::context(when it is an authenticated user)::context(get contact conversation link by account)::context(when the Chatwoot conversation link is successfully generated)::context(when display_format param is "icon")::it#returns the conversation link in icon format with no errors has a flog score of 60
get "/accounts/#{account.id}/contacts/#{contact.id}/chatwoot_conversation_link", params: { display_format: 'icon' }
expect(response).to have_http_status(200)
expect(response.body).to include('icon_chatwoot_conversation_link')
expect(response.body).to include('Go to the last conversation')
expect(response.body).to include(link)
end
end
context 'when display_format param is not provided' do
it 'defaults to icon format and returns the conversation link with no errors' do - describe(GET /accounts/{account.id}/contacts/{contact.id}/chatwoot_conversation_link)::context(when it is an authenticated user)::context(get contact conversation link by account)::context(when the Chatwoot conversation link is successfully generated)::context(when display_format param is not provided)::it#defaults to icon format and returns the conversation link with no errors has a flog score of 60
get "/accounts/#{account.id}/contacts/#{contact.id}/chatwoot_conversation_link"
expect(response).to have_http_status(200)
expect(response.body).to include('icon_chatwoot_conversation_link')
expect(response.body).to include('Go to the last conversation')
expect(response.body).to include(link)
end
end
end
context 'when the Chatwoot conversation link is not generated successfully' do
context 'when GenerateConversationLink returns error' do
before do
allow(Contact::Integrations::Chatwoot::GenerateConversationLink).to receive(:new)
.with(contact)
.and_return(double(call: { error: 'no_chatwoot_or_id' }))
end
it 'assigns nil to chatwoot_conversation_link and sets the error' do - describe(GET /accounts/{account.id}/contacts/{contact.id}/chatwoot_conversation_link)::context(when it is an authenticated user)::context(get contact conversation link by account)::context(when the Chatwoot conversation link is not generated successfully)::context(when GenerateConversationLink returns error)::it#assigns nil to chatwoot_conversation_link and sets the error has a flog score of 34
get "/accounts/#{account.id}/contacts/#{contact.id}/chatwoot_conversation_link"
expect(response).to have_http_status(200)
expect(response.body).to include('No conversations for this contact')
end
end
context 'when GenerateConversationLink raises a Faraday::TimeoutError' do -
before do
allow(Contact::Integrations::Chatwoot::GenerateConversationLink).to receive(:new)
.with(contact)
.and_raise(Faraday::TimeoutError)
end
it 'sets chatwoot_conversation_link to nil and connection_error to true' do - describe(GET /accounts/{account.id}/contacts/{contact.id}/chatwoot_conversation_link)::context(when it is an authenticated user)::context(get contact conversation link by account)::context(when the Chatwoot conversation link is not generated successfully)::context(when GenerateConversationLink raises a Faraday::TimeoutError)::it#sets chatwoot_conversation_link to nil and connection_error to true has a flog score of 34
get "/accounts/#{account.id}/contacts/#{contact.id}/chatwoot_conversation_link"
expect(response).to have_http_status(200)
expect(response.body).to include('Could not connect. Please try again.')
end
end
context 'when GenerateConversationLink raises a JSON::ParserError' do -
before do
allow(Contact::Integrations::Chatwoot::GenerateConversationLink).to receive(:new)
.with(contact)
.and_raise(JSON::ParserError)
end
it 'sets chatwoot_conversation_link to nil and connection_error to true' do - describe(GET /accounts/{account.id}/contacts/{contact.id}/chatwoot_conversation_link)::context(when it is an authenticated user)::context(get contact conversation link by account)::context(when the Chatwoot conversation link is not generated successfully)::context(when GenerateConversationLink raises a JSON::ParserError)::it#sets chatwoot_conversation_link to nil and connection_error to true has a flog score of 34
get "/accounts/#{account.id}/contacts/#{contact.id}/chatwoot_conversation_link"
expect(response).to have_http_status(200)
expect(response.body).to include('Could not connect. Please try again.')
end
end
end
end
end
end
describe 'GET /accounts/{account.id}/contacts/{contact.id}/hovercard_preview' do
let!(:contact) { create(:contact) }
context 'when it is an unauthenticated user' do -
it 'returns unauthorized' do
get "/accounts/#{account.id}/contacts/#{contact.id}/hovercard_preview"
expect(response).to redirect_to(new_user_session_path)
end
end
context 'when it is an authenticated user' do
before do
sign_in(user)
end
it 'get contact hovercard preview by account' do - describe(GET /accounts/{account.id}/contacts/{contact.id}/hovercard_preview)::context(when it is an authenticated user)::it#get contact hovercard preview by account has a flog score of 80
get "/accounts/#{account.id}/contacts/#{contact.id}/hovercard_preview"
expect(response.body).to include(ERB::Util.html_escape(contact.full_name))
expect(response.body).to include(ERB::Util.html_escape(contact.email))
expect(response.body).to include(ERB::Util.html_escape(contact.phone))
expect(response.body).to include("hovercard_preview_contact_#{contact.id}")
end
end
end
end