From 73bcdc23634d22512751c96c6ceae6e8cf14a798 Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Mon, 22 Nov 2021 16:06:44 -0500 Subject: [PATCH 1/2] Allow for manual OPTIONS data validation The 'data' key in the response body can be simple or complex. You can now opt-out of generic data validation in the complex case by not providing the data. This lets you manually validate the data in the specs that know what part of the structure is important, while retaining the rest of the options expectations. --- spec/requests/querying_spec.rb | 2 +- spec/support/api/helpers.rb | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/spec/requests/querying_spec.rb b/spec/requests/querying_spec.rb index 52300d8ccc..79cfc554c4 100644 --- a/spec/requests/querying_spec.rb +++ b/spec/requests/querying_spec.rb @@ -966,7 +966,7 @@ def create_vms_by_name(names) describe 'OPTIONS /api/vms' do it 'returns the options information' do options(api_vms_url) - expect_options_results(:vms) + expect_options_results(:vms, {}) end end diff --git a/spec/support/api/helpers.rb b/spec/support/api/helpers.rb index d3c1248ac7..191c8d2ede 100644 --- a/spec/support/api/helpers.rb +++ b/spec/support/api/helpers.rb @@ -245,7 +245,7 @@ def expect_tagging_result(tag_results, status = :ok) end end - def expect_options_results(type, data = {}) + def expect_options_results(type, data = nil) klass = ::Api::ApiConfig.collections[type].klass.constantize attributes = select_attributes(klass.attribute_names - klass.virtual_attribute_names) reflections = (klass.reflections.keys | klass.virtual_reflections.keys.collect(&:to_s)).sort @@ -255,9 +255,18 @@ def expect_options_results(type, data = {}) 'virtual_attributes' => select_attributes(klass.virtual_attribute_names), 'relationships' => reflections, 'subcollections' => subcollections, - 'data' => data } - expect(response.parsed_body).to eq(expected) + + # Data can have complicated structure, so validating it is now optional. + # Basic data structures can be validated here if desired or not provided if + # validating will be done manually. + expected['data'] = data if data + + body = response.parsed_body + expected.each_key do |key| + expect(body[key]).to eq(expected[key]) + end + expect(response.headers['Access-Control-Allow-Methods']).to include('OPTIONS') end From 83c6c4882d3159f92cdc44416ba3f0caf5b39c51 Mon Sep 17 00:00:00 2001 From: Joe Rafaniello Date: Mon, 22 Nov 2021 16:10:04 -0500 Subject: [PATCH 2/2] Expose MiqEvent/EmsEvent group/levels in event stream OPTIONS The timeline_events key in the response body 'data' field contains information on the type of events, their descriptions, group levels, group names, etc. Currently, we only support EmsEvent and MiqEvent from event_streams. Part of #1090 --- .../api/event_streams_controller.rb | 9 +++++++++ spec/requests/event_streams_spec.rb | 20 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/app/controllers/api/event_streams_controller.rb b/app/controllers/api/event_streams_controller.rb index ce35ab7f52..3e2b16475e 100644 --- a/app/controllers/api/event_streams_controller.rb +++ b/app/controllers/api/event_streams_controller.rb @@ -1,4 +1,13 @@ module Api class EventStreamsController < BaseController + def options + render_options(@req.collection, build_additional_fields) + end + + private + + def build_additional_fields + {:timeline_events => EventStream.timeline_options} + end end end diff --git a/spec/requests/event_streams_spec.rb b/spec/requests/event_streams_spec.rb index 8f05a82b23..ab565f18e2 100644 --- a/spec/requests/event_streams_spec.rb +++ b/spec/requests/event_streams_spec.rb @@ -162,4 +162,24 @@ expect(response).to have_http_status(:forbidden) end end + + describe 'OPTIONS /api/event_streams' do + it 'returns expected and additional attributes' do + MiqEventDefinitionSet.seed + options(api_event_streams_url) + + expect_options_results(:event_streams) + + body = response.parsed_body + expect(body["data"].keys).to eq(["timeline_events"]) + expect(body["data"]["timeline_events"].keys.sort).to eq(%w[EmsEvent MiqEvent]) + expect(body["data"]["timeline_events"]["EmsEvent"].keys.sort).to eq(%w[description group_levels group_names]) + expect(body["data"]["timeline_events"]["EmsEvent"]["group_names"].keys.sort).to include("addition", "other") + expect(body["data"]["timeline_events"]["EmsEvent"]["group_levels"].keys.sort).to eq(%w[critical detail warning]) + + expect(body["data"]["timeline_events"]["MiqEvent"].keys.sort).to eq(%w[description group_levels group_names]) + expect(body["data"]["timeline_events"]["MiqEvent"]["group_names"].keys.sort).to include("auth_validation", "other") + expect(body["data"]["timeline_events"]["MiqEvent"]["group_levels"].keys.sort).to eq(%w[detail failure success]) + end + end end