clearbit_test.rb 13 KB

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