clearbit_test.rb 13 KB

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