Skip to content

Commit

Permalink
Fix double initialization of user authentication in ScriptTag (#357)
Browse files Browse the repository at this point in the history
  • Loading branch information
DamonFstr authored Dec 19, 2024
1 parent b9a9155 commit 3296abf
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
5 changes: 2 additions & 3 deletions intercom-rails.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,13 @@ Gem::Specification.new do |s|
s.test_files = Dir["test/**/*"]

s.add_dependency 'activesupport', '>4.0'

s.add_development_dependency 'rake'
s.add_development_dependency 'actionpack', '>5.0'
s.add_development_dependency 'rspec', '~> 3.13'
s.add_development_dependency 'rspec-rails', '~> 5.0'
s.add_development_dependency 'pry'
s.add_development_dependency 'sinatra', '~> 2.0'
s.add_development_dependency 'thin', '~> 1.7.0'
s.add_development_dependency 'bigdecimal', '1.3.5'
s.add_development_dependency 'sinatra', '~> 3.0'
s.add_development_dependency 'tzinfo'
s.add_development_dependency 'gem-release'
end
14 changes: 10 additions & 4 deletions lib/intercom-rails/script_tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ def initialize(options = {})
self.controller = options[:controller]
@show_everywhere = options[:show_everywhere]
@session_duration = session_duration_from_config
self.user_details = options[:find_current_user_details] ? find_current_user_details : options[:user_details]

initial_user_details = if options[:find_current_user_details]
find_current_user_details
else
options[:user_details] || {}
end

lead_attributes = find_lead_attributes

self.user_details = initial_user_details.merge(lead_attributes)

self.encrypted_mode_enabled = options[:encrypted_mode] || Config.encrypted_mode
self.encrypted_mode = IntercomRails::EncryptedMode.new(secret, options[:initialization_vector], {:enabled => encrypted_mode_enabled})

# Request specific custom data for non-signed up users base on lead_attributes
self.user_details = self.user_details.merge(find_lead_attributes)

self.company_details = if options[:find_current_company_details]
find_current_company_details
elsif options[:user_details]
Expand Down
2 changes: 1 addition & 1 deletion lib/intercom-rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module IntercomRails
VERSION = "1.0.1"
VERSION = "1.0.2"
end
35 changes: 35 additions & 0 deletions spec/script_tag_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,42 @@ def user
# Rejects
expect(script_tag.intercom_settings[:ad_data]).to eq(nil)
end
end

context 'with lead attributes' do
before do
IntercomRails.config.user.lead_attributes = [:plan]
IntercomRails.config.api_secret = 'abcdefgh'
allow_any_instance_of(IntercomRails::ScriptTag).to receive(:controller).and_return(
double(intercom_custom_data: double(user: { 'plan' => 'pro' }))
)
end

it 'merges lead attributes with user details' do
script_tag = ScriptTag.new(
user_details: {
user_id: '1234',
name: 'Test User'
}
)

expect(script_tag.intercom_settings[:plan]).to eq('pro')
expect(script_tag.intercom_settings[:user_hash]).to be_present
end

it 'preserves existing user details when merging lead attributes' do
script_tag = ScriptTag.new(
user_details: {
user_id: '1234',
name: 'Test User',
email: 'test@example.com'
}
)

expect(script_tag.intercom_settings[:plan]).to eq('pro')
expect(script_tag.intercom_settings[:name]).to eq('Test User')
expect(script_tag.intercom_settings[:email]).to eq('test@example.com')
end
end

end

0 comments on commit 3296abf

Please sign in to comment.