clearbit_test.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. require 'integration_test_helper'
  2. class ClearbitTest < ActiveSupport::TestCase
  3. self.use_transactional_tests = true
  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.reload
  48. assert_equal('Should', customer1.firstname)
  49. assert_equal('be still there', customer1.lastname)
  50. assert_equal('O\'Reilly author, software engineer & traveller. Founder of https://clearbit.com', 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. Observer::Transaction.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. Observer::Transaction.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. Observer::Transaction.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. Observer::Transaction.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. Observer::Transaction.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. Observer::Transaction.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. assert_equal('San Francisco, CA 94103, USA', customer6.address)
  218. #assert_equal('3030 16th St, San Francisco, CA 94103, USA', customer6.address)
  219. #assert_equal('San Francisco, CA, USA', customer6.address)
  220. organization6 = Organization.find_by(name: 'APIHub, Inc')
  221. assert(organization6, 'unable to find org of user')
  222. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization6.id))
  223. assert_equal(false, organization6.shared)
  224. assert_equal('Clearbit provides powerful products and data APIs to help your business grow. Contact enrichment, lead generation, financial compliance, and more...', organization6.note)
  225. end
  226. # check
  227. test 'base with invalid input' do
  228. if !ENV['CLEARBIT_CI_API_KEY']
  229. raise "ERROR: Need CLEARBIT_CI_API_KEY - hint CLEARBIT_CI_API_KEY='abc...'"
  230. end
  231. # set system mode to done / to activate
  232. Setting.set('system_init_done', true)
  233. Setting.set('clearbit_integration', true)
  234. Setting.set('clearbit_config', {
  235. api_key: ENV['CLEARBIT_CI_API_KEY'],
  236. organization_autocreate: true,
  237. organization_shared: true,
  238. user_sync: {
  239. 'person.name.givenName' => 'user.firstname',
  240. 'person.name.familyName' => 'user.lastname',
  241. 'person.email' => 'user.email',
  242. 'person.bio' => 'user.note_not_existing',
  243. 'company.url' => 'user.web',
  244. 'person.site' => 'user.web',
  245. 'company.location' => 'user.address',
  246. 'person.location' => 'user.address',
  247. },
  248. organization_sync: {
  249. 'company.legalName' => 'organization.name',
  250. 'company.name' => 'organization.name',
  251. 'company.description' => 'organization.note_not_existing',
  252. },
  253. })
  254. # case 1 - person + company (demo data set)
  255. customer1 = User.create!(
  256. firstname: '',
  257. lastname: 'Should be still there',
  258. email: 'testing6@znuny.com',
  259. note: '',
  260. updated_by_id: 1,
  261. created_by_id: 1,
  262. )
  263. assert(customer1)
  264. Observer::Transaction.commit
  265. Scheduler.worker(true)
  266. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer1.id))
  267. customer1.reload
  268. assert_equal('Should', customer1.firstname)
  269. assert_equal('be still there', customer1.lastname)
  270. assert_equal('', customer1.note)
  271. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer1.address)
  272. organization1 = Organization.find_by(name: 'Znuny2')
  273. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization1.id))
  274. assert_equal(true, organization1.shared)
  275. assert_equal('', organization1.note)
  276. end
  277. end