clearbit_test.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'test_helper'
  3. class ClearbitTest < ActiveSupport::TestCase
  4. include BackgroundJobsHelper
  5. # check
  6. test 'base' do
  7. if !ENV['CLEARBIT_CI_API_KEY']
  8. raise "ERROR: Need CLEARBIT_CI_API_KEY - hint CLEARBIT_CI_API_KEY='abc...'"
  9. end
  10. # set system mode to done / to activate
  11. Setting.set('system_init_done', true)
  12. Setting.set('clearbit_integration', true)
  13. Setting.set('clearbit_config', {
  14. api_key: ENV['CLEARBIT_CI_API_KEY'],
  15. organization_autocreate: true,
  16. organization_shared: false,
  17. user_sync: {
  18. 'person.name.givenName' => 'user.firstname',
  19. 'person.name.familyName' => 'user.lastname',
  20. 'person.email' => 'user.email',
  21. 'person.bio' => 'user.note',
  22. 'company.url' => 'user.web',
  23. 'person.site' => 'user.web',
  24. 'company.location' => 'user.address',
  25. 'person.location' => 'user.address',
  26. # 'person.timeZone' => 'user.preferences[:timezone]',
  27. # 'person.gender' => 'user.preferences[:gender]',
  28. },
  29. organization_sync: {
  30. 'company.legalName' => 'organization.name',
  31. 'company.name' => 'organization.name',
  32. 'company.description' => 'organization.note',
  33. },
  34. })
  35. # case 1 - person + company (demo data set)
  36. customer1 = User.create!(
  37. firstname: '',
  38. lastname: 'Should be still there',
  39. email: 'alex@alexmaccaw.com',
  40. note: '',
  41. updated_by_id: 1,
  42. created_by_id: 1,
  43. )
  44. assert(customer1)
  45. perform_enqueued_jobs commit_transaction: true
  46. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer1.id))
  47. customer1.reload
  48. assert_equal('Should', customer1.firstname)
  49. assert_equal('be still there', customer1.lastname)
  50. assert_equal('O\'Reilly author, software engineer &amp; traveller. Founder of <a href="https://clearbit.com" rel="nofollow noreferrer noopener" target="_blank">https://clearbit.com</a>', customer1.note)
  51. assert_equal('1455 Market Street, San Francisco, CA 94103, USA', customer1.address)
  52. organization1 = Organization.find_by(name: 'Uber, Inc.')
  53. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization1.id))
  54. assert_equal(false, organization1.shared)
  55. assert_equal('Uber is a mobile app connecting passengers with drivers for hire.', organization1.note)
  56. # case 2 - person + company
  57. customer2 = User.create!(
  58. firstname: '',
  59. lastname: '',
  60. email: 'me@example.com',
  61. note: '',
  62. updated_by_id: 1,
  63. created_by_id: 1,
  64. )
  65. assert(customer2)
  66. perform_enqueued_jobs commit_transaction: true
  67. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer2.id))
  68. customer2.reload
  69. assert_equal('Martin', customer2.firstname)
  70. assert_equal('Edenhofer', customer2.lastname)
  71. assert_equal("Open Source professional and geek. Also known as OTRS inventor. ;)\r\nEntrepreneur and Advisor for open source people in need.", customer2.note)
  72. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2.address)
  73. organization2 = Organization.find_by(name: 'OTRS')
  74. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization2.id))
  75. assert_equal(false, organization2.shared)
  76. assert_equal('OTRS is an Open Source helpdesk software and an IT Service Management software free of licence costs. Improve your Customer Service Management with OTRS.', organization2.note)
  77. # update with own values (do not overwrite)
  78. customer2.update!(
  79. firstname: 'Martini',
  80. note: 'changed by my self',
  81. )
  82. perform_enqueued_jobs commit_transaction: true
  83. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer2.id))
  84. customer2.reload
  85. assert_equal('Martini', customer2.firstname)
  86. assert_equal('Edenhofer', customer2.lastname)
  87. assert_equal('changed by my self', customer2.note)
  88. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2.address)
  89. customer2_enrichment = Enrichment::Clearbit::User.new(customer2)
  90. customer2_enrichment.synced?
  91. perform_enqueued_jobs
  92. customer2.reload
  93. assert_equal('Martini', customer2.firstname)
  94. assert_equal('Edenhofer', customer2.lastname)
  95. assert_equal('changed by my self', customer2.note)
  96. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2.address)
  97. # update with own values (do not overwrite)
  98. customer2.update!(
  99. firstname: '',
  100. note: 'changed by my self',
  101. )
  102. customer2_enrichment = Enrichment::Clearbit::User.new(customer2)
  103. customer2_enrichment.synced?
  104. perform_enqueued_jobs
  105. customer2.reload
  106. assert_equal('Martin', customer2.firstname)
  107. assert_equal('Edenhofer', customer2.lastname)
  108. assert_equal('changed by my self', customer2.note)
  109. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2.address)
  110. # update with changed values at clearbit site (do overwrite)
  111. customer2.update!(
  112. email: 'me2@example.com',
  113. )
  114. customer2_enrichment = Enrichment::Clearbit::User.new(customer2)
  115. customer2_enrichment.synced?
  116. perform_enqueued_jobs
  117. customer2.reload
  118. assert_equal('Martini', customer2.firstname)
  119. assert_equal('Edenhofer', customer2.lastname)
  120. assert_equal('changed by my self', customer2.note)
  121. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2.address)
  122. organization2 = Organization.find_by(name: 'OTRS AG')
  123. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization2.id))
  124. assert_equal(false, organization2.shared)
  125. assert_equal('OTRS is an Open Source helpdesk software and an IT Service Management software free of licence costs. Improve your Customer Service Management with OTRS.', organization2.note)
  126. # case 3 - no person
  127. customer3 = User.create!(
  128. firstname: '',
  129. lastname: '',
  130. email: 'testing3@znuny.com',
  131. note: '',
  132. updated_by_id: 1,
  133. created_by_id: 1,
  134. )
  135. assert(customer3)
  136. perform_enqueued_jobs commit_transaction: true
  137. assert_not(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer3.id))
  138. customer3.reload
  139. assert_equal('', customer3.firstname)
  140. assert_equal('', customer3.lastname)
  141. assert_equal('', customer3.note)
  142. assert_equal('http://znuny.com', customer3.web)
  143. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer3.address)
  144. organization3 = Organization.find_by(name: 'Znuny / ES for OTRS')
  145. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization3.id))
  146. assert_equal(false, organization3.shared)
  147. assert_equal('OTRS Support, Consulting, Development, Training and Customizing - Znuny GmbH', organization3.note)
  148. # case 4 - person with organization but organization is already assigned (own created)
  149. customer4 = User.create!(
  150. firstname: '',
  151. lastname: '',
  152. email: 'testing4@znuny.com',
  153. note: '',
  154. organization_id: 1,
  155. updated_by_id: 1,
  156. created_by_id: 1,
  157. )
  158. assert(customer4)
  159. perform_enqueued_jobs commit_transaction: true
  160. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer4.id))
  161. customer4.reload
  162. assert_equal('Fred', customer4.firstname)
  163. assert_equal('Jupiter', customer4.lastname)
  164. assert_equal('some_fred_bio', customer4.note)
  165. assert_equal('http://fred.znuny.com', customer4.web)
  166. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer4.address)
  167. organization4 = Organization.find_by(name: 'ZnunyOfFred')
  168. assert_not(organization4)
  169. # case 5 - person with organization but organization is already assigned (own created)
  170. customer5 = User.create!(
  171. firstname: '',
  172. lastname: '',
  173. email: 'testing5@znuny.com',
  174. note: '',
  175. organization_id: organization3.id,
  176. updated_by_id: 1,
  177. created_by_id: 1,
  178. )
  179. assert(customer5)
  180. perform_enqueued_jobs commit_transaction: true
  181. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer5.id))
  182. customer5.reload
  183. assert_equal('Alex', customer5.firstname)
  184. assert_equal('Dont', customer5.lastname)
  185. assert_equal('some_bio_alex', customer5.note)
  186. assert_equal('http://znuny.com', customer5.web)
  187. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer5.address)
  188. organization5 = Organization.find_by(name: 'Znuny GmbH')
  189. assert_equal(organization3.id, organization5.id)
  190. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization5.id))
  191. assert_equal(false, organization5.shared)
  192. assert_equal('OTRS Support, Consulting, Development, Training and Customizing - Znuny GmbH', organization5.note)
  193. # case 6 - no person / real api call
  194. customer6 = User.create!(
  195. firstname: '',
  196. lastname: '',
  197. email: 'testing6@clearbit.com',
  198. note: '',
  199. updated_by_id: 1,
  200. created_by_id: 1,
  201. )
  202. assert(customer6)
  203. perform_enqueued_jobs commit_transaction: true
  204. assert_not(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer6.id))
  205. customer6.reload
  206. assert_equal('', customer6.firstname)
  207. assert_equal('', customer6.lastname)
  208. assert_equal('', customer6.note)
  209. assert_equal('', customer6.web)
  210. # assert_equal('http://clearbit.com', customer6.web)
  211. sometimes_changing_but_valid_addresses = [
  212. 'San Francisco, CA, USA',
  213. 'San Francisco, CA 94103, USA',
  214. '90 Sheridan St, San Francisco, CA 94103, USA',
  215. '90 Sheridan, San Francisco, CA 94103, USA',
  216. '3030 16th St, San Francisco, CA 94103, USA',
  217. '548 Market St, San Francisco, CA 94104, USA',
  218. ]
  219. assert_includes(sometimes_changing_but_valid_addresses, customer6.address)
  220. organization6 = Organization.find_by('name LIKE ?', 'APIHub Inc%')
  221. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization6.id))
  222. assert_equal(false, organization6.shared)
  223. assert_equal('The Clearbit Data Activation Platform helps B2B teams understand customers, identify prospects, &amp; personalize interactions with real-time intelligence.', organization6.note)
  224. end
  225. # check
  226. test 'base with invalid input' do
  227. if !ENV['CLEARBIT_CI_API_KEY']
  228. raise "ERROR: Need CLEARBIT_CI_API_KEY - hint CLEARBIT_CI_API_KEY='abc...'"
  229. end
  230. # set system mode to done / to activate
  231. Setting.set('system_init_done', true)
  232. Setting.set('clearbit_integration', true)
  233. Setting.set('clearbit_config', {
  234. api_key: ENV['CLEARBIT_CI_API_KEY'],
  235. organization_autocreate: true,
  236. organization_shared: true,
  237. user_sync: {
  238. 'person.name.givenName' => 'user.firstname',
  239. 'person.name.familyName' => 'user.lastname',
  240. 'person.email' => 'user.email',
  241. 'person.bio' => 'user.note_not_existing',
  242. 'company.url' => 'user.web',
  243. 'person.site' => 'user.web',
  244. 'company.location' => 'user.address',
  245. 'person.location' => 'user.address',
  246. },
  247. organization_sync: {
  248. 'company.legalName' => 'organization.name',
  249. 'company.name' => 'organization.name',
  250. 'company.description' => 'organization.note_not_existing',
  251. },
  252. })
  253. # case 1 - person + company (demo data set)
  254. customer1 = User.create!(
  255. firstname: '',
  256. lastname: 'Should be still there',
  257. email: 'testing6@znuny.com',
  258. note: '',
  259. updated_by_id: 1,
  260. created_by_id: 1,
  261. )
  262. assert(customer1)
  263. perform_enqueued_jobs commit_transaction: true
  264. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer1.id))
  265. customer1.reload
  266. assert_equal('Should', customer1.firstname)
  267. assert_equal('be still there', customer1.lastname)
  268. assert_equal('', customer1.note)
  269. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer1.address)
  270. organization1 = Organization.find_by(name: 'Znuny2')
  271. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization1.id))
  272. assert_equal(true, organization1.shared)
  273. assert_equal('', organization1.note)
  274. end
  275. end