Chore: API Improvements (#2956)

- API to fetch info of a single inbox
- Document passing custom_attributes in the API
- Ability to filter contacts with contact identifier in search API
This commit is contained in:
Sojan Jose
2021-09-04 17:56:46 +05:30
committed by GitHub
parent b866c54ad5
commit 2ebd38c3b7
29 changed files with 248 additions and 29 deletions

View File

@@ -3,7 +3,7 @@ require 'rails_helper'
describe ::ContactBuilder do
let(:account) { create(:account) }
let(:inbox) { create(:inbox, account: account) }
let(:contact) { create(:contact, account: account, identifier: '123') }
let(:contact) { create(:contact, email: 'xyc@example.com', phone_number: '+23423424123', account: account, identifier: '123') }
let(:existing_contact_inbox) { create(:contact_inbox, contact: contact, inbox: inbox) }
describe '#perform' do

View File

@@ -2,7 +2,7 @@ require 'rails_helper'
describe ::ContactInboxBuilder do
let(:account) { create(:account) }
let(:contact) { create(:contact, account: account) }
let(:contact) { create(:contact, email: 'xyc@example.com', phone_number: '+23423424123', account: account) }
describe '#perform' do
describe 'twilio sms inbox' do

View File

@@ -14,10 +14,10 @@ RSpec.describe 'Contacts API', type: :request do
context 'when it is an authenticated user' do
let(:admin) { create(:user, account: account, role: :administrator) }
let!(:contact) { create(:contact, account: account) }
let!(:contact) { create(:contact, :with_email, account: account) }
let!(:contact_inbox) { create(:contact_inbox, contact: contact) }
it 'returns all contacts with contact inboxes' do
it 'returns all resolved contacts along with contact inboxes' do
get "/api/v1/accounts/#{account.id}/contacts",
headers: admin.create_new_auth_token,
as: :json
@@ -52,8 +52,8 @@ RSpec.describe 'Contacts API', type: :request do
expect(response_body['payload'].first['last_seen_at']).present?
end
it 'filters contacts based on label filter' do
contact_with_label1, contact_with_label2 = FactoryBot.create_list(:contact, 2, account: account)
it 'filters resolved contacts based on label filter' do
contact_with_label1, contact_with_label2 = FactoryBot.create_list(:contact, 2, :with_email, account: account)
contact_with_label1.update_labels(['label1'])
contact_with_label2.update_labels(['label2'])
@@ -126,7 +126,7 @@ RSpec.describe 'Contacts API', type: :request do
as: :json
expect(response).to have_http_status(:success)
expect(response.body).not_to include(contact.email)
expect(response.body).not_to include(contact.name)
end
it 'returns all contacts who are online' do
@@ -137,7 +137,7 @@ RSpec.describe 'Contacts API', type: :request do
as: :json
expect(response).to have_http_status(:success)
expect(response.body).to include(contact.email)
expect(response.body).to include(contact.name)
end
end
end
@@ -153,10 +153,10 @@ RSpec.describe 'Contacts API', type: :request do
context 'when it is an authenticated user' do
let(:admin) { create(:user, account: account, role: :administrator) }
let!(:contact1) { create(:contact, account: account) }
let!(:contact2) { create(:contact, name: 'testcontact', account: account, email: 'test@test.com') }
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 with contact inboxes' do
it 'returns all resolved contacts with contact inboxes' do
get "/api/v1/accounts/#{account.id}/contacts/search",
params: { q: contact2.email },
headers: admin.create_new_auth_token,
@@ -189,7 +189,7 @@ RSpec.describe 'Contacts API', type: :request do
expect(response.body).not_to include(contact1.email)
end
it 'matches the contact respecting the identifier character casing' do
it 'matches the resolved contact respecting the identifier character casing' do
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",
@@ -224,7 +224,7 @@ RSpec.describe 'Contacts API', type: :request do
as: :json
expect(response).to have_http_status(:success)
expect(response.body).to include(contact.email)
expect(response.body).to include(contact.name)
end
end
end

View File

@@ -44,6 +44,51 @@ RSpec.describe 'Inboxes API', type: :request do
end
end
describe 'GET /api/v1/accounts/{account.id}/inboxes/{inbox.id}' do
let(:inbox) { create(:inbox, account: account) }
context 'when it is an unauthenticated user' do
it 'returns unauthorized' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}"
expect(response).to have_http_status(:unauthorized)
end
end
context 'when it is an authenticated user' do
let(:agent) { create(:user, account: account, role: :agent) }
let(:admin) { create(:user, account: account, role: :administrator) }
let(:inbox) { create(:inbox, account: account) }
it 'returns unauthorized for an agent who is not assigned' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unauthorized)
end
it 'returns the inbox if administrator' do
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}",
headers: admin.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body, symbolize_names: true)[:id]).to eq(inbox.id)
end
it 'returns the inbox if assigned inbox is assigned as agent' do
create(:inbox_member, user: agent, inbox: inbox)
get "/api/v1/accounts/#{account.id}/inboxes/#{inbox.id}",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(JSON.parse(response.body, symbolize_names: true)[:id]).to eq(inbox.id)
end
end
end
describe 'GET /api/v1/accounts/{account.id}/inboxes/{inbox.id}/assignable_agents' do
let(:inbox) { create(:inbox, account: account) }

View File

@@ -2,7 +2,7 @@ require 'rails_helper'
RSpec.describe 'Public Inbox Contact Conversation Messages API', type: :request do
let!(:api_channel) { create(:channel_api) }
let!(:contact) { create(:contact) }
let!(:contact) { create(:contact, phone_number: '+324234324', email: 'dfsadf@sfsda.com') }
let!(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: api_channel.inbox) }
let!(:conversation) { create(:conversation, contact: contact, contact_inbox: contact_inbox) }

View File

@@ -3,9 +3,15 @@
FactoryBot.define do
factory :contact do
sequence(:name) { |n| "Contact #{n}" }
sequence(:email) { |n| "contact-#{n}@example.com" }
phone_number { Faker::PhoneNumber.cell_phone_in_e164 }
avatar { fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png') }
account
trait :with_email do
sequence(:email) { |n| "contact-#{n}@example.com" }
end
trait :with_phone_number do
phone_number { Faker::PhoneNumber.cell_phone_in_e164 }
end
end
end

View File

@@ -13,7 +13,7 @@ FactoryBot.define do
account: conversation.account,
channel: create(:channel_widget, account: conversation.account)
)
conversation.contact ||= create(:contact, account: conversation.account)
conversation.contact ||= create(:contact, :with_email, account: conversation.account)
conversation.contact_inbox ||= create(:contact_inbox, contact: conversation.contact, inbox: conversation.inbox)
end
end

View File

@@ -2,7 +2,7 @@ require 'rails_helper'
describe Contacts::ContactableInboxesService do
let(:account) { create(:account) }
let(:contact) { create(:contact, account: account) }
let(:contact) { create(:contact, account: account, email: 'contact@example.com', phone_number: '+2320000') }
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) }

View File

@@ -34,7 +34,7 @@ describe Twilio::OneoffSmsCampaignService do
end
it 'send messages to contacts in the audience and marks the campaign completed' do
contact_with_label1, contact_with_label2, contact_with_both_labels = FactoryBot.create_list(:contact, 3, account: account)
contact_with_label1, contact_with_label2, contact_with_both_labels = FactoryBot.create_list(:contact, 3, :with_phone_number, account: account)
contact_with_label1.update_labels([label1.title])
contact_with_label2.update_labels([label2.title])
contact_with_both_labels.update_labels([label1.title, label2.title])