feat: Provision captain accounts automatically (#10168)

- Provision accounts on Chatwoot cloud automatically if the feature is enabled
This commit is contained in:
Sojan Jose
2024-09-26 19:21:29 -07:00
committed by GitHub
parent d107d0adec
commit 4a7a0427e9
7 changed files with 136 additions and 31 deletions

View File

@@ -69,4 +69,24 @@ describe ChatwootHub do
end
end
end
context 'when fetching captain settings' do
it 'returns the captain settings' do
account = create(:account)
stub_request(:post, ChatwootHub::CAPTAIN_ACCOUNTS_URL).with(
body: { installation_identifier: described_class.installation_identifier, chatwoot_account_id: account.id, account_name: account.name }
).to_return(
body: { account_email: 'test@test.com', account_id: '123', access_token: '123', assistant_id: '123' }.to_json
)
expect(described_class.get_captain_settings(account).body).to eq(
{
account_email: 'test@test.com',
account_id: '123',
access_token: '123',
assistant_id: '123'
}.to_json
)
end
end
end

View File

@@ -66,6 +66,28 @@ RSpec.describe Integrations::App do
end
end
context 'when the app is captain' do
let(:app_name) { 'captain' }
it 'returns false is the captain feature is not enabled' do
expect(app.active?(account)).to be false
end
it 'returns false if the captain app url is not present' do
account.enable_features('captain_integration')
account.save!
expect(InstallationConfig.find_by(name: 'CAPTAIN_APP_URL')).to be_nil
expect(app.active?(account)).to be false
end
it 'returns true if the captain feature is enabled and the captain app url is present' do
account.enable_features('captain_integration')
account.save!
InstallationConfig.where(name: 'CAPTAIN_APP_URL').first_or_create(value: 'https://app.chatwoot.com')
expect(app.active?(account)).to be true
end
end
context 'when other apps are queried' do
let(:app_name) { 'webhook' }

View File

@@ -51,4 +51,48 @@ RSpec.describe Integrations::Hook do
expect(openai_double).to have_received(:perform)
end
end
describe 'creating a captain hook' do
let(:account) { create(:account) }
let(:inbox) { create(:inbox, account: account) }
let(:settings) { { inbox_ids: inbox.id } }
it 'raises an error if captain is not enabled' do
expect { create(:integrations_hook, app_id: 'captain', account: account, settings: settings) }.to raise_error('Captain is not enabled')
end
context 'when captain is enabled' do
before do
account.enable_features('captain_integration')
account.save!
InstallationConfig.where(name: 'CAPTAIN_APP_URL').first_or_create(value: 'https://app.chatwoot.com')
stub_request(:post, ChatwootHub::CAPTAIN_ACCOUNTS_URL).to_return(body: {
account_email: 'test@example.com',
captain_account_id: 1,
access_token: 'access_token',
assistant_id: 1
}.to_json)
end
it 'populates the settings with captain settings' do
hook = create(:integrations_hook, app_id: 'captain', account: account, settings: settings)
expect(hook.settings['account_email']).to be_present
expect(hook.settings['assistant_id']).to be_present
expect(hook.settings['access_token']).to be_present
expect(hook.settings['assistant_id']).to be_present
end
it 'raises an error if the request to captain hub fails' do
stub_request(:post, ChatwootHub::CAPTAIN_ACCOUNTS_URL).to_return(
status: 500,
body: {
error: 'Failed to get captain settings'
}.to_json
)
expect do
create(:integrations_hook, app_id: 'captain', account: account, settings: settings)
end.to raise_error('Failed to get captain settings: {"error":"Failed to get captain settings"}')
end
end
end
end