From c58d1d4285234a88567a5f04310440fed82d9872 Mon Sep 17 00:00:00 2001 From: Brandon Dunne Date: Thu, 5 Jul 2018 17:31:14 -0400 Subject: [PATCH] Add ServiceTemplate#schedules subcollection https://bugzilla.redhat.com/show_bug.cgi?id=1564255 --- .../api/service_templates_controller.rb | 4 ++ config/api.yml | 1 + spec/requests/service_templates_spec.rb | 66 +++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/app/controllers/api/service_templates_controller.rb b/app/controllers/api/service_templates_controller.rb index c42f2f7dde..5409c76061 100644 --- a/app/controllers/api/service_templates_controller.rb +++ b/app/controllers/api/service_templates_controller.rb @@ -46,6 +46,10 @@ def unarchive_resource(type, id, _data) action_result(false, "Could not unarchive Service Template - #{err}") end + def schedules_query_resource(object) + object.miq_schedules + end + private def set_additional_attributes diff --git a/config/api.yml b/config/api.yml index 4868a29aea..2efcf50bf5 100644 --- a/config/api.yml +++ b/config/api.yml @@ -2806,6 +2806,7 @@ :subcollections: - :resource_actions - :tags + - :schedules - :service_requests - :service_dialogs :collection_actions: diff --git a/spec/requests/service_templates_spec.rb b/spec/requests/service_templates_spec.rb index a70168b37b..5f68ddf112 100644 --- a/spec/requests/service_templates_spec.rb +++ b/spec/requests/service_templates_spec.rb @@ -690,4 +690,70 @@ expect(response.parsed_body).to include(expected) end end + + context "schedules subcollection" do + let!(:service_template) { FactoryGirl.create(:service_template, :with_provision_resource_action_and_dialog) } + + context "with schedules" do + let!(:schedule_1) { FactoryGirl.create(:miq_schedule, :towhat => "ServiceTemplate", :resource_id => service_template.id) } + let!(:schedule_2) { FactoryGirl.create(:miq_schedule, :towhat => "ServiceTemplate", :resource_id => service_template.id) } + + it "can fetch all related schedules" do + api_basic_authorize subcollection_action_identifier(:service_templates, :schedules, :read, :get) + + get(api_service_template_schedules_url(nil, service_template)) + + expect_result_resources_to_include_hrefs( + "resources", + [ + api_service_template_schedule_url(nil, service_template, schedule_1), + api_service_template_schedule_url(nil, service_template, schedule_2), + ] + ) + expect(response).to have_http_status(:ok) + end + + it "will not show the schedules without the appropriate role" do + api_basic_authorize + + get(api_service_template_schedules_url(nil, service_template)) + + expect(response).to have_http_status(:forbidden) + end + + it "can show a single schedule" do + api_basic_authorize subcollection_action_identifier(:service_templates, :schedules, :read, :get) + + get(api_service_template_schedule_url(nil, service_template, schedule_1)) + + expect_result_to_match_hash( + response.parsed_body, + "href" => api_service_template_schedule_url(nil, service_template, schedule_1), + "id" => schedule_1.id.to_s, + ) + expect(response).to have_http_status(:ok) + end + + it "will not show a schedule without the appropriate role" do + api_basic_authorize + + get(api_service_template_schedule_url(nil, service_template, schedule_1)) + + expect(response).to have_http_status(:forbidden) + end + end + + it "without any schedules" do + api_basic_authorize subcollection_action_identifier(:service_templates, :schedules, :read, :get) + + get(api_service_template_schedules_url(nil, service_template)) + + expect(response.parsed_body).to include( + "name" => "schedules", + "resources" => [], + "subcount" => 0 + ) + expect(response).to have_http_status(:ok) + end + end end