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

Policy Event must have at least one action assigned to it #112

Merged
merged 1 commit into from Jan 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/controllers/miq_policy_controller/events.rb
Expand Up @@ -32,6 +32,12 @@ def event_edit
event = MiqEventDefinition.find(@event.id) # Get event record
action_list = @edit[:new][:actions_true].collect { |a| [MiqAction.find(a.last), {:qualifier => :success, :synchronous => a[1]}] } +
@edit[:new][:actions_false].collect { |a| [MiqAction.find(a.last), {:qualifier => :failure, :synchronous => a[1]}] }
add_flash(_("At least one action must be selected to save this Policy Event"), :error) if action_list.blank?
if @flash_array
javascript_flash
return
end

policy.replace_actions_for_event(event, action_list)
AuditEvent.success(build_saved_audit(event))
add_flash(_("Actions for Policy Event \"%{events}\" were saved") % {:events => event.description})
Expand Down
61 changes: 61 additions & 0 deletions spec/controllers/miq_policy_controller/events_spec.rb
@@ -0,0 +1,61 @@
describe MiqPolicyController do
context "::Events" do
context "#event_edit" do
before :each do
stub_user(:features => :all)
@action = FactoryGirl.create(:miq_action, :name => "compliance_failed")
@event = FactoryGirl.create(:miq_event_definition, :name => "vm_compliance_check")
@policy = FactoryGirl.create(:miq_policy, :name => "Foo")

controller.instance_variable_set(:@sb,
:node_ids => {
:policy_tree => {"p" => @policy.id}
},
:active_tree => :policy_tree
)
allow(controller).to receive(:replace_right_cell)
end

it "saves Policy Event with an action" do
new_hash = {
:name => "New Name",
:description => "New Description",
:actions_true => [[@action.name, true, @action.id]],
:actions_false => []
}
edit = {
:new => new_hash,
:current => new_hash,
:key => "event_edit__#{@event.id}",
:event_id => @event.id
}
controller.instance_variable_set(:@edit, edit)
session[:edit] = edit
controller.instance_variable_set(:@_params, :id => @event.id.to_s, :button => "save")
controller.event_edit
expect(@policy.actions_for_event(@event, :success)).to include(@action)
end

it "does not allow to save Policy Event without an action" do
new_hash = {
:name => "New Name",
:description => "New Description",
:actions_true => [],
:actions_false => []
}
edit = {
:new => new_hash,
:current => new_hash,
:key => "event_edit__#{@event.id}",
:event_id => @event.id
}
controller.instance_variable_set(:@edit, edit)
session[:edit] = edit
controller.instance_variable_set(:@_params, :id => @event.id.to_s, :button => "save")
expect(controller).to receive(:render)
controller.event_edit
expect(assigns(:flash_array).first[:message]).to include("At least one action must be selected to save this Policy Event")
end
end
end
end