feat: account onboarding with clearbit (#8857)

* feat: add clearbit lookup

* chore: fix typo in .env.example

* refactor: split lookup to reduce cognitive complexity

* feat: add more fields to lookup

* feat: extend accounts controller

* feat: save extra data to custom_attributes

* feat: allow v2 update with custom_attributes

* feat: add update route

* refactor: reduce complexity

* feat: move update to v1 controller

* test: add locale test

* feat: remove update from routes

* test: update API for custom attributes

* test: all custom attributes

* fix: v2 tests

* test: enterprise accounts controller

* fix: clearbit payload

* fix: with modified env

* feat: allow custom attributes updates to profile

* refactor: reduce complexity

* feat: allow clearbit api key in installation config

* refactor: move clearbit to internal

* feat: allow clearbit

* chore: add display_title for June

* feat: allow more internal options

* refactor: use globalconfig to fetch clearbit token

* test: move response body to a factory

* refactor: update ops

* chore: remove clearbit from .env.example

* chore: apply suggestions from code review

Co-authored-by: sojan-official <sojan@chatwoot.com>

---------

Co-authored-by: sojan-official <sojan@chatwoot.com>
This commit is contained in:
Shivam Mishra
2024-02-12 23:21:42 +05:30
committed by GitHub
parent fc6a22b072
commit 657843960c
14 changed files with 367 additions and 33 deletions

View File

@@ -0,0 +1,60 @@
require 'rails_helper'
RSpec.describe Enterprise::ClearbitLookupService do
describe '.lookup' do
let(:email) { 'test@example.com' }
let(:api_key) { 'clearbit_api_key' }
let(:clearbit_endpoint) { described_class::CLEARBIT_ENDPOINT }
let(:response_body) { build(:clearbit_combined_response) }
context 'when Clearbit is enabled' do
before do
stub_request(:get, "#{clearbit_endpoint}?email=#{email}")
.with(headers: { 'Authorization' => "Bearer #{api_key}" })
.to_return(status: 200, body: response_body, headers: { 'content-type' => ['application/json'] })
end
context 'when the API is working as expected' do
it 'returns the person and company information' do
with_modified_env CLEARBIT_API_KEY: api_key do
result = described_class.lookup(email)
expect(result).to eq({
:avatar => 'https://example.com/avatar.png',
:company_name => 'Doe Inc.',
:company_size => '1-10',
:industry => 'Software',
:logo => nil,
:name => 'John Doe',
:timezone => 'Asia/Kolkata'
})
end
end
end
context 'when the API returns an error' do
before do
stub_request(:get, "#{clearbit_endpoint}?email=#{email}")
.with(headers: { 'Authorization' => "Bearer #{api_key}" })
.to_return(status: 404, body: '', headers: {})
end
it 'logs the error and returns nil' do
with_modified_env CLEARBIT_API_KEY: api_key do
expect(Rails.logger).to receive(:error)
expect(described_class.lookup(email)).to be_nil
end
end
end
end
context 'when Clearbit is not enabled' do
it 'returns nil without making an API call' do
with_modified_env CLEARBIT_API_KEY: nil do
expect(Net::HTTP).not_to receive(:start)
expect(described_class.lookup(email)).to be_nil
end
end
end
end
end