Commit a652a631 authored by James Lopez's avatar James Lopez

Fix parser and add more specs

parent ac4f3c52
......@@ -4,30 +4,48 @@ module EE
module Gitlab
module Scim
class ParamsParser
FILTER_OPERATORS = %w[eq]
OPERATIONS_OPERATORS = %w[Replace Add]
FILTER_OPERATORS = %w[eq].freeze
OPERATIONS_OPERATORS = %w[Replace Add].freeze
ATTRIBUTE_MAP = {
id: :extern_uid,
'name.formatted': :name,
'emails[type eq "work".value': :mail
'emails[type eq "work".value': :mail,
active: :active
}.with_indifferent_access
COERCED_VALUES = {
'True' => true,
'False' => false
}.freeze
def initialize(params)
@filter = params[:filter]
@operations = params[:operations]
@hash = {}
end
def deprovision_user?
hash[:active] == false
end
def to_hash
process_filter
process_operations
@hash ||=
begin
hash = {}
process_filter(hash)
process_operations(hash)
@hash
hash
end
end
alias_method :hash, :to_hash
private :hash
private
def process_filter
def process_filter(hash)
return unless @filter
attribute, operator, value = @filter.split
......@@ -35,20 +53,26 @@ module EE
return unless FILTER_OPERATORS.include?(operator)
return unless ATTRIBUTE_MAP[attribute]
@hash[ATTRIBUTE_MAP[attribute]] = value.tr('\"', '')
hash[ATTRIBUTE_MAP[attribute]] = coerce(value.tr('\"', ''))
end
def process_operations
def process_operations(hash)
return unless @operations
@operations.each do |operation|
next unless OPERATIONS_OPERATORS.contains?(operation[:op])
next unless OPERATIONS_OPERATORS.include?(operation[:op])
attribute = ATTRIBUTE_MAP[operation[:path]]
@hash[attribute] = operation[:value] if attribute
hash[attribute] = coerce(operation[:value]) if attribute
end
end
def coerce(value)
coerced = COERCED_VALUES[value]
coerced.nil? ? value : coerced
end
end
end
end
......
......@@ -9,5 +9,37 @@ describe EE::Gitlab::Scim::ParamsParser do
expect(described_class.new(filter: filter).to_hash).to eq(extern_uid: '6ba81b08-77da')
end
it 'returns an empty hash for the wrong filter' do
filter = 'blah eq "6ba81b08-77da"'
expect(described_class.new(filter: filter).to_hash).to eq({})
end
it 'returns the correct operation attributes' do
operations = [{ "op": "Replace", "path": "active", "value": "False" }]
expect(described_class.new(operations: operations).to_hash).to eq(active: false)
end
it 'returns an empty hash for the wrong operations' do
operations = [{ "op": "Replace", "path": "test", "value": "False" }]
expect(described_class.new(operations: operations).to_hash).to eq({})
end
end
describe '#deprovision_user?' do
it 'returns true when deprovisioning' do
operations = [{ "op": "Replace", "path": "active", "value": "False" }]
expect(described_class.new(operations: operations).deprovision_user?).to be true
end
it 'returns an empty hash for the wrong operations' do
operations = [{ "op": "Replace", "path": "active", "value": "True" }]
expect(described_class.new(operations: operations).deprovision_user?).to be false
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment