From 29d21371d699ed08028c1e34d8d06b7a7116cbaa Mon Sep 17 00:00:00 2001 From: lpichler Date: Mon, 26 Feb 2018 17:22:15 +0100 Subject: [PATCH] Move chargeback code to the method and then move this method to the building screen it will refresh @edit[:cb_users] and @edit[:cb_tenant] when tab filter is pressed. This change also wrapping tenant and user list by RBAC. --- .../report_controller/reports/editor.rb | 11 +--------- app/helpers/report_helper/editor.rb | 5 +++++ .../reports/editor_spec.rb | 20 +++++++++++++++++-- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/app/controllers/report_controller/reports/editor.rb b/app/controllers/report_controller/reports/editor.rb index 8287923ec59..4ee765270b5 100644 --- a/app/controllers/report_controller/reports/editor.rb +++ b/app/controllers/report_controller/reports/editor.rb @@ -187,6 +187,7 @@ def build_edit_screen get_time_profiles # Get time profiles list (global and user specific) cb_entities_by_provider if Chargeback.db_is_chargeback?(@edit[:new][:model]) && [ChargebackContainerImage, ChargebackContainerProject, MeteringContainerImage, MeteringContainerProject].include?(@edit[:new][:model].safe_constantize) + refresh_chargeback_filter_tab if Chargeback.db_is_chargeback?(@edit[:new][:model]) case @sb[:miq_tab].split("_")[1] when "1" # Select columns @edit[:models] ||= reportable_models @@ -1331,16 +1332,6 @@ def set_form_vars cb_entities_by_provider if [ChargebackContainerImage, ChargebackContainerProject, MeteringContainerImage, MeteringContainerProject].include?(@rpt.db.safe_constantize) end - # Only show chargeback users choice if an admin - if admin_user? - @edit[:cb_users] = User.all.each_with_object({}) { |u, h| h[u.userid] = u.name } - @edit[:cb_tenant] = Tenant.all.each_with_object({}) { |t, h| h[t.id] = t.name } - else - @edit[:new][:cb_show_typ] = "owner" - @edit[:new][:cb_owner_id] = session[:userid] - @edit[:cb_owner_name] = current_user.name - end - # Build trend limit cols array if model_report_type(@rpt.db) == :trend @edit[:limit_cols] = VimPerformanceTrend.trend_limit_cols(@edit[:new][:perf_trend_db], @edit[:new][:perf_trend_col], @edit[:new][:perf_interval]) diff --git a/app/helpers/report_helper/editor.rb b/app/helpers/report_helper/editor.rb index 21f750b2665..268504b4d68 100644 --- a/app/helpers/report_helper/editor.rb +++ b/app/helpers/report_helper/editor.rb @@ -13,4 +13,9 @@ def cb_entities_by_provider_id(provider_id, entity_type) def cb_image_labels CustomAttribute.where(:section => "docker_labels").distinct('name').pluck(:name).each_with_object({}) { |l, h| h[l] = l } end + + def refresh_chargeback_filter_tab + @edit[:cb_users] = Rbac::Filterer.filtered(User.in_my_region).each_with_object({}) { |u, h| h[u.userid] = u.name } + @edit[:cb_tenant] = Rbac::Filterer.filtered(Tenant).each_with_object({}) { |t, h| h[t.id] = t.name } + end end diff --git a/spec/controllers/miq_report_controller/reports/editor_spec.rb b/spec/controllers/miq_report_controller/reports/editor_spec.rb index 698ecad9aad..5ba82efbaac 100644 --- a/spec/controllers/miq_report_controller/reports/editor_spec.rb +++ b/spec/controllers/miq_report_controller/reports/editor_spec.rb @@ -270,13 +270,24 @@ def empty_category(attrs = {}) chargeback_tabs = [:formatting, :filter, :preview] describe '#build_edit_screen' do - let(:user) { FactoryGirl.create(:user) } + let(:default_tenant) { Tenant.seed } + let(:user) { FactoryGirl.create(:user_with_group, :tenant => default_tenant) } + let(:user_group) do + group = user.miq_groups.first + group.tenant = default_tenant + group.save + group + end + let(:chargeback_report) do FactoryGirl.create(:miq_report, :db => 'ChargebackVm', :db_options => {:options => {:owner => user.userid}}, :col_order => ['name'], :headers => ['Name']) end - before { login_as user } + before do + EvmSpecHelper.create_guid_miq_server_zone + login_as user + end tabs.slice(*chargeback_tabs).each do |tab_number| it 'flash messages should be nil' do @@ -285,6 +296,11 @@ def empty_category(attrs = {}) controller.instance_variable_set(:@sb, :miq_tab => "edit_#{tab_number.second}") controller.send(:build_edit_screen) + if tab_number == [:filter, 3] + expect(controller.instance_variable_get(:@edit)[:cb_users]).to eq(user.userid => user.name) + expect(controller.instance_variable_get(:@edit)[:cb_tenant]).to eq(default_tenant.id => default_tenant.name) + end + expect(assigns(:flash_array)).to be_nil end end