Skip to content

Fix double initialization of user authentication in ScriptTag #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Comment on lines -28 to -29
Copy link
Contributor Author

@DamonFstr DamonFstr Dec 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed? I couldn't install these on apple silicon/newer ruby. I was going to bump them but removing them appears to have done nothing.

I think big decimal might be included in modern versions of ruby?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look like 1.3.5 version is for ruby version before 2.5 if you want to use BigDecimal.new and subclassing without warning, see here
Screenshot 2024-12-19 at 14 38 18

But I'm not sure if we actually need it in our gem.

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