2020-01-09 07:36:40 +00:00
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe '/api/v1/widget/messages', type: :request do
|
|
|
|
let(:account) { create(:account) }
|
|
|
|
let(:web_widget) { create(:channel_widget, account: account) }
|
2020-04-03 07:34:58 +00:00
|
|
|
let(:contact) { create(:contact, account: account, email: nil) }
|
2020-01-09 07:36:40 +00:00
|
|
|
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 }
|
|
|
|
|
2022-04-05 08:30:07 +00:00
|
|
|
before do |example|
|
|
|
|
2.times.each { create(:message, account: account, inbox: web_widget.inbox, conversation: conversation) } unless example.metadata[:skip_before]
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /api/v1/widget/messages' do
|
|
|
|
context 'when get request is made' do
|
|
|
|
it 'returns messages in conversation' do
|
|
|
|
get api_v1_widget_messages_url,
|
|
|
|
params: { website_token: web_widget.website_token },
|
|
|
|
headers: { 'X-Auth-Token' => token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
2020-06-09 18:24:35 +00:00
|
|
|
# 2 messages created + 2 messages by the email hook
|
2022-03-31 12:05:39 +00:00
|
|
|
expect(json_response['payload'].length).to eq(4)
|
2022-04-05 08:30:07 +00:00
|
|
|
expect(json_response['meta']).not_to be_empty
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns empty messages', :skip_before do
|
|
|
|
get api_v1_widget_messages_url,
|
|
|
|
params: { website_token: web_widget.website_token },
|
|
|
|
headers: { 'X-Auth-Token' => token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
expect(json_response['payload'].length).to eq(0)
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /api/v1/widget/messages' do
|
|
|
|
context 'when post request is made' do
|
|
|
|
it 'creates message in conversation' do
|
2022-03-24 07:58:25 +00:00
|
|
|
conversation.destroy! # Test all params
|
2020-02-06 19:21:47 +00:00
|
|
|
message_params = { content: 'hello world', timestamp: Time.current }
|
2020-01-09 07:36:40 +00:00
|
|
|
post api_v1_widget_messages_url,
|
|
|
|
params: { website_token: web_widget.website_token, message: message_params },
|
|
|
|
headers: { 'X-Auth-Token' => token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
expect(json_response['content']).to eq(message_params[:content])
|
|
|
|
end
|
2020-03-22 10:24:36 +00:00
|
|
|
|
2022-02-02 22:21:17 +00:00
|
|
|
it 'does not create the message' do
|
2022-03-24 07:58:25 +00:00
|
|
|
conversation.destroy! # Test all params
|
2022-02-02 22:21:17 +00:00
|
|
|
message_params = { content: "#{'h' * 150 * 1000}a", timestamp: Time.current }
|
|
|
|
post api_v1_widget_messages_url,
|
|
|
|
params: { website_token: web_widget.website_token, message: message_params },
|
|
|
|
headers: { 'X-Auth-Token' => token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:unprocessable_entity)
|
|
|
|
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json_response['message']).to eq('Content is too long (maximum is 150000 characters)')
|
|
|
|
end
|
|
|
|
|
2020-03-22 10:24:36 +00:00
|
|
|
it 'creates attachment message in conversation' do
|
|
|
|
file = fixture_file_upload(Rails.root.join('spec/assets/avatar.png'), 'image/png')
|
2020-04-17 15:45:20 +00:00
|
|
|
message_params = { content: 'hello world', timestamp: Time.current, attachments: [file] }
|
2020-03-22 10:24:36 +00:00
|
|
|
post api_v1_widget_messages_url,
|
|
|
|
params: { website_token: web_widget.website_token, message: message_params },
|
|
|
|
headers: { 'X-Auth-Token' => token }
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
expect(json_response['content']).to eq(message_params[:content])
|
|
|
|
|
2020-04-17 15:45:20 +00:00
|
|
|
expect(conversation.messages.last.attachments.first.file.present?).to eq(true)
|
|
|
|
expect(conversation.messages.last.attachments.first.file_type).to eq('image')
|
2020-03-22 10:24:36 +00:00
|
|
|
end
|
2020-05-26 12:13:59 +00:00
|
|
|
|
|
|
|
it 'does not reopen conversation when conversation is muted' do
|
|
|
|
conversation.mute!
|
|
|
|
|
|
|
|
message_params = { content: 'hello world', timestamp: Time.current }
|
|
|
|
post api_v1_widget_messages_url,
|
|
|
|
params: { website_token: web_widget.website_token, message: message_params },
|
|
|
|
headers: { 'X-Auth-Token' => token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.resolved?).to eq(true)
|
|
|
|
end
|
2022-03-23 10:38:42 +00:00
|
|
|
|
|
|
|
it 'does not create resolved activity messages when snoozed conversation is opened' do
|
|
|
|
conversation.snoozed!
|
|
|
|
|
|
|
|
message_params = { content: 'hello world', timestamp: Time.current }
|
|
|
|
post api_v1_widget_messages_url,
|
|
|
|
params: { website_token: web_widget.website_token, message: message_params },
|
|
|
|
headers: { 'X-Auth-Token' => token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(Conversations::ActivityMessageJob).not_to have_been_enqueued.at_least(:once).with(
|
|
|
|
conversation,
|
|
|
|
{
|
|
|
|
account_id: conversation.account_id,
|
|
|
|
inbox_id: conversation.inbox_id,
|
|
|
|
message_type: :activity,
|
|
|
|
content: "Conversation was resolved by #{contact.name}"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
expect(conversation.reload.open?).to eq(true)
|
|
|
|
end
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
2020-04-10 11:12:37 +00:00
|
|
|
message = create(:message, content_type: 'input_email', account: account, inbox: web_widget.inbox, conversation: conversation)
|
2020-01-09 07:36:40 +00:00
|
|
|
email = Faker::Internet.email
|
|
|
|
contact_params = { email: email }
|
|
|
|
put api_v1_widget_message_url(message.id),
|
|
|
|
params: { website_token: web_widget.website_token, contact: contact_params },
|
|
|
|
headers: { 'X-Auth-Token' => token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
message.reload
|
2020-04-10 11:12:37 +00:00
|
|
|
expect(message.submitted_email).to eq(email)
|
2020-01-09 07:36:40 +00:00
|
|
|
expect(message.conversation.contact.email).to eq(email)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-02-06 19:21:47 +00:00
|
|
|
context 'when put request is made with invalid email' do
|
|
|
|
it 'rescues the error' do
|
2020-04-10 11:12:37 +00:00
|
|
|
message = create(:message, account: account, content_type: 'input_email', inbox: web_widget.inbox, conversation: conversation)
|
2020-02-06 19:21:47 +00:00
|
|
|
contact_params = { email: nil }
|
|
|
|
put api_v1_widget_message_url(message.id),
|
|
|
|
params: { website_token: web_widget.website_token, contact: contact_params },
|
|
|
|
headers: { 'X-Auth-Token' => token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:internal_server_error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-09 07:36:40 +00:00
|
|
|
context 'when put request is made with existing email' do
|
|
|
|
it 'updates message in conversation and deletes the current contact' do
|
2020-04-10 11:12:37 +00:00
|
|
|
message = create(:message, account: account, content_type: 'input_email', inbox: web_widget.inbox, conversation: conversation)
|
2020-01-09 07:36:40 +00:00
|
|
|
email = Faker::Internet.email
|
|
|
|
create(:contact, account: account, email: email)
|
|
|
|
contact_params = { email: email }
|
|
|
|
put api_v1_widget_message_url(message.id),
|
|
|
|
params: { website_token: web_widget.website_token, contact: contact_params },
|
|
|
|
headers: { 'X-Auth-Token' => token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
message.reload
|
|
|
|
expect { contact.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
2020-01-13 08:55:23 +00:00
|
|
|
|
|
|
|
it 'ignores the casing of email, updates message in conversation and deletes the current contact' do
|
2020-04-10 11:12:37 +00:00
|
|
|
message = create(:message, content_type: 'input_email', account: account, inbox: web_widget.inbox, conversation: conversation)
|
2020-01-13 08:55:23 +00:00
|
|
|
email = Faker::Internet.email
|
|
|
|
create(:contact, account: account, email: email)
|
|
|
|
contact_params = { email: email.upcase }
|
|
|
|
put api_v1_widget_message_url(message.id),
|
|
|
|
params: { website_token: web_widget.website_token, contact: contact_params },
|
|
|
|
headers: { 'X-Auth-Token' => token },
|
|
|
|
as: :json
|
|
|
|
|
|
|
|
expect(response).to have_http_status(:success)
|
|
|
|
message.reload
|
|
|
|
expect { contact.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
2020-01-09 07:36:40 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|