From 1f96a2c29ac1d03078cef78bf74d1ea427263373 Mon Sep 17 00:00:00 2001 From: Serhii Bodnaruk <32734554+serhii-bodnaruk@users.noreply.github.com> Date: Thu, 3 Nov 2022 22:48:23 +0800 Subject: [PATCH] fix: add possibility to passing user_id as integer (#119) --- .rubocop.yml | 2 +- lib/unleash/context.rb | 2 +- spec/unleash/client_spec.rb | 2 + spec/unleash/strategy/user_with_id_spec.rb | 67 +++++++++++++++------- 4 files changed, 49 insertions(+), 24 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 39f7af60..85643225 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -22,7 +22,7 @@ Metrics/BlockLength: Metrics/AbcSize: Max: 30 Metrics/CyclomaticComplexity: - Max: 9 + Max: 10 Metrics/PerceivedComplexity: Max: 10 diff --git a/lib/unleash/context.rb b/lib/unleash/context.rb index 81c924f6..79b4cf1b 100644 --- a/lib/unleash/context.rb +++ b/lib/unleash/context.rb @@ -9,7 +9,7 @@ def initialize(params = {}) self.app_name = value_for('appName', params, Unleash&.configuration&.app_name) self.environment = value_for('environment', params, Unleash&.configuration&.environment || 'default') - self.user_id = value_for('userId', params) + self.user_id = value_for('userId', params)&.to_s self.session_id = value_for('sessionId', params) self.remote_address = value_for('remoteAddress', params) self.current_time = value_for('currentTime', params, Time.now.utc.iso8601.to_s) diff --git a/spec/unleash/client_spec.rb b/spec/unleash/client_spec.rb index 5cfa1d99..f92d591b 100644 --- a/spec/unleash/client_spec.rb +++ b/spec/unleash/client_spec.rb @@ -409,8 +409,10 @@ it "should yield correctly to block when using if_enabled" do unleash_client = Unleash::Client.new + cont = Unleash::Context.new(user_id: 1) expect{ |b| unleash_client.if_enabled('any_feature', {}, true, &b).to yield_with_no_args } + expect{ |b| unleash_client.if_enabled('any_feature', cont, true, &b).to yield_with_no_args } expect{ |b| unleash_client.if_enabled('any_feature', {}, false, &b).not_to yield_with_no_args } end diff --git a/spec/unleash/strategy/user_with_id_spec.rb b/spec/unleash/strategy/user_with_id_spec.rb index 5cf9e3f4..18e326aa 100644 --- a/spec/unleash/strategy/user_with_id_spec.rb +++ b/spec/unleash/strategy/user_with_id_spec.rb @@ -4,35 +4,58 @@ RSpec.describe Unleash::Strategy::UserWithId do describe '#is_enabled?' do let(:strategy) { Unleash::Strategy::UserWithId.new } - let(:unleash_context) { Unleash::Context.new({ 'userId' => 'bob' }) } - it 'should be enabled with correct params' do - expect(strategy.is_enabled?({ 'userIds' => 'alice,bob,carol,dave' }, unleash_context)).to be_truthy + context 'with string params' do + let(:unleash_context) { Unleash::Context.new({ 'userId' => 'bob' }) } - unleash_context2 = Unleash::Context.new - unleash_context2.user_id = 'alice' - expect(strategy.is_enabled?({ 'userIds' => 'alice,bob,carol,dave' }, unleash_context2)).to be_truthy - end + it 'should be enabled with correct params' do + expect(strategy.is_enabled?({ 'userIds' => 'alice,bob,carol,dave' }, unleash_context)).to be_truthy - it 'should be enabled with correct can include spaces' do - expect(strategy.is_enabled?({ 'userIds' => ' alice ,bob,carol,dave' }, unleash_context)).to be_truthy - end + unleash_context2 = Unleash::Context.new + unleash_context2.user_id = 'alice' + expect(strategy.is_enabled?({ 'userIds' => 'alice,bob,carol,dave' }, unleash_context2)).to be_truthy + end - it 'should be disabled with false params' do - expect(strategy.is_enabled?({ 'userIds' => 'alice,dave' }, unleash_context)).to be_falsey - end + it 'should be enabled with correct can include spaces' do + expect(strategy.is_enabled?({ 'userIds' => ' alice ,bob,carol,dave' }, unleash_context)).to be_truthy + end + + it 'should be disabled with false params' do + expect(strategy.is_enabled?({ 'userIds' => 'alice,dave' }, unleash_context)).to be_falsey + end + + it 'should be disabled on invalid params' do + expect(strategy.is_enabled?({ 'userIds' => nil }, unleash_context)).to be_falsey + expect(strategy.is_enabled?({}, unleash_context)).to be_falsey + expect(strategy.is_enabled?('string', unleash_context)).to be_falsey + expect(strategy.is_enabled?(nil, unleash_context)).to be_falsey + end - it 'should be disabled on invalid params' do - expect(strategy.is_enabled?({ 'userIds' => nil }, unleash_context)).to be_falsey - expect(strategy.is_enabled?({}, unleash_context)).to be_falsey - expect(strategy.is_enabled?('string', unleash_context)).to be_falsey - expect(strategy.is_enabled?(nil, unleash_context)).to be_falsey + it 'should be disabled on invalid contexts' do + expect(strategy.is_enabled?({ 'userIds' => 'alice,bob,carol,dave' }, Unleash::Context.new)).to be_falsey + expect(strategy.is_enabled?({ 'userIds' => 'alice,bob,carol,dave' }, nil)).to be_falsey + expect(strategy.is_enabled?({ 'userIds' => 'alice,bob,carol,dave' })).to be_falsey + end end - it 'should be disabled on invalid contexts' do - expect(strategy.is_enabled?({ 'userIds' => 'alice,bob,carol,dave' }, Unleash::Context.new)).to be_falsey - expect(strategy.is_enabled?({ 'userIds' => 'alice,bob,carol,dave' }, nil)).to be_falsey - expect(strategy.is_enabled?({ 'userIds' => 'alice,bob,carol,dave' })).to be_falsey + context 'with int params' do + let(:user_id) { 123 } + let(:unleash_context) { Unleash::Context.new({ 'userId' => user_id }) } + + it 'should be enabled with correct params' do + expect(strategy.is_enabled?({ 'userIds' => '1,2,123' }, unleash_context)).to be_truthy + + unleash_context2 = Unleash::Context.new(user_id: 1) + expect(strategy.is_enabled?({ 'userIds' => '1,2,123' }, unleash_context2)).to be_truthy + end + + it 'should be enabled with correct can include spaces' do + expect(strategy.is_enabled?({ 'userIds' => ' 1 ,2, 123 ,200 ' }, unleash_context)).to be_truthy + end + + it 'should be disabled with false params' do + expect(strategy.is_enabled?({ 'userIds' => '1,2' }, unleash_context)).to be_falsey + end end end end