Project

General

Profile

« Previous | Next » 

Revision d4dff460

Added by Joseph Magen about 10 years ago

fixes #5506 - added scoped search to SmartProxy for UI and API v2

(cherry picked from commit 428b1cfb6f6e19294756078ee70d862a36a566de)

View differences:

app/controllers/api/v2/smart_proxies_controller.rb
include Api::TaxonomyScope
include Api::ImportPuppetclassesCommonController
before_filter :find_resource, :only => %w{show update destroy refresh}
before_filter :check_feature_type, :only => :index
api :GET, "/smart_proxies/", "List all smart_proxies."
param :type, String, :desc => "filter by type"
param :search, String, :desc => "Filter results"
param :order, String, :desc => "Sort results"
param :page, String, :desc => "paginate results"
param :per_page, String, :desc => "number of entries per request"
def index
@smart_proxies = proxies_by_type(params[:type]).paginate(paginate_options)
@subtotal = @smart_proxies.count
@smart_proxies = SmartProxy.authorized(:view_smart_proxies).includes(:features).
search_for(*search_options).paginate(paginate_options)
@total = SmartProxy.authorized(:view_smart_proxies).includes(:features).count
end
api :GET, "/smart_proxies/:id/", "Show a smart proxy."
......
end
private
def proxies_by_type(type = nil)
return SmartProxy.authorized(:view_smart_proxies).includes(:features).with_features(type) if type.present?
return SmartProxy.authorized(:view_smart_proxies).includes(:features).scoped
end
def action_permission
case params[:action]
......
end
end
def check_feature_type
return if params[:type].nil?
allowed_types = Feature.name_map.keys
if not allowed_types.include? params[:type].downcase
raise ArgumentError, "Invalid feature type. Select one of: #{allowed_types.join(", ")}."
end
end
end
end
end
app/controllers/smart_proxies_controller.rb
before_filter :find_by_name, :only => [:edit, :update, :refresh, :ping, :destroy]
def index
@smart_proxies = resource_base.includes(:features).paginate :page => params[:page]
@smart_proxies = resource_base.includes(:features).search_for(params[:search], :order => params[:order]).paginate(:page => params[:page])
end
def new
app/models/smart_proxy.rb
before_save :sanitize_url, :associate_features
scoped_search :on => :name, :complete_value => :true
scoped_search :on => :url, :complete_value => :true
scoped_search :in => :features, :on => :name, :rename => :feature, :complete_value => :true
# with proc support, default_scope can no longer be chained
# include all default scoping here
......
where(:features => { :name => v }).joins(:features)
}
end
scope :with_features, lambda {|*feature_names| where(:features => { :name => feature_names }).joins(:features) }
scope :with_features, lambda {|*feature_names| where(:features => { :name => feature_names }).joins(:features) if feature_names.any? }
def hostname
# This will always match as it is validated
test/functional/api/v2/smart_proxies_controller_test.rb
assert !smart_proxies.empty?
end
test "should get index filtered by type" do
as_user :admin do
get :index, { :type => 'TFTP' }
end
test "should get index filtered by feature" do
get :index, { :search => "feature=TFTP" }
assert_response :success
assert_not_nil assigns(:smart_proxies)
refute_empty assigns(:smart_proxies)
smart_proxies = ActiveSupport::JSON.decode(@response.body)
assert !smart_proxies.empty?
refute_empty smart_proxies
returned_proxy_ids = smart_proxies['results'].map { |p| p["id"] }
expected_proxy_ids = SmartProxy.with_features("TFTP").map { |p| p.id }
assert returned_proxy_ids == expected_proxy_ids
assert_equal expected_proxy_ids, returned_proxy_ids
end
test "index should fail with invalid type filter" do
as_user :admin do
get :index, { :type => 'unknown_type' }
end
assert_response :error
test "should get index filtered by name" do
get :index, { :search => "name=\"TFTP Proxy\"" }
assert_response :success
refute_empty assigns(:smart_proxies)
smart_proxies = ActiveSupport::JSON.decode(@response.body)
refute_empty smart_proxies
returned_proxy_ids = smart_proxies['results'].map { |p| p["id"] }
expected_proxy_ids = SmartProxy.with_features("TFTP").map { |p| p.id }
assert_equal expected_proxy_ids, returned_proxy_ids
end
test "should show individual record" do
test/functional/smart_proxies_controller_test.rb
assert_equal "Unable to communicate with the proxy: it's down", flash[:error]
end
test "should search by name" do
get :index, { :search => "name=\"DNS Proxy\"" }, set_session_user
assert_response :success
refute_empty assigns(:smart_proxies)
assert assigns(:smart_proxies).include?(smart_proxies(:three))
end
test "should search by feature" do
get :index, { :search => "feature=DNS" }, set_session_user
assert_response :success
refute_empty assigns(:smart_proxies)
assert assigns(:smart_proxies).include?(smart_proxies(:three))
end
end

Also available in: Unified diff