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

'.authorize' and '#authorize' return record even with passed record with namespace array #626

Merged

Conversation

QWYNG
Copy link
Contributor

@QWYNG QWYNG commented Nov 10, 2019

Hi, Thank you for the great Gem!
This is a proposal and pull request at once.
'.authorize' and '#authorize' return passed record now.

def show
  @post  = authorize Post.find(params[:id])
end

But When passing record with a namespace. return array

def show
  @post  = authorize [:admin, Post.find(params[:id])]
end

# @post is  [:admin, Post.find(params[:id])]

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.

class AdminController < ApplicationController
  def authorize(record, query = nil)
    super([:admin, record], query)
  end
end

class Admin::PostController < AdminController
  def show
    post = authorize Post.find(params[:id])
    # We don't need `authorize(post)` line!
  end
end

If there is a place to fix, please let me know.
best regards.

@QWYNG QWYNG force-pushed the authorize_return_record_with_namespase_arry branch from 62cfec6 to 3de189c Compare November 10, 2019 12:15
@QWYNG QWYNG changed the title '.authorize' and '#authorize' return record even with passed record with namespace arry '.authorize' and '#authorize' return record even with passed record with namespace array Nov 10, 2019
Copy link
Collaborator

@dgmstuart dgmstuart left a 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.

spec/pundit_spec.rb Show resolved Hide resolved
spec/pundit_spec.rb Show resolved Hide resolved
spec/pundit_spec.rb Show resolved Hide resolved
lib/pundit.rb Show resolved Hide resolved
@dgmstuart
Copy link
Collaborator

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 🤷‍♂

@QWYNG
Copy link
Contributor Author

QWYNG commented Nov 11, 2019

@dgmstuart
Thank you for your comments!
I'll fix spec!
I think that this is a breaking change too.
But I think that most users would expect the only record itself to return in this case.

@QWYNG
Copy link
Contributor Author

QWYNG commented Nov 11, 2019

@dgmstuart
Thank you for your review.
I fixed spec.
I'm really grateful as a pundit user for your interaction even with this PR doesn't marge.

@QWYNG QWYNG requested a review from dgmstuart November 11, 2019 14:37
Copy link
Collaborator

@dgmstuart dgmstuart left a 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.

end

it "returns the class when passed record not a particular instance" do
expect(Pundit.authorize(user, Post, :show?)).to be(Post)
Copy link
Collaborator

@dgmstuart dgmstuart Nov 11, 2019

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK!

end

it "can use without a particular instance" do
expect(Pundit.authorize(user, Post, :show?)).to be_truthy
Copy link
Collaborator

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?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I see.

Copy link
Contributor Author

@QWYNG QWYNG Nov 11, 2019

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?

Copy link
Collaborator

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:

  1. What's the return value (if we care)
  2. 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?

Copy link
Contributor Author

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.

Copy link
Collaborator

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>)

Copy link
Contributor Author

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.

@Linuus
Copy link
Collaborator

Linuus commented Nov 11, 2019

I would definitely say this is a bug 🐛

@QWYNG QWYNG requested a review from dgmstuart November 11, 2019 15:43
@QWYNG
Copy link
Contributor Author

QWYNG commented Nov 11, 2019

@dgmstuart
Thank you for your review!
fixed again

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
Copy link
Collaborator

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.

Copy link
Collaborator

@dgmstuart dgmstuart left a 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 dgmstuart merged commit 772fcac into varvet:master Nov 12, 2019
@QWYNG
Copy link
Contributor Author

QWYNG commented Nov 12, 2019

@dgmstuart @Linuus
I really appreciate both!
Thank you for the useful gem again!

@QWYNG QWYNG deleted the authorize_return_record_with_namespase_arry branch November 12, 2019 09:07
QWYNG added a commit to QWYNG/pundit that referenced this pull request Nov 12, 2019
@QWYNG QWYNG mentioned this pull request Nov 12, 2019
dgmstuart added a commit that referenced this pull request Dec 6, 2019
Burgestrand added a commit that referenced this pull request Aug 13, 2021
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.
@Burgestrand Burgestrand mentioned this pull request Aug 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants