diff --git a/spec/authorization_spec.rb b/spec/authorization_spec.rb index 568afcb4..5bc1d255 100644 --- a/spec/authorization_spec.rb +++ b/spec/authorization_spec.rb @@ -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 } @@ -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 diff --git a/spec/support/models/dummy_current_user.rb b/spec/support/models/dummy_current_user.rb new file mode 100644 index 00000000..9c9af62a --- /dev/null +++ b/spec/support/models/dummy_current_user.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class DummyCurrentUser + def update? + user + end +end diff --git a/spec/support/policies/dummy_current_user_policy.rb b/spec/support/policies/dummy_current_user_policy.rb new file mode 100644 index 00000000..043e779a --- /dev/null +++ b/spec/support/policies/dummy_current_user_policy.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class DummyCurrentUserPolicy < BasePolicy + class Scope < BasePolicy::BaseScope + def resolve + user + end + end +end