Skip to content

Commit

Permalink
Merge c63ea62 into c88196a
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesyang124 committed Sep 3, 2019
2 parents c88196a + c63ea62 commit ce3c9dd
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 4 deletions.
5 changes: 2 additions & 3 deletions lib/unleash/feature_toggle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,12 @@ def get_variant(context, fallback_variant = disabled_variant)

# only check if it is enabled, do not do metrics
def am_enabled?(context, default_result)
result = self.enabled && ( self.strategies.select{ |s|
result = self.enabled ? ( self.strategies.select{ |s|
strategy = Unleash::STRATEGIES.fetch(s.name.to_sym, :unknown)
r = strategy.is_enabled?(s.params, context)
Unleash.logger.debug "Strategy #{s.name} returned #{r} with context: #{context}" #"for params #{s.params} "
r
}.any? || self.strategies.empty? )
result ||= default_result
}.any? || self.strategies.empty? ) : default_result

Unleash.logger.debug "FeatureToggle (enabled:#{self.enabled} default_result:#{default_result} and Strategies combined returned #{result})"
return result
Expand Down
110 changes: 109 additions & 1 deletion spec/unleash/feature_toggle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,118 @@
variants: nil
) }

it 'should return true' do
it 'should return true if default is true' do
context = Unleash::Context.new(user_id: 1)
expect(feature_toggle.is_enabled?(context, true)).to be_truthy
end

it 'should return false if default is false' do
context = Unleash::Context.new(user_id: 1)
expect(feature_toggle.is_enabled?(context, false)).to be_falsey
end
end

describe 'FeatureToggle with empty strategies and disabled toggle' do
let(:feature_toggle) { Unleash::FeatureToggle.new(
JSON.parse('{
"name": "Test.userid",
"description": null,
"enabled": false,
"strategies": [],
"variants": [],
"createdAt": "2019-01-24T10:41:45.236Z"
}')
) }

it 'should return false if disabled and default is false' do
context = Unleash::Context.new(user_id: 1)
expect(feature_toggle.is_enabled?(context, false)).to be_falsey
end

it 'should return true if disabled and default is true' do
context = Unleash::Context.new(user_id: 1)
expect(feature_toggle.is_enabled?(context, true)).to be_truthy
end
end

describe 'FeatureToggle with userId strategy and enabled toggle' do
let(:feature_toggle) { Unleash::FeatureToggle.new(
JSON.parse('{
"name": "Test.userid",
"description": null,
"enabled": true,
"strategies": [
{
"name": "userWithId",
"parameters": {
"userIds": "12345"
}
}
],
"variants": [],
"createdAt": "2019-01-24T10:41:45.236Z"
}')
) }

it 'should return true if enabled, user_id matched, and default is true' do
context = Unleash::Context.new(user_id: "12345")
expect(feature_toggle.is_enabled?(context, true)).to be_truthy
end

it 'should return true if enabled, user_id matched, and default is false' do
context = Unleash::Context.new(user_id: "12345")
expect(feature_toggle.is_enabled?(context, false)).to be_truthy
end

it 'should return false if enabled, user_id unmatched, and default is true' do
context = Unleash::Context.new(user_id: "54321")
expect(feature_toggle.is_enabled?(context, true)).to be_falsey
end

it 'should return false if enabled, user_id unmatched, and default is false' do
context = Unleash::Context.new(user_id: "54321")
expect(feature_toggle.is_enabled?(context, false)).to be_falsey
end
end

describe 'FeatureToggle with userId strategy and disabled toggle' do
let(:feature_toggle) { Unleash::FeatureToggle.new(
JSON.parse('{
"name": "Test.userid",
"description": null,
"enabled": false,
"strategies": [
{
"name": "userWithId",
"parameters": {
"userIds": "12345"
}
}
],
"variants": [],
"createdAt": "2019-01-24T10:41:45.236Z"
}')
) }

it 'should return false if disabled, user_id matched, and default is false' do
context = Unleash::Context.new(user_id: "12345")
expect(feature_toggle.is_enabled?(context, false)).to be_falsey
end

it 'should return false if disabled, user_id unmatched, and default is false' do
context = Unleash::Context.new(user_id: "54321")
expect(feature_toggle.is_enabled?(context, false)).to be_falsey
end

it 'should return true if disabled, user_id matched, and default is true' do
context = Unleash::Context.new(user_id: "12345")
expect(feature_toggle.is_enabled?(context, true)).to be_truthy
end

it 'should return true if disabled, user_id unmatched, and default is true' do
context = Unleash::Context.new(user_id: "54321")
expect(feature_toggle.is_enabled?(context, true)).to be_truthy
end
end

describe 'FeatureToggle with variants' do
Expand Down

0 comments on commit ce3c9dd

Please sign in to comment.