-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Alexander Demichev
committed
Oct 19, 2017
1 parent
48e856c
commit eb40c9e
Showing
4 changed files
with
227 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Api | ||
module Subcollections | ||
module Flavors | ||
def flavors_query_resource(object) | ||
object.flavors | ||
end | ||
|
||
def flavors_create_resource(parent, _type, _id, data) | ||
task_id = Flavor.create_flavor_queue(User.current_user.id, parent, data) | ||
action_result(true, 'Creating Flavor', :task_id => task_id) | ||
rescue => err | ||
action_result(false, err.to_s) | ||
end | ||
|
||
def delete_resource_flavors(_parent, type, id, _data) | ||
flavor = resource_search(id, type, collection_class(type)) | ||
task_id = flavor.delete_flavor_queue(User.current_user.id) | ||
action_result(true, "Deleting #{flavor_ident(flavor)}", :task_id => task_id) | ||
rescue => err | ||
action_result(false, err.to_s) | ||
end | ||
alias flavors_delete_resource delete_resource_flavors | ||
|
||
private | ||
|
||
def flavor_ident(flavor) | ||
"Flavor id:#{flavor.id} name: '#{flavor.name}'" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
RSpec.describe "Flavors API" do | ||
describe "as a subcollection of providers" do | ||
describe "GET /api/providers/:c_id/flavors" do | ||
it "can list the flavors of a provider" do | ||
api_basic_authorize(action_identifier(:flavors, :read, :subcollection_actions, :get)) | ||
ems = FactoryGirl.create(:ems_cloud) | ||
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems) | ||
|
||
get(api_provider_flavors_url(nil, ems)) | ||
|
||
expected = { | ||
"count" => 1, | ||
"name" => "flavors", | ||
"resources" => [ | ||
{"href" => api_provider_flavor_url(nil, ems, flavor)} | ||
] | ||
} | ||
expect(response.parsed_body).to include(expected) | ||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it "will not list flavors unless authorized" do | ||
api_basic_authorize | ||
ems = FactoryGirl.create(:ems_cloud) | ||
FactoryGirl.create(:flavor, :ext_management_system => ems) | ||
|
||
get(api_provider_flavors_url(nil, ems)) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
|
||
describe "GET /api/providers/:c_id/flavors/:id" do | ||
it "can show a provider's flavor" do | ||
api_basic_authorize(action_identifier(:flavors, :read, :subresource_actions, :get)) | ||
ems = FactoryGirl.create(:ems_cloud) | ||
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems) | ||
|
||
get(api_provider_flavor_url(nil, ems, flavor)) | ||
|
||
expected = { | ||
"href" => api_provider_flavor_url(nil, ems, flavor), | ||
"id" => flavor.id.to_s | ||
} | ||
expect(response.parsed_body).to include(expected) | ||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it "will not show a flavor unless authorized" do | ||
api_basic_authorize | ||
ems = FactoryGirl.create(:ems_cloud) | ||
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems) | ||
|
||
get(api_provider_flavor_url(nil, ems, flavor)) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
|
||
describe "POST /api/providers/:c_id/flavors" do | ||
it "can queue the creation of a flavors" do | ||
api_basic_authorize(action_identifier(:flavors, :create, :subcollection_actions)) | ||
ems = FactoryGirl.create(:ems_cloud) | ||
|
||
post(api_provider_flavors_url(nil, ems), :params => { :name => "test-flavor" }) | ||
|
||
expected = { | ||
"results" => [ | ||
a_hash_including( | ||
"success" => true, | ||
"message" => "Creating Flavor", | ||
"task_id" => anything, | ||
"task_href" => a_string_matching(api_tasks_url) | ||
) | ||
] | ||
} | ||
expect(response.parsed_body).to include(expected) | ||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it "will not create a flavor unless authorized" do | ||
api_basic_authorize | ||
ems = FactoryGirl.create(:ems_cloud) | ||
|
||
post(api_provider_flavors_url(nil, ems), :params => { :name => "test-flavor" }) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
|
||
describe "POST /api/providers/:c_id/flavors/:s_id with delete action" do | ||
it "can queue a flavor for deletion" do | ||
api_basic_authorize(action_identifier(:flavors, :delete, :subresource_actions)) | ||
|
||
ems = FactoryGirl.create(:ems_cloud) | ||
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems) | ||
|
||
post(api_provider_flavor_url(nil, ems, flavor), :params => { :action => "delete" }) | ||
|
||
expected = { | ||
"message" => "Deleting Flavor id:#{flavor.id} name: '#{flavor.name}'", | ||
"success" => true, | ||
"task_href" => a_string_matching(api_tasks_url), | ||
"task_id" => anything | ||
} | ||
expect(response.parsed_body).to include(expected) | ||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it "will not delete a flavor unless authorized" do | ||
api_basic_authorize | ||
ems = FactoryGirl.create(:ems_cloud) | ||
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems) | ||
|
||
post(api_provider_flavor_url(nil, ems, flavor), :params => { :action => "delete" }) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
|
||
describe "POST /api/providers/:c_id/flavors/ with delete action" do | ||
it "can delete multiple flavors" do | ||
ems = FactoryGirl.create(:ems_cloud) | ||
flavor1, flavor2 = FactoryGirl.create_list(:flavor, 2) | ||
|
||
api_basic_authorize(action_identifier(:flavors, :delete, :subresource_actions)) | ||
|
||
post(api_provider_flavors_url(nil, ems), :params => { :action => "delete", :resources => [{:id => flavor1.id}, | ||
{:id => flavor2.id}] }) | ||
|
||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it "forbids multiple flavor deletion without an appropriate role" do | ||
ems = FactoryGirl.create(:ems_cloud) | ||
flavor1, flavor2 = FactoryGirl.create_list(:flavor, 2) | ||
|
||
api_basic_authorize | ||
|
||
post(api_provider_flavors_url(nil, ems), :params => { :action => "delete", :resources => [{:id => flavor1.id}, | ||
{:id => flavor2.id}] }) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
|
||
describe "DELETE /api/providers/:c_id/flavors/:s_id" do | ||
it "can delete a flavor" do | ||
ems = FactoryGirl.create(:ems_cloud) | ||
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems) | ||
|
||
api_basic_authorize(action_identifier(:flavors, :delete, :subresource_actions, :delete)) | ||
|
||
delete(api_provider_flavor_url(nil, ems, flavor)) | ||
|
||
expect(response).to have_http_status(:no_content) | ||
end | ||
|
||
it "will not delete a flavor unless authorized" do | ||
ems = FactoryGirl.create(:ems_cloud) | ||
flavor = FactoryGirl.create(:flavor, :ext_management_system => ems) | ||
|
||
api_basic_authorize | ||
|
||
delete(api_provider_flavor_url(nil, ems, flavor)) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
end | ||
end |