Skip to content

Commit

Permalink
feat!: use tracking_api_key instead of the old api_key and secret
Browse files Browse the repository at this point in the history
  • Loading branch information
Yihao-G committed Dec 24, 2024
1 parent 1f26a6e commit c26dcf5
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 83 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.0

- **BREAKING**: You are now required to use the tracking API key to authenticate. Visit https://connect.getvero.com/settings/project/tracking-api-keys to manage your tracking API keys. The original way of using API key and secret is no longer supported in this version.

## 0.9.0

- *Added support for Sidekiq**. You can now use Sidekiq to deliver API requests to Vero. To do so, just specify `config.async = :sidekiq` in your config.rb file.
Expand Down
23 changes: 6 additions & 17 deletions lib/vero/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@
module Vero
class Config
attr_writer :domain
attr_accessor :api_key, :secret, :development_mode, :async, :disabled, :logging
attr_accessor :tracking_api_key, :development_mode, :async, :disabled, :logging

def self.available_attributes
%i[api_key secret development_mode async disabled logging domain]
%i[tracking_api_key development_mode async disabled logging domain]
end

def initialize
reset!
end

def config_params
{ api_key: api_key, secret: secret }
{ tracking_api_key: tracking_api_key }
end

def request_params
{
auth_token: auth_token,
tracking_api_key: tracking_api_key,
development_mode: development_mode
}.compact
end
Expand All @@ -34,18 +34,8 @@ def domain
end
end

def auth_token
return unless auth_token?

::Base64.encode64("#{api_key}:#{secret}").gsub(/[\n ]/, '')
end

def auth_token?
!api_key.blank? && !secret.blank?
end

def configured?
auth_token?
!tracking_api_key.blank?
end

def disable_requests!
Expand All @@ -57,8 +47,7 @@ def reset!
self.development_mode = false
self.async = true
self.logging = false
self.api_key = nil
self.secret = nil
self.tracking_api_key = nil
end

def update_attributes(attributes = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/vero/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Vero
VERSION = '0.10.0'
VERSION = '1.0.0'
end
8 changes: 4 additions & 4 deletions spec/lib/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
describe :track! do
it 'should call the TrackAPI object via the configured sender' do
input = { event_name: 'test_event', identity: { email: '[email protected]' }, data: { test: 'test' } }
expected = input.merge(auth_token: 'abc123', development_mode: true)
expected = input.merge(tracking_api_key: 'abc123', development_mode: true)

mock_context = Vero::Context.new
allow(mock_context.config).to receive(:configured?).and_return(true)
allow(mock_context.config).to receive(:auth_token).and_return('abc123')
allow(mock_context.config).to receive(:tracking_api_key).and_return('abc123')
allow(mock_context.config).to receive(:development_mode).and_return(true)

allow(Vero::App).to receive(:default_context).and_return(mock_context)
Expand All @@ -27,11 +27,11 @@
describe Vero::Api::Users do
let(:subject) { Vero::Api::Users }
let(:mock_context) { Vero::Context.new }
let(:expected) { input.merge(auth_token: 'abc123', development_mode: true) }
let(:expected) { input.merge(tracking_api_key: 'abc123', development_mode: true) }

before :each do
allow(mock_context.config).to receive(:configured?).and_return(true)
allow(mock_context.config).to receive(:auth_token).and_return('abc123')
allow(mock_context.config).to receive(:tracking_api_key).and_return('abc123')
allow(mock_context.config).to receive(:development_mode).and_return(true)
allow(Vero::App).to receive(:default_context).and_return(mock_context)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/lib/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
it 'should pass configuration defined in the block to the config file' do
Vero::App.init

expect(context.config.api_key).to be_nil
expect(context.config.tracking_api_key).to be_nil
Vero::App.init do |c|
c.api_key = 'abcd1234'
c.tracking_api_key = 'abcd1234'
end
expect(context.config.api_key).to eq('abcd1234')
expect(context.config.tracking_api_key).to eq('abcd1234')
end

it 'should init should be able to set async' do
Expand Down
42 changes: 14 additions & 28 deletions spec/lib/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,50 +11,36 @@

describe :reset! do
it 'should reset all attributes' do
config.api_key = 'abcd1234'
config.secret = 'abcd1234'
config.tracking_api_key = 'abcd1234'
config.reset!

expect(config.api_key).to be_nil
expect(config.secret).to be_nil
expect(config.tracking_api_key).to be_nil
end
end

describe :auth_token do
it 'should return nil if either api_key or secret are not set' do
config.api_key = nil
config.secret = 'abcd'
expect(config.auth_token).to be_nil

config.api_key = 'abcd'
config.secret = nil
expect(config.auth_token).to be_nil

config.api_key = 'abcd'
config.secret = 'abcd'
expect(config.auth_token).not_to be_nil
describe :tracking_api_key do
it 'should return nil if tracking_api_key is not set' do
config.tracking_api_key = nil
expect(config.tracking_api_key).to be_nil
end

it 'should return an expected auth_token' do
config.api_key = 'abcd1234'
config.secret = 'efgh5678'
expect(config.auth_token).to eq('YWJjZDEyMzQ6ZWZnaDU2Nzg=')
it 'should return an expected tracking_api_key' do
config.tracking_api_key = 'abcd1234'
expect(config.tracking_api_key).to eq('abcd1234')
end
end

describe :request_params do
it 'should return a hash of auth_token and development_mode if they are set' do
config.api_key = nil
config.secret = nil
it 'should return a hash of tracking_api_key and development_mode if they are set' do
config.tracking_api_key = nil
config.development_mode = nil
expect(config.request_params).to eq({})

config.api_key = 'abcd1234'
config.secret = 'abcd1234'
expect(config.request_params).to eq({ auth_token: 'YWJjZDEyMzQ6YWJjZDEyMzQ=' })
config.tracking_api_key = 'abcd1234'
expect(config.request_params).to eq({ tracking_api_key: 'abcd1234' })

config.development_mode = true
expect(config.request_params).to eq({ auth_token: 'YWJjZDEyMzQ6YWJjZDEyMzQ=', development_mode: true })
expect(config.request_params).to eq({ tracking_api_key: 'abcd1234', development_mode: true })
end
end

Expand Down
35 changes: 17 additions & 18 deletions spec/lib/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@
describe Vero::Context do
let(:context) { Vero::Context.new }

it 'accepts multiple parameter types in contructor' do
context1 = Vero::Context.new({ api_key: 'blah', secret: 'didah' })
expect(context1).to be_a(Vero::Context)
expect(context1.config.api_key).to eq('blah')
expect(context1.config.secret).to eq('didah')

context2 = Vero::Context.new(context1)
expect(context2).to be_a(Vero::Context)
expect(context2).not_to be(context1)
expect(context2.config.api_key).to eq('blah')
expect(context2.config.secret).to eq('didah')

context3 = Vero::Context.new VeroUser.new('api_key', 'secret')
expect(context3).to be_a(Vero::Context)
expect(context3.config.api_key).to eq('api_key')
expect(context3.config.secret).to eq('secret')
describe :initialize do
it 'accepts multiple parameter types' do
context1 = Vero::Context.new({ tracking_api_key: 'didah' })
expect(context1).to be_a(Vero::Context)
expect(context1.config.tracking_api_key).to eq('didah')

context2 = Vero::Context.new(context1)
expect(context2).to be_a(Vero::Context)
expect(context2).not_to be(context1)
expect(context2.config.tracking_api_key).to eq('didah')

context3 = Vero::Context.new VeroUser.new('tracking_api_key')
expect(context3).to be_a(Vero::Context)
expect(context3.config.tracking_api_key).to eq('tracking_api_key')
end
end

describe :configure do
Expand All @@ -31,9 +30,9 @@

it 'should pass configuration defined in the block to the config file' do
context.configure do |c|
c.api_key = 'abcd1234'
c.tracking_api_key = 'abcd1234'
end
expect(context.config.api_key).to eq('abcd1234')
expect(context.config.tracking_api_key).to eq('abcd1234')
end

it 'should init should be able to set async' do
Expand Down
11 changes: 5 additions & 6 deletions spec/lib/trackable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def vero_context(user, logging = true, async = false, disabled = true)
before :each do
@request_params = {
event_name: 'test_event',
auth_token: 'YWJjZDEyMzQ6ZWZnaDU2Nzg=',
tracking_api_key: 'YWJjZDEyMzQ6ZWZnaDU2Nzg=',
identity: { email: '[email protected]', age: 20, _user_type: 'User' },
data: { test: 1 },
development_mode: true
Expand All @@ -40,8 +40,7 @@ def vero_context(user, logging = true, async = false, disabled = true)
context 'the gem has been configured' do
before do
Vero::App.init do |c|
c.api_key = 'abcd1234'
c.secret = 'efgh5678'
c.tracking_api_key = 'YWJjZDEyMzQ6ZWZnaDU2Nzg='
c.async = false
end
end
Expand Down Expand Up @@ -300,8 +299,8 @@ def vero_context(user, logging = true, async = false, disabled = true)
describe :with_vero_context do
it 'should be able to change contexts' do
user = User.new
expect(user.with_default_vero_context.config.config_params).to eq({ api_key: 'abcd1234', secret: 'efgh5678' })
expect(user.with_vero_context({ api_key: 'boom', secret: 'tish' }).config.config_params).to eq({ api_key: 'boom', secret: 'tish' })
expect(user.with_default_vero_context.config.config_params).to eq({ tracking_api_key: 'YWJjZDEyMzQ6ZWZnaDU2Nzg=' })
expect(user.with_vero_context({ tracking_api_key: 'boom' }).config.config_params).to eq({ tracking_api_key: 'boom' })
end
end

Expand All @@ -310,7 +309,7 @@ def vero_context(user, logging = true, async = false, disabled = true)

request_params = {
event_name: 'test_event',
auth_token: 'YWJjZDEyMzQ6ZWZnaDU2Nzg=',
tracking_api_key: 'YWJjZDEyMzQ6ZWZnaDU2Nzg=',
identity: { email: '[email protected]', age: 20, _user_type: 'UserWithoutInterface' },
data: { test: 1 },
development_mode: true
Expand Down
8 changes: 3 additions & 5 deletions spec/lib/view_helpers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,12 @@

context 'Vero::App has been properly configured' do
before :each do
@api_key = 'abcd1234'
@api_secret = 'secret'
@tracking_api_key = 'abcd1234'
@api_dev_mode = false

Vero::App.init do |c|
c.api_key = @api_key
c.secret = @api_secret
c.development_mode = @api_dev_mode
c.tracking_api_key = @tracking_api_key
c.development_mode = @api_dev_mode
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/support/vero_user_support.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# frozen_string_literal: true

VeroUser = Struct.new(:api_key, :secret)
VeroUser = Struct.new(:tracking_api_key)

0 comments on commit c26dcf5

Please sign in to comment.