Commit 0c66c67

mo <mo@mokhan.ca>
2019-05-29 01:10:49
use modified parse tree
1 parent 98c8b7b
Changed files (2)
app
models
spec
app/models/user.rb
@@ -15,11 +15,11 @@ class User < ApplicationRecord
 
   scope :scim_filter_for, -> (tree) do
     attr = SCIM::User::ATTRIBUTES[tree[:attribute].to_s] || tree[:attribute].to_s
-    case tree[:comparison_operator].to_s
+    case tree[:operator].to_s
     when 'eq'
-      where(attr => tree[:comparison_value].to_s[1..-2])
+      where(attr => tree[:value].to_s[1..-2])
     when 'ne'
-      where.not(attr => tree[:comparison_value].to_s[1..-2])
+      where.not(attr => tree[:value].to_s[1..-2])
     else
       self
     end
spec/models/user_spec.rb
@@ -13,22 +13,30 @@ RSpec.describe User do
 
   describe ".scim_filter_for" do
     let!(:users) { create_list(:user, 10) }
-    let(:random_user) { users.sample  }
+    let(:random_user) { users.sample }
+    let(:parser) { Scim::Kit::V2::Filter.new }
+
+    def tree_for(filter)
+      parser.parse(filter)
+    end
+
+    specify do
+      results = User.scim_filter_for(tree_for("userName eq \"#{random_user.email}\""))
+      expect(results).to match_array([random_user])
+    end
 
     specify do
-      expect(User.scim_filter_for(
-        attribute: "userName",
-        comparison_operator: "eq",
-        comparison_value: "\"#{random_user.email}\""
-      )).to match_array([random_user])
+      results = User.scim_filter_for(tree_for("userName ne \"#{random_user.email}\""))
+      expect(results.pluck(:email)).not_to include(random_user.email)
     end
 
     specify do
-      expect(User.scim_filter_for(
-        attribute: "userName",
-        comparison_operator: "ne",
-        comparison_value: "\"#{random_user.email}\""
-      ).pluck(:email)).not_to include(random_user.email)
+      first_user = users.sample
+      second_user = users.sample
+      parse_tree = tree_for(%(userName eq "#{first_user.email}" or userName eq "#{second_user.email}"))
+      puts parse_tree
+      results = User.scim_filter_for(parse_tree)
+      expect(results.pluck(:email)).to match_array([first_user.email, second_user.email])
     end
   end
 end