clearbit_test.rb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. # encoding: utf-8
  2. require 'integration_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. Observer::Transaction.commit
  45. Scheduler.worker(true)
  46. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer1.id))
  47. customer1_lookup = User.lookup(id: customer1.id)
  48. assert_equal('Alex', customer1_lookup.firstname)
  49. assert_equal('Should be still there', customer1_lookup.lastname)
  50. assert_equal('O\'Reilly author, software engineer & traveller. Founder of https://clearbit.com', customer1_lookup.note)
  51. assert_equal('1455 Market Street, San Francisco, CA 94103, USA', customer1_lookup.address)
  52. organization1_lookup = Organization.find_by(name: 'Uber, Inc.')
  53. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization1_lookup.id))
  54. assert_equal(false, organization1_lookup.shared)
  55. assert_equal('Uber is a mobile app connecting passengers with drivers for hire.', organization1_lookup.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. Observer::Transaction.commit
  67. Scheduler.worker(true)
  68. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer2.id))
  69. customer2_lookup = User.lookup(id: customer2.id)
  70. assert_equal('Martin', customer2_lookup.firstname)
  71. assert_equal('Edenhofer', customer2_lookup.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_lookup.note)
  73. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2_lookup.address)
  74. organization2_lookup = Organization.find_by(name: 'OTRS')
  75. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization2_lookup.id))
  76. assert_equal(false, organization2_lookup.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_lookup.note)
  78. # update with own values (do not overwrite)
  79. customer2.update_attributes(
  80. firstname: 'Martini',
  81. note: 'changed by my self',
  82. )
  83. Observer::Transaction.commit
  84. Scheduler.worker(true)
  85. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer2.id))
  86. customer2_lookup = User.lookup(id: customer2.id)
  87. assert_equal('Martini', customer2_lookup.firstname)
  88. assert_equal('Edenhofer', customer2_lookup.lastname)
  89. assert_equal('changed by my self', customer2_lookup.note)
  90. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2_lookup.address)
  91. Transaction::ClearbitEnrichment.sync_user(customer2)
  92. Scheduler.worker(true)
  93. customer2_lookup = User.lookup(id: customer2.id)
  94. assert_equal('Martini', customer2_lookup.firstname)
  95. assert_equal('Edenhofer', customer2_lookup.lastname)
  96. assert_equal('changed by my self', customer2_lookup.note)
  97. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2_lookup.address)
  98. # update with own values (do not overwrite)
  99. customer2.update_attributes(
  100. firstname: '',
  101. note: 'changed by my self',
  102. )
  103. Transaction::ClearbitEnrichment.sync_user(customer2)
  104. Scheduler.worker(true)
  105. customer2_lookup = User.lookup(id: customer2.id)
  106. assert_equal('Martin', customer2_lookup.firstname)
  107. assert_equal('Edenhofer', customer2_lookup.lastname)
  108. assert_equal('changed by my self', customer2_lookup.note)
  109. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2_lookup.address)
  110. # update with changed values at clearbit site (do overwrite)
  111. customer2.update_attributes(
  112. email: 'me2@example.com',
  113. )
  114. Transaction::ClearbitEnrichment.sync_user(customer2)
  115. Scheduler.worker(true)
  116. customer2_lookup = User.lookup(id: customer2.id)
  117. assert_equal('Martini', customer2_lookup.firstname)
  118. assert_equal('Edenhofer', customer2_lookup.lastname)
  119. assert_equal('changed by my self', customer2_lookup.note)
  120. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2_lookup.address)
  121. organization2_lookup = Organization.find_by(name: 'OTRS AG')
  122. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization2_lookup.id))
  123. assert_equal(false, organization2_lookup.shared)
  124. 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_lookup.note)
  125. # case 3 - no person
  126. customer3 = User.create(
  127. firstname: '',
  128. lastname: '',
  129. email: 'testing3@znuny.com',
  130. note: '',
  131. updated_by_id: 1,
  132. created_by_id: 1,
  133. )
  134. assert(customer3)
  135. Observer::Transaction.commit
  136. Scheduler.worker(true)
  137. assert_not(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer3.id))
  138. customer3_lookup = User.lookup(id: customer3.id)
  139. assert_not_equal(customer3.updated_at, customer3_lookup.updated_at)
  140. assert_equal('', customer3_lookup.firstname)
  141. assert_equal('', customer3_lookup.lastname)
  142. assert_equal('', customer3_lookup.note)
  143. assert_equal('http://znuny.com', customer3_lookup.web)
  144. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer3_lookup.address)
  145. organization3_lookup = Organization.find_by(name: 'Znuny / ES for OTRS')
  146. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization3_lookup.id))
  147. assert_equal(false, organization3_lookup.shared)
  148. assert_equal('OTRS Support, Consulting, Development, Training and Customizing - Znuny GmbH', organization3_lookup.note)
  149. # case 4 - person with organization but organization is already assigned (own created)
  150. customer4 = User.create(
  151. firstname: '',
  152. lastname: '',
  153. email: 'testing4@znuny.com',
  154. note: '',
  155. organization_id: 1,
  156. updated_by_id: 1,
  157. created_by_id: 1,
  158. )
  159. assert(customer4)
  160. Observer::Transaction.commit
  161. Scheduler.worker(true)
  162. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer4.id))
  163. customer4_lookup = User.lookup(id: customer4.id)
  164. assert_not_equal(customer4.updated_at, customer4_lookup.updated_at)
  165. assert_equal('Fred', customer4_lookup.firstname)
  166. assert_equal('Jupiter', customer4_lookup.lastname)
  167. assert_equal('some_fred_bio', customer4_lookup.note)
  168. assert_equal('http://fred.znuny.com', customer4_lookup.web)
  169. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer4_lookup.address)
  170. organization4_lookup = Organization.find_by(name: 'ZnunyOfFred')
  171. assert_not(organization4_lookup)
  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_lookup.id,
  179. updated_by_id: 1,
  180. created_by_id: 1,
  181. )
  182. assert(customer5)
  183. Observer::Transaction.commit
  184. Scheduler.worker(true)
  185. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer5.id))
  186. customer5_lookup = User.lookup(id: customer5.id)
  187. assert_not_equal(customer5.updated_at, customer5_lookup.updated_at)
  188. assert_equal('Alex', customer5_lookup.firstname)
  189. assert_equal('Dont', customer5_lookup.lastname)
  190. assert_equal('some_bio_alex', customer5_lookup.note)
  191. assert_equal('http://znuny.com', customer5_lookup.web)
  192. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer5_lookup.address)
  193. organization5_lookup = Organization.find_by(name: 'Znuny GmbH')
  194. assert_equal(organization3_lookup.id, organization5_lookup.id)
  195. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization5_lookup.id))
  196. assert_equal(false, organization5_lookup.shared)
  197. assert_equal('OTRS Support, Consulting, Development, Training and Customizing - Znuny GmbH', organization5_lookup.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. Observer::Transaction.commit
  209. Scheduler.worker(true)
  210. assert_not(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer6.id))
  211. customer6_lookup = User.lookup(id: customer6.id)
  212. assert_not_equal(customer6.updated_at, customer6_lookup.updated_at)
  213. assert_equal('', customer6_lookup.firstname)
  214. assert_equal('', customer6_lookup.lastname)
  215. assert_equal('', customer6_lookup.note)
  216. assert_equal('http://clearbit.com', customer6_lookup.web)
  217. assert_equal('3030 16th St, San Francisco, CA 94103, USA', customer6_lookup.address)
  218. #assert_equal('San Francisco, CA, USA', customer6_lookup.address)
  219. organization6_lookup = Organization.find_by(name: 'Clearbit')
  220. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization6_lookup.id))
  221. assert_equal(false, organization6_lookup.shared)
  222. assert_equal('Clearbit provides powerful products and data APIs to help your business grow. Contact enrichment, lead generation, financial compliance, and more...', organization6_lookup.note)
  223. end
  224. # check
  225. test 'base with invalid input' do
  226. if !ENV['CLEARBIT_CI_API_KEY']
  227. raise "ERROR: Need CLEARBIT_CI_API_KEY - hint CLEARBIT_CI_API_KEY='abc...'"
  228. end
  229. # set system mode to done / to activate
  230. Setting.set('system_init_done', true)
  231. Setting.set('clearbit_integration', true)
  232. Setting.set('clearbit_config', {
  233. api_key: ENV['CLEARBIT_CI_API_KEY'],
  234. organization_autocreate: true,
  235. organization_shared: true,
  236. user_sync: {
  237. 'person.name.givenName' => 'user.firstname',
  238. 'person.name.familyName' => 'user.lastname',
  239. 'person.email' => 'user.email',
  240. 'person.bio' => 'user.note_not_existing',
  241. 'company.url' => 'user.web',
  242. 'person.site' => 'user.web',
  243. 'company.location' => 'user.address',
  244. 'person.location' => 'user.address',
  245. },
  246. organization_sync: {
  247. 'company.legalName' => 'organization.name',
  248. 'company.name' => 'organization.name',
  249. 'company.description' => 'organization.note_not_existing',
  250. },
  251. })
  252. # case 1 - person + company (demo data set)
  253. customer1 = User.create(
  254. firstname: '',
  255. lastname: 'Should be still there',
  256. email: 'testing6@znuny.com',
  257. note: '',
  258. updated_by_id: 1,
  259. created_by_id: 1,
  260. )
  261. assert(customer1)
  262. Observer::Transaction.commit
  263. Scheduler.worker(true)
  264. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer1.id))
  265. customer1_lookup = User.lookup(id: customer1.id)
  266. assert_equal('Bob', customer1_lookup.firstname)
  267. assert_equal('Should be still there', customer1_lookup.lastname)
  268. assert_equal('', customer1_lookup.note)
  269. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer1_lookup.address)
  270. organization1_lookup = Organization.find_by(name: 'Znuny2')
  271. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization1_lookup.id))
  272. assert_equal(true, organization1_lookup.shared)
  273. assert_equal('', organization1_lookup.note)
  274. end
  275. end