Feature: API Channel (#1052)

This commit is contained in:
Sojan Jose
2020-07-21 12:15:24 +05:30
committed by GitHub
parent fa04098c20
commit 8079bf50a0
40 changed files with 735 additions and 246 deletions

View File

@@ -1,6 +1,6 @@
require 'rails_helper'
describe ::Messages::IncomingMessageBuilder do
describe ::Messages::Facebook::MessageBuilder do
subject(:message_builder) { described_class.new(incoming_fb_text_message, facebook_channel.inbox).perform }
let!(:facebook_channel) { create(:channel_facebook_page) }

View File

@@ -0,0 +1,54 @@
require 'rails_helper'
describe ::Messages::MessageBuilder do
subject(:message_builder) { described_class.new(user, conversation, params).perform }
let(:account) { create(:account) }
let(:user) { create(:user, account: account) }
let(:inbox) { create(:inbox, account: account) }
let(:inbox_member) { create(:inbox_member, inbox: inbox, account: account) }
let(:conversation) { create(:conversation, inbox: inbox, account: account) }
let(:params) do
ActionController::Parameters.new({
content: 'test'
})
end
describe '#perform' do
it 'creates a message' do
message = message_builder
expect(message.content).to eq params[:content]
end
end
describe '#perform when message_type is incoming' do
context 'when channel is not api' do
let(:params) do
ActionController::Parameters.new({
content: 'test',
message_type: 'incoming'
})
end
it 'creates throws error when channel is not api' do
expect { message_builder }.to raise_error 'Incoming messages are only allowed in Api inboxes'
end
end
context 'when channel is api' do
let(:channel_api) { create(:channel_api, account: account) }
let(:conversation) { create(:conversation, inbox: channel_api.inbox, account: account) }
let(:params) do
ActionController::Parameters.new({
content: 'test',
message_type: 'incoming'
})
end
it 'creates message when channel is api' do
message = message_builder
expect(message.message_type).to eq params[:message_type]
end
end
end
end

View File

@@ -65,6 +65,7 @@ RSpec.describe 'Contacts API', type: :request do
context 'when it is an authenticated user' do
let(:admin) { create(:user, account: account, role: :administrator) }
let(:inbox) { create(:inbox, account: account) }
it 'creates the contact' do
expect do
@@ -74,6 +75,15 @@ RSpec.describe 'Contacts API', type: :request do
expect(response).to have_http_status(:success)
end
it 'creates the contact identifier when inbox id is passed' do
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
end
end

View File

@@ -0,0 +1,9 @@
FactoryBot.define do
factory :channel_api, class: 'Channel::Api' do
webhook_url { 'http://example.com' }
account
after(:create) do |channel_api|
create(:inbox, channel: channel_api, account: channel_api.account)
end
end
end

View File

@@ -5,10 +5,10 @@ describe Webhooks::Trigger do
describe '#execute' do
it 'triggers webhook' do
params = { hello: 'hello' }
url = 'htpps://test.com'
params = { hello: :hello }
url = 'https://test.com'
expect(RestClient).to receive(:post).with(url, params).once
expect(RestClient).to receive(:post).with(url, params.to_json, { accept: :json, content_type: :json }).once
trigger.execute(url, params)
end
end