Skip to content

Commit

Permalink
Merge pull request #1099 from jrafanie/event_stream_options_for_api
Browse files Browse the repository at this point in the history
Expose MiqEvent/EmsEvent group/levels in event stream OPTIONS
  • Loading branch information
Fryguy authored Nov 22, 2021
2 parents fb2dabd + 83c6c48 commit 298f098
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
9 changes: 9 additions & 0 deletions app/controllers/api/event_streams_controller.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions spec/requests/event_streams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/requests/querying_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
15 changes: 12 additions & 3 deletions spec/support/api/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 298f098

Please sign in to comment.