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

Use Rails path helpers for all routes #40

Merged
merged 1 commit into from
Sep 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,34 @@

Api::ApiConfig.collections.each do |collection_name, collection|
# OPTIONS action for each collection
match collection_name.to_s, :controller => collection_name, :action => :options, :via => :options
match collection_name.to_s, :controller => collection_name, :action => :options, :via => :options, :as => nil

scope collection_name, :controller => collection_name do
collection.verbs.each do |verb|
root :action => Api::VERBS_ACTIONS_MAP[verb], :via => verb if collection.options.include?(:primary)
if collection.options.include?(:primary)
case verb
when :get
root :action => Api::VERBS_ACTIONS_MAP[verb], :as => collection_name
else
root :action => Api::VERBS_ACTIONS_MAP[verb], :via => verb
end
end

next unless collection.options.include?(:collection)

if collection.options.include?(:arbitrary_resource_path)
case verb
when :get
root :action => :index
get "/*c_suffix", :action => :show
root :action => :index, :as => collection_name.to_s.pluralize
get "/*c_suffix", :action => :show, :as => collection_name.to_s.singularize
else
match "(/*c_suffix)", :action => Api::VERBS_ACTIONS_MAP[verb], :via => verb
end
else
case verb
when :get
root :action => :index
get "/:c_id", :action => :show
root :action => :index, :as => collection_name
get "/:c_id", :action => :show, :as => collection_name.to_s.singularize
when :put
put "/:c_id", :action => :update
when :patch
Expand All @@ -50,8 +57,8 @@
Api::ApiConfig.collections[subcollection_name].verbs.each do |verb|
case verb
when :get
get "/:c_id/#{subcollection_name}", :action => :index
get "/:c_id/#{subcollection_name}/:s_id", :action => :show
get "/:c_id/#{subcollection_name}", :action => :index, :as => "#{collection_name.to_s.singularize}_#{subcollection_name.to_s.pluralize}"
get "/:c_id/#{subcollection_name}/:s_id", :action => :show, :as => "#{collection_name.to_s.singularize}_#{subcollection_name.to_s.singularize}"
when :put
put "/:c_id/#{subcollection_name}/:s_id", :action => :update
when :patch
Expand Down
24 changes: 12 additions & 12 deletions spec/requests/actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@
:options => {:ae_message => "message", :ae_request => "request", :ae_hash => {"key"=>"value"}}
}
end
let(:action_url) { actions_url(action.id) }
let(:action_url) { api_action_url(nil, action) }

it "forbids access to actions without an appropriate role" do
action
api_basic_authorize

run_get(actions_url)
run_get(api_actions_url)

expect(response).to have_http_status(:forbidden)
end

it "creates new action" do
api_basic_authorize collection_action_identifier(:actions, :create)
run_post(actions_url, sample_action)
run_post(api_actions_url, sample_action)

expect(response).to have_http_status(:ok)

Expand All @@ -34,7 +34,7 @@

it "creates new actions" do
api_basic_authorize collection_action_identifier(:actions, :create)
run_post(actions_url, gen_request(:create, [sample_action,
run_post(api_actions_url, gen_request(:create, [sample_action,
sample_action.merge(:name => "foo", :description => "bar")]))
expect(response).to have_http_status(:ok)

Expand All @@ -44,7 +44,7 @@
it "reads all actions" do
api_basic_authorize collection_action_identifier(:actions, :read, :get)
FactoryGirl.create(:miq_action)
run_get(actions_url)
run_get(api_actions_url)
expect(response).to have_http_status(:ok)

actions_amount = response.parsed_body["count"]
Expand All @@ -54,7 +54,7 @@

it "deletes action" do
api_basic_authorize collection_action_identifier(:actions, :delete)
run_post(actions_url, gen_request(:delete, "name" => action.name, "href" => action_url))
run_post(api_actions_url, gen_request(:delete, "name" => action.name, "href" => action_url))

expect(response).to have_http_status(:ok)

Expand All @@ -63,10 +63,10 @@

it "deletes actions" do
api_basic_authorize collection_action_identifier(:actions, :delete)
run_post(actions_url, gen_request(:delete, [{"name" => actions.first.name,
"href" => actions_url(actions.first.id)},
{"name" => actions.second.name,
"href" => actions_url(actions.second.id)}]))
run_post(api_actions_url, gen_request(:delete, [{"name" => actions.first.name,
"href" => api_action_url(nil, actions.first)},
{"name" => actions.second.name,
"href" => api_action_url(nil, actions.second)}]))

expect(response).to have_http_status(:ok)

Expand All @@ -76,7 +76,7 @@
it "deletes action via DELETE" do
api_basic_authorize collection_action_identifier(:actions, :delete)

run_delete(actions_url(action.id))
run_delete(api_action_url(nil, action))

expect(response).to have_http_status(:no_content)
expect(MiqAction.exists?(action.id)).to be_falsey
Expand All @@ -93,7 +93,7 @@

it "edits new actions" do
api_basic_authorize collection_action_identifier(:actions, :edit)
run_post(actions_url, gen_request(:edit, [{"id" => actions.first.id, "description" => "change"},
run_post(api_actions_url, gen_request(:edit, [{"id" => actions.first.id, "description" => "change"},
{"id" => actions.second.id, "description" => "change2"}]))
expect(response).to have_http_status(:ok)

Expand Down
64 changes: 32 additions & 32 deletions spec/requests/alert_definitions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@
describe "Alerts Definitions API" do
it "forbids access to alert definitions list without an appropriate role" do
api_basic_authorize
run_get(alert_definitions_url)
run_get(api_alert_definitions_url)
expect(response).to have_http_status(:forbidden)
end

it "reads 2 alert definitions as a collection" do
api_basic_authorize collection_action_identifier(:alert_definitions, :read, :get)
alert_definitions = FactoryGirl.create_list(:miq_alert, 2)
run_get(alert_definitions_url)
run_get(api_alert_definitions_url)
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(
"name" => "alert_definitions",
"count" => 2,
"subcount" => 2,
"resources" => a_collection_containing_exactly(
{
"href" => a_string_matching(alert_definitions_url(alert_definitions[0].compressed_id))
"href" => api_alert_definition_url(nil, alert_definitions[0].compressed_id)
},
{
"href" => a_string_matching(alert_definitions_url(alert_definitions[1].compressed_id))
"href" => api_alert_definition_url(nil, alert_definitions[1].compressed_id)
}
)
)
Expand All @@ -36,7 +36,7 @@
it "forbids access to an alert definition resource without an appropriate role" do
api_basic_authorize
alert_definition = FactoryGirl.create(:miq_alert)
run_get(alert_definitions_url(alert_definition.id))
run_get(api_alert_definition_url(nil, alert_definition))
expect(response).to have_http_status(:forbidden)
end

Expand All @@ -46,10 +46,10 @@
:miq_alert,
:miq_expression => MiqExpression.new("=" => {"field" => "Vm-name", "value" => "foo"})
)
run_get(alert_definitions_url(alert_definition.id))
run_get(api_alert_definition_url(nil, alert_definition))
expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(
"href" => a_string_matching(alert_definitions_url(alert_definition.compressed_id)),
"href" => api_alert_definition_url(nil, alert_definition.compressed_id),
"id" => alert_definition.compressed_id,
"description" => alert_definition.description,
"guid" => alert_definition.guid,
Expand All @@ -63,7 +63,7 @@
"description" => "Test Alert Definition",
"db" => "ContainerNode"
}
run_post(alert_definitions_url, alert_definition)
run_post(api_alert_definitions_url, alert_definition)
expect(response).to have_http_status(:forbidden)
end

Expand All @@ -76,7 +76,7 @@
"enabled" => true
}
api_basic_authorize collection_action_identifier(:alert_definitions, :create)
run_post(alert_definitions_url, sample_alert_definition)
run_post(api_alert_definitions_url, sample_alert_definition)
expect(response).to have_http_status(:ok)
alert_definition = MiqAlert.find(ApplicationRecord.uncompress_id(response.parsed_body["results"].first["id"]))
expect(alert_definition).to be_truthy
Expand All @@ -94,25 +94,25 @@
it "deletes an alert definition via POST" do
api_basic_authorize action_identifier(:alert_definitions, :delete, :resource_actions, :post)
alert_definition = FactoryGirl.create(:miq_alert)
run_post(alert_definitions_url(alert_definition.id), gen_request(:delete))
run_post(api_alert_definition_url(nil, alert_definition), gen_request(:delete))
expect(response).to have_http_status(:ok)
expect_single_action_result(:success => true,
:message => "alert_definitions id: #{alert_definition.id} deleting",
:href => alert_definitions_url(alert_definition.compressed_id))
:href => api_alert_definition_url(nil, alert_definition.compressed_id))
end

it "deletes an alert definition via DELETE" do
api_basic_authorize action_identifier(:alert_definitions, :delete, :resource_actions, :delete)
alert_definition = FactoryGirl.create(:miq_alert)
run_delete(alert_definitions_url(alert_definition.id))
run_delete(api_alert_definition_url(nil, alert_definition))
expect(response).to have_http_status(:no_content)
expect(MiqAlert.exists?(alert_definition.id)).to be_falsey
end

it "deletes alert definitions" do
api_basic_authorize collection_action_identifier(:alert_definitions, :delete)
alert_definitions = FactoryGirl.create_list(:miq_alert, 2)
run_post(alert_definitions_url, gen_request(:delete, [{"id" => alert_definitions.first.id},
run_post(api_alert_definitions_url, gen_request(:delete, [{"id" => alert_definitions.first.id},
{"id" => alert_definitions.second.id}]))
expect(response).to have_http_status(:ok)
expect(response.parsed_body["results"].count).to eq(2)
Expand All @@ -127,7 +127,7 @@
)

run_post(
alert_definitions_url(alert_definition.id),
api_alert_definition_url(nil, alert_definition),
:action => "edit",
:options => { :notifications => {:delay_next_evaluation => 60, :evm_event => {} } }
)
Expand All @@ -152,7 +152,7 @@
it "edits alert definitions" do
api_basic_authorize collection_action_identifier(:alert_definitions, :edit)
alert_definitions = FactoryGirl.create_list(:miq_alert, 2)
run_post(alert_definitions_url, gen_request(:edit, [{"id" => alert_definitions.first.id,
run_post(api_alert_definitions_url, gen_request(:edit, [{"id" => alert_definitions.first.id,
"description" => "Updated Test Alert 1"},
{"id" => alert_definitions.second.id,
"description" => "Updated Test Alert 2"}]))
Expand All @@ -167,41 +167,41 @@
describe "Alerts Definition Profiles API" do
it "forbids access to alert definition profiles list without an appropriate role" do
api_basic_authorize
run_get(alert_definition_profiles_url)
run_get(api_alert_definition_profiles_url)

expect(response).to have_http_status(:forbidden)
end

it "forbids access to an alert definition profile without an appropriate role" do
api_basic_authorize
alert_definition_profile = FactoryGirl.create(:miq_alert_set)
run_get(alert_definition_profiles_url(alert_definition_profile.id))
run_get(api_alert_definition_profile_url(nil, alert_definition_profile))

expect(response).to have_http_status(:forbidden)
end

it "reads 2 alert definition profiles as a collection" do
api_basic_authorize collection_action_identifier(:alert_definition_profiles, :read, :get)
alert_definition_profiles = FactoryGirl.create_list(:miq_alert_set, 2)
run_get(alert_definition_profiles_url)
run_get(api_alert_definition_profiles_url)

expect(response).to have_http_status(:ok)
expect_query_result(:alert_definition_profiles, 2, 2)
expect_result_resources_to_include_hrefs(
"resources",
[alert_definition_profiles_url(alert_definition_profiles.first.compressed_id),
alert_definition_profiles_url(alert_definition_profiles.second.compressed_id)]
[api_alert_definition_profile_url(nil, alert_definition_profiles.first.compressed_id),
api_alert_definition_profile_url(nil, alert_definition_profiles.second.compressed_id)]
)
end

it "reads an alert definition profile as a resource" do
api_basic_authorize action_identifier(:alert_definition_profiles, :read, :resource_actions, :get)
alert_definition_profile = FactoryGirl.create(:miq_alert_set)
run_get(alert_definition_profiles_url(alert_definition_profile.id))
run_get(api_alert_definition_profile_url(nil, alert_definition_profile))

expect(response).to have_http_status(:ok)
expect(response.parsed_body).to include(
"href" => a_string_matching(alert_definition_profiles_url(alert_definition_profile.compressed_id)),
"href" => api_alert_definition_profile_url(nil, alert_definition_profile.compressed_id),
"description" => alert_definition_profile.description,
"guid" => alert_definition_profile.guid
)
Expand All @@ -212,13 +212,13 @@

alert_definitions = FactoryGirl.create_list(:miq_alert, 2)
alert_definition_profile = FactoryGirl.create(:miq_alert_set, :alerts => alert_definitions)
run_get "#{alert_definition_profiles_url}/#{alert_definition_profile.id}/alert_definitions", :expand => "resources"
run_get(api_alert_definition_profile_alert_definitions_url(nil, alert_definition_profile), :expand => "resources")

expect(response).to have_http_status(:ok)
expect_result_resources_to_include_hrefs(
"resources",
["#{alert_definition_profiles_url}/#{alert_definition_profile.compressed_id}/alert_definitions/#{alert_definitions.first.compressed_id}",
"#{alert_definition_profiles_url}/#{alert_definition_profile.compressed_id}/alert_definitions/#{alert_definitions.first.compressed_id}"]
[api_alert_definition_profile_alert_definitions_url(nil, alert_definition_profile.compressed_id, alert_definitions.first.compressed_id),
api_alert_definition_profile_alert_definitions_url(nil, alert_definition_profile.compressed_id, alert_definitions.first.compressed_id)]
)
end

Expand All @@ -227,7 +227,7 @@

alert_definition = FactoryGirl.create(:miq_alert)
alert_definition_profile = FactoryGirl.create(:miq_alert_set, :alerts => [alert_definition])
run_get "#{alert_definition_profiles_url}/#{alert_definition_profile.id}", :expand => "alert_definitions"
run_get(api_alert_definition_profile_url(nil, alert_definition_profile), :expand => "alert_definitions")

expect(response).to have_http_status(:ok)
expect_single_resource_query(
Expand All @@ -245,7 +245,7 @@
"mode" => "ContainerNode",
}
api_basic_authorize collection_action_identifier(:alert_definition_profiles, :create)
run_post(alert_definition_profiles_url, sample_alert_definition_profile)
run_post(api_alert_definition_profiles_url, sample_alert_definition_profile)

expect(response).to have_http_status(:ok)
id = ApplicationRecord.uncompress_id(response.parsed_body["results"].first["id"])
Expand All @@ -260,18 +260,18 @@
it "deletes an alert definition profile via POST" do
api_basic_authorize action_identifier(:alert_definition_profiles, :delete, :resource_actions, :post)
alert_definition_profile = FactoryGirl.create(:miq_alert_set)
run_post(alert_definition_profiles_url(alert_definition_profile.id), gen_request(:delete))
run_post(api_alert_definition_profile_url(nil, alert_definition_profile), gen_request(:delete))

expect(response).to have_http_status(:ok)
expect_single_action_result(:success => true,
:message => "alert_definition_profiles id: #{alert_definition_profile.id} deleting",
:href => alert_definition_profiles_url(alert_definition_profile.compressed_id))
:href => api_alert_definition_profile_url(nil, alert_definition_profile.compressed_id))
end

it "deletes an alert definition profile via DELETE" do
api_basic_authorize action_identifier(:alert_definition_profiles, :delete, :resource_actions, :delete)
alert_definition_profile = FactoryGirl.create(:miq_alert_set)
run_delete(alert_definition_profiles_url(alert_definition_profile.id))
run_delete(api_alert_definition_profile_url(nil, alert_definition_profile))

expect(response).to have_http_status(:no_content)
expect(MiqAlertSet.exists?(alert_definition_profile.id)).to be_falsey
Expand All @@ -280,7 +280,7 @@
it "deletes alert definition profiles" do
api_basic_authorize collection_action_identifier(:alert_definition_profiles, :delete)
alert_definition_profiles = FactoryGirl.create_list(:miq_alert_set, 2)
run_post(alert_definition_profiles_url, gen_request(:delete, [{"id" => alert_definition_profiles.first.id},
run_post(api_alert_definition_profiles_url, gen_request(:delete, [{"id" => alert_definition_profiles.first.id},
{"id" => alert_definition_profiles.second.id}]))
expect(response).to have_http_status(:ok)
expect(response.parsed_body["results"].count).to eq(2)
Expand All @@ -289,7 +289,7 @@
it "edits alert definition profiles" do
api_basic_authorize action_identifier(:alert_definition_profiles, :edit, :resource_actions, :post)
alert_definition_profiles = FactoryGirl.create_list(:miq_alert_set, 2)
run_post(alert_definition_profiles_url, gen_request(:edit,
run_post(api_alert_definition_profiles_url, gen_request(:edit,
[{"id" => alert_definition_profiles.first.id,
"description" => "Updated Test Alert Profile 1"},
{"id" => alert_definition_profiles.second.id,
Expand Down
Loading