Skip to content

Commit

Permalink
Make sure we test clearing policy scope cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Burgestrand committed Nov 22, 2024
1 parent 0031a15 commit c0e2d0e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
23 changes: 17 additions & 6 deletions spec/authorization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def to_params(*args, **kwargs, &block)
end

let(:controller) { Controller.new(user, "update", to_params({})) }
let(:user) { double }
let(:user) { double("user") }
let(:post) { Post.new(user) }
let(:comment) { Comment.new }
let(:article) { Article.new }
Expand Down Expand Up @@ -276,20 +276,31 @@ def to_params(*args, **kwargs, &block)
describe "#pundit_reset!" do
it "allows authorize to react to a user change" do
expect(controller.authorize(post)).to be_truthy

controller.current_user = double
controller.pundit_reset!
expect { controller.authorize(post) }.to raise_error(Pundit::NotAuthorizedError)
end

it "allows policy to react to a user change" do
expect(controller.policy(DummyCurrentUser).user).to be user

new_user = double("new user")
controller.current_user = new_user
controller.pundit_reset!
expect(controller.policy(DummyCurrentUser).user).to be new_user
end

it "allows policy scope to react to a user change" do
expect(controller.policy_scope(Post)).to eq :published
expect { controller.verify_policy_scoped }.not_to raise_error
controller.current_user = double
expect(controller.policy_scope(DummyCurrentUser)).to be user

new_user = double("new user")
controller.current_user = new_user
controller.pundit_reset!
expect { controller.verify_policy_scoped }.to raise_error(Pundit::PolicyScopingNotPerformedError)
expect(controller.policy_scope(DummyCurrentUser)).to be new_user
end

it "clears the pundit context user" do
it "resets the pundit context" do
expect(controller.pundit.user).to be(user)

new_user = double
Expand Down
7 changes: 7 additions & 0 deletions spec/support/models/dummy_current_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class DummyCurrentUser
def update?
user
end
end
9 changes: 9 additions & 0 deletions spec/support/policies/dummy_current_user_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class DummyCurrentUserPolicy < BasePolicy
class Scope < BasePolicy::BaseScope
def resolve
user
end
end
end

0 comments on commit c0e2d0e

Please sign in to comment.