2020-02-17 11:38:29 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Contacts API', type: :request do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/contacts' do
|
2020-02-17 11:38:29 +00:00
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/contacts"
|
2020-02-17 11:38:29 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
2021-09-04 12:26:46 +00:00
|
|
|
let!(:contact) { create(:contact, :with_email, account: account) }
|
2020-08-10 06:45:29 +00:00
|
|
|
let!(:contact_inbox) { create(:contact_inbox, contact: contact) }
|
2020-02-17 11:38:29 +00:00
|
|
|
|
2021-09-04 12:26:46 +00:00
|
|
|
it 'returns all resolved contacts along with contact inboxes' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/contacts",
|
2020-02-17 11:38:29 +00:00
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2020-11-10 09:55:26 +00:00
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body['payload'].first['email']).to eq(contact.email)
|
|
|
|
expect(response_body['payload'].first['contact_inboxes'].first['source_id']).to eq(contact_inbox.source_id)
|
|
|
|
expect(response_body['payload'].first['contact_inboxes'].first['inbox']['name']).to eq(contact_inbox.inbox.name)
|
|
|
|
end
|
|
|
|
|
2021-07-23 13:09:24 +00:00
|
|
|
it 'returns all contacts without contact inboxes' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts?include_contact_inboxes=false",
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body['payload'].first['email']).to eq(contact.email)
|
|
|
|
expect(response_body['payload'].first['contact_inboxes'].blank?).to eq(true)
|
|
|
|
end
|
|
|
|
|
2020-11-10 09:55:26 +00:00
|
|
|
it 'returns includes conversations count and last seen at' do
|
|
|
|
create(:conversation, contact: contact, account: account, inbox: contact_inbox.inbox, contact_last_seen_at: Time.now.utc)
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts",
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body['payload'].first['conversations_count']).to eq(contact.conversations.count)
|
|
|
|
expect(response_body['payload'].first['last_seen_at']).present?
|
2020-08-10 06:45:29 +00:00
|
|
|
end
|
2021-06-18 14:38:58 +00:00
|
|
|
|
2021-09-04 12:26:46 +00:00
|
|
|
it 'filters resolved contacts based on label filter' do
|
|
|
|
contact_with_label1, contact_with_label2 = FactoryBot.create_list(:contact, 2, :with_email, account: account)
|
2021-06-18 14:38:58 +00:00
|
|
|
contact_with_label1.update_labels(['label1'])
|
|
|
|
contact_with_label2.update_labels(['label2'])
|
|
|
|
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts",
|
|
|
|
params: { labels: %w[label1 label2] },
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
response_body = JSON.parse(response.body)
|
|
|
|
expect(response_body['meta']['count']).to eq(2)
|
|
|
|
expect(response_body['payload'].pluck('email')).to include(contact_with_label1.email, contact_with_label2.email)
|
|
|
|
end
|
2020-08-10 06:45:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-02-03 13:54:51 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/contacts/import' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/contacts/import"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user with out permission' do
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/contacts/import",
|
|
|
|
headers: agent.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
|
|
|
|
|
|
|
it 'creates a data import' do
|
|
|
|
file = fixture_file_upload(Rails.root.join('spec/assets/contacts.csv'), 'text/csv')
|
|
|
|
post "/api/v1/accounts/#{account.id}/contacts/import",
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
params: { import_file: file }
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(account.data_imports.count).to eq(1)
|
|
|
|
expect(account.data_imports.first.import_file.attached?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2021-09-21 04:50:12 +00:00
|
|
|
|
|
|
|
context 'when file is empty' do
|
|
|
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
|
|
|
|
|
|
|
it 'returns Unprocessable Entity' do
|
|
|
|
post "/api/v1/accounts/#{account.id}/contacts/import",
|
|
|
|
headers: admin.create_new_auth_token
|
|
|
|
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
|
|
expect(json_response['error']).to eq('File is blank')
|
|
|
|
end
|
|
|
|
end
|
2021-02-03 13:54:51 +00:00
|
|
|
end
|
|
|
|
|
2020-10-05 18:00:27 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/contacts/active' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/active"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
|
|
|
let!(:contact) { create(:contact, account: account) }
|
|
|
|
|
|
|
|
it 'returns no contacts if no are online' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/active",
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2021-09-04 12:26:46 +00:00
|
|
|
expect(response.body).not_to include(contact.name)
|
2020-10-05 18:00:27 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns all contacts who are online' do
|
|
|
|
allow(::OnlineStatusTracker).to receive(:get_available_contact_ids).and_return([contact.id])
|
|
|
|
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/active",
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2021-09-04 12:26:46 +00:00
|
|
|
expect(response.body).to include(contact.name)
|
2020-10-05 18:00:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-10 06:45:29 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/contacts/search' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/search"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
2021-09-04 12:26:46 +00:00
|
|
|
let!(:contact1) { create(:contact, :with_email, account: account) }
|
|
|
|
let!(:contact2) { create(:contact, :with_email, name: 'testcontact', account: account, email: 'test@test.com') }
|
2020-08-10 06:45:29 +00:00
|
|
|
|
2021-09-04 12:26:46 +00:00
|
|
|
it 'returns all resolved contacts with contact inboxes' do
|
2020-08-10 06:45:29 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/search",
|
|
|
|
params: { q: contact2.email },
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(response.body).to include(contact2.email)
|
|
|
|
expect(response.body).not_to include(contact1.email)
|
2020-02-17 11:38:29 +00:00
|
|
|
end
|
2020-12-11 17:55:00 +00:00
|
|
|
|
|
|
|
it 'matches the contact ignoring the case in email' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/search",
|
|
|
|
params: { q: 'Test@Test.com' },
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(response.body).to include(contact2.email)
|
|
|
|
expect(response.body).not_to include(contact1.email)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'matches the contact ignoring the case in name' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/search",
|
|
|
|
params: { q: 'TestContact' },
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(response.body).to include(contact2.email)
|
|
|
|
expect(response.body).not_to include(contact1.email)
|
|
|
|
end
|
2021-08-13 07:32:46 +00:00
|
|
|
|
2021-09-04 12:26:46 +00:00
|
|
|
it 'matches the resolved contact respecting the identifier character casing' do
|
2021-08-13 07:32:46 +00:00
|
|
|
contact_normal = create(:contact, name: 'testcontact', account: account, identifier: 'testidentifer')
|
|
|
|
contact_special = create(:contact, name: 'testcontact', account: account, identifier: 'TestIdentifier')
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/search",
|
|
|
|
params: { q: 'TestIdentifier' },
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(response.body).to include(contact_special.identifier)
|
|
|
|
expect(response.body).not_to include(contact_normal.identifier)
|
|
|
|
end
|
2020-02-17 11:38:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-20 12:44:56 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/contacts/filter' do
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/filter"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
|
|
|
let!(:contact1) { create(:contact, :with_email, account: account) }
|
|
|
|
let!(:contact2) { create(:contact, :with_email, name: 'testcontact', account: account, email: 'test@test.com') }
|
|
|
|
|
|
|
|
it 'returns all contacts when query is empty' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/filter",
|
|
|
|
params: { q: [] },
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(response.body).to include(contact2.email)
|
|
|
|
expect(response.body).to include(contact1.email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/contacts/:id' do
|
2020-02-17 11:38:29 +00:00
|
|
|
let!(:contact) { create(:contact, account: account) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}"
|
2020-02-17 11:38:29 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
|
|
|
|
|
|
|
it 'shows the contact' do
|
2020-03-09 17:57:10 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
|
2020-02-17 11:38:29 +00:00
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2021-09-04 12:26:46 +00:00
|
|
|
expect(response.body).to include(contact.name)
|
2020-02-17 11:38:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-04-15 09:43:01 +00:00
|
|
|
describe 'GET /api/v1/accounts/{account.id}/contacts/:id/contactable_inboxes' do
|
|
|
|
let!(:contact) { create(:contact, account: account) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}/contactable_inboxes"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
2021-06-11 06:14:31 +00:00
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
let!(:twilio_sms) { create(:channel_twilio_sms, account: account) }
|
|
|
|
let!(:twilio_sms_inbox) { create(:inbox, channel: twilio_sms, account: account) }
|
|
|
|
let!(:twilio_whatsapp) { create(:channel_twilio_sms, medium: :whatsapp, account: account) }
|
|
|
|
let!(:twilio_whatsapp_inbox) { create(:inbox, channel: twilio_whatsapp, account: account) }
|
|
|
|
|
|
|
|
it 'shows the contactable inboxes which the user has access to' do
|
|
|
|
create(:inbox_member, user: agent, inbox: twilio_whatsapp_inbox)
|
2021-04-15 09:43:01 +00:00
|
|
|
|
|
|
|
inbox_service = double
|
|
|
|
allow(Contacts::ContactableInboxesService).to receive(:new).and_return(inbox_service)
|
2021-06-11 06:14:31 +00:00
|
|
|
allow(inbox_service).to receive(:get).and_return([
|
|
|
|
{ source_id: '1123', inbox: twilio_sms_inbox },
|
|
|
|
{ source_id: '1123', inbox: twilio_whatsapp_inbox }
|
|
|
|
])
|
|
|
|
expect(inbox_service).to receive(:get)
|
2021-04-15 09:43:01 +00:00
|
|
|
get "/api/v1/accounts/#{account.id}/contacts/#{contact.id}/contactable_inboxes",
|
2021-06-11 06:14:31 +00:00
|
|
|
headers: agent.create_new_auth_token,
|
2021-04-15 09:43:01 +00:00
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2021-06-11 06:14:31 +00:00
|
|
|
# only the inboxes which agent has access to are shown
|
|
|
|
expect(JSON.parse(response.body)['payload'].pluck('inbox').pluck('id')).to eq([twilio_whatsapp_inbox.id])
|
2021-04-15 09:43:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'POST /api/v1/accounts/{account.id}/contacts' do
|
2020-08-21 14:00:27 +00:00
|
|
|
let(:custom_attributes) { { test: 'test', test1: 'test1' } }
|
|
|
|
let(:valid_params) { { contact: { name: 'test', custom_attributes: custom_attributes } } }
|
2020-02-17 11:38:29 +00:00
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
2020-05-01 09:23:43 +00:00
|
|
|
it 'returns unauthorized' do
|
|
|
|
expect { post "/api/v1/accounts/#{account.id}/contacts", params: valid_params }.to change(Contact, :count).by(0)
|
2020-02-17 11:38:29 +00:00
|
|
|
|
2020-05-01 09:23:43 +00:00
|
|
|
expect(response).to have_http_status(:unauthorized)
|
2020-02-17 11:38:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
2020-07-21 06:45:24 +00:00
|
|
|
let(:inbox) { create(:inbox, account: account) }
|
2020-02-17 11:38:29 +00:00
|
|
|
|
|
|
|
it 'creates the contact' do
|
2020-03-09 17:57:10 +00:00
|
|
|
expect do
|
|
|
|
post "/api/v1/accounts/#{account.id}/contacts", headers: admin.create_new_auth_token,
|
|
|
|
params: valid_params
|
2020-05-05 18:40:56 +00:00
|
|
|
end.to change(Contact, :count).by(1)
|
2020-02-17 11:38:29 +00:00
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2020-08-21 14:00:27 +00:00
|
|
|
|
|
|
|
# custom attributes are updated
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
expect(json_response['payload']['contact']['custom_attributes']).to eq({ 'test' => 'test', 'test1' => 'test1' })
|
2020-02-17 11:38:29 +00:00
|
|
|
end
|
2020-07-21 06:45:24 +00:00
|
|
|
|
2021-08-13 07:32:46 +00:00
|
|
|
it 'creates the contact inbox when inbox id is passed' do
|
2020-07-21 06:45:24 +00:00
|
|
|
expect do
|
|
|
|
post "/api/v1/accounts/#{account.id}/contacts", headers: admin.create_new_auth_token,
|
|
|
|
params: valid_params.merge({ inbox_id: inbox.id })
|
|
|
|
end.to change(ContactInbox, :count).by(1)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
end
|
2020-02-17 11:38:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
describe 'PATCH /api/v1/accounts/{account.id}/contacts/:id' do
|
2020-08-21 14:00:27 +00:00
|
|
|
let(:custom_attributes) { { test: 'test', test1: 'test1' } }
|
|
|
|
let!(:contact) { create(:contact, account: account, custom_attributes: custom_attributes) }
|
2020-08-22 18:35:07 +00:00
|
|
|
let(:valid_params) { { contact: { name: 'Test Blub', custom_attributes: { test: 'new test', test2: 'test2' } } } }
|
2020-02-17 11:38:29 +00:00
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
2020-03-09 17:57:10 +00:00
|
|
|
put "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
|
2020-02-17 11:38:29 +00:00
|
|
|
params: valid_params
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
|
|
|
|
|
|
|
it 'updates the contact' do
|
2020-03-09 17:57:10 +00:00
|
|
|
patch "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
|
2020-02-17 11:38:29 +00:00
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
params: valid_params,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
2020-08-21 14:00:27 +00:00
|
|
|
expect(contact.reload.name).to eq('Test Blub')
|
2021-01-28 05:09:37 +00:00
|
|
|
# custom attributes are merged properly without overwriting existing ones
|
2020-08-21 14:00:27 +00:00
|
|
|
expect(contact.custom_attributes).to eq({ 'test' => 'new test', 'test1' => 'test1', 'test2' => 'test2' })
|
2020-02-17 11:38:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'prevents the update of contact of another account' do
|
|
|
|
other_account = create(:account)
|
|
|
|
other_contact = create(:contact, account: other_account)
|
|
|
|
|
2020-03-09 17:57:10 +00:00
|
|
|
patch "/api/v1/accounts/#{account.id}/contacts/#{other_contact.id}",
|
2020-02-17 11:38:29 +00:00
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
params: valid_params,
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:not_found)
|
|
|
|
end
|
2020-08-22 18:35:07 +00:00
|
|
|
|
|
|
|
it 'prevents updating with an existing email' do
|
|
|
|
other_contact = create(:contact, account: account, email: 'test1@example.com')
|
|
|
|
|
|
|
|
patch "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
|
|
|
|
headers: admin.create_new_auth_token,
|
|
|
|
params: valid_params[:contact].merge({ email: other_contact.email }),
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
|
|
expect(JSON.parse(response.body)['contact']['id']).to eq(other_contact.id)
|
|
|
|
end
|
2020-02-17 11:38:29 +00:00
|
|
|
end
|
|
|
|
end
|
2021-09-23 07:22:49 +00:00
|
|
|
|
|
|
|
describe 'DELETE /api/v1/accounts/{account.id}/contacts/:id', :contact_delete do
|
|
|
|
let(:inbox) { create(:inbox, account: account) }
|
|
|
|
let(:contact) { create(:contact, account: account) }
|
|
|
|
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: inbox) }
|
|
|
|
let(:conversation) { create(:conversation, account: account, inbox: inbox, contact: contact, contact_inbox: contact_inbox) }
|
|
|
|
|
|
|
|
context 'when it is an unauthenticated user' do
|
|
|
|
it 'returns unauthorized' do
|
|
|
|
delete "/api/v1/accounts/#{account.id}/contacts/#{contact.id}"
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when it is an authenticated user' do
|
|
|
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
|
|
|
let(:agent) { create(:user, account: account, role: :agent) }
|
|
|
|
|
|
|
|
it 'deletes the contact for administrator user' do
|
|
|
|
allow(::OnlineStatusTracker).to receive(:get_presence).and_return(false)
|
|
|
|
delete "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
|
|
|
|
headers: admin.create_new_auth_token
|
|
|
|
|
|
|
|
expect(contact.conversations).to be_empty
|
|
|
|
expect(contact.inboxes).to be_empty
|
|
|
|
expect(contact.contact_inboxes).to be_empty
|
|
|
|
expect(contact.csat_survey_responses).to be_empty
|
|
|
|
expect { contact.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not delete the contact if online' do
|
|
|
|
allow(::OnlineStatusTracker).to receive(:get_presence).and_return(true)
|
|
|
|
|
|
|
|
delete "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
|
|
|
|
headers: admin.create_new_auth_token
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns unauthorized for agent user' do
|
|
|
|
delete "/api/v1/accounts/#{account.id}/contacts/#{contact.id}",
|
|
|
|
headers: agent.create_new_auth_token
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unauthorized)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2020-02-17 11:38:29 +00:00
|
|
|
end
|