-
Notifications
You must be signed in to change notification settings - Fork 60
Authorize related records on #create_resource #60
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
Authorize related records on #create_resource #60
Conversation
|
It occurs to me that a more ideal fallback when there is no |
Yeah, I actually just answered over at #56 that this fallback logic of using
Yeah those are some very tricky tests and stubbing logic. I'm not quite happy about them myself either, but I'll definitely be able to give you a hand figuring these out :) |
If `create_with_<type>?` is undefined, check for `update?` permissions on each related record.
|
I've added the fallback to Whenever you have time to give me a hand with the specs I can get this wrapped up @valscion. Thanks! |
|
I'll likely have time this week to look into these. I really appreciate you working on these, thank you! |
Specs deeper down the complex spec of included resources change what records CommentPolicy::Scope returns. This in turn affects our ability to expect the exact objects passed to the authorizer. We're fine with relaxing the constraint in these specs as the exact models are tested in the create_resource callback test
Now we have the correct approach of handling this case as we've got the empty array of records into the authorizer
|
Ok I managed to fix the included resources specs and pushed directly to your branch (hopefully that's OK). I also merged in I also fixed the tricky operation specs that were failing, as now they can use this new relationship operation authorization properly! Amazing! EDIT: The build is now failing only due to code style issues, those should be easy to fix — I'll add a commit for them now :) |
Don't worry about WIP commits, I'll squash & merge once we want to get this merged anyway so individual commits aren't that big a deal :) |
`rubocop -a` fixes these automatically :)
|
I think all we need here is the same as in the missing specs at #59 (comment) Right now we no longer have tests for the fallback scenario to
|
|
Awesome, thank you. I will take a look today and see if I can't get all the specs done on this and on #59 today and get this wrapped up! |
|
I think this covers the missing scenarios. Please let me know if more is needed. |
valscion
left a comment
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.
Looking very good! Just a few nitpicks left inline. Almost ready to go! 🎉 💞
| end | ||
| context 'unauthorized for create? on source class and related records is empty' do | ||
| let(:related_records) { [] } | ||
| before { stub_policy_actions(source_class, create?: true) } |
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.
This case seems to conflict from the test description — should this probably be create?: false and then expect to raise an error?
| stub_policy_actions(source_class, create?: true) | ||
| related_records.each { |r| stub_policy_actions(r, update?: true) } | ||
| end | ||
| it { is_expected.not_to raise_error(::Pundit::NotAuthorizedError) } |
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.
When not expecting an error, we should not expect a specific error class, as literally any other error would cause the expectation to pass. RSpec spams the test logs with this warning for these cases:
WARNING: Using
expect { }.not_to raise_error(SpecificErrorClass)risks false positives, since literally any other error would cause the expectation to pass, including those raised by Ruby (e.g. NoMethodError, NameError and ArgumentError), meaning the code you are intending to test may not even get reached. Instead consider usingexpect {}.not_to raise_errororexpect { }.to raise_error(DifferentSpecificErrorClass).
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 see we've made this same mistake in #replace_fields specs, too — I'll add a similar review note to #59 adding more of these specs :)
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.
Yeah, I should have known better on this one as just a few weeks back I was reading about this same sort of scenario. Will update!
|
|
||
| context 'unauthorized for update? on any of the related records' do | ||
| let(:related_records) { [Comment.new(id: 1), Comment.new(id: 2)] } | ||
| context 'unauthorized for update? on related records' do |
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.
This might be a bit of a nitpick, but do you think it'd be a good idea to test this as
context 'unauthorized for update? on any of the related records'
...and then just stub one of the related_records to return false for update? and stub others to return true? It would be similar to what there used to be.
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.
Yes, I will change it. It's a better test.
e96e1be to
8d6c3fd
Compare
- Test for fallback update? authorization failing on any related not all - Dont expect specific error when expecting not to raise error
valscion
left a comment
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.
Superb, thank you!
Based on issue #56.
create_resourcemethod will check for acreate_with_<type>?action on the policy. If it doesn't find one, it will pass so long ascreate?passes.I am having a hard time getting specs in
/spec/requests/include_resources.rbto pass. Mainly, I am struggling with theAuthorizationStubsclass.The errors I can't solve all some variant of this:
Basically, in the spec when a new scoping is applied, the
allow_operationmethod conflicts with it. This is a few levels above my usual RSpec'ing and I have banged my head against the wall long enough not to feel too bad about asking for some help and guidance on getting this suite green again!With help I can get this wrapped up quickly, I do have time available to get this done. Excuse the messy commits for now I will rebase and clean up when I have won my war against RSpec stubs.