From ff5f11f345307397d3d817f2bdef398d1660c22d Mon Sep 17 00:00:00 2001 From: Peter Amstutz Date: Tue, 25 Mar 2014 11:17:11 -0400 Subject: [PATCH] Added 'is_a' filter and tests, refs #2418 #2228 --- .../app/controllers/application_controller.rb | 16 +++++++++ services/api/app/models/arvados_model.rb | 21 ++++++++---- .../arvados/v1/links_controller_test.rb | 33 +++++++++++++++++++ 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/services/api/app/controllers/application_controller.rb b/services/api/app/controllers/application_controller.rb index e1b85aa891..979e2d1bfe 100644 --- a/services/api/app/controllers/application_controller.rb +++ b/services/api/app/controllers/application_controller.rb @@ -160,6 +160,22 @@ class ApplicationController < ActionController::Base cond_out << "#{table_name}.#{attr} IN (?)" param_out << operand end + when 'is_a' + operand = [operand] unless operand.is_a? Array + cond = [] + operand.each do |op| + m = op.match /arvados#(.+)/ + begin + cl = m[1].classify.andand.constantize if m + if cl + cond << "#{table_name}.#{attr} like ?" + param_out << "_____-#{cl.uuid_prefix}-_______________" + end + rescue NameError + cond << "1=0" + end + end + cond_out << cond.join(' OR ') end end if cond_out.any? diff --git a/services/api/app/models/arvados_model.rb b/services/api/app/models/arvados_model.rb index e6f5b36918..8ffd8819ea 100644 --- a/services/api/app/models/arvados_model.rb +++ b/services/api/app/models/arvados_model.rb @@ -187,6 +187,19 @@ class ArvadosModel < ActiveRecord::Base @@UUID_REGEX = /^[0-9a-z]{5}-([0-9a-z]{5})-[0-9a-z]{15}$/ + @@prefixes_hash = nil + def self.uuid_prefixes + unless @@prefixes_hash + @@prefixes_hash = {} + ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k| + if k.respond_to?(:uuid_prefix) + @@prefixes_hash[k.uuid_prefix] = k + end + end + end + @@prefixes_hash + end + def ensure_valid_uuids specials = [system_user_uuid, 'd41d8cd98f00b204e9800998ecf8427e+0'] @@ -221,13 +234,7 @@ class ArvadosModel < ActiveRecord::Base Rails.application.eager_load! uuid.match @@UUID_REGEX do |re| - ActiveRecord::Base.descendants.reject(&:abstract_class?).each do |k| - if k.respond_to?(:uuid_prefix) - if k.uuid_prefix == re[1] - return k - end - end - end + return uuid_prefixes[re[1]] if uuid_prefixes[re[1]] end nil end diff --git a/services/api/test/functional/arvados/v1/links_controller_test.rb b/services/api/test/functional/arvados/v1/links_controller_test.rb index 19e821edaa..32cf1e0066 100644 --- a/services/api/test/functional/arvados/v1/links_controller_test.rb +++ b/services/api/test/functional/arvados/v1/links_controller_test.rb @@ -58,4 +58,37 @@ class Arvados::V1::LinksControllerTest < ActionController::TestCase assert_response 422 end + test "filter links with 'is_a' operator" do + authorize_with :admin + get :index, { + filters: [ ['tail_uuid', 'is_a', 'arvados#user'] ] + } + assert_response :success + found = assigns(:objects) + assert_not_equal 0, found.count + assert_equal found.count, (found.select { |f| f.tail_uuid.match /[a-z0-9]{5}-tpzed-[a-z0-9]{15}/}).count + end + + test "filter links with 'is_a' operator with more than one" do + authorize_with :admin + get :index, { + filters: [ ['tail_uuid', 'is_a', ['arvados#user', 'arvados#group'] ] ], + } + assert_response :success + found = assigns(:objects) + assert_not_equal 0, found.count + assert_equal found.count, (found.select { |f| f.tail_uuid.match /[a-z0-9]{5}-(tpzed|j7d0g)-[a-z0-9]{15}/}).count + end + + test "filter links with 'is_a' operator with bogus type" do + authorize_with :admin + get :index, { + filters: [ ['tail_uuid', 'is_a', ['arvados#bogus'] ] ], + } + assert_response :success + found = assigns(:objects) + assert_equal 0, found.count + end + + end -- 2.30.2