mirror of
https://github.com/lingble/chatwoot.git
synced 2025-11-02 03:57:52 +00:00
Feature: Rich Message Types (#610)
Co-authored-by: Pranav Raj S <pranavrajs@gmail.com> Co-authored-by: Nithin David Thomas <webofnithin@gmail.com>
This commit is contained in:
@@ -19,7 +19,7 @@ RSpec.describe 'Conversation Messages API', type: :request do
|
||||
let(:agent) { create(:user, account: account, role: :agent) }
|
||||
|
||||
it 'creates a new outgoing message' do
|
||||
params = { message: 'test-message', private: true }
|
||||
params = { content: 'test-message', private: true }
|
||||
|
||||
post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id),
|
||||
params: params,
|
||||
@@ -28,12 +28,12 @@ RSpec.describe 'Conversation Messages API', type: :request do
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(conversation.messages.count).to eq(1)
|
||||
expect(conversation.messages.first.content).to eq(params[:message])
|
||||
expect(conversation.messages.first.content).to eq(params[:content])
|
||||
end
|
||||
|
||||
it 'creates a new outgoing message with attachment' do
|
||||
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
|
||||
params = { message: 'test-message', attachment: { file: file } }
|
||||
params = { content: 'test-message', attachment: { file: file } }
|
||||
|
||||
post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id),
|
||||
params: params,
|
||||
@@ -50,7 +50,7 @@ RSpec.describe 'Conversation Messages API', type: :request do
|
||||
|
||||
it 'creates a new outgoing message' do
|
||||
create(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot)
|
||||
params = { message: 'test-message' }
|
||||
params = { content: 'test-message' }
|
||||
|
||||
post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id),
|
||||
params: params,
|
||||
@@ -59,7 +59,39 @@ RSpec.describe 'Conversation Messages API', type: :request do
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(conversation.messages.count).to eq(1)
|
||||
expect(conversation.messages.first.content).to eq(params[:message])
|
||||
expect(conversation.messages.first.content).to eq(params[:content])
|
||||
end
|
||||
|
||||
it 'creates a new outgoing input select message' do
|
||||
create(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot)
|
||||
select_item1 = build(:bot_message_select)
|
||||
select_item2 = build(:bot_message_select)
|
||||
params = { content_type: 'input_select', content_attributes: { items: [select_item1, select_item2] } }
|
||||
|
||||
post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id),
|
||||
params: params,
|
||||
headers: { api_access_token: agent_bot.access_token.token },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(conversation.messages.count).to eq(1)
|
||||
expect(conversation.messages.first.content_type).to eq(params[:content_type])
|
||||
expect(conversation.messages.first.content).to eq nil
|
||||
end
|
||||
|
||||
it 'creates a new outgoing cards message' do
|
||||
create(:agent_bot_inbox, inbox: inbox, agent_bot: agent_bot)
|
||||
card = build(:bot_message_card)
|
||||
params = { content_type: 'cards', content_attributes: { items: [card] } }
|
||||
|
||||
post api_v1_account_conversation_messages_url(account_id: account.id, conversation_id: conversation.display_id),
|
||||
params: params,
|
||||
headers: { api_access_token: agent_bot.access_token.token },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(conversation.messages.count).to eq(1)
|
||||
expect(conversation.messages.first.content_type).to eq(params[:content_type])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
37
spec/controllers/api/v1/widget/events_controller_spec.rb
Normal file
37
spec/controllers/api/v1/widget/events_controller_spec.rb
Normal file
@@ -0,0 +1,37 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe '/api/v1/widget/events', 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 'POST /api/v1/widget/events' do
|
||||
let(:params) { { website_token: web_widget.website_token, name: 'webwidget.triggered' } }
|
||||
|
||||
context 'with invalid website token' do
|
||||
it 'returns unauthorized' do
|
||||
post '/api/v1/widget/events', params: { website_token: '' }
|
||||
expect(response).to have_http_status(:not_found)
|
||||
end
|
||||
end
|
||||
|
||||
context 'with correct website token' do
|
||||
before do
|
||||
allow(Rails.configuration.dispatcher).to receive(:dispatch)
|
||||
end
|
||||
|
||||
it 'dispatches the webwidget event' do
|
||||
post '/api/v1/widget/events',
|
||||
params: params,
|
||||
headers: { 'X-Auth-Token' => token },
|
||||
as: :json
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
expect(Rails.configuration.dispatcher).to have_received(:dispatch).with(params[:name], anything, contact_inbox: contact_inbox)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -65,7 +65,7 @@ RSpec.describe '/api/v1/widget/messages', type: :request do
|
||||
describe 'PUT /api/v1/widget/messages' do
|
||||
context 'when put request is made with non existing email' do
|
||||
it 'updates message in conversation and creates a new contact' do
|
||||
message = create(:message, account: account, inbox: web_widget.inbox, conversation: conversation)
|
||||
message = create(:message, content_type: 'input_email', account: account, inbox: web_widget.inbox, conversation: conversation)
|
||||
email = Faker::Internet.email
|
||||
contact_params = { email: email }
|
||||
put api_v1_widget_message_url(message.id),
|
||||
@@ -75,14 +75,14 @@ RSpec.describe '/api/v1/widget/messages', type: :request do
|
||||
|
||||
expect(response).to have_http_status(:success)
|
||||
message.reload
|
||||
expect(message.input_submitted_email).to eq(email)
|
||||
expect(message.submitted_email).to eq(email)
|
||||
expect(message.conversation.contact.email).to eq(email)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when put request is made with invalid email' do
|
||||
it 'rescues the error' do
|
||||
message = create(:message, account: account, inbox: web_widget.inbox, conversation: conversation)
|
||||
message = create(:message, account: account, content_type: 'input_email', inbox: web_widget.inbox, conversation: conversation)
|
||||
contact_params = { email: nil }
|
||||
put api_v1_widget_message_url(message.id),
|
||||
params: { website_token: web_widget.website_token, contact: contact_params },
|
||||
@@ -95,7 +95,7 @@ RSpec.describe '/api/v1/widget/messages', type: :request do
|
||||
|
||||
context 'when put request is made with existing email' do
|
||||
it 'updates message in conversation and deletes the current contact' do
|
||||
message = create(:message, account: account, inbox: web_widget.inbox, conversation: conversation)
|
||||
message = create(:message, account: account, content_type: 'input_email', inbox: web_widget.inbox, conversation: conversation)
|
||||
email = Faker::Internet.email
|
||||
create(:contact, account: account, email: email)
|
||||
contact_params = { email: email }
|
||||
@@ -110,7 +110,7 @@ RSpec.describe '/api/v1/widget/messages', type: :request do
|
||||
end
|
||||
|
||||
it 'ignores the casing of email, updates message in conversation and deletes the current contact' do
|
||||
message = create(:message, account: account, inbox: web_widget.inbox, conversation: conversation)
|
||||
message = create(:message, content_type: 'input_email', account: account, inbox: web_widget.inbox, conversation: conversation)
|
||||
email = Faker::Internet.email
|
||||
create(:contact, account: account, email: email)
|
||||
contact_params = { email: email.upcase }
|
||||
|
||||
Reference in New Issue
Block a user