clearbit_spec.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. # Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Clearbit', aggregate_failures: true, current_user_id: 1, integration: true, performs_jobs: true, required_envs: %w[CLEARBIT_CI_API_KEY] do
  4. let(:clearbit_config_organization_shared) { false }
  5. let(:clearbit_config_user_bio) { 'user.note' }
  6. let(:clearbit_config_company_bio) { 'organization.note' }
  7. let(:clearbit_config_user_familyname) { 'user.lastname' }
  8. before do
  9. Setting.set('clearbit_integration', true)
  10. Setting.set('clearbit_config', {
  11. api_key: ENV['CLEARBIT_CI_API_KEY'],
  12. organization_autocreate: true,
  13. organization_shared: clearbit_config_organization_shared,
  14. user_sync: {
  15. 'person.name.givenName' => 'user.firstname',
  16. 'person.name.familyName' => clearbit_config_user_familyname,
  17. 'person.email' => 'user.email',
  18. 'person.bio' => clearbit_config_user_bio,
  19. 'company.url' => 'user.web',
  20. 'person.site' => 'user.web',
  21. 'company.location' => 'user.address',
  22. 'person.location' => 'user.address',
  23. },
  24. organization_sync: {
  25. 'company.legalName' => 'organization.name',
  26. 'company.name' => 'organization.name',
  27. 'company.description' => clearbit_config_company_bio,
  28. },
  29. })
  30. end
  31. describe 'case 1 - person + company (demo data set)' do
  32. let(:customer) do
  33. User.create!(
  34. firstname: '',
  35. lastname: 'Should be still there',
  36. email: 'alex@alexmaccaw.com',
  37. note: '',
  38. )
  39. end
  40. before do
  41. customer
  42. perform_enqueued_jobs commit_transaction: true
  43. customer.reload
  44. end
  45. it 'enriches the customer' do
  46. expect(ExternalSync).to be_exist(source: 'clearbit', object: 'User', o_id: customer.id)
  47. expect(customer).to have_attributes(
  48. firstname: 'Should',
  49. lastname: 'be still there',
  50. note: 'O\'Reilly author, software engineer &amp; traveller. Founder of <a href="https://clearbit.com" rel="nofollow noreferrer noopener" target="_blank">https://clearbit.com</a>',
  51. address: '1455 Market Street, San Francisco, CA 94103, USA',
  52. )
  53. end
  54. it 'creates organization with enriched data' do
  55. expect(ExternalSync).to be_exist(source: 'clearbit', object: 'Organization', o_id: customer.organization.id)
  56. expect(customer.organization).to have_attributes(
  57. name: 'Uber, Inc.',
  58. shared: false,
  59. note: 'Uber is a mobile app connecting passengers with drivers for hire.'
  60. )
  61. end
  62. context 'with organization shared set to true' do
  63. let(:clearbit_config_organization_shared) { true }
  64. it 'creates organization with enriched data' do
  65. expect(customer.organization).to have_attributes(
  66. name: 'Uber, Inc.',
  67. shared: true,
  68. )
  69. end
  70. end
  71. context 'with non existing note field' do
  72. let(:clearbit_config_user_bio) { 'user.note_not_existing' }
  73. let(:clearbit_config_company_bio) { 'organization.note_not_existing' }
  74. it 'syncs the rest of user fields' do
  75. expect(ExternalSync).to be_exist(source: 'clearbit', object: 'User', o_id: customer.id)
  76. expect(customer).to have_attributes(
  77. firstname: 'Should',
  78. lastname: 'be still there',
  79. note: '',
  80. )
  81. end
  82. it 'syncs the rest of organization fields' do
  83. expect(ExternalSync).to be_exist(source: 'clearbit', object: 'Organization', o_id: customer.organization.id)
  84. expect(customer.organization).to have_attributes(
  85. name: 'Uber, Inc.',
  86. note: ''
  87. )
  88. end
  89. end
  90. end
  91. describe 'case 2 - person + company' do
  92. let(:customer) do
  93. User.create!(
  94. firstname: '',
  95. lastname: '',
  96. email: 'me@example.com',
  97. note: '',
  98. )
  99. end
  100. before do
  101. customer
  102. perform_enqueued_jobs commit_transaction: true
  103. customer.reload
  104. end
  105. it 'enriches the customer' do
  106. expect(ExternalSync).to be_exist(source: 'clearbit', object: 'User', o_id: customer.id)
  107. expect(customer).to have_attributes(
  108. firstname: 'Martin',
  109. lastname: 'Edenhofer',
  110. note: "Open Source professional and geek. Also known as OTRS inventor. ;)\r\nEntrepreneur and Advisor for open source people in need.",
  111. address: 'Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany',
  112. )
  113. end
  114. it 'creates organization with enriched data' do
  115. expect(ExternalSync).to be_exist(source: 'clearbit', object: 'Organization', o_id: customer.organization.id)
  116. expect(customer.organization).to have_attributes(
  117. name: 'OTRS',
  118. note: 'OTRS is an Open Source helpdesk software and an IT Service Management software free of licence costs. Improve your Customer Service Management with OTRS.'
  119. )
  120. end
  121. context 'when email changes' do
  122. before do
  123. customer.update!(
  124. email: 'me2@example.com',
  125. )
  126. Enrichment::Clearbit::User.new(customer).synced?
  127. perform_enqueued_jobs commit_transaction: true
  128. end
  129. it 'Update with another email data' do
  130. expect(customer.reload).to have_attributes(
  131. firstname: 'Martini',
  132. lastname: 'Edenhofer',
  133. address: 'Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany',
  134. )
  135. end
  136. end
  137. context 'when updated locally' do
  138. before do
  139. customer.update!(
  140. firstname: 'Martini',
  141. note: 'changed by my self',
  142. )
  143. Enrichment::Clearbit::User.new(customer).synced?
  144. perform_enqueued_jobs commit_transaction: true
  145. end
  146. it 'stores locally updated value' do
  147. expect(customer.reload).to have_attributes(
  148. firstname: 'Martini',
  149. lastname: 'Edenhofer',
  150. note: 'changed by my self',
  151. address: 'Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany',
  152. )
  153. end
  154. context 'when set to empty value' do
  155. before do
  156. customer.update!(
  157. firstname: '',
  158. note: 'changed by my self again',
  159. )
  160. Enrichment::Clearbit::User.new(customer).synced?
  161. perform_enqueued_jobs commit_transaction: true
  162. end
  163. it 'reverts to enriched data' do
  164. expect(customer.reload).to have_attributes(
  165. firstname: 'Martin',
  166. lastname: 'Edenhofer',
  167. note: 'changed by my self again',
  168. address: 'Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany',
  169. )
  170. end
  171. end
  172. end
  173. end
  174. describe 'case 3 - no person' do
  175. let(:customer) do
  176. User.create!(
  177. firstname: '',
  178. lastname: '',
  179. email: 'testing3@znuny.com',
  180. note: '',
  181. )
  182. end
  183. before do
  184. customer
  185. perform_enqueued_jobs commit_transaction: true
  186. customer.reload
  187. end
  188. it 'does not enrich the customer' do
  189. expect(ExternalSync).not_to be_exist(source: 'clearbit', object: 'User', o_id: customer.id)
  190. expect(customer.reload).to have_attributes(
  191. firstname: '',
  192. lastname: '',
  193. note: '',
  194. web: 'http://znuny.com',
  195. address: 'Marienstraße 11, 10117 Berlin, Germany',
  196. )
  197. end
  198. it 'creates organization with enriched data' do
  199. expect(ExternalSync).to be_exist(source: 'clearbit', object: 'Organization', o_id: customer.organization.id)
  200. expect(customer.organization).to have_attributes(
  201. name: 'Znuny / ES for OTRS',
  202. note: 'OTRS Support, Consulting, Development, Training and Customizing - Znuny GmbH'
  203. )
  204. end
  205. end
  206. describe 'case 4 - person with organization but organization is already assigned (own created)' do
  207. let(:customer) do
  208. User.create!(
  209. firstname: '',
  210. lastname: '',
  211. email: 'testing4@znuny.com',
  212. note: '',
  213. organization_id: 1,
  214. )
  215. end
  216. before do
  217. customer
  218. perform_enqueued_jobs commit_transaction: true
  219. customer.reload
  220. end
  221. it 'enriches the customer' do
  222. expect(customer).to have_attributes(
  223. firstname: 'Fred',
  224. lastname: 'Jupiter',
  225. note: 'some_fred_bio',
  226. web: 'http://fred.znuny.com',
  227. address: 'Marienstraße 11, 10117 Berlin, Germany',
  228. )
  229. end
  230. it 'does not create organization with enriched data' do
  231. expect(customer.organization).to be_present
  232. expect(Organization).not_to be_exist(name: 'ZnunyOfFred')
  233. end
  234. end
  235. describe 'case 5 - person with organization but organization is already assigned (own created)' do
  236. let(:customer) do
  237. User.create!(
  238. firstname: '',
  239. lastname: '',
  240. email: 'testing5@znuny.com',
  241. note: '',
  242. created_by_id: 1,
  243. updated_by_id: 1,
  244. organization_id: another_clearbit_organization.id,
  245. )
  246. end
  247. let(:another_clearbit_organization) do
  248. user = User.create!(email: 'testing3@znuny.com')
  249. perform_enqueued_jobs commit_transaction: true
  250. user.reload.organization
  251. end
  252. before do
  253. customer
  254. perform_enqueued_jobs commit_transaction: true
  255. customer.reload
  256. end
  257. it 'enriches the customer' do
  258. expect(ExternalSync).to be_exist(source: 'clearbit', object: 'User', o_id: customer.id)
  259. expect(customer).to have_attributes(
  260. firstname: 'Alex',
  261. lastname: 'Dont',
  262. note: 'some_bio_alex',
  263. web: 'http://znuny.com',
  264. address: 'Marienstraße 11, 10117 Berlin, Germany',
  265. )
  266. end
  267. it 'updates existing organization with enriched data' do
  268. expect(customer.organization).to have_attributes(
  269. name: 'Znuny GmbH',
  270. note: 'OTRS Support, Consulting, Development, Training and Customizing - Znuny GmbH'
  271. )
  272. expect(another_clearbit_organization.id).to eq customer.organization.id
  273. end
  274. end
  275. describe 'case 6 - no person / real api call' do
  276. let(:customer) do
  277. User.create!(
  278. firstname: '',
  279. lastname: '',
  280. email: 'testing6@clearbit.com',
  281. note: '',
  282. )
  283. end
  284. before do
  285. customer
  286. VCR.configure do |c|
  287. c.ignore_hosts 'person-stream.clearbit.com'
  288. end
  289. perform_enqueued_jobs commit_transaction: true
  290. VCR.configure do |c|
  291. c.unignore_hosts 'person-stream.clearbit.com'
  292. end
  293. customer.reload
  294. end
  295. it 'does not enrich the customer' do
  296. expect(ExternalSync).not_to be_exist(source: 'clearbit', object: 'User', o_id: customer.id)
  297. expect(customer).to have_attributes(
  298. firstname: '',
  299. lastname: '',
  300. note: '',
  301. web: '',
  302. address: be_in(
  303. [
  304. 'San Francisco, CA, USA',
  305. 'San Francisco, CA 94103, USA',
  306. '90 Sheridan St, San Francisco, CA 94103, USA',
  307. '90 Sheridan, San Francisco, CA 94103, USA',
  308. '3030 16th St, San Francisco, CA 94103, USA',
  309. '548 Market St, San Francisco, CA 94104, USA',
  310. ]
  311. )
  312. )
  313. end
  314. it 'creates organization with enriched data' do
  315. expect(ExternalSync).to be_exist(source: 'clearbit', object: 'Organization', o_id: customer.organization.id)
  316. expect(customer.organization).to have_attributes(
  317. name: 'Clearbit',
  318. note: 'Clearbit is a B2B data activation platform that helps teams understand customers, identify prospects, and personalize interactions with real-time intelligence.',
  319. )
  320. end
  321. end
  322. context 'when using custom attribute', db_strategy: :reset do
  323. let(:clearbit_config_user_familyname) { 'user.test_input' }
  324. let(:customer) do
  325. User.create!(
  326. firstname: '',
  327. lastname: '',
  328. email: 'testing6@znuny.com',
  329. note: '',
  330. )
  331. end
  332. before do
  333. create(:object_manager_attribute_text, object_name: 'User', name: 'test_input', data_option_maxlength: 2)
  334. ObjectManager::Attribute.migration_execute
  335. customer
  336. perform_enqueued_jobs commit_transaction: true
  337. customer.reload
  338. end
  339. it 'Limits enrichment data to database limit' do
  340. expect(ExternalSync).to be_exist(source: 'clearbit', object: 'User', o_id: customer.id)
  341. expect(customer.test_input).to eq 'Sm'
  342. end
  343. end
  344. end