2017-11-23 08:09:44 +00:00
2016-04-25 23:04:59 +00:00
require 'integration_test_helper'
class ClearbitTest < ActiveSupport :: TestCase
2017-11-06 01:32:47 +00:00
self . use_transactional_tests = true
2016-04-25 23:04:59 +00:00
# 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' , {
api_key : ENV [ 'CLEARBIT_CI_API_KEY' ] ,
organization_autocreate : true ,
organization_shared : false ,
user_sync : {
'person.name.givenName' = > 'user.firstname' ,
'person.name.familyName' = > 'user.lastname' ,
'person.email' = > 'user.email' ,
'person.bio' = > 'user.note' ,
'company.url' = > 'user.web' ,
'person.site' = > 'user.web' ,
'company.location' = > 'user.address' ,
'person.location' = > 'user.address' ,
#'person.timeZone' => 'user.preferences[:timezone]',
#'person.gender' => 'user.preferences[:gender]',
} ,
organization_sync : {
'company.legalName' = > 'organization.name' ,
'company.name' = > 'organization.name' ,
'company.description' = > 'organization.note' ,
} ,
} )
# case 1 - person + company (demo data set)
2017-10-24 20:34:52 +00:00
customer1 = User . create! (
2016-04-25 23:04:59 +00:00
firstname : '' ,
lastname : 'Should be still there' ,
email : 'alex@alexmaccaw.com' ,
note : '' ,
updated_by_id : 1 ,
created_by_id : 1 ,
)
assert ( customer1 )
Observer :: Transaction . 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 ( 'O\'Reilly author, software engineer & traveller. Founder of https://clearbit.com' , customer1 . note )
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! (
2016-04-25 23:04:59 +00:00
firstname : '' ,
lastname : '' ,
email : 'me@example.com' ,
note : '' ,
updated_by_id : 1 ,
created_by_id : 1 ,
)
assert ( customer2 )
Observer :: Transaction . 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' ,
note : 'changed by my self' ,
)
Observer :: Transaction . 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 : '' ,
note : 'changed by my self' ,
)
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! (
2016-04-25 23:04:59 +00:00
firstname : '' ,
lastname : '' ,
2016-04-26 06:48:44 +00:00
email : 'testing3@znuny.com' ,
2016-04-25 23:04:59 +00:00
note : '' ,
updated_by_id : 1 ,
created_by_id : 1 ,
)
assert ( customer3 )
Observer :: Transaction . 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! (
2016-04-25 23:04:59 +00:00
firstname : '' ,
lastname : '' ,
2016-04-26 06:48:44 +00:00
email : 'testing4@znuny.com' ,
2016-04-25 23:04:59 +00:00
note : '' ,
2016-04-26 06:48:44 +00:00
organization_id : 1 ,
2016-04-25 23:04:59 +00:00
updated_by_id : 1 ,
created_by_id : 1 ,
)
assert ( customer4 )
Observer :: Transaction . 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! (
2016-04-26 06:48:44 +00:00
firstname : '' ,
lastname : '' ,
email : 'testing5@znuny.com' ,
note : '' ,
2017-10-24 20:34:52 +00:00
organization_id : organization3 . id ,
2016-04-26 06:48:44 +00:00
updated_by_id : 1 ,
created_by_id : 1 ,
)
assert ( customer5 )
Observer :: Transaction . 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! (
2016-04-26 06:48:44 +00:00
firstname : '' ,
lastname : '' ,
email : 'testing6@clearbit.com' ,
note : '' ,
updated_by_id : 1 ,
created_by_id : 1 ,
)
assert ( customer6 )
Observer :: Transaction . 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 )
#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' ,
'3030 16th St, San Francisco, CA 94103, USA' ,
]
assert_includes ( sometimes_changing_but_valid_addresses , customer6 . address )
organization6 = Organization . find_by ( name : 'APIHub, Inc.' )
2018-05-21 23:07:44 +00:00
#assert_nil(organization6, 'unable to find org of user')
assert ( ExternalSync . find_by ( source : 'clearbit' , object : 'Organization' , o_id : organization6 . id ) )
assert_equal ( false , organization6 . shared )
assert_equal ( 'Clearbit provides powerful products and data APIs to help your business grow. Contact enrichment, lead generation, financial compliance, and more...' , 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' , {
api_key : ENV [ 'CLEARBIT_CI_API_KEY' ] ,
organization_autocreate : true ,
organization_shared : true ,
user_sync : {
'person.name.givenName' = > 'user.firstname' ,
'person.name.familyName' = > 'user.lastname' ,
'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' ,
} ,
organization_sync : {
'company.legalName' = > 'organization.name' ,
'company.name' = > 'organization.name' ,
'company.description' = > 'organization.note_not_existing' ,
} ,
} )
# case 1 - person + company (demo data set)
2017-10-24 20:34:52 +00:00
customer1 = User . create! (
2016-04-25 23:04:59 +00:00
firstname : '' ,
lastname : 'Should be still there' ,
2016-04-26 06:48:44 +00:00
email : 'testing6@znuny.com' ,
2016-04-25 23:04:59 +00:00
note : '' ,
updated_by_id : 1 ,
created_by_id : 1 ,
)
assert ( customer1 )
Observer :: Transaction . 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