Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rarruda committed Jun 21, 2020
1 parent a9eb454 commit ea6993f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/unleash/activation_strategy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(name, params, constraints = [])
self.params = {}
end

if constraints.is_a?(Array)
if constraints.is_a?(Array) && constraints.each{ |c| c.is_a?(Constraint) }
self.constraints = constraints
else
Unleash.logger.warn "Invalid constraints provided for ActivationStrategy #{constraints}"
Expand Down
73 changes: 73 additions & 0 deletions spec/unleash/constraint_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require "spec_helper"
require "unleash/constraint"

RSpec.describe Unleash::Constraint do
describe '#is_enabled?' do

it 'matches based on property IN value' do
context_params = {
user_id: '123',
session_id: 'verylongsesssionid',
remote_address: '127.0.0.2',
properties: {
env: 'dev'
}
}
context = Unleash::Context.new(context_params)
constraint = Unleash::Constraint.new('env', 'IN', ['dev'])
expect(constraint.matches_context?(context)).to be_truthy

constraint = Unleash::Constraint.new('env', 'IN', ['dev','pre'])
expect(constraint.matches_context?(context)).to be_truthy

constraint = Unleash::Constraint.new('env', 'NOT_IN', ['dev', 'pre'])
expect(constraint.matches_context?(context)).to be_falsey

constraint = Unleash::Constraint.new('env', 'NOT_IN', ['pre', 'prod'])
expect(constraint.matches_context?(context)).to be_truthy
end

it 'matches based on property NOT_IN value' do
context_params = {
user_id: '123',
session_id: 'verylongsesssionid',
remote_address: '127.0.0.2',
properties: {
env: 'dev'
}
}
context = Unleash::Context.new(context_params)
constraint = Unleash::Constraint.new('env', 'NOT_IN', ['dev'])
expect(constraint.matches_context?(context)).to be_falsey

constraint = Unleash::Constraint.new('env', 'NOT_IN', ['dev','pre'])
expect(constraint.matches_context?(context)).to be_falsey

constraint = Unleash::Constraint.new('env', 'NOT_IN', ['pre', 'prod'])
expect(constraint.matches_context?(context)).to be_truthy
end

it 'matches based on user_id IN/NOT_IN user_id' do
context_params = {
user_id: '123',
session_id: 'verylongsesssionid',
remote_address: '127.0.0.2',
properties: {
fancy: 'polarbear'
}
}
context = Unleash::Context.new(context_params)
constraint = Unleash::Constraint.new('user_id', 'IN', ['123', '456'])
expect(constraint.matches_context?(context)).to be_truthy

constraint = Unleash::Constraint.new('user_id', 'IN', ['456', '789'])
expect(constraint.matches_context?(context)).to be_falsey

constraint = Unleash::Constraint.new('user_id', 'NOT_IN', ['123', '456'])
expect(constraint.matches_context?(context)).to be_falsey

constraint = Unleash::Constraint.new('user_id', 'NOT_IN', ['456', '789'])
expect(constraint.matches_context?(context)).to be_truthy
end
end
end

0 comments on commit ea6993f

Please sign in to comment.