Commit a652a631 authored by James Lopez's avatar James Lopez

Fix parser and add more specs

parent ac4f3c52
...@@ -4,30 +4,48 @@ module EE ...@@ -4,30 +4,48 @@ module EE
module Gitlab module Gitlab
module Scim module Scim
class ParamsParser class ParamsParser
FILTER_OPERATORS = %w[eq] FILTER_OPERATORS = %w[eq].freeze
OPERATIONS_OPERATORS = %w[Replace Add] OPERATIONS_OPERATORS = %w[Replace Add].freeze
ATTRIBUTE_MAP = { ATTRIBUTE_MAP = {
id: :extern_uid, id: :extern_uid,
'name.formatted': :name, 'name.formatted': :name,
'emails[type eq "work".value': :mail 'emails[type eq "work".value': :mail,
active: :active
}.with_indifferent_access }.with_indifferent_access
COERCED_VALUES = {
'True' => true,
'False' => false
}.freeze
def initialize(params) def initialize(params)
@filter = params[:filter] @filter = params[:filter]
@operations = params[:operations] @operations = params[:operations]
@hash = {} end
def deprovision_user?
hash[:active] == false
end end
def to_hash def to_hash
process_filter @hash ||=
process_operations begin
hash = {}
process_filter(hash)
process_operations(hash)
@hash hash
end
end end
alias_method :hash, :to_hash
private :hash
private private
def process_filter def process_filter(hash)
return unless @filter return unless @filter
attribute, operator, value = @filter.split attribute, operator, value = @filter.split
...@@ -35,20 +53,26 @@ module EE ...@@ -35,20 +53,26 @@ module EE
return unless FILTER_OPERATORS.include?(operator) return unless FILTER_OPERATORS.include?(operator)
return unless ATTRIBUTE_MAP[attribute] return unless ATTRIBUTE_MAP[attribute]
@hash[ATTRIBUTE_MAP[attribute]] = value.tr('\"', '') hash[ATTRIBUTE_MAP[attribute]] = coerce(value.tr('\"', ''))
end end
def process_operations def process_operations(hash)
return unless @operations return unless @operations
@operations.each do |operation| @operations.each do |operation|
next unless OPERATIONS_OPERATORS.contains?(operation[:op]) next unless OPERATIONS_OPERATORS.include?(operation[:op])
attribute = ATTRIBUTE_MAP[operation[:path]] attribute = ATTRIBUTE_MAP[operation[:path]]
@hash[attribute] = operation[:value] if attribute hash[attribute] = coerce(operation[:value]) if attribute
end end
end end
def coerce(value)
coerced = COERCED_VALUES[value]
coerced.nil? ? value : coerced
end
end end
end end
end end
......
...@@ -9,5 +9,37 @@ describe EE::Gitlab::Scim::ParamsParser do ...@@ -9,5 +9,37 @@ describe EE::Gitlab::Scim::ParamsParser do
expect(described_class.new(filter: filter).to_hash).to eq(extern_uid: '6ba81b08-77da') expect(described_class.new(filter: filter).to_hash).to eq(extern_uid: '6ba81b08-77da')
end 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
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