From 3b0eef6045ff06f11306e8d0ff30aa48cfd1cc87 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Thu, 8 Sep 2022 13:19:14 +0200 Subject: [PATCH 01/21] fix: constraint test if operator is unknown This makes us ignore the constraint if the constraint operator is unknown. --- .github/workflows/pull_request.yml | 2 +- lib/unleash/constraint.rb | 10 ++++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 8078199d..ab9f9984 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -46,7 +46,7 @@ jobs: - name: Install dependencies run: bundle install - name: Download test cases - run: git clone --depth 5 --branch v4.1.0 https://github.com/Unleash/client-specification.git client-specification + run: git clone --depth 5 --branch v4.2.2 https://github.com/Unleash/client-specification.git client-specification - name: Run tests run: bundle exec rake env: diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 422d243a..e1506da7 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -1,5 +1,4 @@ require 'date' - module Unleash class Constraint attr_accessor :context_name, :operator, :value, :inverted, :case_insensitive @@ -19,15 +18,18 @@ class Constraint DATE_BEFORE: ->(context_v, constraint_v){ on_valid_date(constraint_v, context_v){ |x, y| (x > y) } }, SEMVER_EQ: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x == y) } }, SEMVER_GT: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x < y) } }, - SEMVER_LT: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x > y) } } + SEMVER_LT: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x > y) } }, + FALLBACK_VALIDATOR: ->(context_v, constraint_v){true} }.freeze LIST_OPERATORS = [:IN, :NOT_IN, :STR_STARTS_WITH, :STR_ENDS_WITH, :STR_CONTAINS].freeze def initialize(context_name, operator, value = [], inverted: false, case_insensitive: false) raise ArgumentError, "context_name is not a String" unless context_name.is_a?(String) - raise ArgumentError, "operator does not hold a valid value:" + OPERATORS.keys unless OPERATORS.include? operator.to_sym - + unless OPERATORS.include? operator.to_sym + Unleash.logger.warn "Operator #{operator} is not a supported operator, falling back to FALLBACK_VALIDATOR which skips this constraint" + operator = "FALLBACK_VALIDATOR" + end self.validate_constraint_value_type(operator.to_sym, value) self.context_name = context_name From 5821459584fa166f5798ec311b469041e5710ab6 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Thu, 8 Sep 2022 15:23:24 +0200 Subject: [PATCH 02/21] fix: nothing can't be in anything If input data to a constraint is missing, then its always not present in a set. Nothing is not a part of anything. --- lib/unleash/constraint.rb | 14 ++++++++++++-- spec/unleash/constraint_spec.rb | 10 ++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index e1506da7..d2ae29ed 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -19,7 +19,7 @@ class Constraint SEMVER_EQ: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x == y) } }, SEMVER_GT: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x < y) } }, SEMVER_LT: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x > y) } }, - FALLBACK_VALIDATOR: ->(context_v, constraint_v){true} + FALLBACK_VALIDATOR: ->(context_v, constraint_v){false} }.freeze LIST_OPERATORS = [:IN, :NOT_IN, :STR_STARTS_WITH, :STR_ENDS_WITH, :STR_CONTAINS].freeze @@ -47,7 +47,17 @@ def matches_context?(context) rescue KeyError Unleash.logger.warn "Attemped to resolve a context key during constraint resolution: #{self.context_name} but it wasn't \ found on the context" - false + key_error_handler() + end + + def key_error_handler() + case operator + when "NOT_IN".to_sym + # when there is no input data present, nothing can't be in anything. + true + else + false + end end def self.on_valid_date(val1, val2) diff --git a/spec/unleash/constraint_spec.rb b/spec/unleash/constraint_spec.rb index 9fe0ab65..c5bf1f54 100644 --- a/spec/unleash/constraint_spec.rb +++ b/spec/unleash/constraint_spec.rb @@ -48,6 +48,16 @@ expect(constraint.matches_context?(context)).to be true end + it 'matches based on property not set by operator NOT_IN value' do + context_params = { + properties: { + } + } + context = Unleash::Context.new(context_params) + constraint = Unleash::Constraint.new('env', 'NOT_IN', ['anything']) + expect(constraint.matches_context?(context)).to be true + end + it 'matches based on user_id IN/NOT_IN user_id' do context_params = { user_id: '123', From 71a18ce189f86bdd20d4c5c1e1bb1af454aad23c Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Thu, 8 Sep 2022 15:32:33 +0200 Subject: [PATCH 03/21] linting --- lib/unleash/constraint.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index d2ae29ed..8de884f0 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -19,7 +19,7 @@ class Constraint SEMVER_EQ: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x == y) } }, SEMVER_GT: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x < y) } }, SEMVER_LT: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x > y) } }, - FALLBACK_VALIDATOR: ->(context_v, constraint_v){false} + FALLBACK_VALIDATOR: ->(_context_v, _constraint_v){false} }.freeze LIST_OPERATORS = [:IN, :NOT_IN, :STR_STARTS_WITH, :STR_ENDS_WITH, :STR_CONTAINS].freeze From a8c65ac1778ac159be77deb57806b78a2d332260 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 08:34:34 +0200 Subject: [PATCH 04/21] refactor: remove not_in tests from in test --- spec/unleash/constraint_spec.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/spec/unleash/constraint_spec.rb b/spec/unleash/constraint_spec.rb index c5bf1f54..1706e7ea 100644 --- a/spec/unleash/constraint_spec.rb +++ b/spec/unleash/constraint_spec.rb @@ -20,12 +20,6 @@ constraint = Unleash::Constraint.new('env', 'IN', ['dev', 'pre']) expect(constraint.matches_context?(context)).to be true - - constraint = Unleash::Constraint.new('env', 'NOT_IN', ['dev', 'pre']) - expect(constraint.matches_context?(context)).to be false - - constraint = Unleash::Constraint.new('env', 'NOT_IN', ['pre', 'prod']) - expect(constraint.matches_context?(context)).to be true end it 'matches based on property NOT_IN value' do From b6b6f17ac6201f48e60cf64ae8b01800a5919a93 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 09:15:19 +0200 Subject: [PATCH 05/21] fix: make unleash not rejecting configuration of constraints that is invalid --- .github/workflows/pull_request.yml | 2 +- lib/unleash/constraint.rb | 4 ++-- spec/unleash/constraint_spec.rb | 33 ++++++++++++++---------------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index ab9f9984..8078199d 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -46,7 +46,7 @@ jobs: - name: Install dependencies run: bundle install - name: Download test cases - run: git clone --depth 5 --branch v4.2.2 https://github.com/Unleash/client-specification.git client-specification + run: git clone --depth 5 --branch v4.1.0 https://github.com/Unleash/client-specification.git client-specification - name: Run tests run: bundle exec rake env: diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 8de884f0..3c516f89 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -92,8 +92,8 @@ def self.on_valid_version(val1, val2) # This should be a private method but for some reason this fails on Ruby 2.5 def validate_constraint_value_type(operator, value) - raise ArgumentError, "context_name is not an Array" if LIST_OPERATORS.include?(operator) && value.is_a?(String) - raise ArgumentError, "context_name is not a String" if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) + Unleash.logger.info "value is not an Array" if LIST_OPERATORS.include?(operator) && value.is_a?(String) + Unleash.logger.info "value is not a String" if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) end private diff --git a/spec/unleash/constraint_spec.rb b/spec/unleash/constraint_spec.rb index 1706e7ea..f8df40f6 100644 --- a/spec/unleash/constraint_spec.rb +++ b/spec/unleash/constraint_spec.rb @@ -388,24 +388,21 @@ expect(constraint.matches_context?(context)).to be false end - it 'rejects constraint construction for invalid value types for operator' do - array_constraints = ['STR_CONTAINS', 'STR_ENDS_WITH', 'STR_STARTS_WITH', 'IN', 'NOT_IN'] - - array_constraints.each do |operator_name| - Unleash::Constraint.new('env', operator_name, []) - expect do - Unleash::Constraint.new('env', operator_name, '') - end.to raise_error - end - - string_constraints = ['NUM_EQ', 'NUM_GT', 'NUM_GTE', 'NUM_LT', 'NUM_LTE', - 'DATE_AFTER', 'DATE_BEFORE', 'SEMVER_EQ', 'SEMVER_GT', 'SEMVER_LT'] - string_constraints.each do |operator_name| - Unleash::Constraint.new('env', operator_name, '') - expect do - Unleash::Constraint.new('env', operator_name, []) - end.to raise_error - end + it 'handles malconfiguration of constraint operators' do + context_params = { + user_id: '123', + session_id: 'verylongsesssionid', + remote_address: '127.0.0.1', + properties: { + env: 'development' + } + } + context = Unleash::Context.new(context_params) + constraint = Unleash::Constraint.new('env', 'NOT_A_VALID_OPERATOR', 'dev', inverted: true) + expect(constraint.matches_context?(context)).to be true + + constraint = Unleash::Constraint.new('env', 'NOT_A_VALID_OPERATOR', ['dev'], inverted: true) + expect(constraint.matches_context?(context)).to be true end end end From 154392aab359e48d8cb9319db0969b25218e76e2 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 09:19:03 +0200 Subject: [PATCH 06/21] cleaner log messages --- lib/unleash/constraint.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 3c516f89..862f975a 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -92,8 +92,8 @@ def self.on_valid_version(val1, val2) # This should be a private method but for some reason this fails on Ruby 2.5 def validate_constraint_value_type(operator, value) - Unleash.logger.info "value is not an Array" if LIST_OPERATORS.include?(operator) && value.is_a?(String) - Unleash.logger.info "value is not a String" if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) + Unleash.logger.info "value is not an Array, operator is expecting an Array" if LIST_OPERATORS.include?(operator) && value.is_a?(String) + Unleash.logger.info "value is an Array, operator is not an Array operator" if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) end private From 67d7dfdad09c3a33ef2c20304896541892ce27f7 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 13:46:50 +0200 Subject: [PATCH 07/21] Update lib/unleash/constraint.rb Co-authored-by: Renato Arruda --- lib/unleash/constraint.rb | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 862f975a..45fbe486 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -47,17 +47,10 @@ def matches_context?(context) rescue KeyError Unleash.logger.warn "Attemped to resolve a context key during constraint resolution: #{self.context_name} but it wasn't \ found on the context" - key_error_handler() - end - def key_error_handler() - case operator - when "NOT_IN".to_sym - # when there is no input data present, nothing can't be in anything. - true - else - false - end + # when the operator is NOT_IN and there is no data, return true. In all other cases the operator doesn't match. + return true if operator == :NOT_IN + false end def self.on_valid_date(val1, val2) From 8d1945078633559b9148828844b08cdb15ea5451 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 13:47:16 +0200 Subject: [PATCH 08/21] Update spec/unleash/constraint_spec.rb Co-authored-by: Renato Arruda --- spec/unleash/constraint_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unleash/constraint_spec.rb b/spec/unleash/constraint_spec.rb index f8df40f6..98f4a77e 100644 --- a/spec/unleash/constraint_spec.rb +++ b/spec/unleash/constraint_spec.rb @@ -42,7 +42,7 @@ expect(constraint.matches_context?(context)).to be true end - it 'matches based on property not set by operator NOT_IN value' do + it 'matches based on a value NOT_IN in empty context field' do context_params = { properties: { } From 5adf0c543962dc3ecb43657fdba6d0aac49669cd Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 13:49:57 +0200 Subject: [PATCH 09/21] refactor --- spec/unleash/constraint_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unleash/constraint_spec.rb b/spec/unleash/constraint_spec.rb index 98f4a77e..6b99335c 100644 --- a/spec/unleash/constraint_spec.rb +++ b/spec/unleash/constraint_spec.rb @@ -388,7 +388,7 @@ expect(constraint.matches_context?(context)).to be false end - it 'handles malconfiguration of constraint operators' do + it 'gracefully handles invalid constraint operators' do context_params = { user_id: '123', session_id: 'verylongsesssionid', From a79c144989986c72d40789f391e47774d34f8fa6 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 13:55:39 +0200 Subject: [PATCH 10/21] linting --- lib/unleash/constraint.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 45fbe486..8136ad22 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -19,7 +19,7 @@ class Constraint SEMVER_EQ: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x == y) } }, SEMVER_GT: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x < y) } }, SEMVER_LT: ->(context_v, constraint_v){ on_valid_version(constraint_v, context_v){ |x, y| (x > y) } }, - FALLBACK_VALIDATOR: ->(_context_v, _constraint_v){false} + FALLBACK_VALIDATOR: ->(_context_v, _constraint_v){ false } }.freeze LIST_OPERATORS = [:IN, :NOT_IN, :STR_STARTS_WITH, :STR_ENDS_WITH, :STR_CONTAINS].freeze From 8cf29785df7f307f26b0ff07f9bbf9d6b9fa0d4b Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 13:56:05 +0200 Subject: [PATCH 11/21] linting --- lib/unleash/constraint.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 8136ad22..98648e27 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -26,6 +26,7 @@ class Constraint def initialize(context_name, operator, value = [], inverted: false, case_insensitive: false) raise ArgumentError, "context_name is not a String" unless context_name.is_a?(String) + unless OPERATORS.include? operator.to_sym Unleash.logger.warn "Operator #{operator} is not a supported operator, falling back to FALLBACK_VALIDATOR which skips this constraint" operator = "FALLBACK_VALIDATOR" From fcfbce6ac7b9fad803a0b177215835b6055c1fca Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 13:56:37 +0200 Subject: [PATCH 12/21] linting --- lib/unleash/constraint.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 98648e27..267b41a9 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -28,7 +28,8 @@ def initialize(context_name, operator, value = [], inverted: false, case_insensi raise ArgumentError, "context_name is not a String" unless context_name.is_a?(String) unless OPERATORS.include? operator.to_sym - Unleash.logger.warn "Operator #{operator} is not a supported operator, falling back to FALLBACK_VALIDATOR which skips this constraint" + Unleash.logger.warn "Operator #{operator} is not a supported operator, " + + "falling back to FALLBACK_VALIDATOR which skips this constraint" operator = "FALLBACK_VALIDATOR" end self.validate_constraint_value_type(operator.to_sym, value) From b269699d96ee0c37bd905d21f8c1b6ecb0d1e733 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 13:56:45 +0200 Subject: [PATCH 13/21] linting --- lib/unleash/constraint.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 267b41a9..73687137 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -29,7 +29,7 @@ def initialize(context_name, operator, value = [], inverted: false, case_insensi unless OPERATORS.include? operator.to_sym Unleash.logger.warn "Operator #{operator} is not a supported operator, " + - "falling back to FALLBACK_VALIDATOR which skips this constraint" + "falling back to FALLBACK_VALIDATOR which skips this constraint" operator = "FALLBACK_VALIDATOR" end self.validate_constraint_value_type(operator.to_sym, value) From 0ef33849c9144bd3fe03151be9eb0e50a33745b1 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 13:57:28 +0200 Subject: [PATCH 14/21] linting --- lib/unleash/constraint.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 73687137..db7df4cc 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -52,6 +52,7 @@ def matches_context?(context) # when the operator is NOT_IN and there is no data, return true. In all other cases the operator doesn't match. return true if operator == :NOT_IN + false end From ecc88b03569a21b7755219c23c8945fd267d14a7 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 13:59:03 +0200 Subject: [PATCH 15/21] linting --- lib/unleash/constraint.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index db7df4cc..e9c22322 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -88,8 +88,12 @@ def self.on_valid_version(val1, val2) # This should be a private method but for some reason this fails on Ruby 2.5 def validate_constraint_value_type(operator, value) - Unleash.logger.info "value is not an Array, operator is expecting an Array" if LIST_OPERATORS.include?(operator) && value.is_a?(String) - Unleash.logger.info "value is an Array, operator is not an Array operator" if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) + if LIST_OPERATORS.include?(operator) && value.is_a?(String) + Unleash.logger.info "value is not an Array, operator is expecting an Array" + end + if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) + Unleash.logger.info "value is an Array, operator is not an Array operator" + end end private From 36bd020c322955958879f2e4db9e7b57ea25bfbc Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 14:02:22 +0200 Subject: [PATCH 16/21] linting --- lib/unleash/constraint.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index e9c22322..3c469b6b 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -28,8 +28,8 @@ def initialize(context_name, operator, value = [], inverted: false, case_insensi raise ArgumentError, "context_name is not a String" unless context_name.is_a?(String) unless OPERATORS.include? operator.to_sym - Unleash.logger.warn "Operator #{operator} is not a supported operator, " + - "falling back to FALLBACK_VALIDATOR which skips this constraint" + Unleash.logger.warn "Operator #{operator} is not a supported operator, \ + falling back to FALLBACK_VALIDATOR which skips this constraint" operator = "FALLBACK_VALIDATOR" end self.validate_constraint_value_type(operator.to_sym, value) From 0662bb0707696091c953c5b1f1486820f6314f9e Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 14:15:35 +0200 Subject: [PATCH 17/21] fix --- lib/unleash/constraint.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 3c469b6b..74628c8b 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -91,9 +91,7 @@ def validate_constraint_value_type(operator, value) if LIST_OPERATORS.include?(operator) && value.is_a?(String) Unleash.logger.info "value is not an Array, operator is expecting an Array" end - if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) - Unleash.logger.info "value is an Array, operator is not an Array operator" - end + Unleash.logger.info "value is an Array, operator is not an Array operator" if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) end private From 3564be1e210f97d63963dba98fb5ad9625ce0d34 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 15:20:25 +0200 Subject: [PATCH 18/21] Update lib/unleash/constraint.rb Co-authored-by: Renato Arruda --- lib/unleash/constraint.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 74628c8b..319900fb 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -91,7 +91,7 @@ def validate_constraint_value_type(operator, value) if LIST_OPERATORS.include?(operator) && value.is_a?(String) Unleash.logger.info "value is not an Array, operator is expecting an Array" end - Unleash.logger.info "value is an Array, operator is not an Array operator" if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) + Unleash.logger.info "value is an Array, operator is expecting a String" if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) end private From d4ef1b05642eb8b8f381cfbc5ccf8e605280d451 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 15:20:47 +0200 Subject: [PATCH 19/21] Update lib/unleash/constraint.rb Co-authored-by: Renato Arruda --- lib/unleash/constraint.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 319900fb..8b220e3a 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -88,9 +88,7 @@ def self.on_valid_version(val1, val2) # This should be a private method but for some reason this fails on Ruby 2.5 def validate_constraint_value_type(operator, value) - if LIST_OPERATORS.include?(operator) && value.is_a?(String) - Unleash.logger.info "value is not an Array, operator is expecting an Array" - end + Unleash.logger.info "value is a String, operator is expecting an Array" if LIST_OPERATORS.include?(operator) && value.is_a?(String) Unleash.logger.info "value is an Array, operator is expecting a String" if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) end From 7f54e7491f8aeab01f0671b2383f00d02157a5a7 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Fri, 9 Sep 2022 15:38:38 +0200 Subject: [PATCH 20/21] change loglevel to warn and have test for logging it --- lib/unleash/constraint.rb | 4 ++-- spec/unleash/constraint_spec.rb | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/unleash/constraint.rb b/lib/unleash/constraint.rb index 8b220e3a..e2ed366b 100644 --- a/lib/unleash/constraint.rb +++ b/lib/unleash/constraint.rb @@ -88,8 +88,8 @@ def self.on_valid_version(val1, val2) # This should be a private method but for some reason this fails on Ruby 2.5 def validate_constraint_value_type(operator, value) - Unleash.logger.info "value is a String, operator is expecting an Array" if LIST_OPERATORS.include?(operator) && value.is_a?(String) - Unleash.logger.info "value is an Array, operator is expecting a String" if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) + Unleash.logger.warn "value is a String, operator is expecting an Array" if LIST_OPERATORS.include?(operator) && value.is_a?(String) + Unleash.logger.warn "value is an Array, operator is expecting a String" if !LIST_OPERATORS.include?(operator) && value.is_a?(Array) end private diff --git a/spec/unleash/constraint_spec.rb b/spec/unleash/constraint_spec.rb index 6b99335c..b26f13e5 100644 --- a/spec/unleash/constraint_spec.rb +++ b/spec/unleash/constraint_spec.rb @@ -404,5 +404,23 @@ constraint = Unleash::Constraint.new('env', 'NOT_A_VALID_OPERATOR', ['dev'], inverted: true) expect(constraint.matches_context?(context)).to be true end + + it 'warns about constraint construction for invalid value types for operator' do + array_constraints = ['STR_CONTAINS', 'STR_ENDS_WITH', 'STR_STARTS_WITH', 'IN', 'NOT_IN'] + + array_constraints.each do |operator_name| + Unleash::Constraint.new('env', operator_name, []) + expect(Unleash.logger).to receive(:warn).with("value is a String, operator is expecting an Array") + Unleash::Constraint.new('env', operator_name, '') + end + + string_constraints = ['NUM_EQ', 'NUM_GT', 'NUM_GTE', 'NUM_LT', 'NUM_LTE', + 'DATE_AFTER', 'DATE_BEFORE', 'SEMVER_EQ', 'SEMVER_GT', 'SEMVER_LT'] + string_constraints.each do |operator_name| + Unleash::Constraint.new('env', operator_name, '') + expect(Unleash.logger).to receive(:warn).with("value is an Array, operator is expecting a String") + Unleash::Constraint.new('env', operator_name, []) + end + end end end From 6a418d2fa40b1e6c082fb95e517bb262c3635625 Mon Sep 17 00:00:00 2001 From: Gard Rimestad Date: Mon, 12 Sep 2022 08:29:18 +0200 Subject: [PATCH 21/21] Update spec/unleash/constraint_spec.rb Co-authored-by: Renato Arruda --- spec/unleash/constraint_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/unleash/constraint_spec.rb b/spec/unleash/constraint_spec.rb index b26f13e5..9bdf0593 100644 --- a/spec/unleash/constraint_spec.rb +++ b/spec/unleash/constraint_spec.rb @@ -42,7 +42,7 @@ expect(constraint.matches_context?(context)).to be true end - it 'matches based on a value NOT_IN in empty context field' do + it 'matches based on a value NOT_IN in a not existing context field' do context_params = { properties: { }