-
Notifications
You must be signed in to change notification settings - Fork 632
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' and '#authorize' return record even with passed record with namespace array #626
'.authorize' and '#authorize' return record even with passed record with namespace array #626
Conversation
62cfec6
to
3de189c
Compare
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'm basically in favour of this change - I have a few questions about the tests and some edge cases.
While I think it's unlikely that anyone is relying on getting the namespace array back from the authorize method, technically this is a breaking change: I don't think there's a clear expectation set that we always return the model instance, since the README does say "authorize returns the object passed to it", which I'd argue could mean either "the instance" or "the array". Not sure how seriously we need to take that 🤷♂ |
@dgmstuart |
…reaking any existing behaviour
@dgmstuart |
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.
Good job with the test coverage 👍
we maybe need to tweak a couple of the tests, but this feels close.
spec/pundit_spec.rb
Outdated
end | ||
|
||
it "returns the class when passed record not a particular instance" do | ||
expect(Pundit.authorize(user, Post, :show?)).to be(Post) |
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.
be
feels unusual to me - would eq
be more expressive/conventional?
The same for "comment" above
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.
OK!
spec/pundit_spec.rb
Outdated
end | ||
|
||
it "can use without a particular instance" do | ||
expect(Pundit.authorize(user, Post, :show?)).to be_truthy |
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 feels like quite a weak assertion - is truthiness a property we care about?
Would it be better to make this an assertion about the return value ("returns the... "
) and to expect the specific value we get in this case? (is it Post
?)
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.
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.
But we have not 'authorize can use without a particular instance' spec and 'authorize can use headless policy' spec other.
May I add it at this time?
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 thing for me is that I'm not sure what "can use" (or "can be used") is intending to test.
For me there are usually two types of test, which correspond to the two kinds of property which we care about our code having:
- What's the return value (if we care)
- What side effects happen (if any)
If we have one of these tests for a particular scenario/context then we're also implicitly testing that we "can use" the method in that context, since we're using the code. If we don't care about either 1 or 2, then probably the code doesn't need to exist 😉
Am I making sense, or am I missing the point?
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.
Thanks! It became clear.
Certainly, 1 is testing that we "can use" the method.
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.
Cool, in that case I suggest it should be the same shape as the other specs which test a return value:
title: it "returns..." do
expectation: expect(Pundit.authorize(...).to eq(<specific value>)
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 left only test a return value.
I would definitely say this is a bug 🐛 |
@dgmstuart |
spec/pundit_spec.rb
Outdated
it "returns the policy name symbol when passed record with headless policy" do | ||
expect(Pundit.authorize(user, :publication, :create?)).to eq(:publication) | ||
end | ||
|
||
it "can use without a particular instance" do | ||
expect(Pundit.authorize(user, Post, :show?)).to be_truthy | ||
end |
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.
Ah I see - I hadn't read this correctly before. Yes, it's good to delete these specs, since they're entirely covered by the it "returns..."
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.
Great work - let's get this merged in 👍
@dgmstuart @Linuus |
Friday 13th-release! Careful! The bugfix below (#626) could break existing code. If you rely on the return value for `authorize` and namespaced policies you might need to do some changes.
Hi, Thank you for the great Gem!
This is a proposal and pull request at once.
'.authorize' and '#authorize' return passed record now.
But When passing record with a namespace. return array
These methods are expected to return record not namespase array
When authorize override the helpers in AdminController to automatically apply the namespacing, This change will be very useful.
If there is a place to fix, please let me know.
best regards.