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

Added Api::Utils.resource_search_by_href_slug helper method #14443

Merged
merged 2 commits into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions lib/api/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,15 @@ def self.build_href_slug(klass, id)
collection = Api::CollectionConfig.new.name_for_subclass(klass)
"#{collection}/#{id}" if collection
end

def self.resource_search_by_href_slug(href_slug)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abellotti can we pass in the user_object here with the default being User.curent_user

def self.resource_search_by_href(href_slug, user = User.current_user)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, didn't know the usage from your side. Will update. 👍

return unless href_slug
collection, id = href_slug.split('/')
collection_config = Api::CollectionConfig.new if collection
raise _("Invalid href_slug #{href_slug} specified") unless collection && id && collection_config.collection?(collection)

klass = collection_config.klass(collection)
Rbac.filtered_object(klass.find(id), :user => User.current_user, :class => klass)
end
end
end
63 changes: 63 additions & 0 deletions spec/lib/api/utils_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
RSpec.describe Api::Utils do
describe ".resource_search_by_href_slug" do
before { allow(User).to receive_messages(:server_timezone => "UTC") }

it "returns nil when nil is specified" do
actual = described_class.resource_search_by_href_slug(nil)

expect(actual).to be_nil
end

it "raises an exception with missing id" do
expect { described_class.resource_search_by_href_slug("vms/") }
.to raise_error("Invalid href_slug vms/ specified")
end

it "raises an exception with missing collection" do
expect { described_class.resource_search_by_href_slug("/10") }
.to raise_error("Invalid href_slug /10 specified")
end

it "raises an exception with missing delimited" do
expect { described_class.resource_search_by_href_slug("vms") }
.to raise_error("Invalid href_slug vms specified")
end

it "raises an exception with a bogus href_slug" do
expect { described_class.resource_search_by_href_slug("bogus/123") }
.to raise_error("Invalid href_slug bogus/123 specified")
end

it "raises an exception with a non primary collection href_slug" do
expect { described_class.resource_search_by_href_slug("auth/123") }
.to raise_error("Invalid href_slug auth/123 specified")
end

it "raises an ActiveRecord::RecordNotFound with a non-existent href_slug" do
expect { described_class.resource_search_by_href_slug("vms/99999") }
.to raise_error(ActiveRecord::RecordNotFound)
end

it "returns the resource with a valid href_slug" do
vm = FactoryGirl.create(:vm_vmware)

actual = described_class.resource_search_by_href_slug("vms/#{vm.id}")

expect(actual).to eq(vm)
end

it "does not return the resource when Rbac fails" do
owner_tenant = FactoryGirl.create(:tenant)

unauth_tenant = FactoryGirl.create(:tenant)
unauth_group = FactoryGirl.create(:miq_group, :tenant => unauth_tenant)

vm = FactoryGirl.create(:vm_vmware, :tenant => owner_tenant)
User.current_user = FactoryGirl.create(:user, :miq_groups => [unauth_group])

actual = described_class.resource_search_by_href_slug("vms/#{vm.id}")

expect(actual).to eq(nil)
end
end
end