clearbit_test.rb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. # encoding: utf-8
  2. require 'integration_test_helper'
  3. class ClearbitTest < ActiveSupport::TestCase
  4. self.use_transactional_tests = true
  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. Observer::Transaction.commit
  46. Scheduler.worker(true)
  47. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer1.id))
  48. customer1.reload
  49. assert_equal('Should', customer1.firstname)
  50. assert_equal('be still there', customer1.lastname)
  51. assert_equal('O\'Reilly author, software engineer & traveller. Founder of https://clearbit.com', customer1.note)
  52. assert_equal('1455 Market Street, San Francisco, CA 94103, USA', customer1.address)
  53. organization1 = Organization.find_by(name: 'Uber, Inc.')
  54. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization1.id))
  55. assert_equal(false, organization1.shared)
  56. assert_equal('Uber is a mobile app connecting passengers with drivers for hire.', organization1.note)
  57. # case 2 - person + company
  58. customer2 = User.create!(
  59. firstname: '',
  60. lastname: '',
  61. email: 'me@example.com',
  62. note: '',
  63. updated_by_id: 1,
  64. created_by_id: 1,
  65. )
  66. assert(customer2)
  67. Observer::Transaction.commit
  68. Scheduler.worker(true)
  69. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer2.id))
  70. customer2.reload
  71. assert_equal('Martin', customer2.firstname)
  72. assert_equal('Edenhofer', customer2.lastname)
  73. assert_equal("Open Source professional and geek. Also known as OTRS inventor. ;)\r\nEntrepreneur and Advisor for open source people in need.", customer2.note)
  74. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2.address)
  75. organization2 = Organization.find_by(name: 'OTRS')
  76. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization2.id))
  77. assert_equal(false, organization2.shared)
  78. 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)
  79. # update with own values (do not overwrite)
  80. customer2.update!(
  81. firstname: 'Martini',
  82. note: 'changed by my self',
  83. )
  84. Observer::Transaction.commit
  85. Scheduler.worker(true)
  86. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer2.id))
  87. customer2.reload
  88. assert_equal('Martini', customer2.firstname)
  89. assert_equal('Edenhofer', customer2.lastname)
  90. assert_equal('changed by my self', customer2.note)
  91. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2.address)
  92. customer2_enrichment = Enrichment::Clearbit::User.new(customer2)
  93. customer2_enrichment.synced?
  94. Scheduler.worker(true)
  95. customer2.reload
  96. assert_equal('Martini', customer2.firstname)
  97. assert_equal('Edenhofer', customer2.lastname)
  98. assert_equal('changed by my self', customer2.note)
  99. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2.address)
  100. # update with own values (do not overwrite)
  101. customer2.update!(
  102. firstname: '',
  103. note: 'changed by my self',
  104. )
  105. customer2_enrichment = Enrichment::Clearbit::User.new(customer2)
  106. customer2_enrichment.synced?
  107. Scheduler.worker(true)
  108. customer2.reload
  109. assert_equal('Martin', customer2.firstname)
  110. assert_equal('Edenhofer', customer2.lastname)
  111. assert_equal('changed by my self', customer2.note)
  112. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2.address)
  113. # update with changed values at clearbit site (do overwrite)
  114. customer2.update!(
  115. email: 'me2@example.com',
  116. )
  117. customer2_enrichment = Enrichment::Clearbit::User.new(customer2)
  118. customer2_enrichment.synced?
  119. Scheduler.worker(true)
  120. customer2.reload
  121. assert_equal('Martini', customer2.firstname)
  122. assert_equal('Edenhofer', customer2.lastname)
  123. assert_equal('changed by my self', customer2.note)
  124. assert_equal('Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany', customer2.address)
  125. organization2 = Organization.find_by(name: 'OTRS AG')
  126. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization2.id))
  127. assert_equal(false, organization2.shared)
  128. 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)
  129. # case 3 - no person
  130. customer3 = User.create!(
  131. firstname: '',
  132. lastname: '',
  133. email: 'testing3@znuny.com',
  134. note: '',
  135. updated_by_id: 1,
  136. created_by_id: 1,
  137. )
  138. assert(customer3)
  139. Observer::Transaction.commit
  140. Scheduler.worker(true)
  141. assert_not(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer3.id))
  142. customer3.reload
  143. assert_equal('', customer3.firstname)
  144. assert_equal('', customer3.lastname)
  145. assert_equal('', customer3.note)
  146. assert_equal('http://znuny.com', customer3.web)
  147. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer3.address)
  148. organization3 = Organization.find_by(name: 'Znuny / ES for OTRS')
  149. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization3.id))
  150. assert_equal(false, organization3.shared)
  151. assert_equal('OTRS Support, Consulting, Development, Training and Customizing - Znuny GmbH', organization3.note)
  152. # case 4 - person with organization but organization is already assigned (own created)
  153. customer4 = User.create!(
  154. firstname: '',
  155. lastname: '',
  156. email: 'testing4@znuny.com',
  157. note: '',
  158. organization_id: 1,
  159. updated_by_id: 1,
  160. created_by_id: 1,
  161. )
  162. assert(customer4)
  163. Observer::Transaction.commit
  164. Scheduler.worker(true)
  165. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer4.id))
  166. customer4.reload
  167. assert_equal('Fred', customer4.firstname)
  168. assert_equal('Jupiter', customer4.lastname)
  169. assert_equal('some_fred_bio', customer4.note)
  170. assert_equal('http://fred.znuny.com', customer4.web)
  171. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer4.address)
  172. organization4 = Organization.find_by(name: 'ZnunyOfFred')
  173. assert_not(organization4)
  174. # case 5 - person with organization but organization is already assigned (own created)
  175. customer5 = User.create!(
  176. firstname: '',
  177. lastname: '',
  178. email: 'testing5@znuny.com',
  179. note: '',
  180. organization_id: organization3.id,
  181. updated_by_id: 1,
  182. created_by_id: 1,
  183. )
  184. assert(customer5)
  185. Observer::Transaction.commit
  186. Scheduler.worker(true)
  187. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer5.id))
  188. customer5.reload
  189. assert_equal('Alex', customer5.firstname)
  190. assert_equal('Dont', customer5.lastname)
  191. assert_equal('some_bio_alex', customer5.note)
  192. assert_equal('http://znuny.com', customer5.web)
  193. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer5.address)
  194. organization5 = Organization.find_by(name: 'Znuny GmbH')
  195. assert_equal(organization3.id, organization5.id)
  196. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization5.id))
  197. assert_equal(false, organization5.shared)
  198. assert_equal('OTRS Support, Consulting, Development, Training and Customizing - Znuny GmbH', organization5.note)
  199. # case 6 - no person / real api call
  200. customer6 = User.create!(
  201. firstname: '',
  202. lastname: '',
  203. email: 'testing6@clearbit.com',
  204. note: '',
  205. updated_by_id: 1,
  206. created_by_id: 1,
  207. )
  208. assert(customer6)
  209. Observer::Transaction.commit
  210. Scheduler.worker(true)
  211. assert_not(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer6.id))
  212. customer6.reload
  213. assert_equal('', customer6.firstname)
  214. assert_equal('', customer6.lastname)
  215. assert_equal('', customer6.note)
  216. assert_equal('http://clearbit.com', customer6.web)
  217. assert_equal('3030 16th St, San Francisco, CA 94103, USA', customer6.address)
  218. #assert_equal('San Francisco, CA, USA', customer6.address)
  219. organization6 = Organization.find_by(name: 'APIHub, Inc')
  220. assert(organization6, 'unable to find org of user')
  221. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization6.id))
  222. assert_equal(false, organization6.shared)
  223. assert_equal('Clearbit provides powerful products and data APIs to help your business grow. Contact enrichment, lead generation, financial compliance, and more...', 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. Observer::Transaction.commit
  264. Scheduler.worker(true)
  265. assert(ExternalSync.find_by(source: 'clearbit', object: 'User', o_id: customer1.id))
  266. customer1.reload
  267. assert_equal('Should', customer1.firstname)
  268. assert_equal('be still there', customer1.lastname)
  269. assert_equal('', customer1.note)
  270. assert_equal('Marienstraße 11, 10117 Berlin, Germany', customer1.address)
  271. organization1 = Organization.find_by(name: 'Znuny2')
  272. assert(ExternalSync.find_by(source: 'clearbit', object: 'Organization', o_id: organization1.id))
  273. assert_equal(true, organization1.shared)
  274. assert_equal('', organization1.note)
  275. end
  276. end