Feature: Website SDK (#653)

Add SDK functions

Co-authored-by: Sojan <sojan@pepalo.com>
This commit is contained in:
Pranav Raj S
2020-04-03 13:04:58 +05:30
committed by GitHub
parent 7fcd2d0e85
commit cb22b396eb
38 changed files with 734 additions and 262 deletions

View File

@@ -0,0 +1,45 @@
require 'rails_helper'
describe ::ContactIdentifyAction do
subject(:contact_identify) { described_class.new(contact: contact, params: params).perform }
let!(:account) { create(:account) }
let!(:contact) { create(:contact, account: account) }
let(:params) { { name: 'test', identifier: 'test_id' } }
describe '#perform' do
it 'updates the contact' do
expect(ContactAvatarJob).not_to receive(:perform_later).with(contact, params[:avatar_url])
contact_identify
expect(contact.reload.name).to eq 'test'
expect(contact.reload.identifier).to eq 'test_id'
end
it 'enques avatar job when avatar url parameter is passed' do
params = { name: 'test', avatar_url: 'https://via.placeholder.com/250x250.png' }
expect(ContactAvatarJob).to receive(:perform_later).with(contact, params[:avatar_url]).once
described_class.new(contact: contact, params: params).perform
end
context 'when contact with same identifier exists' do
it 'merges the current contact to identified contact' do
existing_identified_contact = create(:contact, account: account, identifier: 'test_id')
result = contact_identify
expect(result.id).to eq existing_identified_contact.id
expect(result.name).to eq params[:name]
expect { contact.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context 'when contact with same email exists' do
it 'merges the current contact to email contact' do
existing_email_contact = create(:contact, account: account, email: 'test@test.com')
params = { email: 'test@test.com' }
result = described_class.new(contact: contact, params: params).perform
expect(result.id).to eq existing_email_contact.id
expect(result.name).to eq existing_email_contact.name
expect { contact.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end

View File

@@ -0,0 +1,42 @@
require 'rails_helper'
RSpec.describe '/api/v1/widget/contacts', type: :request do
let(:account) { create(:account) }
let(:web_widget) { create(:channel_widget, account: account) }
let(:contact) { create(:contact, account: account) }
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: web_widget.inbox) }
let(:payload) { { source_id: contact_inbox.source_id, inbox_id: web_widget.inbox.id } }
let(:token) { ::Widget::TokenService.new(payload: payload).generate_token }
describe 'PATCH /api/v1/widget/contact' do
let(:params) { { website_token: web_widget.website_token, identifier: 'test' } }
context 'with invalid website token' do
it 'returns unauthorized' do
patch '/api/v1/widget/contact', params: { website_token: '' }
expect(response).to have_http_status(:not_found)
end
end
context 'with correct website token' do
let(:identify_action) { double }
before do
allow(ContactIdentifyAction).to receive(:new).and_return(identify_action)
allow(identify_action).to receive(:perform)
end
it 'calls contact identify' do
patch '/api/v1/widget/contact',
params: params,
headers: { 'X-Auth-Token' => token },
as: :json
expect(response).to have_http_status(:success)
expected_params = { contact: contact, params: params }
expect(ContactIdentifyAction).to have_received(:new).with(expected_params)
expect(identify_action).to have_received(:perform)
end
end
end
end

View File

@@ -0,0 +1,63 @@
require 'rails_helper'
RSpec.describe '/api/v1/widget/labels', type: :request do
let(:account) { create(:account) }
let(:web_widget) { create(:channel_widget, account: account) }
let(:contact) { create(:contact, account: account) }
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: web_widget.inbox) }
let!(:conversation) { create(:conversation, contact: contact, account: account, inbox: web_widget.inbox, contact_inbox: contact_inbox) }
let(:payload) { { source_id: contact_inbox.source_id, inbox_id: web_widget.inbox.id } }
let(:token) { ::Widget::TokenService.new(payload: payload).generate_token }
describe 'POST /api/v1/widget/labels' do
let(:params) { { website_token: web_widget.website_token, label: 'customer-support' } }
context 'with correct website token' do
it 'returns the list of labels' do
post '/api/v1/widget/labels',
params: params,
headers: { 'X-Auth-Token' => token },
as: :json
expect(response).to have_http_status(:success)
expect(conversation.reload.label_list.count).to eq 1
expect(conversation.reload.label_list.first).to eq 'customer-support'
end
end
context 'with invalid website token' do
it 'returns the list of labels' do
post '/api/v1/widget/labels', params: { website_token: '' }
expect(response).to have_http_status(:not_found)
end
end
end
describe 'DELETE /api/v1/widget/labels' do
before do
conversation.label_list.add('customer-support')
conversation.save!
end
let(:params) { { website_token: web_widget.website_token, label: 'customer-support' } }
context 'with correct website token' do
it 'returns the list of labels' do
delete "/api/v1/widget/labels/#{params[:label]}",
params: params,
headers: { 'X-Auth-Token' => token },
as: :json
expect(response).to have_http_status(:success)
expect(conversation.reload.label_list.count).to eq 0
end
end
context 'with invalid website token' do
it 'returns the list of labels' do
delete "/api/v1/widget/labels/#{params[:label]}", params: { website_token: '' }
expect(response).to have_http_status(:not_found)
end
end
end
end

View File

@@ -3,7 +3,7 @@ require 'rails_helper'
RSpec.describe '/api/v1/widget/messages', type: :request do
let(:account) { create(:account) }
let(:web_widget) { create(:channel_widget, account: account) }
let(:contact) { create(:contact, account: account) }
let(:contact) { create(:contact, account: account, email: nil) }
let(:contact_inbox) { create(:contact_inbox, contact: contact, inbox: web_widget.inbox) }
let(:conversation) { create(:conversation, contact: contact, account: account, inbox: web_widget.inbox, contact_inbox: contact_inbox) }
let(:payload) { { source_id: contact_inbox.source_id, inbox_id: web_widget.inbox.id } }

View File

@@ -6,7 +6,10 @@ describe ::MessageFinder do
let!(:account) { create(:account) }
let!(:user) { create(:user, account: account) }
let!(:inbox) { create(:inbox, account: account) }
let!(:conversation) { create(:conversation, account: account, inbox: inbox, assignee: user) }
let!(:contact) { create(:contact, email: nil) }
let!(:conversation) do
create(:conversation, account: account, inbox: inbox, assignee: user, contact: contact)
end
before do
create(:message, account: account, inbox: inbox, conversation: conversation)

View File

@@ -3,7 +3,10 @@ require 'rails_helper'
describe ::MessageTemplates::HookExecutionService do
context 'when it is a first message from web widget' do
it 'calls ::MessageTemplates::Template::EmailCollect' do
message = create(:message)
contact = create(:contact, email: nil)
conversation = create(:conversation, contact: contact)
message = create(:message, conversation: conversation)
# this hook will only get executed for conversations with out any template messages
message.conversation.messages.template.destroy_all