Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix specs related to widget set factories #7594

Merged
merged 7 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions spec/controllers/dashboard_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,27 +208,33 @@
end

context "Create Dashboard" do
let(:widget) { FactoryBot.create(:miq_widget) }

before do
EvmSpecHelper.local_miq_server
@group = FactoryBot.create(:miq_group, :miq_user_role => FactoryBot.create(:miq_user_role, :features => %w[everything]))
@user = FactoryBot.create(:user, :miq_groups => [@group])
# create dashboard for a group
@ws = FactoryBot.create(:miq_widget_set,
:name => "group_default",
:set_data => {:last_group_db_updated => Time.now.utc,
:col1 => [], :col2 => [], :col3 => []},
:set_data => {:col1 => [widget.id], :col2 => [], :col3 => [], :last_group_db_updated => Time.now.utc},
# :userid => @user.userid,
:group_id => @group.id)
:owner => @group)
@group.update(:settings => {:dashboard_order => [@ws.id]})
end

it "dashboard show" do
EvmSpecHelper.local_miq_server
controller.instance_variable_set(:@sb, :active_db => @ws.name)
controller.instance_variable_set(:@tabs, [])
login_as @user
# create a user's dashboard using group dashboard name.
FactoryBot.create(:miq_widget_set,
:name => "#{@user.userid}|#{@group.id}|#{@ws.name}",
:set_data => {:last_group_db_updated => Time.now.utc, :col1 => [1], :col2 => [], :col3 => []})
:name => @user.userid.to_s,
:owner => @user,
:group_id => @user.current_group_id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean we should fix the before validation in the model to work with users?
Thinking having a user_with_group may be what we want

:set_data => {:col1 => [widget.id], :col2 => [], :col3 => [], :last_group_db_updated => Time.now.utc})

controller.show
expect(controller.send(:flash_errors?)).not_to be_truthy
end
Expand Down Expand Up @@ -259,7 +265,8 @@
controller.show

# change original dashboard and set reset_upon_login flag to true
@ws.update(:set_data => {:last_group_db_updated => Time.now.utc, :reset_upon_login => true, :col1 => [], :col2 => [], :col3 => []})
@ws.set_data.merge(:last_group_db_updated => Time.now.utc, :reset_upon_login => true)
@ws.save!

# get user's copy of dashboard and add widgets
user_dashboard = MiqWidgetSet.find_by(:name => @ws.name, :userid => @user.userid)
Expand Down Expand Up @@ -329,27 +336,29 @@

let(:user) { FactoryBot.create(:user, :miq_groups => [group]) }

let(:widget) { FactoryBot.create(:miq_widget) }
let(:wset) do
FactoryBot.create(
:miq_widget_set,
:name => "Widgets",
:userid => user.userid,
:group_id => group.id,
:owner => group,
:set_data => {
:last_group_db_updated => Time.now.utc,
:col1 => [1], :col2 => [], :col3 => []
:col1 => [widget.id], :col2 => [], :col3 => []
}
)
end

before do
EvmSpecHelper.local_miq_server
login_as user

controller.params = {:tab => wset.id}
controller.instance_variable_set(
:@sb,
:active_db => wset.name, :active_db_id => wset.id,
:dashboards => { wset.name => {:col1 => [1], :col2 => [], :col3 => []} }
:dashboards => {wset.name => {:col1 => [widget.id], :col2 => [], :col3 => []}}
)

controller.show
Expand Down Expand Up @@ -461,20 +470,24 @@
context 'changing tabs' do
let(:group) { FactoryBot.create(:miq_group, :features => %w[everything]) }
let(:user) { FactoryBot.create(:user_admin, :current_group => group, :miq_groups => [group]) }
let(:widget) { FactoryBot.create(:miq_widget) }
let(:ws1) do
FactoryBot.create(:miq_widget_set,
:name => 'A',
:owner_id => group.id,
:set_data => {:col1 => [], :col2 => [], :col3 => []})
:set_data => {:col1 => [widget.id],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that a lot of these sets were incomplete which made me uncomfortable. That is where the smarter factory with encapsulation came into play. An alternative would be to put the setter into the actual model and treat these like fields (which is probably the correct schema/interface)

:col2 => [],
:col3 => []},
:owner => group)
end
let(:ws2) do
FactoryBot.create(:miq_widget_set,
:name => 'B',
:owner_id => group.id,
:set_data => {:col1 => [], :col2 => [], :col3 => []})
:set_data => {:col1 => [widget.id], :col2 => [], :col3 => []},
:owner => group)
end

before do
EvmSpecHelper.local_miq_server
login_as user
controller.params = {'uib-tab' => ws2.id.to_s}
controller.instance_variable_set(:@sb, {})
Expand Down
13 changes: 7 additions & 6 deletions spec/controllers/miq_report_controller/dashboards_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
describe ReportController do
context "::Dashboards" do
let(:miq_widget_set) { FactoryBot.create(:miq_widget_set, :owner => user.current_group, :set_data => {:col1 => [], :col2 => [], :col3 => []}) }
let(:miq_widget) { FactoryBot.create(:miq_widget) }
let(:miq_widget_set) { FactoryBot.create(:miq_widget_set, :owner => user.current_group, :set_data => {:col1 => [miq_widget.id], :col2 => [], :col3 => []}) }
let(:user) { FactoryBot.create(:user, :features => "db_edit") }

before do
Expand Down Expand Up @@ -102,9 +103,9 @@
allow(controller).to receive(:db_fields_validation)
allow(controller).to receive(:replace_right_cell)
owner = miq_widget_set.owner
new_hash = {:name => "New Name", :description => "New Description", :col1 => [1], :col2 => [], :col3 => []}
current = {:name => "New Name", :description => "New Description", :col1 => [], :col2 => [], :col3 => []}
controller.instance_variable_set(:@edit, :new => new_hash, :db_id => miq_widget_set.id, :current => current)
new_widget = FactoryBot.create(:miq_widget)
new_hash = {:name => "New Name", :description => "New Description", :col1 => [new_widget.id], :col2 => [], :col3 => []}
controller.instance_variable_set(:@edit, :new => new_hash, :db_id => miq_widget_set.id, :current => miq_widget_set.set_data)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasn't sure if this needed a deep clone. One thing is for certain, setting this to an invalid hash (with no widgets at all) was not productive

controller.params = {:id => miq_widget_set.id, :button => "save"}
controller.db_edit
expect(miq_widget_set.owner.id).to eq(owner.id)
Expand All @@ -115,7 +116,7 @@

describe "#db_save_members" do
let(:set_data) do
Array.new(3) { |n| ["col#{(n + 1)}".to_sym, Array.new(2, FactoryBot.create(:miq_widget))] }.to_h
Array.new(3) { |n| ["col#{(n + 1)}".to_sym, Array.new(2, FactoryBot.create(:miq_widget).id)] }.to_h
end

before do
Expand All @@ -132,7 +133,7 @@
controller.send(:db_save_members)

miq_widget_set.reload
expect(miq_widget_set.members.uniq).to match_array((set_data[:col1] + set_data[:col2] + set_data[:col3]).uniq)
expect(miq_widget_set.members.uniq.map(&:id)).to match_array((set_data[:col1] + set_data[:col2] + set_data[:col3]).uniq)
end
end

Expand Down
6 changes: 5 additions & 1 deletion spec/controllers/miq_report_controller/trees_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
end

it 'renders list of Dashboards in Dashboards tree' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not thrilled about these but had trouble seeding a widget set without valid data. I guess manually creating the default would have worked as well

MiqReport.seed
MiqWidget.seed
MiqWidgetSet.seed
post :tree_select, :params => { :id => 'root', :format => :js, :accord => 'db' }
expect(response).to render_template('report/_db_list')
Expand All @@ -107,11 +109,13 @@
expect(response.body).not_to include(other_group.name)
end

let(:miq_widget) { FactoryBot.create(:miq_widget) }

it 'renders show of Dashboards in Dashboards tree' do
ApplicationController.handle_exceptions = true

MiqWidgetSet.seed
widget_set = FactoryBot.create(:miq_widget_set, :group_id => user.current_group.id)
widget_set = FactoryBot.create(:miq_widget_set, :set_data => {:col1 => [miq_widget.id]}, :owner_id => user.current_group.id, :group_id => user.current_group.id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are a bunch of places where the owner_type is not set - for this reason the group was passed in. if you pass owner (or owner_id and owner_type) then group will get set correctly

post :tree_select, :params => { :id => "xx-g_g-#{user.current_group.id}_ws-#{widget_set.id}", :format => :js, :accord => 'db' }
expect(response).to render_template('report/_db_show')
end
Expand Down
4 changes: 3 additions & 1 deletion spec/controllers/report_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1191,8 +1191,10 @@
end

it "Can build all the trees" do
allow(User).to receive(:server_timezone).and_return("UTC")
EvmSpecHelper.local_miq_server
sb[:rep_tree_build_time] = Time.now.utc
MiqReport.seed
MiqWidget.seed
MiqWidgetSet.seed
user2 = create_user_with_group('User-2', "Group-1", MiqUserRole.find_by(:name => "EvmRole-operator"))
@rpt = create_and_generate_report_for_user("Vendor and Guest OS", user2)
Expand Down