diff --git a/spec/models/service_template_spec.rb b/spec/models/service_template_spec.rb index 542d2fadca2..4d410c6c9ba 100644 --- a/spec/models/service_template_spec.rb +++ b/spec/models/service_template_spec.rb @@ -82,6 +82,65 @@ expect(service_template.custom_actions).to match(expected) end + context "expression evaluation" do + let(:service_template) { FactoryGirl.create(:service_template, :prov_type=> "vmware") } + let(:service) { FactoryGirl.create(:service, :name => "foo", :service_template => service_template) } + let(:true_expression_on_template) do + MiqExpression.new("=" => {"field" => "ServiceTemplate-prov_type", "value" => "vmware"}) + end + let(:false_expression_on_template) do + MiqExpression.new("=" => {"field" => "ServiceTemplate-prov_type", "value" => "not_vmware"}) + end + let(:true_expression_on_service) do + MiqExpression.new("=" => {"field" => "Service-name", "value" => "foo"}) + end + let(:false_expression_on_service) do + MiqExpression.new("=" => {"field" => "Service-name", "value" => "not_foo"}) + end + + before do + FactoryGirl.create(:custom_button, + :name => "visible button on service", + :applies_to_class => "Service", + :visibility_expression => true_expression_on_service) + FactoryGirl.create(:custom_button, + :name => "hidden button on service", + :applies_to_class => "Service", + :visibility_expression => false_expression_on_service) + FactoryGirl.create(:custom_button, + :name => "visible button on template", + :applies_to_class => "ServiceTemplate", + :applies_to_id => service_template.id, + :visibility_expression => true_expression_on_template) + FactoryGirl.create(:custom_button, + :name => "hidden visible button on template", + :applies_to_class => "ServiceTemplate", + :applies_to_id => service_template.id, + :visibility_expression => false_expression_on_template) + end + + it "uses ServiceTemplate object to evaluate expression defined on Service Template if no parameter passed" do + expected = { + :buttons => [ + a_hash_including("name" => "visible button on template") + ], + :button_groups => [] + } + expect(service_template.custom_actions).to match(expected) + end + + it "uses passed object for expression defined on that object and ServiceTemplate for expression on template" do + expected = { + :buttons => a_collection_containing_exactly( + a_hash_including("name" => "visible button on service"), + a_hash_including("name" => "visible button on template") + ), + :button_groups => [] + } + expect(service_template.custom_actions(service)).to match(expected) + end + end + it "serializes the enablement" do service_template = FactoryGirl.create(:service_template, :name => "foo") true_expression = MiqExpression.new("=" => {"field" => "ServiceTemplate-name", "value" => "foo"})