From 9c77b3a2cdde7f2ac20282ea9499ed5e6a82337d Mon Sep 17 00:00:00 2001 From: Jillian Tullo Date: Wed, 12 Apr 2017 14:16:22 -0400 Subject: [PATCH 1/2] return subcollection href https://bugzilla.redhat.com/show_bug.cgi?id=1417320 --- .../api/base_controller/normalizer.rb | 19 +++++++++++++++++++ spec/requests/api/custom_attributes_spec.rb | 12 ++++++++++++ 2 files changed, 31 insertions(+) diff --git a/app/controllers/api/base_controller/normalizer.rb b/app/controllers/api/base_controller/normalizer.rb index bf127d9ff8c..bb6441de821 100644 --- a/app/controllers/api/base_controller/normalizer.rb +++ b/app/controllers/api/base_controller/normalizer.rb @@ -91,6 +91,25 @@ def normalize_url(value) # Let's normalize an href based on type and id value # def normalize_href(type, value) + # If it is already a string, ie "/vms/:id/", OR it is defined as a collection + if type.kind_of?(String) || collection_config[type].options.include?(:collection) + collection_href(type, value) + else # If it is not a string and not defined as a collection + subcollection_href(type, value) + end + end + + # + # Subcollection href + # + def subcollection_href(type, value) + normalize_url("#{@req.collection}/#{@req.c_id}/#{type}/#{value}") + end + + # + # Collection href + # + def collection_href(type, value) normalize_url("#{type}/#{value}") end diff --git a/spec/requests/api/custom_attributes_spec.rb b/spec/requests/api/custom_attributes_spec.rb index 64ce3d79c1d..e45beff1d5f 100644 --- a/spec/requests/api/custom_attributes_spec.rb +++ b/spec/requests/api/custom_attributes_spec.rb @@ -10,4 +10,16 @@ expect(response).to have_http_status(:no_content) end + + it 'returns the correct href' do + provider = FactoryGirl.create(:ext_management_system) + custom_attribute = FactoryGirl.create(:custom_attribute, :resource => provider, :name => 'foo', :value => 'bar') + url = "#{providers_url(provider.id)}/custom_attributes/#{custom_attribute.id}" + api_basic_authorize subcollection_action_identifier(:providers, :custom_attributes, :edit, :post) + + run_post(url, :action => :edit, :name => 'name1') + + expect(response).to have_http_status(:ok) + expect(response.parsed_body['href']).to include(url) + end end From 6c2adb71492ff525357314fe9b448b0cb630d13e Mon Sep 17 00:00:00 2001 From: Jillian Tullo Date: Thu, 13 Apr 2017 10:24:10 -0400 Subject: [PATCH 2/2] update tenant_quotas_spec to check for correct href simplify normalize_href --- app/controllers/api/base_controller/normalizer.rb | 13 +------------ spec/requests/api/tenant_quotas_spec.rb | 2 +- 2 files changed, 2 insertions(+), 13 deletions(-) diff --git a/app/controllers/api/base_controller/normalizer.rb b/app/controllers/api/base_controller/normalizer.rb index bb6441de821..188be4513c5 100644 --- a/app/controllers/api/base_controller/normalizer.rb +++ b/app/controllers/api/base_controller/normalizer.rb @@ -91,24 +91,13 @@ def normalize_url(value) # Let's normalize an href based on type and id value # def normalize_href(type, value) - # If it is already a string, ie "/vms/:id/", OR it is defined as a collection - if type.kind_of?(String) || collection_config[type].options.include?(:collection) - collection_href(type, value) - else # If it is not a string and not defined as a collection - subcollection_href(type, value) - end + type.to_s == @req.subcollection ? subcollection_href(type, value) : collection_href(type, value) end - # - # Subcollection href - # def subcollection_href(type, value) normalize_url("#{@req.collection}/#{@req.c_id}/#{type}/#{value}") end - # - # Collection href - # def collection_href(type, value) normalize_url("#{type}/#{value}") end diff --git a/spec/requests/api/tenant_quotas_spec.rb b/spec/requests/api/tenant_quotas_spec.rb index b2634972da1..83b8b9cf7be 100644 --- a/spec/requests/api/tenant_quotas_spec.rb +++ b/spec/requests/api/tenant_quotas_spec.rb @@ -76,7 +76,7 @@ expect(response).to have_http_status(:ok) quota.reload expect(quota.value).to eq(5) - expect(response.parsed_body).to include('href' => /quotas/) + expect(response.parsed_body).to include('href' => a_string_including("tenants/#{tenant.id}/quotas/#{quota.id}")) end it "can update multiple quotas from a tenant with POST" do