2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
2021-06-01 12:20:20 +00:00
2018-06-25 11:07:47 +00:00
require 'test_helper'
2016-04-25 23:04:59 +00:00
class ClearbitTest < ActiveSupport :: TestCase
# check
test 'base' do
if ! ENV [ 'CLEARBIT_CI_API_KEY' ]
raise " ERROR: Need CLEARBIT_CI_API_KEY - hint CLEARBIT_CI_API_KEY='abc...' "
end
# set system mode to done / to activate
Setting . set ( 'system_init_done' , true )
Setting . set ( 'clearbit_integration' , true )
Setting . set ( 'clearbit_config' , {
2018-12-19 17:31:51 +00:00
api_key : ENV [ 'CLEARBIT_CI_API_KEY' ] ,
2016-04-25 23:04:59 +00:00
organization_autocreate : true ,
2018-12-19 17:31:51 +00:00
organization_shared : false ,
user_sync : {
'person.name.givenName' = > 'user.firstname' ,
2016-04-25 23:04:59 +00:00
'person.name.familyName' = > 'user.lastname' ,
2018-12-19 17:31:51 +00:00
'person.email' = > 'user.email' ,
'person.bio' = > 'user.note' ,
'company.url' = > 'user.web' ,
'person.site' = > 'user.web' ,
'company.location' = > 'user.address' ,
'person.location' = > 'user.address' ,
2021-07-16 13:44:10 +00:00
# 'person.timeZone' => 'user.preferences[:timezone]',
# 'person.gender' => 'user.preferences[:gender]',
2016-04-25 23:04:59 +00:00
} ,
2018-12-19 17:31:51 +00:00
organization_sync : {
'company.legalName' = > 'organization.name' ,
'company.name' = > 'organization.name' ,
2016-04-25 23:04:59 +00:00
'company.description' = > 'organization.note' ,
} ,
} )
# case 1 - person + company (demo data set)
2017-10-24 20:34:52 +00:00
customer1 = User . create! (
2018-12-19 17:31:51 +00:00
firstname : '' ,
lastname : 'Should be still there' ,
email : 'alex@alexmaccaw.com' ,
note : '' ,
2016-04-25 23:04:59 +00:00
updated_by_id : 1 ,
created_by_id : 1 ,
)
assert ( customer1 )
2021-05-20 06:59:02 +00:00
TransactionDispatcher . commit
2016-04-26 09:30:46 +00:00
Scheduler . worker ( true )
2016-04-25 23:04:59 +00:00
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'User' , o_id : customer1 . id ) )
2017-10-24 20:34:52 +00:00
customer1 . reload
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
assert_equal ( 'Should' , customer1 . firstname )
assert_equal ( 'be still there' , customer1 . lastname )
2019-02-18 14:34:13 +00:00
assert_equal ( 'O\'Reilly author, software engineer & traveller. Founder of <a href="https://clearbit.com" rel="nofollow noreferrer noopener" target="_blank">https://clearbit.com</a>' , customer1 . note )
2017-10-24 20:34:52 +00:00
assert_equal ( '1455 Market Street, San Francisco, CA 94103, USA' , customer1 . address )
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
organization1 = Organization . find_by ( name : 'Uber, Inc.' )
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'Organization' , o_id : organization1 . id ) )
assert_equal ( false , organization1 . shared )
assert_equal ( 'Uber is a mobile app connecting passengers with drivers for hire.' , organization1 . note )
2016-04-25 23:04:59 +00:00
# case 2 - person + company
2017-10-24 20:34:52 +00:00
customer2 = User . create! (
2018-12-19 17:31:51 +00:00
firstname : '' ,
lastname : '' ,
email : 'me@example.com' ,
note : '' ,
2016-04-25 23:04:59 +00:00
updated_by_id : 1 ,
created_by_id : 1 ,
)
assert ( customer2 )
2021-05-20 06:59:02 +00:00
TransactionDispatcher . commit
2016-04-26 09:30:46 +00:00
Scheduler . worker ( true )
2016-04-25 23:04:59 +00:00
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'User' , o_id : customer2 . id ) )
2017-10-24 20:34:52 +00:00
customer2 . reload
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
assert_equal ( 'Martin' , customer2 . firstname )
assert_equal ( 'Edenhofer' , customer2 . lastname )
assert_equal ( " Open Source professional and geek. Also known as OTRS inventor. ;) \r \n Entrepreneur and Advisor for open source people in need. " , customer2 . note )
assert_equal ( 'Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany' , customer2 . address )
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
organization2 = Organization . find_by ( name : 'OTRS' )
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'Organization' , o_id : organization2 . id ) )
assert_equal ( false , organization2 . shared )
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 )
2016-04-25 23:04:59 +00:00
# update with own values (do not overwrite)
2017-09-11 11:16:08 +00:00
customer2 . update! (
2016-04-25 23:04:59 +00:00
firstname : 'Martini' ,
2018-12-19 17:31:51 +00:00
note : 'changed by my self' ,
2016-04-25 23:04:59 +00:00
)
2021-05-20 06:59:02 +00:00
TransactionDispatcher . commit
2016-04-26 09:30:46 +00:00
Scheduler . worker ( true )
2016-04-25 23:04:59 +00:00
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'User' , o_id : customer2 . id ) )
2017-10-24 20:34:52 +00:00
customer2 . reload
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
assert_equal ( 'Martini' , customer2 . firstname )
assert_equal ( 'Edenhofer' , customer2 . lastname )
assert_equal ( 'changed by my self' , customer2 . note )
assert_equal ( 'Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany' , customer2 . address )
2016-04-25 23:04:59 +00:00
2017-03-09 10:32:05 +00:00
customer2_enrichment = Enrichment :: Clearbit :: User . new ( customer2 )
customer2_enrichment . synced?
2016-04-26 09:30:46 +00:00
Scheduler . worker ( true )
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
customer2 . reload
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
assert_equal ( 'Martini' , customer2 . firstname )
assert_equal ( 'Edenhofer' , customer2 . lastname )
assert_equal ( 'changed by my self' , customer2 . note )
assert_equal ( 'Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany' , customer2 . address )
2016-04-25 23:04:59 +00:00
# update with own values (do not overwrite)
2017-09-11 11:16:08 +00:00
customer2 . update! (
2016-04-25 23:04:59 +00:00
firstname : '' ,
2018-12-19 17:31:51 +00:00
note : 'changed by my self' ,
2016-04-25 23:04:59 +00:00
)
2017-03-09 10:32:05 +00:00
customer2_enrichment = Enrichment :: Clearbit :: User . new ( customer2 )
customer2_enrichment . synced?
2016-04-26 09:30:46 +00:00
Scheduler . worker ( true )
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
customer2 . reload
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
assert_equal ( 'Martin' , customer2 . firstname )
assert_equal ( 'Edenhofer' , customer2 . lastname )
assert_equal ( 'changed by my self' , customer2 . note )
assert_equal ( 'Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany' , customer2 . address )
2016-04-25 23:04:59 +00:00
# update with changed values at clearbit site (do overwrite)
2017-09-11 11:16:08 +00:00
customer2 . update! (
2016-04-25 23:04:59 +00:00
email : 'me2@example.com' ,
)
2017-03-09 10:32:05 +00:00
customer2_enrichment = Enrichment :: Clearbit :: User . new ( customer2 )
customer2_enrichment . synced?
2016-04-26 09:30:46 +00:00
Scheduler . worker ( true )
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
customer2 . reload
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
assert_equal ( 'Martini' , customer2 . firstname )
assert_equal ( 'Edenhofer' , customer2 . lastname )
assert_equal ( 'changed by my self' , customer2 . note )
assert_equal ( 'Norsk-Data-Straße 1, 61352 Bad Homburg vor der Höhe, Germany' , customer2 . address )
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
organization2 = Organization . find_by ( name : 'OTRS AG' )
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'Organization' , o_id : organization2 . id ) )
assert_equal ( false , organization2 . shared )
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 )
2016-04-25 23:04:59 +00:00
# case 3 - no person
2017-10-24 20:34:52 +00:00
customer3 = User . create! (
2018-12-19 17:31:51 +00:00
firstname : '' ,
lastname : '' ,
email : 'testing3@znuny.com' ,
note : '' ,
2016-04-25 23:04:59 +00:00
updated_by_id : 1 ,
created_by_id : 1 ,
)
assert ( customer3 )
2021-05-20 06:59:02 +00:00
TransactionDispatcher . commit
2016-04-26 09:30:46 +00:00
Scheduler . worker ( true )
2016-04-25 23:04:59 +00:00
assert_not ( ExternalSync . find_by ( source : 'clearbit' , object : 'User' , o_id : customer3 . id ) )
2017-10-24 20:34:52 +00:00
customer3 . reload
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
assert_equal ( '' , customer3 . firstname )
assert_equal ( '' , customer3 . lastname )
assert_equal ( '' , customer3 . note )
assert_equal ( 'http://znuny.com' , customer3 . web )
assert_equal ( 'Marienstraße 11, 10117 Berlin, Germany' , customer3 . address )
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
organization3 = Organization . find_by ( name : 'Znuny / ES for OTRS' )
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'Organization' , o_id : organization3 . id ) )
assert_equal ( false , organization3 . shared )
assert_equal ( 'OTRS Support, Consulting, Development, Training and Customizing - Znuny GmbH' , organization3 . note )
2016-04-25 23:04:59 +00:00
2016-04-26 06:48:44 +00:00
# case 4 - person with organization but organization is already assigned (own created)
2017-10-24 20:34:52 +00:00
customer4 = User . create! (
2018-12-19 17:31:51 +00:00
firstname : '' ,
lastname : '' ,
email : 'testing4@znuny.com' ,
note : '' ,
2016-04-26 06:48:44 +00:00
organization_id : 1 ,
2018-12-19 17:31:51 +00:00
updated_by_id : 1 ,
created_by_id : 1 ,
2016-04-25 23:04:59 +00:00
)
assert ( customer4 )
2021-05-20 06:59:02 +00:00
TransactionDispatcher . commit
2016-04-26 09:30:46 +00:00
Scheduler . worker ( true )
2016-04-25 23:04:59 +00:00
2016-04-26 06:48:44 +00:00
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'User' , o_id : customer4 . id ) )
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
customer4 . reload
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
assert_equal ( 'Fred' , customer4 . firstname )
assert_equal ( 'Jupiter' , customer4 . lastname )
assert_equal ( 'some_fred_bio' , customer4 . note )
assert_equal ( 'http://fred.znuny.com' , customer4 . web )
assert_equal ( 'Marienstraße 11, 10117 Berlin, Germany' , customer4 . address )
2016-04-26 06:48:44 +00:00
2017-10-24 20:34:52 +00:00
organization4 = Organization . find_by ( name : 'ZnunyOfFred' )
assert_not ( organization4 )
2016-04-26 06:48:44 +00:00
# case 5 - person with organization but organization is already assigned (own created)
2017-10-24 20:34:52 +00:00
customer5 = User . create! (
2018-12-19 17:31:51 +00:00
firstname : '' ,
lastname : '' ,
email : 'testing5@znuny.com' ,
note : '' ,
2017-10-24 20:34:52 +00:00
organization_id : organization3 . id ,
2018-12-19 17:31:51 +00:00
updated_by_id : 1 ,
created_by_id : 1 ,
2016-04-26 06:48:44 +00:00
)
assert ( customer5 )
2021-05-20 06:59:02 +00:00
TransactionDispatcher . commit
2016-04-26 09:30:46 +00:00
Scheduler . worker ( true )
2016-04-26 06:48:44 +00:00
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'User' , o_id : customer5 . id ) )
2017-10-24 20:34:52 +00:00
customer5 . reload
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
assert_equal ( 'Alex' , customer5 . firstname )
assert_equal ( 'Dont' , customer5 . lastname )
assert_equal ( 'some_bio_alex' , customer5 . note )
assert_equal ( 'http://znuny.com' , customer5 . web )
assert_equal ( 'Marienstraße 11, 10117 Berlin, Germany' , customer5 . address )
2016-04-26 06:48:44 +00:00
2017-10-24 20:34:52 +00:00
organization5 = Organization . find_by ( name : 'Znuny GmbH' )
assert_equal ( organization3 . id , organization5 . id )
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'Organization' , o_id : organization5 . id ) )
assert_equal ( false , organization5 . shared )
assert_equal ( 'OTRS Support, Consulting, Development, Training and Customizing - Znuny GmbH' , organization5 . note )
2016-04-26 06:48:44 +00:00
# case 6 - no person / real api call
2017-10-24 20:34:52 +00:00
customer6 = User . create! (
2018-12-19 17:31:51 +00:00
firstname : '' ,
lastname : '' ,
email : 'testing6@clearbit.com' ,
note : '' ,
2016-04-26 06:48:44 +00:00
updated_by_id : 1 ,
created_by_id : 1 ,
)
assert ( customer6 )
2021-05-20 06:59:02 +00:00
TransactionDispatcher . commit
2016-04-26 09:30:46 +00:00
Scheduler . worker ( true )
2016-04-26 06:48:44 +00:00
assert_not ( ExternalSync . find_by ( source : 'clearbit' , object : 'User' , o_id : customer6 . id ) )
2017-10-24 20:34:52 +00:00
customer6 . reload
2016-04-26 06:48:44 +00:00
2017-10-24 20:34:52 +00:00
assert_equal ( '' , customer6 . firstname )
assert_equal ( '' , customer6 . lastname )
assert_equal ( '' , customer6 . note )
2018-04-18 09:20:27 +00:00
assert_equal ( '' , customer6 . web )
2021-07-16 13:44:10 +00:00
# assert_equal('http://clearbit.com', customer6.web)
2018-06-22 12:25:01 +00:00
sometimes_changing_but_valid_addresses = [
'San Francisco, CA, USA' ,
'San Francisco, CA 94103, USA' ,
'90 Sheridan St, San Francisco, CA 94103, USA' ,
2021-07-05 12:25:39 +00:00
'90 Sheridan, San Francisco, CA 94103, USA' ,
2018-06-22 12:25:01 +00:00
'3030 16th St, San Francisco, CA 94103, USA' ,
]
assert_includes ( sometimes_changing_but_valid_addresses , customer6 . address )
2021-07-05 12:25:39 +00:00
organization6 = Organization . find_by ( 'name LIKE ?' , 'APIHub Inc%' )
2018-05-21 23:07:44 +00:00
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'Organization' , o_id : organization6 . id ) )
assert_equal ( false , organization6 . shared )
2021-04-09 14:52:53 +00:00
assert_equal ( 'The marketing data engine to deeply understand your customers, identify future prospects, & personalize every single marketing & sales interaction.' , organization6 . note )
2016-04-25 23:04:59 +00:00
end
# check
test 'base with invalid input' do
if ! ENV [ 'CLEARBIT_CI_API_KEY' ]
raise " ERROR: Need CLEARBIT_CI_API_KEY - hint CLEARBIT_CI_API_KEY='abc...' "
end
# set system mode to done / to activate
Setting . set ( 'system_init_done' , true )
Setting . set ( 'clearbit_integration' , true )
Setting . set ( 'clearbit_config' , {
2018-12-19 17:31:51 +00:00
api_key : ENV [ 'CLEARBIT_CI_API_KEY' ] ,
2016-04-25 23:04:59 +00:00
organization_autocreate : true ,
2018-12-19 17:31:51 +00:00
organization_shared : true ,
user_sync : {
'person.name.givenName' = > 'user.firstname' ,
2016-04-25 23:04:59 +00:00
'person.name.familyName' = > 'user.lastname' ,
2018-12-19 17:31:51 +00:00
'person.email' = > 'user.email' ,
'person.bio' = > 'user.note_not_existing' ,
'company.url' = > 'user.web' ,
'person.site' = > 'user.web' ,
'company.location' = > 'user.address' ,
'person.location' = > 'user.address' ,
2016-04-25 23:04:59 +00:00
} ,
2018-12-19 17:31:51 +00:00
organization_sync : {
'company.legalName' = > 'organization.name' ,
'company.name' = > 'organization.name' ,
2016-04-25 23:04:59 +00:00
'company.description' = > 'organization.note_not_existing' ,
} ,
} )
# case 1 - person + company (demo data set)
2017-10-24 20:34:52 +00:00
customer1 = User . create! (
2018-12-19 17:31:51 +00:00
firstname : '' ,
lastname : 'Should be still there' ,
email : 'testing6@znuny.com' ,
note : '' ,
2016-04-25 23:04:59 +00:00
updated_by_id : 1 ,
created_by_id : 1 ,
)
assert ( customer1 )
2021-05-20 06:59:02 +00:00
TransactionDispatcher . commit
2016-04-26 09:30:46 +00:00
Scheduler . worker ( true )
2016-04-25 23:04:59 +00:00
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'User' , o_id : customer1 . id ) )
2017-10-24 20:34:52 +00:00
customer1 . reload
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
assert_equal ( 'Should' , customer1 . firstname )
assert_equal ( 'be still there' , customer1 . lastname )
assert_equal ( '' , customer1 . note )
assert_equal ( 'Marienstraße 11, 10117 Berlin, Germany' , customer1 . address )
2016-04-25 23:04:59 +00:00
2017-10-24 20:34:52 +00:00
organization1 = Organization . find_by ( name : 'Znuny2' )
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'Organization' , o_id : organization1 . id ) )
assert_equal ( true , organization1 . shared )
assert_equal ( '' , organization1 . note )
2016-04-25 23:04:59 +00:00
end
end