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

[WIP] ActionsBuilder #119

Closed
wants to merge 3 commits into from
Closed

[WIP] ActionsBuilder #119

wants to merge 3 commits into from

Conversation

jntullo
Copy link

@jntullo jntullo commented Oct 12, 2017

Breaking out a lot of the action logic that is held in the renderer into an ActionsBuilder. We have a lot of tests in random places for actions, so this will allow us to centralize those tests as well as reduce what the renderer does.

@miq-bot add_label wip, refactoring

@jntullo jntullo changed the title [WIP] ActionsBuilder ActionsBuilder Oct 12, 2017
@miq-bot miq-bot removed the wip label Oct 12, 2017
@miq-bot
Copy link
Member

miq-bot commented Oct 13, 2017

This pull request is not mergeable. Please rebase and repush.

@miq-bot
Copy link
Member

miq-bot commented Oct 16, 2017

Checked commits jntullo/manageiq-api@8214edd~...cd6ace1 with ruby 2.3.3, rubocop 0.47.1, and haml-lint 0.20.0
7 files checked, 0 offenses detected
Everything looks fine. 🍪

@jntullo
Copy link
Author

jntullo commented Oct 25, 2017

@miq-bot assign @imtayadeway

Copy link
Contributor

@imtayadeway imtayadeway left a comment

Choose a reason for hiding this comment

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

@jntullo this is looking great! I left a few comments above, LMK if any are problematic or if you want to 🍐


def user_role_allows?(action_identifier)
return true unless action_identifier
User.current_user.role_allows?(:identifier => action_identifier)
Copy link
Contributor

Choose a reason for hiding this comment

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

WDYT about passing the current user into the constructor, so that this class doesn't need to know about the User class? It should make it easier to test, for one 😁

RSpec.describe Api::ActionsBuilder do
let(:role) { FactoryGirl.create(:miq_user_role) }
let(:group) { FactoryGirl.create(:miq_group, :miq_user_role => role) }
let(:user) { FactoryGirl.create(:user, :miq_groups => [group]) }
Copy link
Contributor

Choose a reason for hiding this comment

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

If you instead pass the user in, you could use a stub instead of creating these three objects (and accompanying role-updating code):

user = instance_double("User")
# ...
allow(user).to receive(:role_allows?) do |arg|
  %w(service_edit service_create).include?(arg) #etc
end

I think this is OK because we should have good confidence that #role_allows does the right thing

let(:role) { FactoryGirl.create(:miq_user_role) }
let(:group) { FactoryGirl.create(:miq_group, :miq_user_role => role) }
let(:user) { FactoryGirl.create(:user, :miq_groups => [group]) }
let(:request) { instance_double(Api::BaseController::Parser::RequestAdapter) }
Copy link
Contributor

Choose a reason for hiding this comment

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

We should really move the RequestAdapter 😉

Nit picking here, but if you enclose this class in quotes it'll defer the loading of it until it's required later

attr_reader :config, :href, :type, :cspec, :collection, :subcollection, :resource

def initialize(request, href, type, resource = nil)
@config = CollectionConfig.new
Copy link
Contributor

Choose a reason for hiding this comment

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

Similarly, you could pass this into the constructor (but maybe leaving it as the default), meaning you could test this with a fake config that conforms to the same structure. I think this would be OK since there should be good coverage that this works in general in the request specs, but would enable you to create edge cases that are not easy to find in the wild, and also decouples the tests from the specifics of the api.yml. Small changes there could cause these tests to fail, which would be unfortunate.

User.current_user = user
end

describe '#collection_actions' do
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like the only public method is #actions - perhaps these example groups need to be grouped under a single #actions group?

@@ -128,7 +128,7 @@ def expect_result_to_have_custom_actions_hash

expect_result_to_have_keys(%w(id href actions))
action_specs = response.parsed_body["actions"]
expect(action_specs.size).to eq(1)
expect(action_specs.size).to eq(3)
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like this behavior changed during a refactoring. Nit, but it might help to break up this commit into the refactoring part and the part that introduced the change in behavior.

@@ -822,108 +822,6 @@ def create_vms_by_name(names)
end

describe "Querying resources" do
it "does not return actions if not entitled" do
Copy link
Contributor

Choose a reason for hiding this comment

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

I think if we have good unit tests for the extracted object, we probably don't need exhaustive edge cases tested at this level 👍 I would however leave one or two general cases to show that everything works when integrated. WDYT?

@miq-bot
Copy link
Member

miq-bot commented Oct 26, 2017

This pull request is not mergeable. Please rebase and repush.

@jntullo jntullo mentioned this pull request Jan 4, 2018
@jntullo jntullo closed this Jan 4, 2018
@miq-bot miq-bot changed the title ActionsBuilder [WIP] ActionsBuilder Jan 4, 2018
@miq-bot miq-bot added the wip label Jan 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants