-
Notifications
You must be signed in to change notification settings - Fork 141
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
Allow Zones to be edited, deleted #691
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,32 @@ | ||
module Api | ||
class ZonesController < BaseController | ||
INVALID_ZONES_ATTRS = ID_ATTRS + %w[created_on updated_on].freeze | ||
|
||
# Edit an existing zone. Certain fields are meant for internal use only | ||
# and may not be edited. Attempting to edit one of the forbidden fields | ||
# will result in a bad request error. | ||
# | ||
def edit_resource(type, id, data) | ||
bad_attrs = data_includes_invalid_attrs(data) | ||
|
||
if bad_attrs.present? | ||
msg = "Attribute(s) '#{bad_attrs}' should not be specified for updating a zone resource" | ||
raise BadRequestError, msg | ||
end | ||
|
||
super | ||
end | ||
|
||
private | ||
|
||
# Check to see if any of the data attributes contain an invalid field. | ||
# Returns a list of invalid fields as a comma separated string that you | ||
# can use for error messages, or nil if the data argument is blank. | ||
# | ||
def data_includes_invalid_attrs(data) | ||
return nil unless data | ||
|
||
data.keys.select { |key| INVALID_ZONES_ATTRS.include?(key) }.compact.join(", ") | ||
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 |
---|---|---|
@@ -1,7 +1,145 @@ | ||
RSpec.describe "Zones" do | ||
let(:zone) { FactoryBot.create(:zone) } | ||
|
||
describe "/api/zones/:id?expand=settings" do | ||
context "authorization", :authorization do | ||
it "forbids access to zones without an appropriate role" do | ||
api_basic_authorize | ||
|
||
get(api_zones_url) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
|
||
it "forbids access to a zone resource without an appropriate role" do | ||
api_basic_authorize | ||
|
||
get(api_zone_url(nil, zone)) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
|
||
context "get", :get do | ||
it "allows GETs of a zone" do | ||
api_basic_authorize action_identifier(:zones, :read, :resource_actions, :get) | ||
|
||
get(api_zone_url(nil, zone)) | ||
|
||
expect_single_resource_query( | ||
"href" => api_zone_url(nil, zone), | ||
"id" => zone.id.to_s | ||
) | ||
end | ||
end | ||
|
||
context "edit", :edit do | ||
it "will fail if you try to edit forbidden fields" do | ||
api_basic_authorize action_identifier(:zones, :edit) | ||
|
||
zone = FactoryBot.create(:zone, :description => "Current Zone description") | ||
|
||
post api_zone_url(nil, zone), :params => gen_request(:edit, :created_on => Time.now.utc) | ||
expect_bad_request("Attribute(s) 'created_on' should not be specified for updating a zone resource") | ||
|
||
post api_zone_url(nil, zone), :params => gen_request(:edit, :updated_on => Time.now.utc) | ||
expect_bad_request("Attribute(s) 'updated_on' should not be specified for updating a zone resource") | ||
end | ||
|
||
it "can update multiple zones with POST" do | ||
api_basic_authorize action_identifier(:zones, :edit) | ||
|
||
zone1 = FactoryBot.create(:zone, :description => "Test Zone 1") | ||
zone2 = FactoryBot.create(:zone, :description => "Test Zone 2") | ||
|
||
options = [ | ||
{"href" => api_zone_url(nil, zone1), "description" => "Updated Test Zone 1"}, | ||
{"href" => api_zone_url(nil, zone2), "description" => "Updated Test Zone 2"} | ||
] | ||
|
||
post api_zones_url, :params => gen_request(:edit, options) | ||
|
||
expect(response).to have_http_status(:ok) | ||
|
||
expect_results_to_match_hash( | ||
"results", | ||
[ | ||
{"id" => zone1.id.to_s, "description" => "Updated Test Zone 1"}, | ||
{"id" => zone2.id.to_s, "description" => "Updated Test Zone 2"} | ||
] | ||
) | ||
|
||
expect(zone1.reload.description).to eq("Updated Test Zone 1") | ||
expect(zone2.reload.description).to eq("Updated Test Zone 2") | ||
end | ||
|
||
it "will fail to update multiple zones if any forbidden fields are edited" do | ||
api_basic_authorize action_identifier(:zones, :edit) | ||
|
||
zone1 = FactoryBot.create(:zone, :description => "Test Zone 1") | ||
zone2 = FactoryBot.create(:zone, :description => "Test Zone 2") | ||
|
||
options = [ | ||
{"href" => api_zone_url(nil, zone1), "description" => "New description"}, | ||
{"href" => api_zone_url(nil, zone2), "created_on" => Time.now.utc} | ||
] | ||
|
||
post api_zones_url, :params => gen_request(:edit, options) | ||
|
||
expect_bad_request("Attribute(s) 'created_on' should not be specified for updating a zone resource") | ||
end | ||
|
||
it "forbids edit of a zone without an appropriate role" do | ||
api_basic_authorize | ||
|
||
zone = FactoryBot.create(:zone, :description => "Current Zone description") | ||
|
||
post api_zone_url(nil, zone), :params => gen_request(:edit, :description => "New Zone description") | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
|
||
context "delete", :delete do | ||
it "can delete a zone with POST" do | ||
api_basic_authorize action_identifier(:zones, :delete) | ||
zone = FactoryBot.create(:zone) | ||
|
||
expect { post api_zone_url(nil, zone), :params => gen_request(:delete) }.to change(Zone, :count).by(-1) | ||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it "can delete a zone with DELETE" do | ||
api_basic_authorize action_identifier(:zones, :delete) | ||
zone = FactoryBot.create(:zone) | ||
|
||
expect { delete api_zone_url(nil, zone) }.to change(Zone, :count).by(-1) | ||
expect(response).to have_http_status(:no_content) | ||
end | ||
|
||
it "can delete multiple zones with POST" do | ||
api_basic_authorize action_identifier(:zones, :delete) | ||
zones = FactoryBot.create_list(:zone, 2) | ||
|
||
options = [ | ||
{"href" => api_zone_url(nil, zones.first)}, | ||
{"href" => api_zone_url(nil, zones.last)} | ||
] | ||
|
||
expect { post api_zones_url, :params => gen_request(:delete, options) }.to change(Zone, :count).by(-2) | ||
expect(response).to have_http_status(:ok) | ||
end | ||
|
||
it "forbids deletion of a zone without an appropriate role" do | ||
api_basic_authorize | ||
zone = FactoryBot.create(:zone, :description => "Current Region description") | ||
|
||
delete api_zone_url(nil, zone) | ||
|
||
expect(response).to have_http_status(:forbidden) | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice test coverage !! 👍 |
||
|
||
describe "/api/zones/:id?expand=settings", :settings do | ||
it "expands the settings subcollection" do | ||
api_basic_authorize(action_identifier(:zones, :read, :resource_actions, :get), :ops_settings) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit concerned about this being the same as this:
https://github.com/ManageIQ/manageiq-api/pull/690/files#diff-a8c5eb840e76e0a1632c7acdb2b03a43R20
Well also the part above.
I think that the behavior should be moved to a/the? parent class or a mixin.
We can agree on merging this (and the 3rd one) and then refactor it once the functionality is there. If you prefer it that way.
WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fields actually aren't identical - created_at vs created_on. But yes, we should eventually try to put this into some common code.